Mercurial > hg > extraction-interface
comparison models/extractapp.php @ 47:886f43b26ee2 extractapp
move/remove develop folder
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 17 Mar 2015 10:54:13 +0100 |
| parents | |
| children | c98a0c6d7eb4 |
comparison
equal
deleted
inserted
replaced
| 46:b3ca5d2b4d3f | 47:886f43b26ee2 |
|---|---|
| 1 | |
| 2 | |
| 3 <?php | |
| 4 | |
| 5 class ExtractappModel extends BaseModel{ | |
| 6 | |
| 7 public function Index() { | |
| 8 return array("Index Value 1", "Value 2", "Value 3"); | |
| 9 } | |
| 10 | |
| 11 protected $section_id = 0, $data_path, $file_id = 0, $current_fileId=0, | |
| 12 $branch_id = 0, $user_id = 0, $lg_text = "", $topic, $taglist_infile = "", $book_meta = ""; | |
| 13 public $messages = ""; | |
| 14 | |
| 15 private function Initialize($_urlvalues) { | |
| 16 $this->SetSectionId($_urlvalues); | |
| 17 | |
| 18 } | |
| 19 | |
| 20 public function GetTextFromFileId($_postdata) { | |
| 21 $this->file_id = $_postdata['fileId']; | |
| 22 $branch_id = $_postdata['branchId']; | |
| 23 $section_id = $_postdata['sectionId']; | |
| 24 | |
| 25 $this->branch_id = $branch_id; | |
| 26 $this->user_id = $_postdata['userId']; | |
| 27 $this->section_id = $section_id; | |
| 28 | |
| 29 $this->section_name = $_postdata['sectionName']; | |
| 30 $this->book_id = $_postdata['bookId']; | |
| 31 $this->book_name = $_postdata['bookName']; | |
| 32 | |
| 33 // get from URL with file_id | |
| 34 $lg_text_url = $this->get_text_from_fileId_url.$this->file_id; | |
| 35 //$lg_text = file_get_contents($lg_text_url); | |
| 36 // --- parsing meta data | |
| 37 $lg_text = $this->ParseMetaData($lg_text_url); | |
| 38 | |
| 39 // ---- | |
| 40 | |
| 41 | |
| 42 $stringInput = $lg_text; | |
| 43 $stringInput = preg_replace("/ /u", "○", $stringInput); | |
| 44 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | |
| 45 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); | |
| 46 $lg_text = $stringInput; | |
| 47 | |
| 48 $this->lg_text = $lg_text; | |
| 49 | |
| 50 } | |
| 51 | |
| 52 public function GetTextFromSectionId($_postdata) { | |
| 53 $section_id = $_postdata['sectionId']; | |
| 54 $this->section_id = $section_id; | |
| 55 $this->user_id = $_postdata['userId']; | |
| 56 | |
| 57 $this->section_name = $_postdata['sectionName']; | |
| 58 $this->book_id = $_postdata['bookId']; | |
| 59 $this->book_name = $_postdata['bookName']; | |
| 60 | |
| 61 // get from URL with file_id | |
| 62 $lg_text_url = $this->get_text_from_sectionId_url.$section_id; | |
| 63 $lg_text = file_get_contents($lg_text_url); | |
| 64 | |
| 65 $stringInput = $lg_text; | |
| 66 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput); | |
| 67 $stringInput = preg_replace("/ /u", "○", $stringInput); | |
| 68 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | |
| 69 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); | |
| 70 $lg_text = $stringInput; | |
| 71 | |
| 72 $this->lg_text = $lg_text; | |
| 73 } | |
| 74 | |
| 75 public function GetTextFromLocal($_id){ | |
| 76 $this->section_id = $_id; | |
| 77 $this->messages .= "DEBUG: from my local"."<br>"; | |
| 78 $this->lg_text = $this->GetSectionContent(); | |
| 79 | |
| 80 } | |
| 81 | |
| 82 public function GetInfoFromPreviousPage($_postdata) { | |
| 83 | |
| 84 if ($_postdata['fileId']) { | |
| 85 $this->file_id = $_postdata['fileId']; | |
| 86 } | |
| 87 if ($_postdata['sectionId']) { | |
| 88 $this->section_id = $_postdata['sectionId']; | |
| 89 } | |
| 90 if ($_postdata['text']) { | |
| 91 $this->lg_text = $_postdata['text']; | |
| 92 } | |
| 93 if ($_postdata['branchId']) { | |
| 94 $this->branch_id = $_postdata['branchId']; | |
| 95 } | |
| 96 if ($_postdata['userId']) { | |
| 97 $this->user_id = $_postdata['userId']; | |
| 98 } | |
| 99 if ($_postdata['topic_id']) { | |
| 100 $this->topic = $_postdata['topic_id']; | |
| 101 } | |
| 102 | |
| 103 if ($_postdata['sectionName']) { | |
| 104 $this->section_name = $_postdata['sectionName']; | |
| 105 } | |
| 106 if ($_postdata['bookName']) { | |
| 107 $this->book_name = $_postdata['bookName']; | |
| 108 } | |
| 109 if ($_postdata['bookId']) { | |
| 110 $this->book_id = $_postdata['bookId']; | |
| 111 } | |
| 112 if ($_postdata['currentFileId']) { | |
| 113 $this->current_fileId = $_postdata['currentFileId']; | |
| 114 } | |
| 115 if ($_postdata['taglistArray']) { | |
| 116 $this->taglist_infile = json_decode($_postdata['taglistArray']); | |
| 117 } | |
| 118 if ($_postdata['book_meta']) { | |
| 119 $this->book_meta = json_decode($_postdata['book_meta']); | |
| 120 } | |
| 121 | |
| 122 } | |
| 123 public function InitData($_postdata) { | |
| 124 $file_id = $_postdata['fileId']; | |
| 125 $branch_id = $_postdata['branchId']; | |
| 126 $section_id = $_postdata['sectionId']; | |
| 127 | |
| 128 $this->branch_id = $branch_id; | |
| 129 $this->file_id = $file_id; | |
| 130 $this->user_id = $_postdata['userId']; | |
| 131 $this->section_id = $section_id; | |
| 132 | |
| 133 if ($file_id != 0 && $branch_id != 0) { | |
| 134 // get from URL with file_id | |
| 135 $lg_text_url = $this->get_text_from_fileId_url.$file_id; | |
| 136 } else if ($section_id != 0) { | |
| 137 // get from URL with section_id | |
| 138 $lg_text_url = $this->get_text_from_sectionId_url.$section_id; | |
| 139 } else { | |
| 140 echo "wrong url!!"; | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 | |
| 145 $lg_text = file_get_contents($lg_text_url); | |
| 146 | |
| 147 /* | |
| 148 $stringInput = preg_replace("/ /u", "○", $stringInput); | |
| 149 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | |
| 150 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); | |
| 151 */ | |
| 152 | |
| 153 // the text is from database | |
| 154 $stringInput = $lg_text; | |
| 155 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput); | |
| 156 $stringInput = preg_replace("/ /u", "○", $stringInput); | |
| 157 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | |
| 158 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); | |
| 159 $lg_text = $stringInput; | |
| 160 | |
| 161 $this->lg_text = $lg_text; | |
| 162 | |
| 163 | |
| 164 } | |
| 165 /* | |
| 166 public function Reload($_postdata) { | |
| 167 | |
| 168 // update text using $this->current_fileId | |
| 169 // getTextbyFileId | |
| 170 $current_fileId = $_postdata['currentFileId']; | |
| 171 //$this->file_id = $_postdata['currentFileId']; | |
| 172 //$this->GetTextFromFileId($_postdata); | |
| 173 //$latest_editing = $this->lg_text; | |
| 174 // get from URL with file_id | |
| 175 | |
| 176 $lg_text_url = $this->get_text_from_fileId_url.$current_fileId; | |
| 177 $lg_text = file_get_contents($lg_text_url); | |
| 178 | |
| 179 $stringInput = $lg_text; | |
| 180 $stringInput = preg_replace("/ /u", "○", $stringInput); | |
| 181 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | |
| 182 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); | |
| 183 $lg_text = $stringInput; | |
| 184 | |
| 185 | |
| 186 //var_dump($lg_text); | |
| 187 | |
| 188 echo json_encode($lg_text); | |
| 189 //var_dump($this->file_id); | |
| 190 | |
| 191 } | |
| 192 */ | |
| 193 | |
| 194 | |
| 195 // TODO: comparison not correct | |
| 196 private function Taglist_infileUpToDate($taglistArray) { | |
| 197 // compare $this->taglist_infile is the same as $taglistArray | |
| 198 $taglist_infile = $this->taglist_infile; | |
| 199 if (count($taglist_infile) != count($taglistArray)) { | |
| 200 return false; | |
| 201 } | |
| 202 | |
| 203 foreach ($taglistArray as $row_indb) { | |
| 204 $cnt = 0; | |
| 205 foreach ($taglist_infile as $row) { | |
| 206 //$taglistArray: array( $row['id'], $row['name'], $row['tag'], $row['color'] ) | |
| 207 if ($row[0] == $row_indb[0] && $row[1] == $row_indb[1] && $row[2] == $row_indb[2] && $row[3] == $row_indb[3]) { | |
| 208 | |
| 209 } else { | |
| 210 $cnt ++; | |
| 211 } | |
| 212 } | |
| 213 if ($cnt == count($row)) { | |
| 214 return false; | |
| 215 } | |
| 216 } | |
| 217 return true; | |
| 218 } | |
| 219 // === for tagging === | |
| 220 public function StartTagging() { | |
| 221 | |
| 222 $section_id = $this->section_id; | |
| 223 $stringInput = $this->lg_text; | |
| 224 | |
| 225 $data = array(); // data array to be passed to view | |
| 226 | |
| 227 //$taglistArray = $this->GetTaglistArray(); | |
| 228 //for GetTaglistByTopicID: | |
| 229 $taglistArray = $this->GetTaglistByTopicID($this->GetTopic()); | |
| 230 | |
| 231 $data['taglist_infile'] = $this->taglist_infile; | |
| 232 // TODO: check if taglist_infile is up-to-date | |
| 233 /* | |
| 234 if ( !$this->Taglist_infileUpToDate($taglistArray)) { | |
| 235 $data['taglist_infile'] = $this->taglist_infile; | |
| 236 } else { | |
| 237 $data['taglist_infile'] = ""; | |
| 238 } | |
| 239 */ | |
| 240 | |
| 241 // book_meta | |
| 242 $data['book_meta'] = $this->book_meta; | |
| 243 | |
| 244 // topic list | |
| 245 $topiclistArray = $this->GetTopiclistArray(); | |
| 246 | |
| 247 | |
| 248 $wordlistArray = $this->GetWordlistArray(); | |
| 249 | |
| 250 $data['stringInput'] = $stringInput; | |
| 251 $data['taglistArray'] = $taglistArray; | |
| 252 $data['wordlistArray'] = $wordlistArray; | |
| 253 $data['section_id'] = $section_id; | |
| 254 $data['topiclistArray'] = $topiclistArray; | |
| 255 $data['default_topic_id'] = $this->GetTopic(); | |
| 256 $data['topic_tag'] = $this->GetTopicTag($this->GetTopic()); | |
| 257 $data['topic_name'] = $this->GetTopicName($this->GetTopic()); | |
| 258 | |
| 259 | |
| 260 $data['info'] = array('file_id'=>$this->file_id, 'user_id'=>$this->user_id, | |
| 261 'branch_id'=>$this->branch_id, 'section_id'=>$this->section_id, 'book_id'=>$this->book_id, | |
| 262 'book_name'=>$this->book_name, 'section_name'=>$this->section_name, | |
| 263 'current_fileId'=>$this->current_fileId); | |
| 264 //'current_fileId'=>$this->file_id); | |
| 265 //'current_fileId'=>'123'); | |
| 266 | |
| 267 $this->messages .= "Info: "; | |
| 268 $this->messages .= "file_id=".$this->file_id.", section_id=".$this->section_id; | |
| 269 $this->messages .= ", user_id=".$this->user_id.", branch_id=".$this->branch_id.", topic_id=".$this->topic; | |
| 270 $this->messages .= ", book_id=".$this->book_id.", book_name=".$this->book_name.", section_name=".$this->section_name; | |
| 271 $this->messages .= ", current_fileId=".$this->current_fileId."<br>"; | |
| 272 | |
| 273 $data['messages'] = $this->messages; | |
| 274 | |
| 275 return $data; | |
| 276 | |
| 277 } | |
| 278 | |
| 279 public function SaveFullText($postdata) { | |
| 280 if ($postdata['text']){ | |
| 281 $date = date('Y_m_d_H_i_s', time()); | |
| 282 if ( file_exists("data/parsing_files/".$postdata['filename'].".txt") ) { | |
| 283 $oldFile = file_get_contents("data/parsing_files/".$postdata['filename'].".txt"); | |
| 284 file_put_contents("data/parsing_files/".$postdata['filename']."_".$date.".txt", $oldFile); | |
| 285 } | |
| 286 | |
| 287 if (get_magic_quotes_gpc()) { | |
| 288 $require = stripslashes($postdata['text']); | |
| 289 } else { | |
| 290 $require = $postdata['text']; | |
| 291 } | |
| 292 | |
| 293 $require = preg_replace("/【<a(.*?)>(.*?)<\/a>】/u", "【\\2】", $require); | |
| 294 $require = preg_replace('/&/u', "&", $require); | |
| 295 $require = preg_replace("/○/u", " ", $require); | |
| 296 $require = preg_replace("/<br>/u", "\n", $require); | |
| 297 $require = preg_replace("/<br>/u", "\n", $require); | |
| 298 file_put_contents("data/parsing_files/".$postdata['filename'].".txt", $require); | |
| 299 } | |
| 300 | |
| 301 } | |
| 302 | |
| 303 public function UpdateInfoByResponseFromLGService($response) { | |
| 304 | |
| 305 /* | |
| 306 $response_file = $response["file"]; | |
| 307 $response_branch = $response["branch"]; | |
| 308 | |
| 309 | |
| 310 $status = (string)$response["status"]; | |
| 311 | |
| 312 if ((string)$response["status"] == "error") { | |
| 313 $response_currentFile = $response["currentFile"]; | |
| 314 | |
| 315 $this->current_fileId = (string)$response_currentFile['id']; | |
| 316 $this->messages .= "saving does not success! ".(string)$response["message"]; | |
| 317 | |
| 318 | |
| 319 } else { | |
| 320 | |
| 321 $this->messages .= "saving success."; | |
| 322 var_dump((string)$response_file["id"]); | |
| 323 | |
| 324 $this->file_id = (string)$response_file["id"]; | |
| 325 $this->branch_id = (string)$response_branch["id"]; | |
| 326 | |
| 327 } | |
| 328 */ | |
| 329 | |
| 330 | |
| 331 $response_file = $response["file"]; | |
| 332 $response_branch = $response["branch"]; | |
| 333 | |
| 334 $status = (string)$response["status"]; | |
| 335 if ($status == "ok") { | |
| 336 $this->messages .= "saving success."; | |
| 337 $this->file_id = (string)$response_file["id"]; | |
| 338 $this->branch_id = (string)$response_branch["id"]; | |
| 339 | |
| 340 } else if ($status == "error") { | |
| 341 // saving not success | |
| 342 $this->messages .= "saving does not success! ".(string)$response["message"]; | |
| 343 $response_currentFile = $response["currentFile"]; | |
| 344 //$this->current_fileId = $response["currentFileId"]; | |
| 345 $this->current_fileId = (string)$response_currentFile["id"]; | |
| 346 | |
| 347 } | |
| 348 | |
| 349 | |
| 350 } | |
| 351 private function AppendMetaData($text_content) { | |
| 352 $text = '<?xml version="1.0" encoding="UTF-8"?>'; | |
| 353 $text .= "\n<text>\n"; | |
| 354 // topic | |
| 355 $text .= "<topic>".$this->topic."</topic>\n"; | |
| 356 // book meta data | |
| 357 $book = $this->book_meta; | |
| 358 foreach ($book as $b) { | |
| 359 $text .= "<book>\n"; | |
| 360 $text .= "<title>".$b[0]."</title>\n"; | |
| 361 $text .= "<author>".$b[1]."</author>\n"; | |
| 362 $text .= "<year>".$b[2]."</year>\n"; | |
| 363 $text .= "<pagenumber>".$b[3]."</pagenumber>\n"; | |
| 364 $text .= "</book>\n"; | |
| 365 } | |
| 366 // taglist | |
| 367 $taglist = $this->taglist_infile; | |
| 368 foreach ($taglist as $tagitem) { | |
| 369 $text .= "<tagitem>\n"; | |
| 370 $text .= "<id>".$tagitem[0]."</id>\n"; | |
| 371 $text .= "<name>".$tagitem[1]."</name>\n"; | |
| 372 $text .= "<tag>".$tagitem[2]."</tag>\n"; | |
| 373 $text .= "<color>".$tagitem[3]."</color>\n"; | |
| 374 $text .= "</tagitem>\n"; | |
| 375 } | |
| 376 | |
| 377 // text_content | |
| 378 $text .= $text_content; | |
| 379 $text .= "\n</text>"; | |
| 380 | |
| 381 return $text; | |
| 382 } | |
| 383 public function SaveFullTextToLGService($_postdata) { | |
| 384 // save tagged text (full text) by Jorge's API to lg service | |
| 385 | |
| 386 // -------- | |
| 387 if ($_postdata['text']){ | |
| 388 $date = date('Y_m_d_H_i_s', time()); | |
| 389 if ( file_exists("data/parsing_files/".$_postdata['sectionId'].".txt") ) { | |
| 390 $oldFile = file_get_contents("data/parsing_files/".$_postdata['sectionId'].".txt"); | |
| 391 //saving in my local machine in developing phrase | |
| 392 file_put_contents("data/parsing_files/".$_postdata['sectionId']."_".$date.".txt", $oldFile); | |
| 393 } | |
| 394 | |
| 395 if (get_magic_quotes_gpc()) { | |
| 396 $require = stripslashes($_postdata['text']); | |
| 397 } else { | |
| 398 $require = $_postdata['text']; | |
| 399 } | |
| 400 | |
| 401 $require = preg_replace("/【<a(.*?)>(.*?)<\/a>】/u", "【\\2】", $require); | |
| 402 $require = preg_replace('/&/u', "&", $require); | |
| 403 $require = preg_replace("/○/u", " ", $require); | |
| 404 $require = preg_replace("/<br>/u", "\n", $require); | |
| 405 //$require = preg_replace("/<br>/u", "\n", $require); | |
| 406 | |
| 407 if ($_postdata['branchId'] == 0) { | |
| 408 // -- new branch case | |
| 409 $require = "<text_content>\n".$require."</text_content>\n"; | |
| 410 } | |
| 411 $require = $this->AppendMetaData($require); | |
| 412 | |
| 413 //saving in my local machine in developing phrase | |
| 414 file_put_contents("data/parsing_files/".$_postdata['sectionId'].".txt", $require); | |
| 415 } | |
| 416 | |
| 417 // ------ | |
| 418 | |
| 419 | |
| 420 if ($_postdata['branchId'] != 0) { | |
| 421 // -- exiting branch case | |
| 422 $postfields = array( | |
| 423 "text" => $require, | |
| 424 "branchId" => $_postdata['branchId'], | |
| 425 "userId" => $_postdata['userId'], | |
| 426 "userPreviousFileId" => $_postdata['fileId'], | |
| 427 ); | |
| 428 $save_url = $this->save_to_LGService_url; | |
| 429 | |
| 430 } else { | |
| 431 // -- new branch case | |
| 432 echo "saveNew!"; | |
| 433 $user_id = "0"; | |
| 434 if ($_postdata['userId']) { | |
| 435 $user_id = $_postdata['userId']; | |
| 436 } | |
| 437 | |
| 438 $postfields = array( | |
| 439 "text" => $require, | |
| 440 "sectionId" => $_postdata['sectionId'], | |
| 441 "userId" => $user_id, | |
| 442 "label" => $_postdata['label'], | |
| 443 ); | |
| 444 $save_url = $this->save_new_to_LGService_url; | |
| 445 } | |
| 446 | |
| 447 | |
| 448 // set up the curl resource | |
| 449 $ch = curl_init(); | |
| 450 curl_setopt($ch, CURLOPT_URL, $save_url); | |
| 451 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| 452 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
| 453 curl_setopt($ch, CURLOPT_POST, true); | |
| 454 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); | |
| 455 //curl_setopt($ch, CURLOPT_HEADER, true); | |
| 456 curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
| 457 'Content-type: multipart/form-data;charset=utf-8' | |
| 458 )); | |
| 459 | |
| 460 // execute the request | |
| 461 // **** commended to DEBUG *** | |
| 462 //$output = curl_exec($ch); | |
| 463 // ***** | |
| 464 | |
| 465 // output the profile information - includes the header | |
| 466 //echo($output) . PHP_EOL; | |
| 467 | |
| 468 // close curl resource to free up system resources | |
| 469 curl_close($ch); | |
| 470 | |
| 471 $response = json_decode($output, true); | |
| 472 return $response; | |
| 473 | |
| 474 } | |
| 475 | |
| 476 // --- for regex ---- | |
| 477 public function GetRegexFilenameById($topic_id) { | |
| 478 $query = sprintf("SELECT regex_filename FROM `topic_regex_relation` WHERE topic_id=\"%s\"", $topic_id); | |
| 479 $result = mysql_query($query); | |
| 480 if (!$result) { | |
| 481 return json_encode("Failed during selecting topic_regex_relation table."); | |
| 482 } | |
| 483 | |
| 484 $filenames = array(); | |
| 485 while ($row = mysql_fetch_assoc($result)) { | |
| 486 array_push($filenames, $row['regex_filename']); | |
| 487 } | |
| 488 | |
| 489 return $filenames; | |
| 490 } | |
| 491 public function SmartRegexLoad($topic_id) { | |
| 492 | |
| 493 // Load regex file based on current topic. Only shows the regex in this topic -- | |
| 494 $filenames = $this->GetRegexFilenameById($topic_id); | |
| 495 | |
| 496 // Get regex file from filesystem ---- | |
| 497 $data_path = "./data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" | |
| 498 $returnArray = array(); | |
| 499 $files1 = scandir($data_path."regex_files"); | |
| 500 foreach ( $files1 as $file ) { | |
| 501 if ( $file != "." && $file != ".." && $file != ".DS_Store") { | |
| 502 foreach ($filenames as $filename) { | |
| 503 if ($file == $filename) { | |
| 504 $returnArray[preg_replace("/\.txt/", "", $file)] = file_get_contents( $data_path."regex_files/".$file ); | |
| 505 break; | |
| 506 } | |
| 507 } | |
| 508 } | |
| 509 } | |
| 510 echo json_encode($returnArray); | |
| 511 return; | |
| 512 } | |
| 513 | |
| 514 | |
| 515 public function SmartRegexSave($_postdata) { | |
| 516 if ($_postdata['text']){ | |
| 517 | |
| 518 // --- update topic_regex_relation table --- | |
| 519 $topic_id = $_postdata['topic_id']; | |
| 520 $filename = $_postdata['filename'].'.txt'; | |
| 521 | |
| 522 // check if already existing a record for this regex file | |
| 523 $query = "SELECT * FROM topic_regex_relation WHERE topic_id=".$topic_id." AND regex_filename='".$filename."'"; | |
| 524 $result = mysql_query($query); | |
| 525 if (!$result) { | |
| 526 echo json_encode("ErrorDB"); | |
| 527 } | |
| 528 while ($row = mysql_fetch_assoc($result)) { | |
| 529 if ($row && !$_postdata['forcesave']) { | |
| 530 // promt user a comfirm to force saving or not | |
| 531 echo json_encode("ForceSave"); | |
| 532 return; | |
| 533 } | |
| 534 } | |
| 535 | |
| 536 | |
| 537 if (!$_postdata['forcesave']) { | |
| 538 $query = "INSERT INTO topic_regex_relation (topic_id, regex_filename) VALUES (".$topic_id.",'".$filename."')"; | |
| 539 $result = mysql_query($query); | |
| 540 if (!$result) { | |
| 541 echo json_encode("ErrorDB"); | |
| 542 } | |
| 543 } | |
| 544 // --- write to filesystem --- | |
| 545 | |
| 546 /* | |
| 547 $date = date('Y_m_d_H_i_s', time()); | |
| 548 if ( file_exists("regex_files/".$_POST['filename'].".txt") ) { | |
| 549 $oldFile = file_get_contents("regex_files/".$_POST['filename'].".txt"); | |
| 550 file_put_contents("regex_files/".$_POST['filename']."_".$date.".txt", $oldFile); | |
| 551 } | |
| 552 */ | |
| 553 $data_path = "./data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" | |
| 554 | |
| 555 if (get_magic_quotes_gpc()) { | |
| 556 $require = stripslashes($_postdata['text']); | |
| 557 } else { | |
| 558 $require = $_postdata['text']; | |
| 559 } | |
| 560 | |
| 561 file_put_contents( $data_path."regex_files/".$_postdata['filename'].".txt", $require); | |
| 562 | |
| 563 | |
| 564 } | |
| 565 } | |
| 566 | |
| 567 | |
| 568 // === for export table === | |
| 569 public function ExportTable($postdata) { | |
| 570 // $this->Initialize($urlvalues); | |
| 571 | |
| 572 $content = $postdata['content']; | |
| 573 $topic = $postdata['topic']; | |
| 574 $section_id = $postdata['sectionId']; | |
| 575 | |
| 576 //$section_id = $this->section_id; | |
| 577 | |
| 578 | |
| 579 // TODO: this should be get from LGService: sectionName, bookId, bookName | |
| 580 $sectionName = $postdata['sectionName']; | |
| 581 $bookId = $postdata['bookId']; | |
| 582 $bookName = $postdata['bookName']; | |
| 583 | |
| 584 | |
| 585 // --- replace if get info from LGService | |
| 586 /* | |
| 587 $section_info = $this->GetSectionInfo(); | |
| 588 | |
| 589 $sectionName = $section_info['sectionName']; | |
| 590 $bookId = $section_info['bookId']; | |
| 591 | |
| 592 $books_info = $this->GetBooksInfo($bookId); | |
| 593 $bookName = $books_info['bookName']; | |
| 594 */ | |
| 595 // ===== | |
| 596 | |
| 597 | |
| 598 $outputTableArray = array(); | |
| 599 | |
| 600 //$taglistArray = $this->GetTaglistArray(); | |
| 601 $taglistArray = $this->GetTaglistByTopicID($topic); | |
| 602 | |
| 603 $outputTableArray[0]=array(); | |
| 604 $outputTableArray[0][0]=array(); | |
| 605 $outputTableArray[0][1]=array(); | |
| 606 foreach ( $taglistArray as $value ) { | |
| 607 $outputTableArray[0][0][$value[2]] = $value[1]; | |
| 608 $outputTableArray[0][1][$value[2]] = $value[1]."(Title)"; | |
| 609 } | |
| 610 $outputTableArray[0]["other"] = "其他"; | |
| 611 $outputTableArray[0]["page"] = "頁數"; | |
| 612 $outputTableArray[0]["full"] = "全文"; | |
| 613 | |
| 614 foreach ( $taglistArray as $tagValue ) { | |
| 615 $content = preg_replace("/<\/".$tagValue[2].">○*<".$tagValue[2].">/u", "", $content); | |
| 616 $content = preg_replace("/<".$tagValue[2].">[ ]*<\/".$tagValue[2].">/u", "", $content); | |
| 617 } | |
| 618 | |
| 619 $contentLineArray = explode( "<br>", $content ); | |
| 620 | |
| 621 $count=0; | |
| 622 $pageNow=NULL; | |
| 623 foreach ( $contentLineArray as $value ) { | |
| 624 $count++; | |
| 625 $recordString = $value; | |
| 626 $otherString = $recordString; | |
| 627 //echo $recordString."<br>\n"; | |
| 628 if ( preg_match("/【<a(.*?)>(.*?)<\/a>】/u", $recordString, $matches) ) { | |
| 629 $pageNow = $matches[2]; | |
| 630 } | |
| 631 foreach ( $taglistArray as $tagValue ) { | |
| 632 if ( preg_match_all("/<".$tagValue[2].">(.*?)<\/".$tagValue[2].">/u", $recordString, $matches, PREG_SET_ORDER) ) { | |
| 633 foreach ( $matches as $matchesValue ) { | |
| 634 $matchesValue[1] = preg_replace("/○/u", "", $matchesValue[1]); | |
| 635 if ( preg_match_all("/〈(.*?)〉/u", $matchesValue[1], $matches2, PREG_SET_ORDER) ) { | |
| 636 foreach ( $matches2 as $matches2Value ) { | |
| 637 if ( isset($outputTableArray[$count][0][$tagValue[2]]) ) { | |
| 638 $outputTableArray[$count][0][$tagValue[2]] .= ";".$matches2Value[1]; | |
| 639 } else { | |
| 640 $outputTableArray[$count][0][$tagValue[2]] = $matches2Value[1]; | |
| 641 } | |
| 642 } | |
| 643 } else { | |
| 644 if ( isset($outputTableArray[$count][0][$tagValue[2]]) ) { | |
| 645 $outputTableArray[$count][0][$tagValue[2]] .= ";".$matchesValue[1]; | |
| 646 } else { | |
| 647 $outputTableArray[$count][0][$tagValue[2]] = $matchesValue[1]; | |
| 648 } | |
| 649 } | |
| 650 } | |
| 651 $otherString = preg_replace("/<".$tagValue[2].">(.*?)<\/".$tagValue[2].">/u", " ", $otherString); | |
| 652 } | |
| 653 } | |
| 654 $otherString = preg_replace("/○/u", "", $otherString); | |
| 655 $outputTableArray[$count]["other"] = $otherString; | |
| 656 $outputTableArray[$count]["page"] = $pageNow; | |
| 657 $value = preg_replace("/>/u", ">", $value); | |
| 658 $value = preg_replace("/</u", "<", $value); | |
| 659 $outputTableArray[$count]["full"] = $value; | |
| 660 } | |
| 661 /* | |
| 662 echo "<br>"."DEBUG: outputTableArray:"."<br>"; | |
| 663 var_dump($outputTableArray); | |
| 664 */ | |
| 665 | |
| 666 $topic_tag = $this->GetTopicTag($topic); | |
| 667 | |
| 668 foreach ( $outputTableArray as $arrayIndex => $arrayValue ) { | |
| 669 // output each row which the topic tag | |
| 670 // e.g. the original version is for 'person' | |
| 671 if ( !isset($arrayValue[0][$topic_tag]) ) { | |
| 672 unset($outputTableArray[$arrayIndex]); | |
| 673 } | |
| 674 | |
| 675 /* | |
| 676 if ($topic == 1) { | |
| 677 if ( !isset($arrayValue[0]["person"]) ) { | |
| 678 unset($outputTableArray[$arrayIndex]); | |
| 679 } | |
| 680 } else if ($topic == 2) { | |
| 681 | |
| 682 // for object topic | |
| 683 if ( !isset($arrayValue[0]["object"]) ) { // only the record with the tag of 'object' will become a row | |
| 684 unset($outputTableArray[$arrayIndex]); | |
| 685 } | |
| 686 } | |
| 687 */ | |
| 688 | |
| 689 } | |
| 690 | |
| 691 $data = array(); | |
| 692 | |
| 693 $data['outputTableArray'] = $outputTableArray; | |
| 694 $data['bookId'] = $bookId; | |
| 695 $data['section_id'] = $section_id; | |
| 696 $data['bookName'] = $bookName; | |
| 697 $data['sectionName'] = $sectionName; | |
| 698 | |
| 699 return $data; | |
| 700 | |
| 701 } | |
| 702 | |
| 703 // === for manage tag list === | |
| 704 public function EditTaglist($_postdata) { | |
| 705 $query = "SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='taglist'"; | |
| 706 $result = mysql_query($query); | |
| 707 if (!$result) { | |
| 708 echo json_encode(mysql_error()); | |
| 709 } | |
| 710 $row = mysql_fetch_assoc($result); | |
| 711 $largest_id = $row['AUTO_INCREMENT']; | |
| 712 | |
| 713 | |
| 714 $topic_id = $_postdata['topic_id']; | |
| 715 $result = $this->GetTaglistByTopicID($topic_id); | |
| 716 | |
| 717 $taglistArray = array(); | |
| 718 | |
| 719 foreach ($result as $row) { | |
| 720 $taglistArray[$row[0]] = array($row[1], $row[2], $row[3]); | |
| 721 //$taglistArray[$row['id']] = array($row['name'], $row['tag'], $row['color']); | |
| 722 // TODO: the format is not good, so I use $row[0], $row[1],... This could be changed some time. | |
| 723 } | |
| 724 | |
| 725 $topic_tag_name = $this->GetTopicTag($topic_id); | |
| 726 | |
| 727 $query = "SELECT id FROM taglist WHERE taglist.tag='".$topic_tag_name."'"; | |
| 728 $result = mysql_query($query); | |
| 729 if (!$result) { | |
| 730 echo json_encode(mysql_error()); | |
| 731 } | |
| 732 $row = mysql_fetch_assoc($result); | |
| 733 $topic_tag_id = $row['id']; | |
| 734 | |
| 735 $data = array(); | |
| 736 $data['taglistArray'] = $taglistArray; | |
| 737 $data['topic_id'] = $topic_id; | |
| 738 $data['largest_id'] = $largest_id; | |
| 739 $data['topic_tag_id'] = $topic_tag_id; | |
| 740 | |
| 741 | |
| 742 return $data; | |
| 743 | |
| 744 } | |
| 745 | |
| 746 private function _GetTag($_postdata) { | |
| 747 if (get_magic_quotes_gpc()) { | |
| 748 $id = stripslashes($_postdata['id']); | |
| 749 $name = stripslashes($_postdata['name']); | |
| 750 $tag = stripslashes($_postdata['tag']); | |
| 751 $color = stripslashes($_postdata['color']); | |
| 752 } else { | |
| 753 $id = $_postdata['id']; | |
| 754 $name = $_postdata['name']; | |
| 755 $tag = $_postdata['tag']; | |
| 756 $color = $_postdata['color']; | |
| 757 } | |
| 758 return array($id, $name, $tag, $color); | |
| 759 } | |
| 760 | |
| 761 public function NewTagElement($postdata) { | |
| 762 if ($postdata['id']){ | |
| 763 list($id, $name, $tag, $color) = $this->_GetTag($postdata); | |
| 764 | |
| 765 $query1 = sprintf("INSERT INTO `taglist` (`id`, `name`, `tag`, `color`, `systemName`) VALUES (%s, %s, %s, %s, %s)", | |
| 766 $this->GetSQLValueString($id, "int"), | |
| 767 $this->GetSQLValueString($name, "text"), | |
| 768 $this->GetSQLValueString($tag, "text"), | |
| 769 $this->GetSQLValueString($color, "text"), | |
| 770 $this->GetSQLValueString($this->systemNAME, "text")); | |
| 771 $result1 = mysql_query($query1); | |
| 772 | |
| 773 | |
| 774 // add it to topic_tag_relation table | |
| 775 $topic_id = $postdata['topic_id']; | |
| 776 | |
| 777 // tag_id is $id; | |
| 778 $query = sprintf("INSERT INTO `topic_tag_relation` (`topic_id`,`tag_id`) VALUES (%s,%s)", $topic_id, $id); | |
| 779 $result = mysql_query($query); | |
| 780 if (!$result) { | |
| 781 echo json_encode("error when insert into topic_tag_relation table"); | |
| 782 } | |
| 783 } | |
| 784 | |
| 785 } | |
| 786 | |
| 787 public function SaveTagElement($postdata) { | |
| 788 if ($postdata['id']){ | |
| 789 list($id, $name, $tag, $color) = $this->_GetTag($postdata); | |
| 790 | |
| 791 $query = sprintf("UPDATE taglist SET `name`=%s, `tag`=%s, `color`=%s WHERE `id`=%s", | |
| 792 $this->GetSQLValueString($name, "text"), | |
| 793 $this->GetSQLValueString($tag, "text"), | |
| 794 $this->GetSQLValueString($color, "text"), | |
| 795 $this->GetSQLValueString($id, "int")); | |
| 796 $result = mysql_query($query); | |
| 797 | |
| 798 } | |
| 799 | |
| 800 } | |
| 801 | |
| 802 public function DeleteTag($postdata) { | |
| 803 if ($postdata['id']) { | |
| 804 $queryInsert = sprintf("DELETE FROM `taglist` WHERE `id` = %s", stripslashes($postdata['id'])); | |
| 805 $resultInsert = mysql_query($queryInsert); | |
| 806 } | |
| 807 | |
| 808 } | |
| 809 | |
| 810 // === for config topic === | |
| 811 public function ConfigTagsInTopic($postdata) { | |
| 812 $topic = $postdata['topic']; | |
| 813 $result = $this->GetTopicByID($topic); | |
| 814 $row = mysql_fetch_assoc($result); | |
| 815 $topic_name = $row['name']; | |
| 816 | |
| 817 /* | |
| 818 $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`"; | |
| 819 $result = mysql_query($query); | |
| 820 if (!$result) { | |
| 821 return json_encode("Failed during selecting/joining taglist and topic_tag_relation table."); | |
| 822 } | |
| 823 $taglistArray = array(); | |
| 824 while ($row = mysql_fetch_assoc($result)) { | |
| 825 array_push($taglistArray, array('id'=>$row['id'],'name'=>$row['name'], 'tag'=>$row['tag'], 'color'=>$row['color'], 'topic_id'=>$row['topic_id']) ); | |
| 826 } | |
| 827 */ | |
| 828 | |
| 829 // ------ | |
| 830 $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`"; | |
| 831 $result = mysql_query($query); | |
| 832 if (!$result) { | |
| 833 return json_encode("Failed during selecting/joining taglist and topic_tag_relation table."); | |
| 834 } | |
| 835 | |
| 836 $tag_intopic = array(); | |
| 837 $tag_others = array(); | |
| 838 $tag_tmp_others = array(); | |
| 839 while ($row = mysql_fetch_assoc($result)) { | |
| 840 if ($row['topic_id'] == $topic) { | |
| 841 array_push($tag_intopic, array('id'=>$row['id'],'name'=>$row['name'], 'tag'=>$row['tag'], 'color'=>$row['color'], 'topic_id'=>$row['topic_id'])); | |
| 842 } else { | |
| 843 array_push($tag_tmp_others, array('id'=>$row['id'],'name'=>$row['name'], 'tag'=>$row['tag'], 'color'=>$row['color'], 'topic_id'=>$row['topic_id'])); | |
| 844 } | |
| 845 } | |
| 846 | |
| 847 // --- check if there's any duplicated tags in tag_tmp_others. remove the duplicated tags | |
| 848 $num_others = count($tag_tmp_others); | |
| 849 for ($i=0; $i < $num_others; $i++) { | |
| 850 $cnt = 0; | |
| 851 for ($j=$i+1; $j < $num_others; $j++) { | |
| 852 if ($tag_tmp_others[$i]['tag'] == $tag_tmp_others[$j]['tag']) { | |
| 853 break; | |
| 854 } else { | |
| 855 $cnt++; | |
| 856 } | |
| 857 } | |
| 858 if ($cnt == ($num_others-$i-1) ) { | |
| 859 $row = $tag_tmp_others[$i]; | |
| 860 array_push($tag_others, array('id'=>$row['id'],'name'=>$row['name'], 'tag'=>$row['tag'], 'color'=>$row['color'], 'topic_id'=>$row['topic_id']) ); | |
| 861 } | |
| 862 } | |
| 863 | |
| 864 // --- remove duplicated tags in tag_others that is duplicated with tags in tag_intopic | |
| 865 $tag_tmp_others = $tag_others; | |
| 866 $tag_others = array(); | |
| 867 | |
| 868 foreach ($tag_tmp_others as $tmp) { | |
| 869 $cnt = 0; | |
| 870 foreach ($tag_intopic as $intopic) { | |
| 871 if ($tmp['tag'] == $intopic['tag']) { | |
| 872 break; | |
| 873 } else { | |
| 874 $cnt ++; | |
| 875 } | |
| 876 } | |
| 877 if ($cnt == count($tag_intopic)) { | |
| 878 // not appear in $tag_intopic | |
| 879 array_push($tag_others, $tmp); | |
| 880 } | |
| 881 } | |
| 882 | |
| 883 // ----- | |
| 884 | |
| 885 | |
| 886 $data = array(); | |
| 887 //$data['taglistArray'] = $taglistArray; | |
| 888 $data['topic'] = $topic; | |
| 889 $data['topic_name'] = $topic_name; | |
| 890 | |
| 891 $data['tag_intopic'] = $tag_intopic; | |
| 892 $data['tag_others'] = $tag_others; | |
| 893 | |
| 894 return $data; | |
| 895 } | |
| 896 | |
| 897 | |
| 898 public function UpdateTagsInTopic($_postdata) { | |
| 899 $topic_id = $_postdata['topic_id']; | |
| 900 $tag_ids = json_decode(str_replace('\\', '', $_postdata['ids'])); | |
| 901 | |
| 902 // update topic_tag_relation by tags_ids array as `tag_id` and topic_id as `topic_id` | |
| 903 // --- add new topic_tag_relation --- | |
| 904 foreach ($tag_ids as $tag_id) { | |
| 905 $query = "SELECT * FROM topic_tag_relation WHERE tag_id=".$tag_id; | |
| 906 $result = mysql_query($query); | |
| 907 if (!$result) { | |
| 908 echo json_encode("error when select from topic_tag_relation"); | |
| 909 } | |
| 910 $topic_tag = array(); | |
| 911 $flag = false; | |
| 912 while ($row = mysql_fetch_assoc($result)) { | |
| 913 if ($row['topic_id'] == $topic_id) { | |
| 914 $flag = true; | |
| 915 break; | |
| 916 } | |
| 917 } | |
| 918 if (!$flag) { | |
| 919 // insert a row into topic_tag_relation table | |
| 920 $queryUpdate = "INSERT INTO topic_tag_relation (topic_id, tag_id) VALUES (".$topic_id.",".$tag_id.")"; | |
| 921 $resultUpdate = mysql_query($queryUpdate); | |
| 922 if (!$resultUpdate) { | |
| 923 return json_encode("error when insert topic_tag_relation table"); | |
| 924 } | |
| 925 } | |
| 926 | |
| 927 } | |
| 928 | |
| 929 // --- remove tags from this topic --- | |
| 930 $query = "SELECT * FROM topic_tag_relation WHERE topic_id=".$topic_id; | |
| 931 $result = mysql_query($query); | |
| 932 if (!$result) { | |
| 933 echo json_encode("error when select from topic_tag_relation"); | |
| 934 } | |
| 935 | |
| 936 while ($row = mysql_fetch_assoc($result)) { | |
| 937 $cnt = 0; | |
| 938 foreach ($tag_ids as $tag_id) { | |
| 939 if ($row['tag_id'] == $tag_id) { | |
| 940 break; | |
| 941 } else { | |
| 942 $cnt ++; | |
| 943 } | |
| 944 } | |
| 945 $_id = $row['id']; | |
| 946 if ($cnt == count($tag_ids)) { | |
| 947 // delete row with (topic_id, tag_ids) | |
| 948 $queryDelete = "DELETE FROM topic_tag_relation WHERE id=".$_id; | |
| 949 $resultDelete = mysql_query($queryDelete); | |
| 950 if (!$resultDelete) { | |
| 951 echo json_encode("error when delete from topic_tag_relation"); | |
| 952 } | |
| 953 } | |
| 954 } | |
| 955 | |
| 956 | |
| 957 | |
| 958 /* | |
| 959 $query = "SELECT * FROM topic_tag_relation"; | |
| 960 $result = mysql_query($query); | |
| 961 if (!$result) { | |
| 962 echo json_encode("error when select from topic_tag_relation"); | |
| 963 } | |
| 964 $topic_tag = array(); | |
| 965 while ($row = mysql_fetch_assoc($result)) { | |
| 966 array_push($topic_tag, array('tag_id'=>$row['tag_id'], 'topic_id'=>$row['topic_id'])); | |
| 967 } | |
| 968 | |
| 969 | |
| 970 foreach ($topic_tag as $value) { | |
| 971 $flag = false; | |
| 972 foreach ($tag_ids as $tag_id) { | |
| 973 if ($value['tag_id'] == $tag_id) { | |
| 974 //update its topic_id to $topic_id | |
| 975 $queryUpdate = "UPDATE topic_tag_relation SET topic_id=".$topic_id." WHERE tag_id=".$tag_id; | |
| 976 $resultUpdate = mysql_query($queryUpdate); | |
| 977 if (!$resultUpdate) { | |
| 978 return json_encode("error when update topic_tag_relation table"); | |
| 979 } | |
| 980 $flag = true; | |
| 981 break; | |
| 982 } | |
| 983 } | |
| 984 if (!$flag && $value['topic_id'] == $topic_id) { | |
| 985 // set its topic_id to 0, indicating unsigned | |
| 986 $queryUpdate = "UPDATE topic_tag_relation SET topic_id=0 WHERE tag_id=".$value['tag_id']; | |
| 987 $resultUpdate = mysql_query($queryUpdate); | |
| 988 if (!$resultUpdate) { | |
| 989 return json_encode("error when update topic_tag_relation table"); | |
| 990 } | |
| 991 } | |
| 992 } | |
| 993 */ | |
| 994 | |
| 995 } | |
| 996 | |
| 997 | |
| 998 private function GetTaglistByTopicID($topic_id) { | |
| 999 $taglistArray = array(); | |
| 1000 // select taglist ids from topic_tag_relation table | |
| 1001 $query = sprintf("SELECT * FROM `topic_tag_relation` WHERE `topic_id`='%s'", $topic_id); | |
| 1002 $result = mysql_query($query); | |
| 1003 if (!$result) { | |
| 1004 return json_encode("Failed during selecting topic_tag_relation table."); | |
| 1005 } | |
| 1006 $taglist_ids = array(); | |
| 1007 | |
| 1008 while ($row = mysql_fetch_assoc($result)) { | |
| 1009 array_push($taglist_ids, $row['tag_id']); | |
| 1010 } | |
| 1011 | |
| 1012 // select taglist by tag ids | |
| 1013 foreach ($taglist_ids as $tag_id) { | |
| 1014 $query = sprintf("SELECT * FROM `taglist` WHERE `id`='%s'", $tag_id); | |
| 1015 $result = mysql_query($query); | |
| 1016 if (!$result) { | |
| 1017 echo mysql_error(); | |
| 1018 return json_encode("Failed during selecting taglist table."); | |
| 1019 } | |
| 1020 | |
| 1021 $row = mysql_fetch_assoc($result); | |
| 1022 array_push($taglistArray, array( $row['id'], $row['name'], $row['tag'], $row['color'] )); | |
| 1023 | |
| 1024 } | |
| 1025 return $taglistArray; | |
| 1026 } | |
| 1027 | |
| 1028 public function SetTopic($topic) { | |
| 1029 $this->topic = $topic; | |
| 1030 } | |
| 1031 | |
| 1032 public function GetTopic() { | |
| 1033 return $this->topic; | |
| 1034 } | |
| 1035 | |
| 1036 private function GetTopiclistArray() { | |
| 1037 $topiclistArray = array(); | |
| 1038 $result = $this->GetTopiclist(); | |
| 1039 while ($row = mysql_fetch_assoc($result)) { | |
| 1040 array_push($topiclistArray, array('id'=>$row['id'],'name'=>$row['name'],'tag'=>$row['tag'])); | |
| 1041 } | |
| 1042 return $topiclistArray; | |
| 1043 } | |
| 1044 | |
| 1045 | |
| 1046 private function GetTopicTag($topic_id) { | |
| 1047 $result = $this->GetTopicByID($topic_id); | |
| 1048 $row = mysql_fetch_assoc($result); | |
| 1049 $tag = $row['tag']; | |
| 1050 return $tag; | |
| 1051 | |
| 1052 } | |
| 1053 private function GetTopicName($topic_id) { | |
| 1054 $result = $this->GetTopicByID($topic_id); | |
| 1055 $row = mysql_fetch_assoc($result); | |
| 1056 $name = $row['name']; | |
| 1057 return $name; | |
| 1058 | |
| 1059 } | |
| 1060 | |
| 1061 | |
| 1062 // =========================== | |
| 1063 | |
| 1064 // === for manage wordlist === | |
| 1065 public function EditWordlist() { | |
| 1066 $result = $this->GetWordlist(); | |
| 1067 $wordlistArray = array(); | |
| 1068 while ($row = mysql_fetch_assoc($result)) { | |
| 1069 $wordlistArray[$row['id']] = $row['name']; | |
| 1070 } | |
| 1071 | |
| 1072 $data = array(); | |
| 1073 $data['wordlistArray'] = $wordlistArray; | |
| 1074 return $data; | |
| 1075 } | |
| 1076 | |
| 1077 | |
| 1078 public function AddNewList($postdata) { | |
| 1079 if ($postdata['text']){ | |
| 1080 if (get_magic_quotes_gpc()) { | |
| 1081 $name = stripslashes($postdata['text']); | |
| 1082 } else { | |
| 1083 $name = $postdata['text']; | |
| 1084 } | |
| 1085 | |
| 1086 $query1 = sprintf("INSERT INTO `wordlist` (`name`, `systemName`) VALUES (%s, %s)", | |
| 1087 $this->GetSQLValueString($name, "text"), | |
| 1088 $this->GetSQLValueString($this->systemNAME, "text")); | |
| 1089 $result1 = mysql_query($query1); | |
| 1090 file_put_contents( "data/wordlist/".mysql_insert_id().".txt", "(empty now)"); | |
| 1091 } | |
| 1092 } | |
| 1093 | |
| 1094 | |
| 1095 public function SaveWordlist($postdata) { | |
| 1096 | |
| 1097 if ($postdata['text']){ | |
| 1098 $date = date('Y_m_d_H_i_s', time()); | |
| 1099 if ( file_exists("data/wordlist/".$postdata['filename'].".txt") ) { | |
| 1100 $filename = "data/wordlist/".$postdata['filename']."_".$date.".txt"; | |
| 1101 $oldFile = file_get_contents("data/wordlist/".$postdata['filename'].".txt"); | |
| 1102 file_put_contents($filename, $oldFile); | |
| 1103 } | |
| 1104 /*else { | |
| 1105 $filename = "data/wordlist/".$postdata['filename'].".txt"; | |
| 1106 } | |
| 1107 */ | |
| 1108 | |
| 1109 | |
| 1110 if (get_magic_quotes_gpc()) { | |
| 1111 $require = stripslashes($postdata['text']); | |
| 1112 } else { | |
| 1113 $require = $postdata['text']; | |
| 1114 } | |
| 1115 | |
| 1116 $require = preg_replace("/<br>/u", "<br>", $require); | |
| 1117 file_put_contents("data/wordlist/".$postdata['filename'].".txt", $require); | |
| 1118 } | |
| 1119 /* | |
| 1120 if ($postdata['text']){ | |
| 1121 $date = date('Y_m_d_H_i_s', time()); | |
| 1122 if ( file_exists("data/wordlist/".$postdata['filename'].".txt") ) { | |
| 1123 $oldFile = file_get_contents("data/wordlist/".$postdata['filename'].".txt"); | |
| 1124 file_put_contents("data/wordlist/".$postdata['filename']."_".$date.".txt", $oldFile); | |
| 1125 } | |
| 1126 | |
| 1127 if (get_magic_quotes_gpc()) { | |
| 1128 $require = stripslashes($postdata['text']); | |
| 1129 } else { | |
| 1130 $require = $postdata['text']; | |
| 1131 } | |
| 1132 | |
| 1133 // $require = preg_replace("/<br>/u", "", $require); | |
| 1134 file_put_contents("data/wordlist/".$postdata['filename'].".txt", $require); | |
| 1135 } | |
| 1136 */ | |
| 1137 } | |
| 1138 | |
| 1139 | |
| 1140 // ======================================= | |
| 1141 | |
| 1142 public function sortFunction($a,$b) { | |
| 1143 return strlen($b)-strlen($a); | |
| 1144 } | |
| 1145 | |
| 1146 // TODO: delete this | |
| 1147 private function SetSectionId($_urlvalues) { | |
| 1148 // TODO: maybe get user info also | |
| 1149 // get book id from url | |
| 1150 if ($_urlvalues['id'] != "") { | |
| 1151 $section_id = $_urlvalues['id']; | |
| 1152 } else { | |
| 1153 return json_encode("Error: No section id"); | |
| 1154 /* ???? */ | |
| 1155 /* | |
| 1156 $get_book_id = $urlvalues['book']; | |
| 1157 $get_start = $urlvalues['start']; | |
| 1158 $get_end = $urlvalues['end']; | |
| 1159 */ | |
| 1160 } | |
| 1161 $this->section_id = $section_id; | |
| 1162 | |
| 1163 } | |
| 1164 | |
| 1165 private function GetSectionId() { | |
| 1166 | |
| 1167 if (is_numeric($this->section_id)) { | |
| 1168 return $this->section_id; | |
| 1169 } else { | |
| 1170 return json_encode("Error: No section id"); | |
| 1171 } | |
| 1172 } | |
| 1173 | |
| 1174 private function GetSectionInfo() { | |
| 1175 $section_id = $this->GetSectionId(); | |
| 1176 if (!is_numeric($section_id)){ | |
| 1177 return $section_id; | |
| 1178 } | |
| 1179 | |
| 1180 $result = $this->GetSectionsByID($section_id); | |
| 1181 | |
| 1182 | |
| 1183 while ($row = mysql_fetch_assoc($result)) { | |
| 1184 $bookId=$row['books_id']; | |
| 1185 $startPage=$row['start_page']; | |
| 1186 $endPage=$row['end_page']; | |
| 1187 $sectionName = $row['name']; | |
| 1188 } | |
| 1189 | |
| 1190 $data = array(); | |
| 1191 $data['bookId'] = $bookId; | |
| 1192 $data['startPage'] = $startPage; | |
| 1193 $data['endPage'] = $endPage; | |
| 1194 $data['sectionName'] = $sectionName; | |
| 1195 | |
| 1196 | |
| 1197 return $data; | |
| 1198 } | |
| 1199 | |
| 1200 private function ParseMetaData($filename) { | |
| 1201 $text = file_get_contents($filename); | |
| 1202 $xml = simplexml_load_string($text); //or die("Error: Cannot load from xml string"); | |
| 1203 if (!$xml) { | |
| 1204 // when file created by section_id, read the plain text from file_get_contents | |
| 1205 return $text; | |
| 1206 } | |
| 1207 | |
| 1208 $this->topic = (string)$xml->topic; // set topic id | |
| 1209 | |
| 1210 // get taglist in file | |
| 1211 $taglist_infile = $xml->tagitem; | |
| 1212 $taglistArray = array(); | |
| 1213 foreach ($taglist_infile as $row) { | |
| 1214 array_push($taglistArray, array((string)$row->id,(string)$row->name,(string)$row->tag,(string)$row->color )); | |
| 1215 } | |
| 1216 if ($taglistArray) { | |
| 1217 $this->taglist_infile = $taglistArray; | |
| 1218 } | |
| 1219 | |
| 1220 // get book meta data | |
| 1221 $book_meta = $xml->book; | |
| 1222 $book_metaArray = array(); | |
| 1223 foreach ($book_meta as $row) { | |
| 1224 //array_push($book_metaArray, array('title'=>(string)$row->title,'author'=>(string)$row->author,'year'=>(string)$row->year,'pagenumber'=>(string)$row->pagenumber )); | |
| 1225 array_push($book_metaArray, array((string)$row->title,(string)$row->author,(string)$row->year,(string)$row->pagenumber )); | |
| 1226 } | |
| 1227 if ($book_metaArray) { | |
| 1228 $this->book_meta = $book_metaArray; | |
| 1229 } | |
| 1230 | |
| 1231 // echo $taglist->name.", ".$taglist->tag."," .$taglist->color; | |
| 1232 // --- detect if the taglist set is up-to-date or not --- | |
| 1233 $contentString = (string)$xml->text_content->asXML(); | |
| 1234 //$removed_str = array("<text_content>","</text_content>"); | |
| 1235 //$new_contentString = str_replace($removed_str, "", $contentString); | |
| 1236 | |
| 1237 return $contentString; | |
| 1238 } | |
| 1239 private function GetSectionContent() { | |
| 1240 $section_id = $this->GetSectionId(); | |
| 1241 $section_info = $this->GetSectionInfo(); | |
| 1242 | |
| 1243 $bookId = $section_info['bookId']; | |
| 1244 $startPage = $section_info['startPage']; | |
| 1245 $endPage = $section_info['endPage']; | |
| 1246 | |
| 1247 $contentString=""; | |
| 1248 $data_path = $this->GetDataPath(); | |
| 1249 if ( file_exists($data_path."parsing_files/".$section_id.".txt") ) { | |
| 1250 $filename = $data_path."parsing_files/".$section_id.".txt"; | |
| 1251 | |
| 1252 // --- parsing meta data | |
| 1253 $stringInput = $this->ParseMetaData($filename); | |
| 1254 // ---- | |
| 1255 | |
| 1256 // if the text is from file system | |
| 1257 $stringInput = preg_replace("/ /u", "○", $stringInput); | |
| 1258 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | |
| 1259 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); | |
| 1260 } else { | |
| 1261 $query = sprintf("SELECT `content`, `line`, `books_id` FROM `contents` WHERE `books_id`=\"%s\" AND `line`>=%d AND `line`<=%d", $bookId, $startPage, $endPage); | |
| 1262 $result = mysql_query($query); | |
| 1263 if (!$result) { | |
| 1264 return json_encode("Failed during selecting content table."); | |
| 1265 } | |
| 1266 while ($row = mysql_fetch_assoc($result)) { | |
| 1267 $contentString.="【".$row['line']."】".$row['content']."\n"; | |
| 1268 } | |
| 1269 | |
| 1270 // the text is from database | |
| 1271 $stringInput = $contentString; | |
| 1272 $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput); | |
| 1273 $stringInput = preg_replace("/ /u", "○", $stringInput); | |
| 1274 $stringInput = preg_replace("/\n/u", "<br>", $stringInput); | |
| 1275 $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput); | |
| 1276 } | |
| 1277 | |
| 1278 return $stringInput; | |
| 1279 } | |
| 1280 | |
| 1281 private function GetDataPath() { | |
| 1282 return getcwd()."/data/"; // get the current file path, which is getcwd(), and concatenate with "/data/" | |
| 1283 } | |
| 1284 | |
| 1285 private function GetTaglistArray() { | |
| 1286 $taglistArray = array(); | |
| 1287 $result = $this->GetTaglist(); | |
| 1288 while ($row = mysql_fetch_assoc($result)) { | |
| 1289 array_push($taglistArray, array( $row['id'], $row['name'], $row['tag'], $row['color'] )); | |
| 1290 } | |
| 1291 | |
| 1292 return $taglistArray; | |
| 1293 } | |
| 1294 | |
| 1295 private function GetWordlistArray() { | |
| 1296 $wordlistArray=""; | |
| 1297 $result = $this->GetWordlist(); | |
| 1298 while ($row = mysql_fetch_assoc($result)) { | |
| 1299 $listString = file_get_contents("data/wordlist/".$row['id'].".txt"); | |
| 1300 $listString = preg_replace("/<div>/u", "\n", $listString); | |
| 1301 $listString = preg_replace("/<\/div>/u", "", $listString); | |
| 1302 $listString = preg_replace("/<span(.*?)>/u", "", $listString); | |
| 1303 $listString = preg_replace("/<\/span>/u", "", $listString); | |
| 1304 //$listString = preg_replace("/\n/u", "|", $listString); | |
| 1305 | |
| 1306 $wordlistArray2 = explode( "\n", $listString ); | |
| 1307 usort($wordlistArray2,'sortFunction'); | |
| 1308 foreach ( $wordlistArray2 as $index=>$value ) { | |
| 1309 $wordlistArray2[$index] = implode("○?", preg_split("/(?<!^)(?!$)/u", $value)); | |
| 1310 } | |
| 1311 foreach ( $wordlistArray2 as $index=>$value ) { | |
| 1312 if ($value=="") unset($wordlistArray2[$index]); | |
| 1313 | |
| 1314 } | |
| 1315 $listString = implode("|", $wordlistArray2); | |
| 1316 | |
| 1317 if ( $listString[0]=="|" ) $listString = substr($listString, 1); | |
| 1318 $wordlistArray[] = array( $row['id'], $row['name'], $listString ); | |
| 1319 } | |
| 1320 | |
| 1321 | |
| 1322 return $wordlistArray; | |
| 1323 | |
| 1324 } | |
| 1325 | |
| 1326 protected function GetBooksInfo($bookId) { | |
| 1327 $result = $this->GetSectionsByID($bookId); | |
| 1328 while ($row = mysql_fetch_assoc($result)) { | |
| 1329 $bookName = $row['name']; | |
| 1330 } | |
| 1331 | |
| 1332 $data = array(); | |
| 1333 $data['bookName'] = $bookName; | |
| 1334 | |
| 1335 return $data; | |
| 1336 } | |
| 1337 | |
| 1338 | |
| 1339 } | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 ?> |
