diff 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
line wrap: on
line diff
--- a/develop/models/extractapp.php	Wed Feb 11 12:33:59 2015 +0100
+++ b/develop/models/extractapp.php	Mon Feb 16 16:51:39 2015 +0100
@@ -8,29 +8,140 @@
 		return array("Index Value 1", "Value 2", "Value 3");
 	}
     
-    protected $section_id, $data_path;
-
+    protected $section_id, $data_path, $file_id, $branch_id, $user_id, $lg_tex;
 
     private function Initialize($_urlvalues) {
         $this->SetSectionId($_urlvalues);
 
     }
 
-    // === for tagging ===
-    public function StartTagging($urlvalues) {
+    public function GetTextFromFileId($_postdata) {
+        $file_id = $_postdata['fileId'];
+        $branch_id = $_postdata['branchId'];
+        $section_id = $_postdata['sectionId'];
+
+        $this->branch_id = $branch_id;
+        $this->file_id = $file_id;
+        $this->user_id = $_postdata['userId'];
+        $this->section_id = $section_id;
+        
+        // get from URL with file_id
+        $lg_text_url = $this->get_text_from_fileId_url.$file_id;
+        $lg_text = file_get_contents($lg_text_url);
+
+        $stringInput = $lg_text;
+        $stringInput = preg_replace("/ /u", "○", $stringInput);
+        $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
+        $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
+        $lg_text = $stringInput;
+
+        $this->lg_text = $lg_text;
+
+    }
+
+    public function GetTextFromSectionId($_postdata) {
+        
+        $section_id = $_postdata['sectionId'];
+        $this->section_id = $section_id;
+        $this->user_id = $_postdata['userId'];
+
+        // get from URL with file_id
+        $lg_text_url = $this->get_text_from_sectionId_url.$section_id;
+        $lg_text = file_get_contents($lg_text_url);
+
+        $stringInput = $lg_text;
+        $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput);
+        $stringInput = preg_replace("/ /u", "○", $stringInput);
+        $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
+        $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
+        $lg_text = $stringInput;
+
+        $this->lg_text = $lg_text;
+    }
+
+    public function GetTextFromLocal($_id){
+        $this->section_id = $_id;
+        echo "DEBUG: from my local"."<br>";
+        $this->lg_text = $this->GetSectionContent();
+
+    }
+
+    public function GetInfoFromPreviousPage($_postdata) {
+
+        if ($_postdata['fileId']) {
+            $this->file_id = $_postdata['fileId'];
+        }
+        if ($_postdata['sectionId']) {
+            $this->section_id = $_postdata['sectionId'];
+        }
+        if ($_postdata['text']) {
+            $this->lg_text = $_postdata['text'];
+        }
+        if ($_postdata['branchId']) {
+            $this->branch_id = $_postdata['branchId'];
+        }
+        if ($_postdata['userId']) {
+            $this->user_id = $_postdata['userId'];
+        }
+
+
+    }
+    public function InitData($_postdata) {
+        $file_id = $_postdata['fileId'];
+        $branch_id = $_postdata['branchId'];
+        $section_id = $_postdata['sectionId'];
+
+        $this->branch_id = $branch_id;
+        $this->file_id = $file_id;
+        $this->user_id = $_postdata['userId'];
+        $this->section_id = $section_id;
+        
+        if ($file_id != 0 && $branch_id != 0) {
+            // get from URL with file_id
+            $lg_text_url = $this->get_text_from_fileId_url.$file_id;
+        } else if ($section_id != 0) {
+            // get from URL with section_id
+            $lg_text_url = $this->get_text_from_sectionId_url.$section_id;
+        } else {
+            echo "wrong url!!";
+            return;
+        }
        
-        $this->Initialize($urlvalues);
+
+        $lg_text = file_get_contents($lg_text_url);
+
+        /*
+        $stringInput = preg_replace("/ /u", "○", $stringInput);
+        $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
+        $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
+        */
 
-        $section_id = $this->GetSectionId();
+        // the text is from database
+        $stringInput = $lg_text;
+        $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput);
+        $stringInput = preg_replace("/ /u", "○", $stringInput);
+        $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
+        $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
+        $lg_text = $stringInput;
+
+        $this->lg_text = $lg_text;
         
