_binary From 320236e14143080e5254e07817a74fd11ccae529 Mon Sep 17 00:00:00 2001
From: Yashi Kamboj <yashi.kamboj@gmail.com>
Date: Fri, 7 Oct 2016 09:57:52 -0700
Subject: [PATCH] Fixes Issue 182, added relationship type to the database and
 updated What Links Here with the relationship name and not ID, a=chris

---
 src/library/WikiParser.php         | 12 ++++++++-
 src/models/GroupModel.php          | 54 ++++++++++++++++++++++++++++++++------
 src/views/elements/WikiElement.php |  2 +-
 3 files changed, 58 insertions(+), 10 deletions(-)

diff --git a/src/library/WikiParser.php b/src/library/WikiParser.php
index fadaa9e..8589a50 100644
--- a/src/library/WikiParser.php
+++ b/src/library/WikiParser.php
@@ -234,6 +234,8 @@ class WikiParser implements CrawlConstants
                 "\n\n$esc<div class='align-right'>\n\n$1\n\n$esc</div>"],
         ];
         $link_substitutions = [
+            ['/\[\[([^\[\]]+?)\|([^\[\]]+?)\|([^\[\]]+?)\]\]/s',
+                "<a href=\"{$base_address}$2\">$1:$3</a>\t"],
             ['/\[\[([^\[\]]+\@[^\[\]]+?)\|([^\[\]]+?)\]\]/s',
                 "<a href=\"@@$1@@\">$2</a>\t"],
             ['/\[\[([^\[\]]+?)\|([^\[\]]+?)\]\]/s',
@@ -712,6 +714,7 @@ class WikiParser implements CrawlConstants
         }
         foreach ($links as $key => $link) {
             $extract = trim($link, "[] \t\n\r\0\x0B");
+            $relationType = null;
             //removing page names with http, .com and .org
             if(preg_match('/http/',$extract)||
                 preg_match('/.com/',$extract)||preg_match('/.org/',$extract)) {
@@ -723,8 +726,15 @@ class WikiParser implements CrawlConstants
                 continue;
             }
             if(($pos = strpos($extract, '|')) !== false){
-                $extract = substr($extract, 0, $pos);
+                $nextExtract = substr($extract,$pos+1);
+                if(($nextPos = strpos($nextExtract, '|')) !== false){
+                    $relationType = substr($extract, 0, $pos);
+                    $extract = substr($nextExtract, 0, $nextPos);
+                }
+                else
+                    $extract = substr($extract, 0, $pos);
             }
+            $extract = $extract."|".$relationType;
             $links[$key] = $extract;
         }
         $links = array_unique($links);
diff --git a/src/models/GroupModel.php b/src/models/GroupModel.php
index 1ea2052..33b5ae5 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -609,6 +609,24 @@ class GroupModel extends Model implements MediaConstants
         return $row;
     }
     /**
+     * Returns the id of the relationship between wiki pages with the given name
+     *
+     * @param string $relationship_type the relationship type to get id about
+     * @return array row from PAGE_RELATIONSHIP table or false (if no access or doesn't
+     *     exist)
+     */
+    public function getRelationshipID($relationship_type)
+    {
+        $db = $this->db;
+        $sql = "SELECT ID as Id FROM PAGE_RELATIONSHIP WHERE NAME=? " . $db->limitOffset(1);
+        $result = $db->execute($sql, [$relationship_type]);
+        if (!$result) {
+            return false;
+        }
+        $row = $db->fetchArray($result);
+        return $row['Id'];
+    }
+    /**
      * Returns an array of user information about users who have contributed
      * to a thread or own the group a thread belongs to
      *
@@ -1004,7 +1022,7 @@ class GroupModel extends Model implements MediaConstants
         $db = $this->db;
         $pubdate = time();
         $parser = new WikiParser($base_address, $additional_substitutions);
-        $fetched_links = $parser->fetchLinks($page);
+        $links_relationships =$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
@@ -1050,16 +1068,35 @@ class GroupModel extends Model implements MediaConstants
         $pre_link_sql = "INSERT INTO GROUP_PAGE_PRE_LINK ".
             "(LINK_TYPE_ID, FROM_ID, TO_GROUP_ID, TO_PAGE_NAME) ".
             " VALUES (?, ?, ?, ?)";
-        //get ID for each of the child links stored in $fetched_links
-        foreach($fetched_links as $fetched_link) {
+        //get ID for each of the child links stored in $links_relationships
+        foreach($links_relationships as $links_relationship) {
+            //extract link and relation type from $links_relationship
+            $linkPos = strpos($links_relationship, '|');
+            $fetched_link = substr($links_relationship, 0, $linkPos);
+            $relationship_type = strtolower(substr($links_relationship, $linkPos+1));
+            //get relationship id, if it exists provided relationship type is not null
+            if($relationship_type == null){
+                $relationship_id = C\WIKI_STANDARD_LINK;
+            }
+            else{
+                if($relationship_id = $this->getRelationshipID($relationship_type)){
+                    //do nothing, already updated relationship_id
+                }
+                else{
+                    $sql = "INSERT INTO PAGE_RELATIONSHIP (NAME) VALUES (?)";
+                    $db->execute($sql, [$relationship_type]);
+                    $relationship_id = $this->getRelationshipID($relationship_type);
+                }
+            }
+            //get page id
             $linked_page_id = $this->getPageID($group_id,
                 $fetched_link, $locale_tag);
             //insert into GROUP_PAGE_LINK values of parent and child links
             if($linked_page_id === false) {
-                $db->execute($pre_link_sql, [C\WIKI_STANDARD_LINK, $page_id,
+                $db->execute($pre_link_sql, [$relationship_id, $page_id,
                     $group_id, $fetched_link]);
             } else {
-                $db->execute($link_sql, [C\WIKI_STANDARD_LINK, $page_id,
+                $db->execute($link_sql, [$relationship_id, $page_id,
                     $linked_page_id]);
             }
         }
@@ -1136,9 +1173,10 @@ class GroupModel extends Model implements MediaConstants
         }
         $i = 0;
         $pages = [];
-        $sql = "SELECT G.TITLE AS PAGES_THAT_LINK, L.LINK_TYPE_ID AS RELATIONSHIP_TYPE 
-            FROM GROUP_PAGE_LINK L, GROUP_PAGE G where L.TO_ID = ? 
-            AND (L.FROM_ID = G.ID)".$db->limitOffset($limit, $num);
+        $sql = "SELECT G.TITLE AS PAGES_THAT_LINK, R.NAME AS RELATIONSHIP_TYPE 
+            FROM GROUP_PAGE_LINK L, GROUP_PAGE G, PAGE_RELATIONSHIP R 
+            where L.TO_ID = ? AND (L.FROM_ID = G.ID) AND (L.LINK_TYPE_ID = R.ID)"
+            .$db->limitOffset($limit, $num);
         $result = $db->execute($sql, [$page_id]);
         if ($result) {
             while ($pages[$i] = $db->fetchArray($result)) {
diff --git a/src/views/elements/WikiElement.php b/src/views/elements/WikiElement.php
index 1149d48..3d7a48a 100644
--- a/src/views/elements/WikiElement.php
+++ b/src/views/elements/WikiElement.php
@@ -1226,7 +1226,7 @@ class WikiElement extends Element implements CrawlConstants
     {       
         foreach ($data["PAGES"] as $key => $value) {
             e($data["PAGES"][$key]["PAGES_THAT_LINK"]);
-            e("\t");
+            e("\t".":"."\t");
             e($data["PAGES"][$key]["RELATIONSHIP_TYPE"]); 
             e("<br />");
         }
-- 
1.9.5.msysgit.0

