comparison develop/models/extractapp.php @ 12:e82ca7375e93

Integration with LocalGazetteerService (LGService), getting text from LGService and saving text to LGService. Adjust the control flow (controller).
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 16 Feb 2015 16:51:39 +0100
parents 3d6fba07bfbd
children cc36a20a68ab
comparison
equal deleted inserted replaced
11:3d6fba07bfbd 12:e82ca7375e93
6 6
7 public function Index() { 7 public function Index() {
8 return array("Index Value 1", "Value 2", "Value 3"); 8 return array("Index Value 1", "Value 2", "Value 3");
9 } 9 }
10 10
11 protected $section_id, $data_path; 11 protected $section_id, $data_path, $file_id, $branch_id, $user_id, $lg_tex;
12
13 12
14 private function Initialize($_urlvalues) { 13 private function Initialize($_urlvalues) {
15 $this->SetSectionId($_urlvalues); 14 $this->SetSectionId($_urlvalues);
16 15
17 } 16 }
18 17
18 public function GetTextFromFileId($_postdata) {
19 $file_id = $_postdata['fileId'];
20 $branch_id = $_postdata['branchId'];
21 $section_id = $_postdata['sectionId'];
22
23 $this->branch_id = $branch_id;
24 $this->file_id = $file_id;
25 $this->user_id = $_postdata['userId'];
26 $this->section_id = $section_id;
27
28 // get from URL with file_id
29 $lg_text_url = $this->get_text_from_fileId_url.$file_id;
30 $lg_text = file_get_contents($lg_text_url);
31
32 $stringInput = $lg_text;
33 $stringInput = preg_replace("/ /u", "○", $stringInput);
34 $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
35 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
36 $lg_text = $stringInput;
37
38 $this->lg_text = $lg_text;
39
40 }
41
42 public function GetTextFromSectionId($_postdata) {
43
44 $section_id = $_postdata['sectionId'];
45 $this->section_id = $section_id;
46 $this->user_id = $_postdata['userId'];
47
48 // get from URL with file_id
49 $lg_text_url = $this->get_text_from_sectionId_url.$section_id;
50 $lg_text = file_get_contents($lg_text_url);
51
52 $stringInput = $lg_text;
53 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput);
54 $stringInput = preg_replace("/ /u", "○", $stringInput);
55 $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
56 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
57 $lg_text = $stringInput;
58
59 $this->lg_text = $lg_text;
60 }
61
62 public function GetTextFromLocal($_id){
63 $this->section_id = $_id;
64 echo "DEBUG: from my local"."<br>";
65 $this->lg_text = $this->GetSectionContent();
66
67 }
68
69 public function GetInfoFromPreviousPage($_postdata) {
70
71 if ($_postdata['fileId']) {
72 $this->file_id = $_postdata['fileId'];
73 }
74 if ($_postdata['sectionId']) {
75 $this->section_id = $_postdata['sectionId'];
76 }
77 if ($_postdata['text']) {
78 $this->lg_text = $_postdata['text'];
79 }
80 if ($_postdata['branchId']) {
81 $this->branch_id = $_postdata['branchId'];
82 }
83 if ($_postdata['userId']) {
84 $this->user_id = $_postdata['userId'];
85 }
86
87
88 }
89 public function InitData($_postdata) {
90 $file_id = $_postdata['fileId'];
91 $branch_id = $_postdata['branchId'];
92 $section_id = $_postdata['sectionId'];
93
94 $this->branch_id = $branch_id;
95 $this->file_id = $file_id;
96 $this->user_id = $_postdata['userId'];
97 $this->section_id = $section_id;
98
99 if ($file_id != 0 && $branch_id != 0) {
100 // get from URL with file_id
101 $lg_text_url = $this->get_text_from_fileId_url.$file_id;
102 } else if ($section_id != 0) {
103 // get from URL with section_id
104 $lg_text_url = $this->get_text_from_sectionId_url.$section_id;
105 } else {
106 echo "wrong url!!";
107 return;
108 }
109
110
111 $lg_text = file_get_contents($lg_text_url);
112
113 /*
114 $stringInput = preg_replace("/ /u", "○", $stringInput);
115 $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
116 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
117 */
118
119 // the text is from database
120 $stringInput = $lg_text;
121 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput);
122 $stringInput = preg_replace("/ /u", "○", $stringInput);
123 $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
124 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
125 $lg_text = $stringInput;
126
127 $this->lg_text = $lg_text;
128
129
130 }
131
19 // === for tagging === 132 // === for tagging ===
20 public function StartTagging($urlvalues) { 133 public function StartTagging() {
21 134
22 $this->Initialize($urlvalues); 135 // $this->Initialize($urlvalues);
23 136
24 $section_id = $this->GetSectionId(); 137 $section_id = $this->section_id;
25 138 $stringInput = $this->lg_text;
26 $stringInput = $this->GetSectionContent(); 139
27 140
28 //$taglistArray = $this->GetTaglistArray(); 141 //$taglistArray = $this->GetTaglistArray();
29 //for GetTaglistByTopicID: 142 //for GetTaglistByTopicID:
30
31 $taglistArray = $this->GetTaglistByTopicID($this->GetTopic()); 143 $taglistArray = $this->GetTaglistByTopicID($this->GetTopic());
32 144 // topic list
33 // topic list?
34 $topiclistArray = $this->GetTopiclistArray(); 145 $topiclistArray = $this->GetTopiclistArray();
35 146
36 147
37 $wordlistArray = $this->GetWordlistArray(); 148 $wordlistArray = $this->GetWordlistArray();
38 149
43 $data['section_id'] = $section_id; 154 $data['section_id'] = $section_id;
44 $data['topiclistArray'] = $topiclistArray; 155 $data['topiclistArray'] = $topiclistArray;
45 $data['default_topic_id'] = $this->GetTopic(); 156 $data['default_topic_id'] = $this->GetTopic();
46 $data['topic_tag'] = $this->GetTopicTag($this->GetTopic()); 157 $data['topic_tag'] = $this->GetTopicTag($this->GetTopic());
47 158
159 $data['info'] = array('file_id'=>$this->file_id, 'user_id'=>$this->user_id, 'branch_id'=>$this->branch_id, 'section_id'=>$this->section_id);
160
48 return $data; 161 return $data;
49 162
50 } 163 }
51 164
52 public function SaveFullText($postdata) { 165 public function SaveFullText($postdata) {
71 file_put_contents("data/parsing_files/".$postdata['filename'].".txt", $require); 184 file_put_contents("data/parsing_files/".$postdata['filename'].".txt", $require);
72 } 185 }
73 186
74 } 187 }
75 188
189 public function UpdateInfoByResonseFromLGService($response) {
190
191 $response_file = $response["file"];
192 $response_branch = $response["branch"];
193
194 $this->file_id = (string)$response_file["id"]; // should updated
195 echo "file_id: ".$this->file_id;
196 echo "<br>";
197 $this->branch_id = (string)$response_branch["id"];
198 echo "branch_id: ".$this->branch_id;
199 echo "<br>";
200
201 }
202 public function SaveFullTextToLGService($_postdata) {
203 // save tagged text (full text) by Jorge's API to lg service
204
205 // --------
206 // saving in my local machine in developing phrase
207 if ($_postdata['text']){
208 $date = date('Y_m_d_H_i_s', time());
209 if ( file_exists("data/parsing_files/".$_postdata['sectionId'].".txt") ) {
210 $oldFile = file_get_contents("data/parsing_files/".$_postdata['sectionId'].".txt");
211 file_put_contents("data/parsing_files/".$_postdata['sectionId']."_".$date.".txt", $oldFile);
212 }
213
214 if (get_magic_quotes_gpc()) {
215 $require = stripslashes($_postdata['text']);
216 } else {
217 $require = $_postdata['text'];
218 }
219
220 $require = preg_replace("/【<a(.*?)>(.*?)<\/a>】/u", "【\\2】", $require);
221 $require = preg_replace('/&amp;/u', "&", $require);
222 $require = preg_replace("/○/u", " ", $require);
223 $require = preg_replace("/<br>/u", "\n", $require);
224 $require = preg_replace("/<br>/u", "\n", $require);
225 file_put_contents("data/parsing_files/".$_postdata['sectionId'].".txt", $require);
226 }
227
228 echo getcwd()."<br>";
229 // ------
230
231
232 if ($_postdata['branchId'] != 0) {
233 // exiting branch case
234 $postfields = array(
235 //"text" => $_postdata['text'],
236 "text" => $require,
237 "branchId" => $_postdata['branchId'],
238 "userId" => $_postdata['userId'],
239 "userPreviousFileId" => $_postdata['fileId'],
240 );
241 $save_url = $this->save_to_LGService_url;
242
243 } else {
244 // new branch case
245 echo "saveNew!";
246 $postfields = array(
247 //"text" => $_postdata['text'],
248 "text" => $require,
249 "sectionId" => $_postdata['sectionId'],
250 //"userId" => $_postdata['userId'],
251 // TODO: change userId when we can query by sectionId from LGService using search
252 "userId" => "12",
253 // "label" => $_postdata['label'],
254 // TODO: ask user for lable
255 "label" => "label for section ".$_postdata['sectionId'],
256 );
257 $save_url = $this->save_new_to_LGService_url;
258 }
259
260
261
262
263 // set up the curl resource
264 $ch = curl_init();
265 curl_setopt($ch, CURLOPT_URL, $save_url);
266 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
267 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
268 curl_setopt($ch, CURLOPT_POST, true);
269 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
270 //curl_setopt($ch, CURLOPT_HEADER, true);
271 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
272 'Content-type: multipart/form-data;charset=utf-8'
273 ));
274
275 // execute the request
276
277 $output = curl_exec($ch);
278
279 // output the profile information - includes the header
280 //echo($output) . PHP_EOL;
281
282 // close curl resource to free up system resources
283 curl_close($ch);
284
285 $response = json_decode($output, true);
286 return $response;
287
288 }
76 289
77 // === for export table === 290 // === for export table ===
78 public function ExportTable($urlvalues, $postdata) { 291 public function ExportTable($urlvalues, $postdata) {
79 $this->Initialize($urlvalues); 292 // $this->Initialize($urlvalues);
80 $content = $postdata['content']; 293 $content = $postdata['content'];
81 $topic = $postdata['topic']; 294 $topic = $postdata['topic'];
82 295
83 // outputTableArray: 296 // outputTableArray:
84 $section_id = $this->GetSectionId(); 297 // $section_id = $this->GetSectionId();
298 $section_id = $this->section_id;
299 if (!$section_id) {
300 //$this->Initialize($urlvalues);
301 $section_id = $this->GetSectionId();
302 }
85 303
86 $section_info = $this->GetSectionInfo(); 304 $section_info = $this->GetSectionInfo();
87 305
88 $sectionName = $section_info['sectionName']; 306 $sectionName = $section_info['sectionName'];
89 $bookId = $section_info['bookId']; 307 $bookId = $section_info['bookId'];
193 411
194 } 412 }
195 413
196 // === for manage tag list === 414 // === for manage tag list ===
197 public function EditTaglist($urlvalues) { 415 public function EditTaglist($urlvalues) {
198 $this->Initialize($urlvalues); 416 // $this->Initialize($urlvalues);
199 $result = $this->GetTaglist(); 417 $result = $this->GetTaglist();
200 $taglistArray = array(); 418 $taglistArray = array();
201 while ($row = mysql_fetch_assoc($result)) { 419 while ($row = mysql_fetch_assoc($result)) {
202 $taglistArray[$row['id']] = array($row['name'], $row['tag'], $row['color']); 420 $taglistArray[$row['id']] = array($row['name'], $row['tag'], $row['color']);
203 } 421 }
260 } 478 }
261 479
262 } 480 }
263 481
264 // === for config topic === 482 // === for config topic ===
265 public function ConfigTagsInTopic($urlvalues, $postdata) { 483 public function ConfigTagsInTopic($postdata) {
266 484
485 $query = "SELECT taglist.*, topic_tag_relation.topic_id FROM taglist LEFT JOIN topic_tag_relation ON taglist.id = topic_tag_relation.tag_id ORDER BY `topic_id`";
486 $result = mysql_query($query);
487 if (!$result) {
488 return json_encode("Failed during selecting/joining taglist and topic_tag_relation table.");
489 }
490 $taglistArray = array();
491 while ($row = mysql_fetch_assoc($result)) {
492 array_push($taglistArray, array('id'=>$row['id'],'name'=>$row['name'], 'tag'=>$row['tag'], 'color'=>$row['color'], 'topic_id'=>$row['topic_id']) );
493 }
494
495 $topic = $postdata['topic'];
496 $result = $this->GetTopicByID($topic);
497 $row = mysql_fetch_assoc($result);
498 $topic_name = $row['name'];
499
500 $data = array();
501 $data['taglistArray'] = $taglistArray;
502 $data['topic'] = $topic;
503 $data['topic_name'] = $topic_name;
504
505 return $data;
267 } 506 }
268 507
269 private function GetTaglistByTopicID($topic_id) { 508 private function GetTaglistByTopicID($topic_id) {
270 $taglistArray = array(); 509 $taglistArray = array();
271 // select taglist ids from topic_tag_relation table 510 // select taglist ids from topic_tag_relation table
324 563
325 // =========================== 564 // ===========================
326 565
327 // === for manage wordlist === 566 // === for manage wordlist ===
328 public function EditWordlist($urlvalues) { 567 public function EditWordlist($urlvalues) {
329 $this->Initialize($urlvalues); 568 //$this->Initialize($urlvalues);
330 $result = $this->GetWordlist(); 569 $result = $this->GetWordlist();
331 $wordlistArray = array(); 570 $wordlistArray = array();
332 while ($row = mysql_fetch_assoc($result)) { 571 while ($row = mysql_fetch_assoc($result)) {
333 $wordlistArray[$row['id']] = $row['name']; 572 $wordlistArray[$row['id']] = $row['name'];
334 } 573 }
403 642
404 public function sortFunction($a,$b) { 643 public function sortFunction($a,$b) {
405 return strlen($b)-strlen($a); 644 return strlen($b)-strlen($a);
406 } 645 }
407 646
408 647 // TODO: delete this
409 private function SetSectionId($_urlvalues) { 648 private function SetSectionId($_urlvalues) {
410 // TODO: maybe get user info also 649 // TODO: maybe get user info also
411 // get book id from url 650 // get book id from url
412 if ($_urlvalues['id'] != "") { 651 if ($_urlvalues['id'] != "") {
413 $section_id = $_urlvalues['id']; 652 $section_id = $_urlvalues['id'];
423 $this->section_id = $section_id; 662 $this->section_id = $section_id;
424 663
425 } 664 }
426 665
427 private function GetSectionId() { 666 private function GetSectionId() {
428 if ($this->section_id) { 667
668 if (is_numeric($this->section_id)) {
429 return $this->section_id; 669 return $this->section_id;
430 } else { 670 } else {
431 return json_encode("Error: No section id"); 671 return json_encode("Error: No section id");
432 } 672 }
433 } 673 }
452 $data['bookId'] = $bookId; 692 $data['bookId'] = $bookId;
453 $data['startPage'] = $startPage; 693 $data['startPage'] = $startPage;
454 $data['endPage'] = $endPage; 694 $data['endPage'] = $endPage;
455 $data['sectionName'] = $sectionName; 695 $data['sectionName'] = $sectionName;
456 696
697
457 return $data; 698 return $data;
458 } 699 }
459 700
460 private function GetSectionContent() { 701 private function GetSectionContent() {
461 $section_id = $this->GetSectionId(); 702 $section_id = $this->GetSectionId();
462 $section_info = $this->GetSectionInfo(); 703 $section_info = $this->GetSectionInfo();
463 704
705 var_dump($section_id);
706 var_dump($section_info);
707
464 $bookId = $section_info['bookId']; 708 $bookId = $section_info['bookId'];
465 $startPage = $section_info['startPage']; 709 $startPage = $section_info['startPage'];
466 $endPage = $section_id['endPage']; 710 $endPage = $section_info['endPage'];
467
468 711
469 $contentString=""; 712 $contentString="";
470 $data_path = $this->GetDataPath(); 713 $data_path = $this->GetDataPath();
471 if ( file_exists($data_path."parsing_files/".$section_id.".txt") ) { 714 if ( file_exists($data_path."parsing_files/".$section_id.".txt") ) {
472 $contentString=file_get_contents($data_path."parsing_files/".$section_id.".txt"); 715 $contentString=file_get_contents($data_path."parsing_files/".$section_id.".txt");
473 $stringInput = $contentString; 716 $stringInput = $contentString;
717
718 // if the text is from file system
474 $stringInput = preg_replace("/ /u", "○", $stringInput); 719 $stringInput = preg_replace("/ /u", "○", $stringInput);
475 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); 720 $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
476 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); 721 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
477 } else { 722 } else {
478 $query = sprintf("SELECT `content`, `line`, `books_id` FROM `contents` WHERE `books_id`=\"%s\" AND `line`>=%d AND `line`<=%d", $bookId, $startPage, $endPage); 723 $query = sprintf("SELECT `content`, `line`, `books_id` FROM `contents` WHERE `books_id`=\"%s\" AND `line`>=%d AND `line`<=%d", $bookId, $startPage, $endPage);
479 $result = mysql_query($query); 724 $result = mysql_query($query);
725 if (!$result) {
726 return json_encode("Failed during selecting content table.");
727 }
480 while ($row = mysql_fetch_assoc($result)) { 728 while ($row = mysql_fetch_assoc($result)) {
481 $contentString.="【".$row['line']."】".$row['content']."\n"; 729 $contentString.="【".$row['line']."】".$row['content']."\n";
482 } 730 }
731
732 // the text is from database
483 $stringInput = $contentString; 733 $stringInput = $contentString;
484 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput); 734 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput);
485 $stringInput = preg_replace("/ /u", "○", $stringInput); 735 $stringInput = preg_replace("/ /u", "○", $stringInput);
486 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); 736 $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
487 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); 737 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);