Mercurial > hg > extraction-interface
comparison models/extractapp.php @ 112:7bdbc7fc7936 extractapp
Replace to space
author | Calvin Yeh <cyeh@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 04 May 2017 16:57:16 +0200 |
parents | 025530e37dcd |
children | 507070df62e6 |
comparison
equal
deleted
inserted
replaced
111:3eacb630b9eb | 112:7bdbc7fc7936 |
---|---|
14 * GNU General Public License for more details. | 14 * GNU General Public License for more details. |
15 * | 15 * |
16 * You should have received a copy of the GNU General Public License | 16 * You should have received a copy of the GNU General Public License |
17 * along with Extraction-interface. If not, see <http://www.gnu.org/licenses/>. | 17 * along with Extraction-interface. If not, see <http://www.gnu.org/licenses/>. |
18 */ | 18 */ |
19 /** | 19 /** |
20 * ExtractappModel class is extended from the BaseModel class. | 20 * ExtractappModel class is extended from the BaseModel class. |
21 * This is where we implement the functions/models to prepare data that been sent to views. | 21 * This is where we implement the functions/models to prepare data that been sent to views. |
22 * ExtractappModel is notified by Extractapp controller when there has been a change in its state, | 22 * ExtractappModel is notified by Extractapp controller when there has been a change in its state, |
23 * and produces the output/data to the corresponding view in "views/Extractapp/". | 23 * and produces the output/data to the corresponding view in "views/Extractapp/". |
24 * | 24 * |
25 */ | 25 */ |
26 | 26 |
27 class ExtractappModel extends BaseModel{ | 27 class ExtractappModel extends BaseModel{ |
28 | 28 |
29 protected $section_id = 0, $data_path, $file_id = 0, $current_fileId=0, | 29 protected $section_id = 0, $data_path, $file_id = 0, $current_fileId=0, |
30 $branch_id = 0, $user_id = 0, $lg_text = "", $topic, $taglist_infile = "", | 30 $branch_id = 0, $user_id = 0, $lg_text = "", $topic, $taglist_infile = "", |
31 $book_meta = "", $book_dynasty = ""; | 31 $book_meta = "", $book_dynasty = ""; |
32 | 32 |
33 public $messages = array('info'=>"",'error'=>"",'warning'=>"",'debug'=>""); | 33 public $messages = array('info'=>"",'error'=>"",'warning'=>"",'debug'=>""); |
34 | 34 |
35 | 35 |
36 public function SetTextFromFileId() { // remove $_postdata as input | 36 public function SetTextFromFileId() { // remove $_postdata as input |
37 /** | 37 /** |
38 * Get text content from LGService by $this->file_id. | 38 * Get text content from LGService by $this->file_id. |
39 * After preprocessing the text string (replace space to circle symbol, link for page number, etc), | 39 * After preprocessing the text string (replace space to circle symbol, link for page number, etc), |
40 * set $this->lg_text to the processed text gotten from LGService. | 40 * set $this->lg_text to the processed text gotten from LGService. |
41 */ | 41 */ |
42 | 42 |
43 /* | 43 /* |
76 // get book_meta from $$this->get_section_metadata_by_sectionId_url | 76 // get book_meta from $$this->get_section_metadata_by_sectionId_url |
77 $section_meta_url = $this->get_section_metadata_by_sectionId_url.$this->section_id; | 77 $section_meta_url = $this->get_section_metadata_by_sectionId_url.$this->section_id; |
78 $section_meta = json_decode(file_get_contents($section_meta_url), true); | 78 $section_meta = json_decode(file_get_contents($section_meta_url), true); |
79 | 79 |
80 $b = $section_meta['section']['book']; | 80 $b = $section_meta['section']['book']; |
81 | 81 |
82 $this->book_name = $b['name']; | 82 $this->book_name = $b['name']; |
83 $this->book_id = $b['id']; | 83 $this->book_id = $b['id']; |
84 /* | 84 /* |
85 $book_meta = array(); | 85 $book_meta = array(); |
86 array_push($book_meta, array($b['id'],$b['name'],$b['level1'],$b['level2'], | 86 array_push($book_meta, array($b['id'],$b['name'],$b['level1'],$b['level2'], |
120 | 120 |
121 } | 121 } |
122 | 122 |
123 public function SetTextFromSectionId() { // remove $_postdata as input | 123 public function SetTextFromSectionId() { // remove $_postdata as input |
124 /** | 124 /** |
125 * Get text content from LGService by $this->section_id. | 125 * Get text content from LGService by $this->section_id. |
126 * After preprocessing the text string (replace space to circle symbol, link for page number, etc), | 126 * After preprocessing the text string (replace space to circle symbol, link for page number, etc), |
127 * set $this->lg_text to the processed text gotten from LGService. | 127 * set $this->lg_text to the processed text gotten from LGService. |
128 */ | 128 */ |
129 | 129 |
130 /* | 130 /* |
152 | 152 |
153 $this->lg_text = $lg_text; | 153 $this->lg_text = $lg_text; |
154 } | 154 } |
155 | 155 |
156 // This is only used on localhost | 156 // This is only used on localhost |
157 public function SetTextFromLocal($_id){ | 157 public function SetTextFromLocal($_id){ |
158 $this->section_id = $_id; | 158 $this->section_id = $_id; |
159 //$this->branch_id = 1; // local test sets branch_id to 1 | 159 //$this->branch_id = 1; // local test sets branch_id to 1 |
160 $this->messages['debug'] .= "[Debug] from my local"."<br>"; | 160 $this->messages['debug'] .= "[Debug] from my local"."<br>"; |
161 $this->lg_text = $this->GetSectionContent(); | 161 $this->lg_text = $this->GetSectionContent(); |
162 } | 162 } |
163 | 163 |
164 | 164 |
165 public function SetInfoFromPreviousPage($_postdata) { | 165 public function SetInfoFromPreviousPage($_postdata) { |
166 /** | 166 /** |
167 * Get information from post data passsed by the previous page, and set variables in $this. | 167 * Get information from post data passsed by the previous page, and set variables in $this. |
168 * The previous page could be: | 168 * The previous page could be: |
169 * | 169 * |
170 * (1) LGService: the first time user enters Extraction-interface, which should linked from LGService. | 170 * (1) LGService: the first time user enters Extraction-interface, which should linked from LGService. |
171 * | 171 * |
172 * (2) Extraction-interface itself: changing topic then continue to tag, saving text to LGService, reload the text from LGService. | 172 * (2) Extraction-interface itself: changing topic then continue to tag, saving text to LGService, reload the text from LGService. |
173 * | 173 * |
174 */ | 174 */ |
175 | 175 |
176 if (isset($_postdata['fileId'])) { | 176 if (isset($_postdata['fileId'])) { |
177 $this->file_id = $_postdata['fileId']; | 177 $this->file_id = $_postdata['fileId']; |
178 } | 178 } |
189 $this->user_id = $_postdata['userId']; | 189 $this->user_id = $_postdata['userId']; |
190 } | 190 } |
191 if (isset($_postdata['topic'])) { | 191 if (isset($_postdata['topic'])) { |
192 $this->topic = $_postdata['topic']; | 192 $this->topic = $_postdata['topic']; |
193 } | 193 } |
194 | 194 |
195 | 195 |
196 if (isset($_postdata['sectionName'])) { | 196 if (isset($_postdata['sectionName'])) { |
197 $this->section_name = $_postdata['sectionName']; | 197 $this->section_name = $_postdata['sectionName']; |
198 } | 198 } |
199 if (isset($_postdata['bookName'])) { | 199 if (isset($_postdata['bookName'])) { |
226 foreach ($list1 as $v1) { | 226 foreach ($list1 as $v1) { |
227 $cnt = 0; | 227 $cnt = 0; |
228 foreach ($list2 as $v2) { | 228 foreach ($list2 as $v2) { |
229 $num_field = count($v2); | 229 $num_field = count($v2); |
230 $matched = true; | 230 $matched = true; |
231 for ($i=0; $i < $num_field; $i++) { | 231 for ($i=0; $i < $num_field; $i++) { |
232 if ($v1[$i] != $v2[$i]) { | 232 if ($v1[$i] != $v2[$i]) { |
233 $matched = false; | 233 $matched = false; |
234 break; | 234 break; |
235 } | 235 } |
236 } | 236 } |
242 break; | 242 break; |
243 } | 243 } |
244 | 244 |
245 } | 245 } |
246 if ($cnt == $cnt_list2) { | 246 if ($cnt == $cnt_list2) { |
247 return false; | 247 return false; |
248 } | 248 } |
249 } | 249 } |
250 return true; | 250 return true; |
251 | 251 |
252 } | 252 } |
253 | 253 |
254 // === for tagging === | 254 // === for tagging === |
255 private function ReplaceLgTextWithLatestTag() { | 255 private function ReplaceLgTextWithLatestTag() { |
256 // modify $this->lg_text with the latest tags from database | 256 // modify $this->lg_text with the latest tags from database |
257 // can do it by getting the tag's id using tag's information in the file, then by the id to get latest tag (in db) | 257 // can do it by getting the tag's id using tag's information in the file, then by the id to get latest tag (in db) |
258 $lg_text = $this->lg_text; | 258 $lg_text = $this->lg_text; |
259 | 259 |
260 $taglist_indb = $this->GetTaglistByTopicId($this->GetTopic()); | 260 $taglist_indb = $this->GetTaglistByTopicId($this->GetTopic()); |
261 $taglist_infile = $this->taglist_infile; | 261 $taglist_infile = $this->taglist_infile; |
262 | 262 |
263 if ($taglist_infile == "") return; | 263 if ($taglist_infile == "") return; |
264 | 264 |
265 $count_tag_indb = count($taglist_indb); | 265 $count_tag_indb = count($taglist_indb); |
266 | 266 |
267 foreach ($taglist_infile as $t1) { | 267 foreach ($taglist_infile as $t1) { |
268 $id = $t1[0]; | 268 $id = $t1[0]; |
269 $tag = $t1[2]; | 269 $tag = $t1[2]; |
272 $cnt = 0; | 272 $cnt = 0; |
273 foreach ($taglist_indb as $t2) { | 273 foreach ($taglist_indb as $t2) { |
274 if ($t2[0] == $id) { | 274 if ($t2[0] == $id) { |
275 $latest_tag = $t2[2]; | 275 $latest_tag = $t2[2]; |
276 } | 276 } |
277 | 277 |
278 if ($tag != $t2[2]) { | 278 if ($tag != $t2[2]) { |
279 // not match with this t2 | 279 // not match with this t2 |
280 $cnt += 1; | 280 $cnt += 1; |
281 } else { | 281 } else { |
282 break; | 282 break; |
307 public function StartTagging() { | 307 public function StartTagging() { |
308 /** | 308 /** |
309 * This is the main method for tagging text. It passes all the information to "views/Extractapp/TaggingText.php" view. | 309 * This is the main method for tagging text. It passes all the information to "views/Extractapp/TaggingText.php" view. |
310 * The information contain the text string, taglist array, wordlis array, topic, etc. | 310 * The information contain the text string, taglist array, wordlis array, topic, etc. |
311 */ | 311 */ |
312 | 312 |
313 $section_id = $this->section_id; | 313 $section_id = $this->section_id; |
314 | 314 |
315 $data = array(); // data array to be passed to view | 315 $data = array(); // data array to be passed to view |
316 | 316 |
317 //for GetTaglistByTopicId: | 317 //for GetTaglistByTopicId: |
318 $taglistArray = $this->GetTaglistByTopicId($this->GetTopic()); | 318 $taglistArray = $this->GetTaglistByTopicId($this->GetTopic()); |
319 | 319 |
320 // $this->taglist_infile is set (1) from file or (2) from _postdata['taglistArray'] which comes from frontend that user decided | 320 // $this->taglist_infile is set (1) from file or (2) from _postdata['taglistArray'] which comes from frontend that user decided |
321 // $this->taglist_infile is the most up-to-date taglist decided by user. Should be written into file. | 321 // $this->taglist_infile is the most up-to-date taglist decided by user. Should be written into file. |
322 if( $this->TaglistSubsetIn($this->taglist_infile, $taglistArray) ) { // TaglistSubsetIn($l1,$l2): $l1 is a subset of $l2 or not | 322 if( $this->TaglistSubsetIn($this->taglist_infile, $taglistArray) ) { // TaglistSubsetIn($l1,$l2): $l1 is a subset of $l2 or not |
323 $this->taglist_infile = ""; | 323 $this->taglist_infile = ""; |
324 | 324 |
325 } else { | 325 } else { |
326 // taglist_infile is out-to-date | 326 // taglist_infile is out-to-date |
327 // replace all the old tag with new ones in database | 327 // replace all the old tag with new ones in database |
328 // replace old tags for $this->lg_text with the latest tags from db | 328 // replace old tags for $this->lg_text with the latest tags from db |
329 $this->ReplaceLgTextWithLatestTag(); | 329 $this->ReplaceLgTextWithLatestTag(); |
331 } | 331 } |
332 $stringInput = $this->lg_text; | 332 $stringInput = $this->lg_text; |
333 | 333 |
334 | 334 |
335 $data['taglist_infile'] = $this->taglist_infile; // if taglist_infile == "" means taglist in file is up-to-date (will use taglist in db), otherwise means the taglist in file | 335 $data['taglist_infile'] = $this->taglist_infile; // if taglist_infile == "" means taglist in file is up-to-date (will use taglist in db), otherwise means the taglist in file |
336 | 336 |
337 // book_meta | 337 // book_meta |
338 $data['book_meta'] = $this->book_meta; | 338 $data['book_meta'] = $this->book_meta; |
339 | 339 |
340 // topic list | 340 // topic list |
341 $topiclistArray = $this->GetTopiclistArray(); | 341 $topiclistArray = $this->GetTopiclistArray(); |
354 $data['topic_tag'] = $this->GetTopicTag($topic_id); | 354 $data['topic_tag'] = $this->GetTopicTag($topic_id); |
355 $data['topic_name'] = $this->GetTopicName($topic_id); | 355 $data['topic_name'] = $this->GetTopicName($topic_id); |
356 $data['topic_tag_ch'] = $this->GetTopicTagName($topic_id); | 356 $data['topic_tag_ch'] = $this->GetTopicTagName($topic_id); |
357 | 357 |
358 $data['info'] = array('file_id'=>$this->file_id, 'user_id'=>$this->user_id, | 358 $data['info'] = array('file_id'=>$this->file_id, 'user_id'=>$this->user_id, |
359 'branch_id'=>$this->branch_id, 'section_id'=>$this->section_id, 'book_id'=>$this->book_id, | 359 'branch_id'=>$this->branch_id, 'section_id'=>$this->section_id, 'book_id'=>$this->book_id, |
360 'book_name'=>$this->book_name,'section_name'=>$this->section_name, 'period'=>$this->book_meta->period, | 360 'book_name'=>$this->book_name,'section_name'=>$this->section_name, 'period'=>$this->book_meta->period, |
361 'current_fileId'=>$this->current_fileId); | 361 'current_fileId'=>$this->current_fileId); |
362 | 362 |
363 | 363 |
364 $this->messages['debug'] .= "[Debug] "; | 364 $this->messages['debug'] .= "[Debug] "; |
365 $this->messages['debug'] .= "file_id=".$this->file_id.", section_id=".$this->section_id; | 365 $this->messages['debug'] .= "file_id=".$this->file_id.", section_id=".$this->section_id; |
366 $this->messages['debug'] .= ", user_id=".$this->user_id.", branch_id=".$this->branch_id.", topic_id=".$this->topic; | 366 $this->messages['debug'] .= ", user_id=".$this->user_id.", branch_id=".$this->branch_id.", topic_id=".$this->topic; |
378 | 378 |
379 | 379 |
380 $data['messages'] = $this->messages; | 380 $data['messages'] = $this->messages; |
381 | 381 |
382 return $data; | 382 return $data; |
383 | 383 |
384 } | 384 } |
385 | 385 |
386 public function SaveFullText($postdata) { | 386 public function SaveFullText($postdata) { |
387 // locally | 387 // locally |
388 if ($postdata['text']){ | 388 if ($postdata['text']){ |
389 $date = date('Y_m_d_H_i_s', time()); | 389 $date = date('Y_m_d_H_i_s', time()); |
390 if ( file_exists("data/parsing_files/".$postdata['filename'].".txt") ) { | 390 if ( file_exists("data/parsing_files/".$postdata['filename'].".txt") ) { |
391 $oldFile = file_get_contents("data/parsing_files/".$postdata['filename'].".txt"); | 391 $oldFile = file_get_contents("data/parsing_files/".$postdata['filename'].".txt"); |
392 file_put_contents("data/parsing_files/".$postdata['filename']."_".$date.".txt", $oldFile); | 392 file_put_contents("data/parsing_files/".$postdata['filename']."_".$date.".txt", $oldFile); |
393 } | 393 } |
394 | 394 |
395 if (get_magic_quotes_gpc()) { | 395 if (get_magic_quotes_gpc()) { |
396 $require = stripslashes($postdata['text']); | 396 $require = stripslashes($postdata['text']); |
397 } else { | 397 } else { |
398 $require = $postdata['text']; | 398 $require = $postdata['text']; |
399 } | 399 } |
400 | 400 |
401 $require = preg_replace("/【<a(.*?)>(.*?)<\/a>】/u", "【\\2】", $require); | 401 $require = preg_replace("/【<a(.*?)>(.*?)<\/a>】/u", "【\\2】", $require); |
402 $require = preg_replace('/&/u', "&", $require); | 402 $require = preg_replace('/&/u', "&", $require); |
403 $require = preg_replace("/○/u", " ", $require); | 403 $require = preg_replace('/ /u', " ", $require); //avoiding invaild xml format |
404 $require = preg_replace("/○/u", " ", $require); | |
404 $require = preg_replace("/<br>/u", "\n", $require); | 405 $require = preg_replace("/<br>/u", "\n", $require); |
405 $require = preg_replace("/<br>/u", "\n", $require); | 406 $require = preg_replace("/<br>/u", "\n", $require); |
406 file_put_contents("data/parsing_files/".$postdata['filename'].".txt", $require); | 407 file_put_contents("data/parsing_files/".$postdata['filename'].".txt", $require); |
407 } | 408 } |
408 | 409 |
409 } | 410 } |
410 | 411 |
411 | 412 |
412 public function UpdateInfoResponsedFromLGService($response) { | 413 public function UpdateInfoResponsedFromLGService($response) { |
413 /** | 414 /** |
414 * | 415 * |
415 */ | 416 */ |
416 | 417 |
417 if (isset($response["file"])) { | 418 if (isset($response["file"])) { |
418 $response_file = $response["file"]; | 419 $response_file = $response["file"]; |
419 } | 420 } |
422 } | 423 } |
423 | 424 |
424 $status = (string)$response["status"]; | 425 $status = (string)$response["status"]; |
425 if ($status == "ok") { | 426 if ($status == "ok") { |
426 $this->messages['info'] .= "[Info] Saving success. <br>"; | 427 $this->messages['info'] .= "[Info] Saving success. <br>"; |
427 $this->file_id = (string)$response_file["id"]; | 428 $this->file_id = (string)$response_file["id"]; |
428 $this->branch_id = (string)$response_branch["id"]; | 429 $this->branch_id = (string)$response_branch["id"]; |
429 | 430 |
430 } else if ($status == "error") { | 431 } else if ($status == "error") { |
431 // saving not success | 432 // saving not success |
432 $this->messages['error'] .= "[Error] Saving Failed! ".(string)$response["message"]."<br>"; | 433 $this->messages['error'] .= "[Error] Saving Failed! ".(string)$response["message"]."<br>"; |
436 // pass the orignal text? | 437 // pass the orignal text? |
437 | 438 |
438 $response_currentFile = $response["currentFile"]; | 439 $response_currentFile = $response["currentFile"]; |
439 $this->current_fileId = (string)$response_currentFile["id"]; | 440 $this->current_fileId = (string)$response_currentFile["id"]; |
440 } | 441 } |
441 | 442 |
442 | 443 |
443 } | 444 } |
444 | 445 |
445 public function ReloadSetting() { | 446 public function ReloadSetting() { |
446 if ($this->current_fileId != 0) { | 447 if ($this->current_fileId != 0) { |
455 $text .= "\n<text>\n"; | 456 $text .= "\n<text>\n"; |
456 // --- topic --- | 457 // --- topic --- |
457 $text .= "<topic>".$this->topic."</topic>\n"; | 458 $text .= "<topic>".$this->topic."</topic>\n"; |
458 // --- book meta data --- | 459 // --- book meta data --- |
459 $book = $this->book_meta; | 460 $book = $this->book_meta; |
460 | 461 |
461 $text .= "<book>\n"; | 462 $text .= "<book>\n"; |
462 $text .= "<id>".$book->id."</id>\n"; | 463 $text .= "<id>".$book->id."</id>\n"; |
463 $text .= "<name>".$book->name."</name>\n"; | 464 $text .= "<name>".$book->name."</name>\n"; |
464 $text .= "<level1>".$book->level1."</level1>\n"; | 465 $text .= "<level1>".$book->level1."</level1>\n"; |
465 $text .= "<level2>".$book->level2."</level2>\n"; | 466 $text .= "<level2>".$book->level2."</level2>\n"; |
471 $text .= "<volume>".$book->volume."</volume>\n"; | 472 $text .= "<volume>".$book->volume."</volume>\n"; |
472 $text .= "<author>".$book->author."</author>\n"; | 473 $text .= "<author>".$book->author."</author>\n"; |
473 $text .= "<edition>".$book->edition."</edition>\n"; | 474 $text .= "<edition>".$book->edition."</edition>\n"; |
474 $text .= "<in_jibengujiku>".$book->in_jibengujiku."</in_jibengujiku>\n"; | 475 $text .= "<in_jibengujiku>".$book->in_jibengujiku."</in_jibengujiku>\n"; |
475 $text .= "<admin_type>".$book->admin_type."</admin_type>\n"; | 476 $text .= "<admin_type>".$book->admin_type."</admin_type>\n"; |
476 | 477 |
477 $text .= "<place_name>".$book->place_name."</place_name>\n"; | 478 $text .= "<place_name>".$book->place_name."</place_name>\n"; |
478 $text .= "<x>".$book->x."</x>\n"; | 479 $text .= "<x>".$book->x."</x>\n"; |
479 $text .= "<y>".$book->y."</y>\n"; | 480 $text .= "<y>".$book->y."</y>\n"; |
480 $text .= "</book>\n"; | 481 $text .= "</book>\n"; |
481 | 482 |
482 | 483 |
483 // --- section info --- | 484 // --- section info --- |
484 $text .= "<section>\n"; | 485 $text .= "<section>\n"; |
485 $text .= "<id>".$this->section_id."</id>\n"; | 486 $text .= "<id>".$this->section_id."</id>\n"; |
486 $text .= "<name>".$this->section_name."</name>\n"; | 487 $text .= "<name>".$this->section_name."</name>\n"; |
513 } | 514 } |
514 | 515 |
515 public function SaveFullTextToLGService($_postdata) { | 516 public function SaveFullTextToLGService($_postdata) { |
516 /** | 517 /** |
517 * It saves the full tagged text string in postdata to LGService. | 518 * It saves the full tagged text string in postdata to LGService. |
518 * The responses returned by LGService will later be handled in | 519 * The responses returned by LGService will later be handled in |
519 */ | 520 */ |
520 | 521 |
521 // -------- | 522 // -------- |
522 global $AT_LOCAL; | 523 global $AT_LOCAL; |
523 | 524 |
528 //saving in my local machine in developing phrase | 529 //saving in my local machine in developing phrase |
529 if ($AT_LOCAL) { | 530 if ($AT_LOCAL) { |
530 file_put_contents("data/parsing_files/".$_postdata['sectionId']."_".$date.".txt", $oldFile); | 531 file_put_contents("data/parsing_files/".$_postdata['sectionId']."_".$date.".txt", $oldFile); |
531 } | 532 } |
532 } | 533 } |
533 | 534 |
534 if (get_magic_quotes_gpc()) { | 535 if (get_magic_quotes_gpc()) { |
535 $require = stripslashes($_postdata['text']); | 536 $require = stripslashes($_postdata['text']); |
536 } else { | 537 } else { |
537 $require = $_postdata['text']; | 538 $require = $_postdata['text']; |
538 } | 539 } |
539 | 540 |
540 $require = preg_replace("/【<a(.*?)>(.*?)<\/a>】/u", "【\\2】", $require); | 541 $require = preg_replace("/【<a(.*?)>(.*?)<\/a>】/u", "【\\2】", $require); |
541 $require = preg_replace('/&/u', "&", $require); | 542 $require = preg_replace('/&/u', "&", $require); |
542 $require = preg_replace("/○/u", " ", $require); | 543 $require = preg_replace('/ /u', " ", $require); //avoiding invaild xml format |
544 $require = preg_replace("/○/u", " ", $require); | |
543 $require = preg_replace("/<br(.*?)>/u", "\n", $require); | 545 $require = preg_replace("/<br(.*?)>/u", "\n", $require); |
544 //$require = preg_replace("/<br>/u", "\n", $require); | 546 //$require = preg_replace("/<br>/u", "\n", $require); |
545 | 547 |
546 $require = "<text_content>".$require."</text_content>\n"; | 548 $require = "<text_content>".$require."</text_content>\n"; |
547 | 549 |
548 /* | 550 /* |
549 if ($_postdata['branchId'] == 0) { | 551 if ($_postdata['branchId'] == 0) { |
550 // -- new branch case | 552 // -- new branch case |
551 $require = "<text_content>".$require."</text_content>\n"; | 553 $require = "<text_content>".$require."</text_content>\n"; |
552 } | 554 } |
553 */ | 555 */ |
554 | 556 |
555 $require = $this->AppendMetadata($require); | 557 $require = $this->AppendMetadata($require); |
556 | 558 |
557 //saving in my local machine in developing phrase | 559 //saving in my local machine in developing phrase |
558 if ($AT_LOCAL) { | 560 if ($AT_LOCAL) { |
559 file_put_contents("data/parsing_files/".$_postdata['sectionId'].".txt", $require); | 561 file_put_contents("data/parsing_files/".$_postdata['sectionId'].".txt", $require); |
560 } | 562 } |
561 } | 563 } |
562 | 564 |
563 // ------ | 565 // ------ |
564 | 566 |
565 | 567 |
566 if ($_postdata['branchId'] != 0) { | 568 if ($_postdata['branchId'] != 0) { |
567 // -- exiting branch case | 569 // -- exiting branch case |
568 $postfields = array( | 570 $postfields = array( |
569 "text" => $require, | 571 "text" => $require, |
570 "branchId" => $_postdata['branchId'], | 572 "branchId" => $_postdata['branchId'], |
571 "userId" => $_postdata['userId'], | 573 "userId" => $_postdata['userId'], |
572 "userPreviousFileId" => $_postdata['fileId'], | 574 "userPreviousFileId" => $_postdata['fileId'], |
573 ); | 575 ); |
574 $save_url = $this->save_to_LGService_url; | 576 $save_url = $this->save_to_LGService_url; |
575 | 577 |
576 } else { | 578 } else { |
577 // -- new branch case | 579 // -- new branch case |
578 // echo "saveNew!"; | 580 // echo "saveNew!"; |
579 $user_id = "0"; | 581 $user_id = "0"; |
580 if ($_postdata['userId']) { | 582 if ($_postdata['userId']) { |
581 $user_id = $_postdata['userId']; | 583 $user_id = $_postdata['userId']; |
582 } | 584 } |
583 | 585 |
584 $postfields = array( | 586 $postfields = array( |
585 "text" => $require, | 587 "text" => $require, |
586 "sectionId" => $_postdata['sectionId'], | 588 "sectionId" => $_postdata['sectionId'], |
587 "userId" => $user_id, | 589 "userId" => $user_id, |
588 "label" => $_postdata['label'], | 590 "label" => $_postdata['label'], |
589 ); | 591 ); |
590 $save_url = $this->save_new_to_LGService_url; | 592 $save_url = $this->save_new_to_LGService_url; |
591 } | 593 } |
592 | 594 |
593 | 595 |
594 // set up the curl resource | 596 // set up the curl resource |
595 $ch = curl_init(); | 597 $ch = curl_init(); |
596 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | 598 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
597 curl_setopt($ch, CURLOPT_URL, $save_url); | 599 curl_setopt($ch, CURLOPT_URL, $save_url); |
598 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 600 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
599 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | 601 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
600 curl_setopt($ch, CURLOPT_POST, true); | 602 curl_setopt($ch, CURLOPT_POST, true); |
601 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); | 603 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); |
602 //curl_setopt($ch, CURLOPT_HEADER, true); | 604 //curl_setopt($ch, CURLOPT_HEADER, true); |
603 curl_setopt($ch, CURLOPT_HTTPHEADER, array( | 605 curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
604 'Content-type: multipart/form-data;charset=utf-8' | 606 'Content-type: multipart/form-data;charset=utf-8' |
605 )); | 607 )); |
606 | 608 |
607 //if (!$AT_LOCAL) { | 609 //if (!$AT_LOCAL) { |
608 // execute the request | 610 // execute the request |
609 $output = curl_exec($ch); | 611 $output = curl_exec($ch); |
610 //} | 612 //} |
613 // output the profile information - includes the header | 615 // output the profile information - includes the header |
614 //echo($output) . PHP_EOL; | 616 //echo($output) . PHP_EOL; |
615 | 617 |
616 // close curl resource to free up system resources | 618 // close curl resource to free up system resources |
617 curl_close($ch); | 619 curl_close($ch); |
618 | 620 |
619 $response = json_decode($output, true); | 621 $response = json_decode($output, true); |
620 return $response; | 622 return $response; |
621 | 623 |
622 } | 624 } |
623 | 625 |
638 } | 640 } |
639 public function LoadSmartRegex($topic_id) { | 641 public function LoadSmartRegex($topic_id) { |
640 | 642 |
641 // Load regex file based on current topic. Only shows the regex in this topic -- | 643 // Load regex file based on current topic. Only shows the regex in this topic -- |
642 $filenames = $this->GetRegexFilenameById($topic_id); | 644 $filenames = $this->GetRegexFilenameById($topic_id); |
643 | 645 |
644 // Get regex file from filesystem ---- | 646 // Get regex file from filesystem ---- |
645 $data_path = "./data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" | 647 $data_path = "./data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" |
646 $returnArray = array(); | 648 $returnArray = array(); |
647 $files1 = scandir($data_path."regex_files"); | 649 $files1 = scandir($data_path."regex_files"); |
648 foreach ( $files1 as $file ) { | 650 foreach ( $files1 as $file ) { |
664 if ($_postdata['text']){ | 666 if ($_postdata['text']){ |
665 | 667 |
666 // --- update topic_regex_relation table --- | 668 // --- update topic_regex_relation table --- |
667 $topic_id = $_postdata['topic_id']; | 669 $topic_id = $_postdata['topic_id']; |
668 $filename = $_postdata['filename'].'.txt'; | 670 $filename = $_postdata['filename'].'.txt'; |
669 | 671 |
670 // check if already existing a record for this regex file | 672 // check if already existing a record for this regex file |
671 $query = "SELECT * FROM TopicRegexRelation WHERE topic_id=".$topic_id." AND regex_filename='".$filename."'"; | 673 $query = "SELECT * FROM TopicRegexRelation WHERE topic_id=".$topic_id." AND regex_filename='".$filename."'"; |
672 $result = mysql_query($query); | 674 $result = mysql_query($query); |
673 if (!$result) { | 675 if (!$result) { |
674 echo json_encode("ErrorDB"); | 676 echo json_encode("ErrorDB"); |
677 if ($row && !$_postdata['forcesave']) { | 679 if ($row && !$_postdata['forcesave']) { |
678 // promt user a comfirm to force saving or not | 680 // promt user a comfirm to force saving or not |
679 echo json_encode("ForceSave"); | 681 echo json_encode("ForceSave"); |
680 return; | 682 return; |
681 } | 683 } |
682 } | 684 } |
683 | 685 |
684 | 686 |
685 if (!$_postdata['forcesave']) { | 687 if (!$_postdata['forcesave']) { |
686 $query = "INSERT INTO TopicRegexRelation (topic_id, regex_filename) VALUES (".$topic_id.",'".$filename."')"; | 688 $query = "INSERT INTO TopicRegexRelation (topic_id, regex_filename) VALUES (".$topic_id.",'".$filename."')"; |
687 $result = mysql_query($query); | 689 $result = mysql_query($query); |
688 if (!$result) { | 690 if (!$result) { |
689 echo json_encode("ErrorDB"); | 691 echo json_encode("ErrorDB"); |
690 } | 692 } |
691 } | 693 } |
692 // --- write to filesystem --- | 694 // --- write to filesystem --- |
693 | 695 |
694 /* | 696 /* |
695 $date = date('Y_m_d_H_i_s', time()); | 697 $date = date('Y_m_d_H_i_s', time()); |
696 if ( file_exists("regex_files/".$_POST['filename'].".txt") ) { | 698 if ( file_exists("regex_files/".$_POST['filename'].".txt") ) { |
697 $oldFile = file_get_contents("regex_files/".$_POST['filename'].".txt"); | 699 $oldFile = file_get_contents("regex_files/".$_POST['filename'].".txt"); |
698 file_put_contents("regex_files/".$_POST['filename']."_".$date.".txt", $oldFile); | 700 file_put_contents("regex_files/".$_POST['filename']."_".$date.".txt", $oldFile); |
699 } | 701 } |
700 */ | 702 */ |
701 $data_path = "./data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" | 703 $data_path = "./data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" |
702 | 704 |
703 if (get_magic_quotes_gpc()) { | 705 if (get_magic_quotes_gpc()) { |
704 $require = stripslashes($_postdata['text']); | 706 $require = stripslashes($_postdata['text']); |
705 } else { | 707 } else { |
706 $require = $_postdata['text']; | 708 $require = $_postdata['text']; |
707 } | 709 } |
708 | 710 |
709 file_put_contents( $data_path."regex_files/".$_postdata['filename'].".txt", $require); | 711 file_put_contents( $data_path."regex_files/".$_postdata['filename'].".txt", $require); |
710 | 712 |
711 | 713 |
712 } | 714 } |
713 } | 715 } |
714 | 716 |
768 } else { | 770 } else { |
769 $outputTableArray[$count][0][$tag_name] = $matchesValue[1]; | 771 $outputTableArray[$count][0][$tag_name] = $matchesValue[1]; |
770 } | 772 } |
771 } | 773 } |
772 } | 774 } |
773 $otherString = preg_replace("/<".$tag_name.">(.*?)<\/".$tag_name.">/u", " ", $otherString); | 775 $otherString = preg_replace("/<".$tag_name.">(.*?)<\/".$tag_name.">/u", " ", $otherString); |
774 } | 776 } |
775 } | 777 } |
776 $otherString = preg_replace("/○/u", "", $otherString); | 778 $otherString = preg_replace("/○/u", "", $otherString); |
777 $outputTableArray[$count]["other"] = $otherString; | 779 $outputTableArray[$count]["other"] = $otherString; |
778 $outputTableArray[$count]["page"] = $pageNow; | 780 $outputTableArray[$count]["page"] = $pageNow; |
779 $value = preg_replace("/>/u", ">", $value); | 781 $value = preg_replace("/>/u", ">", $value); |
780 $value = preg_replace("/</u", "<", $value); | 782 $value = preg_replace("/</u", "<", $value); |
781 $outputTableArray[$count]["full"] = $value; | 783 $outputTableArray[$count]["full"] = $value; |
782 } | 784 } |
783 | 785 |
784 | 786 |
785 | 787 |
786 foreach ( $outputTableArray as $arrayIndex => $arrayValue ) { | 788 foreach ( $outputTableArray as $arrayIndex => $arrayValue ) { |
787 // output each row which the topic tag | 789 // output each row which the topic tag |
788 // e.g. the original version is for 'person' | 790 // e.g. the original version is for 'person' |
789 if ( !isset($arrayValue[0][$_topic_tag]) ) { | 791 if ( !isset($arrayValue[0][$_topic_tag]) ) { |
790 unset($outputTableArray[$arrayIndex]); | 792 unset($outputTableArray[$arrayIndex]); |
791 } | 793 } |
792 | 794 |
793 } | 795 } |
794 // ----- | 796 // ----- |
795 | 797 |
796 return $outputTableArray; | 798 return $outputTableArray; |
797 } | 799 } |
798 | 800 |
799 // === for export table === | 801 // === for export table === |
882 $data['largest_id'] = $largest_id; | 884 $data['largest_id'] = $largest_id; |
883 $data['topic_tag_id'] = $topic_tag_id; | 885 $data['topic_tag_id'] = $topic_tag_id; |
884 | 886 |
885 | 887 |
886 return $data; | 888 return $data; |
887 | 889 |
888 } | 890 } |
889 | 891 |
890 private function GetTag($_postdata) { | 892 private function GetTag($_postdata) { |
891 if (get_magic_quotes_gpc()) { | 893 if (get_magic_quotes_gpc()) { |
892 $id = stripslashes($_postdata['id']); | 894 $id = stripslashes($_postdata['id']); |
901 } | 903 } |
902 return array($id, $name, $tag, $color); | 904 return array($id, $name, $tag, $color); |
903 } | 905 } |
904 | 906 |
905 public function NewTagElement($postdata) { | 907 public function NewTagElement($postdata) { |
906 if ($postdata['id']){ | 908 if ($postdata['id']){ |
907 list($id, $name, $tag, $color) = $this->GetTag($postdata); | 909 list($id, $name, $tag, $color) = $this->GetTag($postdata); |
908 | 910 |
909 // for creationDate and lastChangeDate | 911 // for creationDate and lastChangeDate |
910 $date = date('Y-m-d H:i:s', time()); | 912 $date = date('Y-m-d H:i:s', time()); |
911 | 913 |
915 $this->GetSQLValueString($tag, "text"), | 917 $this->GetSQLValueString($tag, "text"), |
916 $this->GetSQLValueString($color, "text"), | 918 $this->GetSQLValueString($color, "text"), |
917 $this->GetSQLValueString($date, "date"), | 919 $this->GetSQLValueString($date, "date"), |
918 $this->GetSQLValueString($date, "date")); | 920 $this->GetSQLValueString($date, "date")); |
919 //$this->GetSQLValueString($this->systemNAME, "text")); | 921 //$this->GetSQLValueString($this->systemNAME, "text")); |
920 | 922 |
921 $result1 = mysql_query($query1); | 923 $result1 = mysql_query($query1); |
922 if (!$result1) { | 924 if (!$result1) { |
923 //echo json_encode("error when insert into Taglist table"); | 925 //echo json_encode("error when insert into Taglist table"); |
924 echo 0; | 926 echo 0; |
925 return; | 927 return; |
926 } | 928 } |
927 | 929 |
928 // add it to topic_tag_relation table | 930 // add it to topic_tag_relation table |
929 $topic_id = $postdata['topic_id']; | 931 $topic_id = $postdata['topic_id']; |
930 | 932 |
931 // tag_id is $id; | 933 // tag_id is $id; |
941 $result = mysql_query($query); | 943 $result = mysql_query($query); |
942 if (!$result) { | 944 if (!$result) { |
943 echo 1; | 945 echo 1; |
944 //echo json_encode("error when insert into TopicTagRelation table"); | 946 //echo json_encode("error when insert into TopicTagRelation table"); |
945 } | 947 } |
946 | 948 |
947 } | 949 } |
948 | 950 |
949 } | 951 } |
950 | 952 |
951 public function SaveTagElement($postdata) { | 953 public function SaveTagElement($postdata) { |
952 if ($postdata['id']){ | 954 if ($postdata['id']){ |
953 list($id, $name, $tag, $color) = $this->GetTag($postdata); | 955 list($id, $name, $tag, $color) = $this->GetTag($postdata); |
954 | 956 |
955 // for lastChangeDate | 957 // for lastChangeDate |
956 $date = date('Y-m-d H:i:s', time()); | 958 $date = date('Y-m-d H:i:s', time()); |
957 | 959 |
958 $query = sprintf("UPDATE Taglist SET `name`=%s, `tag`=%s, `color`=%s , `lastChangeDate`=%s WHERE `id`=%s", | 960 $query = sprintf("UPDATE Taglist SET `name`=%s, `tag`=%s, `color`=%s , `lastChangeDate`=%s WHERE `id`=%s", |
959 $this->GetSQLValueString($name, "text"), | 961 $this->GetSQLValueString($name, "text"), |
960 $this->GetSQLValueString($tag, "text"), | 962 $this->GetSQLValueString($tag, "text"), |
961 $this->GetSQLValueString($color, "text"), | 963 $this->GetSQLValueString($color, "text"), |
962 $this->GetSQLValueString($date, "date"), | 964 $this->GetSQLValueString($date, "date"), |
963 $this->GetSQLValueString($id, "int")); | 965 $this->GetSQLValueString($id, "int")); |
964 $result = mysql_query($query); | 966 $result = mysql_query($query); |
965 | 967 |
966 } | 968 } |
967 | 969 |
968 } | 970 } |
969 | 971 |
970 public function DeleteTag($postdata) { | 972 public function DeleteTag($postdata) { |
979 $topic_id = $postdata['topic_id']; | 981 $topic_id = $postdata['topic_id']; |
980 $query = sprintf("DELETE FROM `TopicTagRelation` WHERE `tagId` = %s and `topicId` = %s", stripslashes($tag_id), stripcslashes($topic_id)); | 982 $query = sprintf("DELETE FROM `TopicTagRelation` WHERE `tagId` = %s and `topicId` = %s", stripslashes($tag_id), stripcslashes($topic_id)); |
981 $result = mysql_query($query); | 983 $result = mysql_query($query); |
982 | 984 |
983 } | 985 } |
984 | 986 |
985 } | 987 } |
986 | 988 |
987 // === for config topic === | 989 // === for config topic === |
988 public function CreateNewTopic($_postdata) { | 990 public function CreateNewTopic($_postdata) { |
989 $new_topic_id = "2"; // default topic_id=1 for 'person' topic | 991 $new_topic_id = "2"; // default topic_id=1 for 'person' topic |
990 | 992 |
991 // get user input data from postdata | 993 // get user input data from postdata |
992 $new_topic_name_en = $_postdata['new_topic_name_en']; | 994 $new_topic_name_en = $_postdata['new_topic_name_en']; |
993 $new_topic_name_ch = $_postdata['new_topic_name_ch']; | 995 $new_topic_name_ch = $_postdata['new_topic_name_ch']; |
994 $new_topic_name_pi = $_postdata['new_topic_name_pi']; | 996 $new_topic_name_pi = $_postdata['new_topic_name_pi']; |
995 $new_topic_tag = $_postdata['new_topic_tag']; | 997 $new_topic_tag = $_postdata['new_topic_tag']; |
996 $new_topic_tag_name = $_postdata['new_topic_tag_name']; | 998 $new_topic_tag_name = $_postdata['new_topic_tag_name']; |
997 | 999 |
998 // -- Start Transaction -- | 1000 // -- Start Transaction -- |
999 mysql_query("BEGIN"); | 1001 mysql_query("BEGIN"); |
1000 | 1002 |
1001 // create records for the new topic in database | 1003 // create records for the new topic in database |
1002 /* In table: | 1004 /* In table: |
1003 taglist: name, tag, color (default, user can change later in the editing page) | 1005 taglist: name, tag, color (default, user can change later in the editing page) |
1004 topics: tag, name_en, name_ch, name_pinyin | 1006 topics: tag, name_en, name_ch, name_pinyin |
1005 topic_tag_relation: topic_id (new), tag_id (new) | 1007 topic_tag_relation: topic_id (new), tag_id (new) |
1006 | 1008 |
1007 - need transaction for the following actions: | 1009 - need transaction for the following actions: |
1008 taglist.name: topic_tag name in chinese | 1010 taglist.name: topic_tag name in chinese |
1009 taglist.tag: topic_tag name in eng | 1011 taglist.tag: topic_tag name in eng |
1010 topics.tag: (the same a taglist.tag) | 1012 topics.tag: (the same a taglist.tag) |
1011 topics.name_en: | 1013 topics.name_en: |
1012 topics.name_ch: | 1014 topics.name_ch: |
1013 topics.name_pinyin: | 1015 topics.name_pinyin: |
1014 | 1016 |
1015 | 1017 |
1016 topic_tag_relation.topic_id = new_topic_id | 1018 topic_tag_relation.topic_id = new_topic_id |
1017 topic_tag_relation.tag_id = new_tag_id | 1019 topic_tag_relation.tag_id = new_tag_id |
1018 which are the new record that just been created in the 'taglist' and 'topics' table | 1020 which are the new record that just been created in the 'taglist' and 'topics' table |
1019 */ | 1021 */ |
1020 | 1022 |
1021 // new record in topics table | 1023 // new record in topics table |
1022 $query="INSERT INTO Topic (tag, name_en, name_ch, name_pinyin) VALUES ('".$new_topic_tag."','".$new_topic_name_en."','".$new_topic_name_ch."','".$new_topic_name_pi."')"; | 1024 $query="INSERT INTO Topic (tag, name_en, name_ch, name_pinyin) VALUES ('".$new_topic_tag."','".$new_topic_name_en."','".$new_topic_name_ch."','".$new_topic_name_pi."')"; |
1023 $result_topics=mysql_query($query); | 1025 $result_topics=mysql_query($query); |
1024 if($result_topics){ | 1026 if($result_topics){ |
1025 $new_topic_id = mysql_insert_id(); | 1027 $new_topic_id = mysql_insert_id(); |
1026 } else { | 1028 } else { |
1027 //echo mysql_error(); | 1029 //echo mysql_error(); |
1028 } | 1030 } |
1029 | 1031 |
1030 // new record in taglist table | 1032 // new record in taglist table |
1031 $query="INSERT INTO Taglist (name, tag, color) VALUES ('".$new_topic_tag_name."','".$new_topic_tag."','rgb(255, 0, 174)')"; | 1033 $query="INSERT INTO Taglist (name, tag, color) VALUES ('".$new_topic_tag_name."','".$new_topic_tag."','rgb(255, 0, 174)')"; |
1032 $result_taglist=mysql_query($query); | 1034 $result_taglist=mysql_query($query); |
1033 if($result_taglist){ | 1035 if($result_taglist){ |
1034 $new_tag_id = mysql_insert_id(); | 1036 $new_tag_id = mysql_insert_id(); |
1035 } else { | 1037 } else { |
1036 //echo mysql_error(); | 1038 //echo mysql_error(); |
1037 } | 1039 } |
1038 | 1040 |
1039 // new record in topic_tag_relation | 1041 // new record in topic_tag_relation |
1040 $query="INSERT INTO TopicTagRelation (topicId, tagId) VALUES ('".$new_topic_id."','".$new_tag_id."')"; | 1042 $query="INSERT INTO TopicTagRelation (topicId, tagId) VALUES ('".$new_topic_id."','".$new_tag_id."')"; |
1041 $result_topic_tag_relation=mysql_query($query); | 1043 $result_topic_tag_relation=mysql_query($query); |
1042 if($result_topic_tag_relation){ | 1044 if($result_topic_tag_relation){ |
1043 | 1045 |
1044 } else { | 1046 } else { |
1045 //echo mysql_error(); | 1047 //echo mysql_error(); |
1046 } | 1048 } |
1047 | 1049 |
1048 | 1050 |
1049 // --- End Transaction --- | 1051 // --- End Transaction --- |
1050 if ($result_taglist && $result_topics && $result_topic_tag_relation) { | 1052 if ($result_taglist && $result_topics && $result_topic_tag_relation) { |
1051 mysql_query("COMMIT"); | 1053 mysql_query("COMMIT"); |
1052 | 1054 |
1053 } else { | 1055 } else { |
1054 mysql_query("ROLLBACK"); | 1056 mysql_query("ROLLBACK"); |
1055 return "error"; | 1057 return "error"; |
1056 } | 1058 } |
1057 | 1059 |
1058 | 1060 |
1059 $_postdata['topic'] = $new_topic_id; | 1061 $_postdata['topic'] = $new_topic_id; |
1060 return $_postdata; | 1062 return $_postdata; |
1061 } | 1063 } |
1062 | 1064 |
1063 public function ConfigTagsInTopic($_postdata) { | 1065 public function ConfigTagsInTopic($_postdata) { |
1064 /** | 1066 /** |
1065 * | 1067 * |
1066 */ | 1068 */ |
1067 | 1069 |
1068 $topic = $_postdata['topic']; | 1070 $topic = $_postdata['topic']; |
1069 $result = $this->GetTopicById($topic); | 1071 $result = $this->GetTopicById($topic); |
1070 $row = mysql_fetch_assoc($result); | 1072 $row = mysql_fetch_assoc($result); |
1096 $query = "SELECT Taglist.*, TopicTagRelation.topicId FROM Taglist LEFT JOIN TopicTagRelation ON Taglist.id = TopicTagRelation.tagId ORDER BY `topicId`"; | 1098 $query = "SELECT Taglist.*, TopicTagRelation.topicId FROM Taglist LEFT JOIN TopicTagRelation ON Taglist.id = TopicTagRelation.tagId ORDER BY `topicId`"; |
1097 $result = mysql_query($query); | 1099 $result = mysql_query($query); |
1098 if (!$result) { | 1100 if (!$result) { |
1099 return json_encode("Failed during selecting/joining Taglist and TopicTagRelation table."); | 1101 return json_encode("Failed during selecting/joining Taglist and TopicTagRelation table."); |
1100 } | 1102 } |
1101 | 1103 |
1102 $tag_intopic = array(); | 1104 $tag_intopic = array(); |
1103 $tag_others = array(); | 1105 $tag_others = array(); |
1104 $tag_tmp_others = array(); | 1106 $tag_tmp_others = array(); |
1105 while ($row = mysql_fetch_assoc($result)) { | 1107 while ($row = mysql_fetch_assoc($result)) { |
1106 // should exclude the topic tag, which is in either $topic_intopic nor $tag_tmp_others | 1108 // should exclude the topic tag, which is in either $topic_intopic nor $tag_tmp_others |
1113 } | 1115 } |
1114 } | 1116 } |
1115 | 1117 |
1116 // --- check if there's any duplicated tags in tag_tmp_others. remove the duplicated tags | 1118 // --- check if there's any duplicated tags in tag_tmp_others. remove the duplicated tags |
1117 $num_others = count($tag_tmp_others); | 1119 $num_others = count($tag_tmp_others); |
1118 for ($i=0; $i < $num_others; $i++) { | 1120 for ($i=0; $i < $num_others; $i++) { |
1119 $cnt = 0; | 1121 $cnt = 0; |
1120 for ($j=$i+1; $j < $num_others; $j++) { | 1122 for ($j=$i+1; $j < $num_others; $j++) { |
1121 if ($tag_tmp_others[$i]['tag'] == $tag_tmp_others[$j]['tag']) { | 1123 if ($tag_tmp_others[$i]['tag'] == $tag_tmp_others[$j]['tag']) { |
1122 break; | 1124 break; |
1123 } else { | 1125 } else { |
1124 $cnt++; | 1126 $cnt++; |
1125 } | 1127 } |
1131 } | 1133 } |
1132 | 1134 |
1133 // --- remove duplicated tags in tag_others that is duplicated with tags in tag_intopic | 1135 // --- remove duplicated tags in tag_others that is duplicated with tags in tag_intopic |
1134 $tag_tmp_others = $tag_others; | 1136 $tag_tmp_others = $tag_others; |
1135 $tag_others = array(); | 1137 $tag_others = array(); |
1136 | 1138 |
1137 foreach ($tag_tmp_others as $tmp) { | 1139 foreach ($tag_tmp_others as $tmp) { |
1138 $cnt = 0; | 1140 $cnt = 0; |
1139 foreach ($tag_intopic as $intopic) { | 1141 foreach ($tag_intopic as $intopic) { |
1140 if ($tmp['tag'] == $intopic['tag']) { | 1142 if ($tmp['tag'] == $intopic['tag']) { |
1141 break; | 1143 break; |
1146 if ($cnt == count($tag_intopic)) { | 1148 if ($cnt == count($tag_intopic)) { |
1147 // not appear in $tag_intopic | 1149 // not appear in $tag_intopic |
1148 array_push($tag_others, $tmp); | 1150 array_push($tag_others, $tmp); |
1149 } | 1151 } |
1150 } | 1152 } |
1151 | 1153 |
1152 // ----- | 1154 // ----- |
1153 | 1155 |
1154 | 1156 |
1155 $data = array(); | 1157 $data = array(); |
1156 //$data['taglistArray'] = $taglistArray; | 1158 //$data['taglistArray'] = $taglistArray; |
1169 $topic_id = $_postdata['topic_id']; | 1171 $topic_id = $_postdata['topic_id']; |
1170 $tag_ids = json_decode(str_replace('\\', '', $_postdata['ids'])); | 1172 $tag_ids = json_decode(str_replace('\\', '', $_postdata['ids'])); |
1171 | 1173 |
1172 // update topic_tag_relation by tags_ids array as `tag_id` and topic_id as `topic_id` | 1174 // update topic_tag_relation by tags_ids array as `tag_id` and topic_id as `topic_id` |
1173 | 1175 |
1174 | 1176 |
1175 // --- add new topic_tag_relation --- | 1177 // --- add new topic_tag_relation --- |
1176 foreach ($tag_ids as $tag_id) { | 1178 foreach ($tag_ids as $tag_id) { |
1177 $query = "SELECT * FROM TopicTagRelation WHERE tagId=".$tag_id; | 1179 $query = "SELECT * FROM TopicTagRelation WHERE tagId=".$tag_id; |
1178 $result = mysql_query($query); | 1180 $result = mysql_query($query); |
1179 if (!$result) { | 1181 if (!$result) { |
1189 } | 1191 } |
1190 if (!$flag) { | 1192 if (!$flag) { |
1191 // insert a row into topic_tag_relation table | 1193 // insert a row into topic_tag_relation table |
1192 // $queryUpdate = "INSERT INTO TopicTagRelation (topicId, tagId) VALUES (".$topic_id.",".$tag_id.")"; | 1194 // $queryUpdate = "INSERT INTO TopicTagRelation (topicId, tagId) VALUES (".$topic_id.",".$tag_id.")"; |
1193 | 1195 |
1194 $date = date('Y-m-d H:i:s', time()); | 1196 $date = date('Y-m-d H:i:s', time()); |
1195 $queryUpdate = sprintf("INSERT INTO TopicTagRelation (`topicId`, `tagId`, `creationDate`, `lastChangeDate`) VALUES (%s, %s, %s, %s)", | 1197 $queryUpdate = sprintf("INSERT INTO TopicTagRelation (`topicId`, `tagId`, `creationDate`, `lastChangeDate`) VALUES (%s, %s, %s, %s)", |
1196 $this->GetSQLValueString($topic_id, "int"), | 1198 $this->GetSQLValueString($topic_id, "int"), |
1197 $this->GetSQLValueString($tag_id, "int"), | 1199 $this->GetSQLValueString($tag_id, "int"), |
1198 $this->GetSQLValueString($date, "date"), | 1200 $this->GetSQLValueString($date, "date"), |
1199 $this->GetSQLValueString($date, "date")); | 1201 $this->GetSQLValueString($date, "date")); |
1200 | 1202 |
1201 $resultUpdate = mysql_query($queryUpdate); | 1203 $resultUpdate = mysql_query($queryUpdate); |
1202 if (!$resultUpdate) { | 1204 if (!$resultUpdate) { |
1203 return json_encode("error when insert TopicTagRelation table"); | 1205 return json_encode("error when insert TopicTagRelation table"); |
1204 } | 1206 } |
1205 } | 1207 } |
1206 | 1208 |
1207 } | 1209 } |
1208 | 1210 |
1209 | 1211 |
1300 $row = mysql_fetch_assoc($result); | 1302 $row = mysql_fetch_assoc($result); |
1301 $tag = $row['tag']; | 1303 $tag = $row['tag']; |
1302 | 1304 |
1303 // TODO need to reconstruct the structure for topic tag considering user may use Uppercase but only lowercase accpetable in html tag that why we store only lowercase in Taglist.tag field in database table. | 1305 // TODO need to reconstruct the structure for topic tag considering user may use Uppercase but only lowercase accpetable in html tag that why we store only lowercase in Taglist.tag field in database table. |
1304 // Temporate solution is we replace " " with "_" and .lowercase(), which are as the same as we do in LGServices when create a topic and also create the correpsonding topic tag | 1306 // Temporate solution is we replace " " with "_" and .lowercase(), which are as the same as we do in LGServices when create a topic and also create the correpsonding topic tag |
1305 | 1307 |
1306 $tag = str_replace(' ', '_', $tag); | 1308 $tag = str_replace(' ', '_', $tag); |
1307 return strtolower($tag); | 1309 return strtolower($tag); |
1308 | 1310 |
1309 //return $tag; | 1311 //return $tag; |
1310 } | 1312 } |
1325 } | 1327 } |
1326 | 1328 |
1327 | 1329 |
1328 | 1330 |
1329 | 1331 |
1330 // =========================== | 1332 // =========================== |
1331 | 1333 |
1332 // === for manage wordlist === | 1334 // === for manage wordlist === |
1333 public function EditWordlist() { | 1335 public function EditWordlist() { |
1334 /** | 1336 /** |
1335 * EditWordlist shows the wordlist in a new window. | 1337 * EditWordlist shows the wordlist in a new window. |
1347 return $data; | 1349 return $data; |
1348 } | 1350 } |
1349 | 1351 |
1350 | 1352 |
1351 public function AddNewList($postdata) { | 1353 public function AddNewList($postdata) { |
1352 if ($postdata['text']){ | 1354 if ($postdata['text']){ |
1353 if (get_magic_quotes_gpc()) { | 1355 if (get_magic_quotes_gpc()) { |
1354 $name = stripslashes($postdata['text']); | 1356 $name = stripslashes($postdata['text']); |
1355 } else { | 1357 } else { |
1356 $name = $postdata['text']; | 1358 $name = $postdata['text']; |
1357 } | 1359 } |
1360 $this->GetSQLValueString($name, "text")); | 1362 $this->GetSQLValueString($name, "text")); |
1361 //$this->GetSQLValueString($this->systemNAME, "text")); | 1363 //$this->GetSQLValueString($this->systemNAME, "text")); |
1362 $result1 = mysql_query($query1); | 1364 $result1 = mysql_query($query1); |
1363 file_put_contents( "data/wordlist/".mysql_insert_id().".txt", "(empty now)"); | 1365 file_put_contents( "data/wordlist/".mysql_insert_id().".txt", "(empty now)"); |
1364 } | 1366 } |
1365 } | 1367 } |
1366 | 1368 |
1367 | 1369 |
1368 public function SaveWordlist($postdata) { | 1370 public function SaveWordlist($postdata) { |
1369 | 1371 |
1370 if ($postdata['text']){ | 1372 if ($postdata['text']){ |
1371 $date = date('Y_m_d_H_i_s', time()); | 1373 $date = date('Y_m_d_H_i_s', time()); |
1372 if ( file_exists("data/wordlist/".$postdata['filename'].".txt") ) { | 1374 if ( file_exists("data/wordlist/".$postdata['filename'].".txt") ) { |
1373 $filename = "data/wordlist/".$postdata['filename']."_".$date.".txt"; | 1375 $filename = "data/wordlist/".$postdata['filename']."_".$date.".txt"; |
1374 $oldFile = file_get_contents("data/wordlist/".$postdata['filename'].".txt"); | 1376 $oldFile = file_get_contents("data/wordlist/".$postdata['filename'].".txt"); |
1375 file_put_contents($filename, $oldFile); | 1377 file_put_contents($filename, $oldFile); |
1376 } | 1378 } |
1377 /*else { | 1379 /*else { |
1378 $filename = "data/wordlist/".$postdata['filename'].".txt"; | 1380 $filename = "data/wordlist/".$postdata['filename'].".txt"; |
1379 } | 1381 } |
1380 */ | 1382 */ |
1381 | 1383 |
1394 $date = date('Y_m_d_H_i_s', time()); | 1396 $date = date('Y_m_d_H_i_s', time()); |
1395 if ( file_exists("data/wordlist/".$postdata['filename'].".txt") ) { | 1397 if ( file_exists("data/wordlist/".$postdata['filename'].".txt") ) { |
1396 $oldFile = file_get_contents("data/wordlist/".$postdata['filename'].".txt"); | 1398 $oldFile = file_get_contents("data/wordlist/".$postdata['filename'].".txt"); |
1397 file_put_contents("data/wordlist/".$postdata['filename']."_".$date.".txt", $oldFile); | 1399 file_put_contents("data/wordlist/".$postdata['filename']."_".$date.".txt", $oldFile); |
1398 } | 1400 } |
1399 | 1401 |
1400 if (get_magic_quotes_gpc()) { | 1402 if (get_magic_quotes_gpc()) { |
1401 $require = stripslashes($postdata['text']); | 1403 $require = stripslashes($postdata['text']); |
1402 } else { | 1404 } else { |
1403 $require = $postdata['text']; | 1405 $require = $postdata['text']; |
1404 } | 1406 } |
1422 return $this->section_id; | 1424 return $this->section_id; |
1423 } else { | 1425 } else { |
1424 return json_encode("Error: No section id"); | 1426 return json_encode("Error: No section id"); |
1425 } | 1427 } |
1426 } | 1428 } |
1427 | 1429 |
1428 | 1430 |
1429 /* only used locally */ | 1431 /* only used locally */ |
1430 /* | 1432 /* |
1431 private function GetSectionInfo() { | 1433 private function GetSectionInfo() { |
1432 $section_id = $this->GetSectionId(); | 1434 $section_id = $this->GetSectionId(); |
1434 return $section_id; | 1436 return $section_id; |
1435 } | 1437 } |
1436 | 1438 |
1437 $result = $this->GetSectionsById($section_id); | 1439 $result = $this->GetSectionsById($section_id); |
1438 | 1440 |
1439 | 1441 |
1440 while ($row = mysql_fetch_assoc($result)) { | 1442 while ($row = mysql_fetch_assoc($result)) { |
1441 $bookId=$row['books_id']; | 1443 $bookId=$row['books_id']; |
1442 $startPage=$row['start_page']; | 1444 $startPage=$row['start_page']; |
1443 $endPage=$row['end_page']; | 1445 $endPage=$row['end_page']; |
1444 $sectionName = $row['name']; | 1446 $sectionName = $row['name']; |
1470 $taglistArray = array(); | 1472 $taglistArray = array(); |
1471 foreach ($taglist_infile as $row) { | 1473 foreach ($taglist_infile as $row) { |
1472 array_push($taglistArray, array((string)$row->id,(string)$row->name,(string)$row->tag,(string)$row->color )); | 1474 array_push($taglistArray, array((string)$row->id,(string)$row->name,(string)$row->tag,(string)$row->color )); |
1473 } | 1475 } |
1474 if ($taglistArray) { | 1476 if ($taglistArray) { |
1475 $this->taglist_infile = $taglistArray; | 1477 $this->taglist_infile = $taglistArray; |
1476 } | 1478 } |
1477 | 1479 |
1478 // --- get book meta data from file, replaced by SetBookMetaDataBySectionId(), which retrives book meta data from LGService | 1480 // --- get book meta data from file, replaced by SetBookMetaDataBySectionId(), which retrives book meta data from LGService |
1479 /* | 1481 /* |
1480 $book_meta = $xml->book; | 1482 $book_meta = $xml->book; |
1513 return $new_contentString; | 1515 return $new_contentString; |
1514 } | 1516 } |
1515 | 1517 |
1516 | 1518 |
1517 /** This is only been called by SetTextFromLocal(). | 1519 /** This is only been called by SetTextFromLocal(). |
1518 */ | 1520 */ |
1519 /* | 1521 /* |
1520 private function GetSectionContent() { | 1522 private function GetSectionContent() { |
1521 | 1523 |
1522 $section_id = $this->GetSectionId(); | 1524 $section_id = $this->GetSectionId(); |
1523 $section_info = $this->GetSectionInfo(); | 1525 $section_info = $this->GetSectionInfo(); |
1524 | 1526 |
1525 $bookId = $section_info['bookId']; | 1527 $bookId = $section_info['bookId']; |
1530 | 1532 |
1531 $contentString=""; | 1533 $contentString=""; |
1532 $data_path = $this->GetDataPath(); | 1534 $data_path = $this->GetDataPath(); |
1533 if ( file_exists($data_path."parsing_files/".$section_id.".txt") ) { | 1535 if ( file_exists($data_path."parsing_files/".$section_id.".txt") ) { |
1534 $filename = $data_path."parsing_files/".$section_id.".txt"; | 1536 $filename = $data_path."parsing_files/".$section_id.".txt"; |
1535 | 1537 |
1536 // --- parsing meta data | 1538 // --- parsing meta data |
1537 $stringInput = $this->ParseDataInFile($filename); | 1539 $stringInput = $this->ParseDataInFile($filename); |
1538 // ---- | 1540 // ---- |
1539 | 1541 |
1540 // if the text is from file system | 1542 // if the text is from file system |
1541 $stringInput = preg_replace("/ /u", "○", $stringInput); | 1543 $stringInput = preg_replace("/ /u", "○", $stringInput); |
1542 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | 1544 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); |
1543 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); | 1545 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); |
1544 | 1546 |
1545 $this->branch_id = 1; // testing at local | 1547 $this->branch_id = 1; // testing at local |
1546 | 1548 |
1547 } else { | 1549 } else { |
1548 $query = sprintf("SELECT `content`, `line`, `books_id` FROM `contents` WHERE `books_id`=\"%s\" AND `line`>=%d AND `line`<=%d", $bookId, $startPage, $endPage); | 1550 $query = sprintf("SELECT `content`, `line`, `books_id` FROM `contents` WHERE `books_id`=\"%s\" AND `line`>=%d AND `line`<=%d", $bookId, $startPage, $endPage); |
1549 $result = mysql_query($query); | 1551 $result = mysql_query($query); |
1551 return json_encode("Failed during selecting content table."); | 1553 return json_encode("Failed during selecting content table."); |
1552 } | 1554 } |
1553 while ($row = mysql_fetch_assoc($result)) { | 1555 while ($row = mysql_fetch_assoc($result)) { |
1554 $contentString.="【".$row['line']."】".$row['content']."\n"; | 1556 $contentString.="【".$row['line']."】".$row['content']."\n"; |
1555 } | 1557 } |
1556 | 1558 |
1557 // the text is from database | 1559 // the text is from database |
1558 $stringInput = $contentString; | 1560 $stringInput = $contentString; |
1559 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput); | 1561 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput); |
1560 $stringInput = preg_replace("/ /u", "○", $stringInput); | 1562 $stringInput = preg_replace("/ /u", "○", $stringInput); |
1561 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | 1563 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); |
1565 return $stringInput; | 1567 return $stringInput; |
1566 | 1568 |
1567 } | 1569 } |
1568 */ | 1570 */ |
1569 | 1571 |
1570 | 1572 |
1571 private function GetDataPath() { | 1573 private function GetDataPath() { |
1572 return getcwd()."/data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" | 1574 return getcwd()."/data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" |
1573 } | 1575 } |
1574 | 1576 |
1575 private function GetTaglistArray() { | 1577 private function GetTaglistArray() { |
1590 $listString = preg_replace("/<div>/u", "\n", $listString); | 1592 $listString = preg_replace("/<div>/u", "\n", $listString); |
1591 $listString = preg_replace("/<\/div>/u", "", $listString); | 1593 $listString = preg_replace("/<\/div>/u", "", $listString); |
1592 $listString = preg_replace("/<span(.*?)>/u", "", $listString); | 1594 $listString = preg_replace("/<span(.*?)>/u", "", $listString); |
1593 $listString = preg_replace("/<\/span>/u", "", $listString); | 1595 $listString = preg_replace("/<\/span>/u", "", $listString); |
1594 //$listString = preg_replace("/\n/u", "|", $listString); | 1596 //$listString = preg_replace("/\n/u", "|", $listString); |
1595 | 1597 |
1596 $wordlistArray2 = explode( "\n", $listString ); | 1598 $wordlistArray2 = explode( "\n", $listString ); |
1597 //usort($wordlistArray2,'sortFunction'); | 1599 //usort($wordlistArray2,'sortFunction'); |
1598 foreach ( $wordlistArray2 as $index=>$value ) { | 1600 foreach ( $wordlistArray2 as $index=>$value ) { |
1599 $wordlistArray2[$index] = implode("○?", preg_split("/(?<!^)(?!$)/u", $value)); | 1601 $wordlistArray2[$index] = implode("○?", preg_split("/(?<!^)(?!$)/u", $value)); |
1600 } | 1602 } |
1601 foreach ( $wordlistArray2 as $index=>$value ) { | 1603 foreach ( $wordlistArray2 as $index=>$value ) { |
1602 if ($value=="") unset($wordlistArray2[$index]); | 1604 if ($value=="") unset($wordlistArray2[$index]); |
1603 | 1605 |
1604 } | 1606 } |
1605 $listString = implode("|", $wordlistArray2); | 1607 $listString = implode("|", $wordlistArray2); |
1606 | 1608 |
1607 if ( $listString[0]=="|" ) $listString = substr($listString, 1); | 1609 if ( $listString[0]=="|" ) $listString = substr($listString, 1); |
1608 $wordlistArray[] = array( $row['id'], $row['name'], $listString ); | 1610 $wordlistArray[] = array( $row['id'], $row['name'], $listString ); |
1609 } | 1611 } |
1610 | 1612 |
1611 | 1613 |
1612 return $wordlistArray; | 1614 return $wordlistArray; |
1613 | 1615 |
1614 } | 1616 } |
1615 | 1617 |
1616 | 1618 |
1617 } | 1619 } |
1618 | 1620 |
1619 | 1621 |
1620 | 1622 |
1621 ?> | 1623 ?> |