Mercurial > hg > LGToc
annotate edit_section_db.php @ 3:5d0bfd909857
modify updating to sections_index table when deletion/recovery
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Fri, 20 Mar 2015 15:32:58 +0100 |
parents | bd46d26a7b18 |
children | 373c8ecad8b4 |
rev | line source |
---|---|
0 | 1 <?php |
2 include_once('Lib_mb_utf8.php'); | |
3 include_once('config.php'); | |
4 set_time_limit(0); | |
5 ini_set('memory_limit', '-1'); | |
6 | |
7 $link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password); | |
8 mysql_query("SET NAMES utf8"); | |
9 | |
10 if (!$link_mysql) { | |
11 die('Could not connect: ' . mysql_error()); | |
12 } | |
13 | |
14 $db_selected = mysql_select_db($mysql_database, $link_mysql); | |
15 if (!$db_selected) { | |
16 die ('Can\'t use foo : ' . mysql_error()); | |
17 } | |
18 function checkSectionInfo($bookId){ | |
19 $query="SELECT * FROM sections_versions WHERE books_id=".$bookId." ORDER BY version DESC"; | |
20 $result=mysql_query($query); | |
21 if(mysql_num_rows($result)==0){ | |
22 $row['version']=0; | |
23 $row['editor']=""; | |
24 $row['date']=""; | |
25 return $row; | |
26 }else{ | |
27 $row=mysql_fetch_assoc($result); | |
28 return $row; | |
29 } | |
30 } | |
31 function checkMissingPage($bookInfo,$versionInfo){ | |
32 $bookId=$bookInfo['id']; | |
33 $bookPage=$bookInfo['line']; | |
34 if($versionInfo['version']!=0){ | |
35 $table="sections_revisions"; | |
36 $condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 "; | |
37 $versionId=$versionInfo['id']; | |
38 }else{ | |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
39 // $table="sections"; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
40 $table="sections_index"; |
0 | 41 $condition=""; |
42 } | |
43 $query="SELECT * | |
44 FROM ".$table." | |
45 WHERE books_id=".$bookId." ".$condition." | |
46 GROUP BY books_id,start_page,end_page | |
47 ORDER BY books_id,start_page,level,end_page"; | |
48 $result=mysql_query($query); | |
49 $lastPage=1; | |
50 $i=0; | |
51 $pageArray=array(); | |
52 while($row=mysql_fetch_assoc($result)){ | |
53 if($row['start_page']-1>$lastPage){ | |
54 $pageArray[$i]['start_page']=$lastPage; | |
55 $pageArray[$i]['end_page']=$row['start_page']; | |
56 //echo $pageArray[$i]['start_page']." ".$pageArray[$i]['end_page']."<br>"; | |
57 $i++; | |
58 } | |
59 $lastPage=$row['end_page']; | |
60 } | |
61 if($bookPage>$lastPage){ | |
62 $pageArray[$i]['start_page']=$lastPage; | |
63 $pageArray[$i]['end_page']=$bookPage; | |
64 } | |
65 return $pageArray; | |
66 } | |
67 function getSectionArray($bookId,$count,$versionInfo){ | |
68 | |
69 $str=""; | |
70 if($versionInfo['version']!=0){ | |
71 $table="sections_revisions"; | |
72 $condition="AND deleted=0 "; | |
73 $condition=" AND versions_id=".$versionInfo['id']." AND deleted=0 "; | |
74 }else{ | |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
75 // $table="sections"; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
76 $table="sections_index"; |
0 | 77 $condition=""; |
78 } | |
79 | |
80 $query="SELECT * | |
81 FROM ".$table." | |
82 WHERE books_id=".$bookId." ".$condition." | |
83 GROUP BY books_id, start_page, end_page | |
84 HAVING COUNT( * ) >=".$count." | |
85 ORDER BY id ASC"; | |
86 /*ORDER BY start_page ASC, level ASC, id ASC,end_page DESC";*/ | |
87 $result=mysql_query($query); | |
88 | |
89 $i=0; | |
90 $pageArray=array(); | |
91 while($row=mysql_fetch_assoc($result)){ | |
92 $pageArray[$i]['start_page']=$row['start_page']; | |
93 $pageArray[$i]['end_page']=$row['end_page']; | |
94 $i++; | |
95 } | |
96 if($versionInfo['version']!=0){ | |
97 $query="SELECT id AS revisions_id, name,books_id,section_after,start_page,end_page,level,split_from,sections_id AS id, deleted FROM sections_revisions WHERE versions_id=".$versionInfo['id']." ORDER BY revisions_id ASC"; | |
98 }else{ | |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
99 $query="SELECT * FROM sections_index WHERE books_id=".$bookId." ORDER BY id ASC"; |
0 | 100 } |
101 $result=mysql_query($query); | |
102 | |
103 $i=0; | |
104 $sectionArray=array(); | |
105 while($row=mysql_fetch_assoc($result)){ | |
106 $flag=""; | |
107 if($i!=sizeof($pageArray) && $pageArray[$i]['start_page']==$row['start_page'] && $pageArray[$i]['end_page']==$row['end_page']){ | |
108 $flag="overlapper"; | |
109 $i++; | |
110 } | |
111 if($i!=0 && $pageArray[$i-1]['start_page']==$row['start_page'] && $pageArray[$i-1]['end_page']==$row['end_page']){ | |
112 $flag="overlapper"; | |
113 } | |
114 $row['flag']=$flag; | |
115 if(!isset($row['deleted'])){ | |
116 $row['deleted']=""; | |
117 }else{ | |
118 if($row['deleted']==0){ | |
119 $row['deleted']=""; | |
120 }else{ | |
121 $row['deleted']="deleted"; | |
122 } | |
123 } | |
124 $sectionArray[]=$row; | |
125 } | |
126 return $sectionArray; | |
127 } | |
128 function updateSectionArray($bookId,$sectionArray,$version,$editor){ | |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
129 |
2
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
130 // -- Start Transaction -- |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
131 mysql_query("BEGIN"); |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
132 |
0 | 133 $query="INSERT INTO sections_versions (version,editor,date,books_id) VALUES (".($version+1).",'".$editor."',NOW(),'".$bookId."')"; |
2
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
134 $result_versions=mysql_query($query); |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
135 if(!$result_versions){ |
0 | 136 echo json_encode("Failed during inserting sections_version records."); // .mysql_error(); |
137 } | |
138 $versionId=mysql_insert_id(); | |
2
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
139 |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
140 |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
141 |
0 | 142 $query="INSERT INTO sections_revisions (name,books_id,section_after,start_page,end_page,level,split_from,sections_id,versions_id,deleted) VALUES "; |
143 foreach($sectionArray as $idx=>$row){ | |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
144 // get the section_id |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
145 $section_id = $row['id']; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
146 $name = $row['name']; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
147 $books_id = sprintf("%05d",$row['booksId']); |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
148 $section_after = $row['sectionAfter']; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
149 $start_page = $row['startPage']; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
150 $end_page = $row['endPage']; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
151 $level = $row['level']; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
152 $split_from = $row['splitFrom']; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
153 $deleted = $row['deleted']; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
154 |
2
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
155 // --- update to section_index table, which is the up-to-date section table --- |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
156 $qry = ""; |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
157 if ($section_id == 0) { // section_id = 0 means it is a new record created by user |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
158 // insert new row to section_index table |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
159 $qry = "INSERT INTO sections_index (name,books_id,section_after,start_page,end_page,level,split_from) VALUES |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
160 ('".$name."','".$books_id."','".$section_after."',".$start_page. |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
161 ",".$end_page.",".$level.",".$split_from.")"; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
162 |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
163 } else { |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
164 // update |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
165 $qry = "UPDATE sections_index SET name='".$name."',books_id='".$books_id."',section_after='".$section_after. |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
166 "',start_page=".$start_page.",end_page=".$end_page.",level=".$level.",split_from=".$split_from." WHERE id=".$section_id; |
2
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
167 } |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
168 |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
169 $rst = mysql_query($qry); |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
170 if (!$rst) { |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
171 break; // handle to ROLLBACK |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
172 } |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
173 |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
174 if ($section_id == 0) { |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
175 // set section_id from section_index if this is a new section |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
176 $section_id = mysql_insert_id(); |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
177 } |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
178 |
3
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
179 |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
180 if ($deleted == 1) { |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
181 // deleted section from sections_index where id = $section_id |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
182 $qry = "DELETE FROM sections_index WHERE id=".$section_id; |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
183 $rst = mysql_query($qry); |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
184 if (!$rst) { |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
185 break; // handle to ROLLBACK |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
186 } |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
187 |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
188 } else { |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
189 $qry = "SELECT * FROM sections_index WHERE id=".$section_id; |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
190 $rst = mysql_query($qry); |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
191 if (!$rst) { |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
192 break; // handle to ROLLBACK |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
193 } |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
194 if (mysql_num_rows($rst) == 0) { |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
195 $qry = "INSERT INTO sections_index (id,name,books_id,section_after,start_page,end_page,level,split_from) VALUES |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
196 (".$section_id.",'".$name."','".$books_id."','".$section_after."',".$start_page. |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
197 ",".$end_page.",".$level.",".$split_from.")"; |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
198 // recover section in sections_index table where id = $section_id |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
199 $rst = mysql_query($qry); |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
200 if (!$rst) { |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
201 break; // handle to ROLLBACK |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
202 } |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
203 } |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
204 |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
205 } |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
206 |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
207 // query for sections_revisions table |
5d0bfd909857
modify updating to sections_index table when deletion/recovery
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
208 |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
209 $str="("; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
210 $str.="'".$name."',"; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
211 $str.="'".$books_id."',"; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
212 $str.="'".$section_after."',"; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
213 $str.=$start_page.","; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
214 $str.=$end_page.","; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
215 $str.=$level.","; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
216 $str.=$split_from.","; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
217 $str.=$section_id.","; // sections_id should get from section_index table |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
218 $str.=$versionId.","; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
219 $str.=$deleted; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
220 $str.=")"; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
221 if($idx!=sizeof($sectionArray)-1){ |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
222 $str.=", "; |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
223 } |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
224 |
2
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
225 $query.=$str; |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
226 |
0 | 227 } |
228 $result=mysql_query($query); | |
2
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
229 |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
230 // --- End Transaction --- |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
231 if ($result and $rst) { |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
232 mysql_query("COMMIT"); |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
233 echo json_encode("Succeeded."); // important returning info to js |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
234 |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
235 } else { |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
236 mysql_query("ROLLBACK"); |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
237 echo json_encode("Failed during updating sections."); |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
238 return; |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
239 } |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
240 |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
241 /* |
0 | 242 if(!$result){ |
243 echo json_encode("Failed during inserting sections_revisions records."); // .mysql_error(); | |
244 | |
245 // Delete the sections_versions for this user | |
246 // delete $versionId in table sections_versions | |
247 $query="DELETE FROM sections_versions WHERE id=".$versionId; | |
248 $result_deleting=mysql_query($query); | |
249 if(!$result_deleting){ | |
250 echo json_encode("Failed during deleting wrongly inserted sections_version record."); | |
251 } | |
252 return; | |
253 }else{ | |
254 echo json_encode("Succeeded."); | |
255 } | |
2
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
256 */ |
bd46d26a7b18
adding transaction when updating sections
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
257 |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
258 |
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
259 |
0 | 260 } |
261 | |
262 | |
263 function _select_distinct(){ | |
264 /* | |
265 $query="SELECT DISTINCT (books_id) FROM sections"; | |
266 | |
267 $result=mysql_query($query); | |
268 if(!$result){ | |
269 echo json_encode("Failed during inserting section records."); | |
270 return; | |
271 }else{ | |
272 echo json_encode("Succeeded."); | |
273 } | |
274 | |
275 while($row=mysql_fetch_assoc($result)){ | |
276 // echo $row['books_id'], ", "; | |
277 } | |
278 | |
279 echo "section num: ".mysql_num_rows($result); | |
280 echo '\n'; | |
281 */ | |
282 | |
283 | |
284 //$query="SELECT id FROM books WHERE id NOT IN (SELECT DISTINCT (books_id) FROM sections)"; | |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
285 $query="SELECT books.id, books.name FROM books LEFT JOIN sections_index ON books.id=sections_index.books_id WHERE sections_index.books_id IS NULL"; |
0 | 286 |
287 $result=mysql_query($query); | |
288 if(!$result){ | |
289 echo json_encode("Failed during querying records."); | |
290 return; | |
291 } | |
292 | |
293 echo "missing books num: ".mysql_num_rows($result); | |
294 echo '\n'; | |
295 | |
296 | |
297 | |
298 | |
299 } | |
300 | |
301 | |
302 | |
303 function add_missing_books_to_sections(){ | |
304 // find missing books | |
305 // Run the following line/mysql to find out what books are missing in sections table | |
306 // _select_distinct(); | |
307 // Or load missing_books_id.csv | |
308 if (($missing_books = fopen("intermediate_results/missing_books.csv", "r")) !== FALSE) { | |
309 while (($data = fgetcsv($missing_books, ",")) !== FALSE) { | |
310 | |
311 $bookId = $data[0]; | |
312 $name = $data[1]; | |
313 | |
314 _add_book_to_sections($name, $bookId); | |
315 | |
316 } | |
317 fclose($missing_books); | |
318 } | |
319 | |
320 } | |
321 | |
322 | |
323 function _add_book_to_sections($name, $bookId){ | |
324 | |
1
1f9d2bfe1d13
handle new section created by user, update sections_index table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
325 $query="INSERT INTO sections_index (name,books_id,section_after) VALUES "; |
0 | 326 |
327 $str="("; | |
328 $str.="'".$name."','"; | |
329 $str.=$bookId."',"; | |
330 $str.="''"; | |
331 $str.=")"; | |
332 $query.=$str; | |
333 | |
334 // echo $name.", ".$bookId."\n"; | |
335 | |
336 $result=mysql_query($query); | |
337 if(!$result){ | |
338 echo json_encode("Failed during inserting section records."); | |
339 return; | |
340 } | |
341 // echo mysql_insert_id(); | |
342 | |
343 } | |
344 | |
345 | |
346 if(isset($_POST['command']) && $_POST['command']!=''){ | |
347 $command=$_POST['command']; | |
348 if($command=="write"){ | |
349 | |
350 if(isset($_POST['bookId'])&&isset($_POST['sectionArray'])&&isset($_POST['version'])&&isset($_POST['editor'])&&\ | |
351 is_numeric($_POST['bookId']) && sizeof($_POST['sectionArray'])!=0 && is_numeric($_POST['version']) && $_POST['editor']!=""){ | |
352 $bookId=$_POST['bookId']; | |
353 $sectionArray=$_POST['sectionArray']; | |
354 $version=$_POST['version']; | |
355 $editor=$_POST['editor']; | |
356 | |
357 updateSectionArray($bookId,$sectionArray,$version,$editor); | |
358 } | |
359 else{ | |
360 echo json_encode("Editing info not correct."); | |
361 | |
362 } | |
363 } | |
364 else{ | |
365 echo json_encode("Wrong command."); | |
366 return; | |
367 } | |
368 } | |
369 | |
370 ?> |