_binary From d3ec89ad8e84f7fcb8f2484f68036c1a3860eeb2 Mon Sep 17 00:00:00 2001
From: Yashi Kamboj <yashi.kamboj@gmail.com>
Date: Sat, 14 May 2016 11:14:28 -0700
Subject: [PATCH] Adding link extraction functionality to wiki system, fixing
 issue 0000179, a=chris

---
 src/configs/Config.php     |  4 ++--
 src/library/WikiParser.php | 56 ++++++++++++++++++++++++++++++++++++++++------
 src/models/GroupModel.php  | 15 +++++++++++++
 3 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/src/configs/Config.php b/src/configs/Config.php
index cf697ee..625b081 100755
--- a/src/configs/Config.php
+++ b/src/configs/Config.php
@@ -248,9 +248,9 @@ if (MAINTENANCE_MODE && $_SERVER["SERVER_ADDR"] != $_SERVER["REMOTE_ADDR"]) {
 nsdefine('DEFAULT_WORK_DIRECTORY', PARENT_DIR . "/work_directory");
 
 if (!nsdefined('WORK_DIRECTORY')) {
-/*+++ The next block of code is machine edited, change at
+/*+++ The next block of code is machine edited, change at 
 your own risk, please use configure web page instead +++*/
-nsdefine('WORK_DIRECTORY', DEFAULT_WORK_DIRECTORY);
+nsdefine('WORK_DIRECTORY', 'C:/xampp/htdocs/yioop/work_directory');
 /*++++++*/
 // end machine edited code
 }
diff --git a/src/library/WikiParser.php b/src/library/WikiParser.php
index e58a5f8..e2808da 100644
--- a/src/library/WikiParser.php
+++ b/src/library/WikiParser.php
@@ -74,7 +74,7 @@ class WikiParser implements CrawlConstants
         $not_paragraph = '(?:\A|[^\n]|[^\n]\n)';
         $class_or_id = '0-9a-zA-Z\_\-\s';
         $this->minimal = $minimal;
-        //assume substitutions are applied after htmlentities called on string
+        //assume substitutions are applied after html entities called on string
         $substitutions = [
             ['/(\A|\n)=\s*([^=]+)\s*=/', "\n<h1 id='$2'>$2</h1>"],
             ['/(\A|\n)==\s*([^=]+)\s*==/', "\n<h2 id='$2'>$2</h2>"],
@@ -103,12 +103,6 @@ class WikiParser implements CrawlConstants
         $minimal_substitutions = [
             ['/\[\[(http[^\s\|\]]+)\|([^\[\]]+?)\]\]/s',
                 "<a href=\"$1\">$2</a>"],
-            ['/\[\[([^\[\]]+\@[^\[\]]+?)\|([^\[\]]+?)\]\]/s',
-                "<a href=\"@@$1@@\">$2</a>\t"],
-            ['/\[\[([^\[\]]+?)\|([^\[\]]+?)\]\]/s',
-                "<a href=\"{$base_address}$1\">$2</a>\t"],
-            ['/\[\[((\#?)[^\[\]]+?)\]\]/s',
-                "<a href=\"{$base_address}$1\">$1</a>\t"],
             ["/\[(http[^\s\]]+)\s+(.+?)\]/s", "[<a href=\"$1\">$2</a>]"],
             ["/\[(http[^\]\s]+)\s*\]/","(<a href=\"$1\">&rarr;</a>)"],
             ["/&#039;&#039;&#039;&#039;&#039;(.+?)".
@@ -239,18 +233,30 @@ class WikiParser implements CrawlConstants
             ['/\n*?{{right\s*\|\s*('.$not_braces .')}}/',
                 "\n\n$esc<div class='align-right'>\n\n$1\n\n$esc</div>"],
         ];
+        $link_substitutions = [
+            ['/\[\[([^\[\]]+\@[^\[\]]+?)\|([^\[\]]+?)\]\]/s',
+                "<a href=\"@@$1@@\">$2</a>\t"],
+            ['/\[\[([^\[\]]+?)\|([^\[\]]+?)\]\]/s',
+                "<a href=\"{$base_address}$1\">$2</a>\t"],
+            ['/\[\[((\#?)[^\[\]]+?)\]\]/s',
+                "<a href=\"{$base_address}$1\">$1</a>\t"],
+        ];
         if ($minimal) {
             $substitutions = [
                 ['/(\A|\n)=\s*([^=]+)\s*=/',
                     "\n<h1 id='$2'>$2</h1>"],
             ];
         }
+        $minimal_substitutions = array_merge($link_substitutions,
+            $minimal_substitutions);
         $substitutions = array_merge($substitutions,
             $minimal_substitutions);
         $this->matches = [];
         $this->replaces = [];
         $this->braces_matches = [];
         $this->braces_replaces = [];
+        $this->link_matches = [];
+        $this->link_replaces = [];
         $this->base_address = $base_address;
         if ($add_substitutions != []) {
             $substitutions = array_merge($substitutions,
@@ -263,6 +269,9 @@ class WikiParser implements CrawlConstants
             list($this->braces_matches[], $this->braces_replaces[]) =
                 $substitution;
         }
+        foreach($link_substitutions as $link_substitution) {
+            list($this->link_matches[], $this->link_replaces[]) = $link_substitution;
+        }
     }
     /**
      * Parses a mediawiki document to produce an HTML equivalent
@@ -684,6 +693,39 @@ class WikiParser implements CrawlConstants
         $page = preg_replace('/{{reflist(.+?)}}/si', $references, $page);
         return $page;
     }
+    /**
+     * fetches internal links from wiki syntax
+     * @param array $document a wiki document
+     * @return array of links
+     */
+    function fetchLinks($document)
+    {
+        $matches =null;
+        $links = array();
+        $link_substitutions = $this->link_matches;
+        foreach($link_substitutions as $keyLink=>$valueLink){
+            $link_substitution = $link_substitutions[$keyLink];
+            preg_match_all($link_substitution,$document,$matches, PREG_PATTERN_ORDER);
+            $links = array_unique(array_merge($links, $matches[0]));
+        }
+        foreach($links as $key=>$link){
+            $extract= substr($link,2);
+            //removing page names with http, .com and .org
+            if(preg_match('/http/',$extract)||preg_match('/.com/',$extract)||preg_match('/.org/',$extract)){
+                unset($links[$key]);
+                continue;
+            }
+            if(($pos=strpos($extract, '#')) !== false){
+                $links[$key]=substr($extract,0,$pos);
+                continue;
+            }
+            if(($pos=strpos($extract, '|')) !== false){
+                $links[$key]=substr($extract,0,$pos);
+            }
+        }
+        $links = array_unique($links);
+        return $links;
+    }
 }
 /**
  * Callback used by a preg_replace_callback in nextPage to make a table
diff --git a/src/models/GroupModel.php b/src/models/GroupModel.php
index 78f3e6f..1584584 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -1003,7 +1003,11 @@ class GroupModel extends Model implements MediaConstants
     {
         $db = $this->db;
         $pubdate = time();
+        $fetched_ids = array();
+        $parent_id_link = 0;
+        $link_type_id = -1;
         $parser = new WikiParser($base_address, $additional_substitutions);
+        $fetched_links = $parser->fetchLinks($page);
         $parsed_page = $parser->parse($page);
         if ($page_id = $this->getPageID($group_id, $page_name, $locale_tag)) {
             //can only add and use resources for a page that exists
@@ -1011,6 +1015,7 @@ class GroupModel extends Model implements MediaConstants
                 $locale_tag, $parsed_page);
             $sql = "UPDATE GROUP_PAGE SET PAGE=? WHERE ID = ?";
             $result = $db->execute($sql, [$parsed_page, $page_id]);
+            $parent_id_link = $page_id;
         } else {
             $discuss_thread = $this->addGroupItem(0, $group_id, $user_id,
                 $thread_title, $thread_description." ".date("r", $pubdate),
@@ -1020,11 +1025,21 @@ class GroupModel extends Model implements MediaConstants
             $result = $db->execute($sql, [$discuss_thread, $group_id,
                 $page_name, $parsed_page, $locale_tag]);
             $page_id = $db->insertID("GROUP_PAGE");
+            $parent_id_link = $page_id;
             ImpressionModel::initWithDb($user_id, $page_id, C\WIKI_IMPRESSION,
                 $db);
             ImpressionModel::initWithDb(C\PUBLIC_USER_ID, $page_id,
                 C\WIKI_IMPRESSION, $db);
         }
+        $sql = "INSERT INTO GROUP_PAGE_LINK (LINK_TYPE_ID, FROM_ID, TO_ID) VALUES (?,?,?)";
+        //get ID for each of the child links stored in $fetched_links
+        foreach($fetched_links as $key=>$link) {
+            $fetched_ids[$key] = $this->getPageID($group_id, $fetched_links[$key], $locale_tag);
+            //insert into GROUP_PAGE_LINK values of parent and child links
+            if($fetched_ids[$key]!== false){
+                $db->execute($sql, [$link_type_id, $parent_id_link, $fetched_ids[$key]]);
+            }
+        }
         $sql = "INSERT INTO GROUP_PAGE_HISTORY (PAGE_ID, EDITOR_ID,
             GROUP_ID, TITLE, PAGE, LOCALE_TAG, PUBDATE, EDIT_COMMENT)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
-- 
1.9.5.msysgit.0