-        $stringInput = $this->GetSectionContent();
+
+    }
+
+    // === for tagging ===
+    public function StartTagging() {
+       
+        // $this->Initialize($urlvalues);
+
+        $section_id = $this->section_id;
+        $stringInput = $this->lg_text;
+        
 
         //$taglistArray = $this->GetTaglistArray();
         //for GetTaglistByTopicID: 
-       
         $taglistArray = $this->GetTaglistByTopicID($this->GetTopic());
-
-        // topic list?
+        // topic list
         $topiclistArray = $this->GetTopiclistArray();
 
 
@@ -45,6 +156,8 @@
         $data['default_topic_id'] = $this->GetTopic();
         $data['topic_tag'] = $this->GetTopicTag($this->GetTopic());
         
+        $data['info'] = array('file_id'=>$this->file_id, 'user_id'=>$this->user_id, 'branch_id'=>$this->branch_id, 'section_id'=>$this->section_id);
+        
         return $data;
         
     }
@@ -73,15 +186,120 @@
 
     }
 
+    public function UpdateInfoByResonseFromLGService($response) {
+
+        $response_file = $response["file"];
+        $response_branch = $response["branch"];
+
+        $this->file_id = (string)$response_file["id"];  // should updated
+        echo "file_id: ".$this->file_id;
+        echo "<br>";
+        $this->branch_id = (string)$response_branch["id"];
+        echo "branch_id: ".$this->branch_id;
+        echo "<br>";
+
+    }
+    public function SaveFullTextToLGService($_postdata) {
+        // save tagged text (full text) by Jorge's API to lg service
+
+        // --------
+        // saving in my local machine in developing phrase
+       if ($_postdata['text']){
+            $date = date('Y_m_d_H_i_s', time());
+            if ( file_exists("data/parsing_files/".$_postdata['sectionId'].".txt") ) {
+                $oldFile = file_get_contents("data/parsing_files/".$_postdata['sectionId'].".txt");
+                file_put_contents("data/parsing_files/".$_postdata['sectionId']."_".$date.".txt", $oldFile);
+            }
+            
+            if (get_magic_quotes_gpc()) {
+                $require = stripslashes($_postdata['text']);
+            } else {
+                $require = $_postdata['text'];
+            }
+            
+            $require = preg_replace("/【<a(.*?)>(.*?)<\/a>】/u", "【\\2】", $require);
+            $require = preg_replace('/&amp;/u', "&", $require); 
+            $require = preg_replace("/○/u", " ", $require); 
+            $require = preg_replace("/<br>/u", "\n", $require);
+            $require = preg_replace("/<br>/u", "\n", $require);
+            file_put_contents("data/parsing_files/".$_postdata['sectionId'].".txt", $require);
+        } 
+
+        echo getcwd()."<br>";
+        // ------
+
+
+        if ($_postdata['branchId'] != 0) {
+            // exiting branch case
+            $postfields = array(
+                //"text" => $_postdata['text'], 
+                "text" => $require, 
+                "branchId" => $_postdata['branchId'],
+                "userId" => $_postdata['userId'],
+                "userPreviousFileId" => $_postdata['fileId'],
+            );  
+            $save_url = $this->save_to_LGService_url;     
+
+        } else {
+            // new branch case
+            echo "saveNew!";
+            $postfields = array(
+                //"text" => $_postdata['text'], 
+                "text" => $require, 
+                "sectionId" => $_postdata['sectionId'],
+                //"userId" => $_postdata['userId'],
+                // TODO: change userId when we can query by sectionId from LGService using search
+                "userId" => "12",
+                // "label" => $_postdata['label'],
+                // TODO: ask user for lable
+                "label" => "label for section ".$_postdata['sectionId'],
+            ); 
+            $save_url = $this->save_new_to_LGService_url;
+        }   
+
+        
+
+        
+        // set up the curl resource
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $save_url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
+        curl_setopt($ch, CURLOPT_POST, true);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
+        //curl_setopt($ch, CURLOPT_HEADER, true);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
+            'Content-type: multipart/form-data;charset=utf-8'                                                          
+        ));       
+
+        // execute the request
+
+        $output = curl_exec($ch);
+
+        // output the profile information - includes the header
+        //echo($output) . PHP_EOL;
+
+        // close curl resource to free up system resources
+        curl_close($ch);
+        
+        $response = json_decode($output, true);
+        return $response;
+
+    }
     
     // === for export table ===
     public function ExportTable($urlvalues, $postdata) {
-        $this->Initialize($urlvalues);
+        // $this->Initialize($urlvalues);
         $content = $postdata['content'];
         $topic = $postdata['topic'];
         
         // outputTableArray:
-        $section_id = $this->GetSectionId();
+        // $section_id = $this->GetSectionId();
+        $section_id = $this->section_id;
+        if (!$section_id) {
+            //$this->Initialize($urlvalues);
+            $section_id = $this->GetSectionId();
+        }
 
         $section_info = $this->GetSectionInfo();
 
@@ -195,7 +413,7 @@
     
     // === for manage tag list ===
     public function EditTaglist($urlvalues) {
-        $this->Initialize($urlvalues);
+        // $this->Initialize($urlvalues);
         $result = $this->GetTaglist();
         $taglistArray = array();
         while ($row = mysql_fetch_assoc($result)) {
@@ -262,8 +480,29 @@
     }
 
     // === for config topic ===
-    public function ConfigTagsInTopic($urlvalues, $postdata) {
+    public function ConfigTagsInTopic($postdata) {
         
+        $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`";
+        $result = mysql_query($query);
+        if (!$result) {
+            return json_encode("Failed during selecting/joining taglist and topic_tag_relation table.");
+        }
+        $taglistArray = array();
+        while ($row = mysql_fetch_assoc($result)) {
+            array_push($taglistArray, array('id'=>$row['id'],'name'=>$row['name'], 'tag'=>$row['tag'], 'color'=>$row['color'], 'topic_id'=>$row['topic_id']) );
+        }
+
+        $topic = $postdata['topic'];
+        $result = $this->GetTopicByID($topic);
+        $row = mysql_fetch_assoc($result);
+        $topic_name = $row['name'];
+
+        $data = array();
+        $data['taglistArray'] = $taglistArray;
+        $data['topic'] = $topic;
+        $data['topic_name'] = $topic_name;
+
+        return $data;
     }
 
     private function GetTaglistByTopicID($topic_id) {
@@ -326,7 +565,7 @@
 
     // === for manage wordlist ===
     public function EditWordlist($urlvalues) {
-        $this->Initialize($urlvalues);
+        //$this->Initialize($urlvalues);
         $result = $this->GetWordlist();
         $wordlistArray = array();
         while ($row = mysql_fetch_assoc($result)) {
@@ -405,7 +644,7 @@
         return strlen($b)-strlen($a);
     }
 
-
+    // TODO: delete this
     private function SetSectionId($_urlvalues) {
         // TODO: maybe get user info also
         // get book id from url
@@ -425,7 +664,8 @@
     }
 
     private function GetSectionId() {
-        if ($this->section_id) {
+
+        if (is_numeric($this->section_id)) {
             return $this->section_id;
         } else {
             return json_encode("Error: No section id");
@@ -454,6 +694,7 @@
         $data['endPage'] = $endPage;
         $data['sectionName'] = $sectionName;
 
+
         return $data;
     }
 
@@ -461,25 +702,34 @@
         $section_id = $this->GetSectionId();
         $section_info = $this->GetSectionInfo();
 
+        var_dump($section_id);
+        var_dump($section_info);
+
         $bookId = $section_info['bookId'];
         $startPage = $section_info['startPage'];
-        $endPage = $section_id['endPage'];
-
+        $endPage = $section_info['endPage'];
 
         $contentString="";
         $data_path = $this->GetDataPath();
         if ( file_exists($data_path."parsing_files/".$section_id.".txt") ) {
             $contentString=file_get_contents($data_path."parsing_files/".$section_id.".txt");
             $stringInput = $contentString;
+
+            // if the text is from file system
             $stringInput = preg_replace("/ /u", "○", $stringInput);
             $stringInput = preg_replace("/\n/u", "<br>", $stringInput);
             $stringInput = preg_replace("/【(.*?)】/u", "【<a href=\"review_index_xml_images.php?books_id=".$bookId."&pages=\\1&entry=0\" target=\"_bookImg\">\\1</a>】", $stringInput);
         } else {
             $query = sprintf("SELECT `content`, `line`, `books_id` FROM `contents` WHERE `books_id`=\"%s\" AND `line`>=%d AND `line`<=%d", $bookId, $startPage, $endPage);
             $result = mysql_query($query);
+            if (!$result) {
+                return json_encode("Failed during selecting content table.");
+            }
             while ($row = mysql_fetch_assoc($result)) {
                 $contentString.="【".$row['line']."】".$row['content']."\n";
             }
+           
+            // the text is from database
             $stringInput = $contentString;
             $stringInput = preg_replace("/<(.*?)>/u", "○", $stringInput);
             $stringInput = preg_replace("/ /u", "○", $stringInput);