_binary From b07e2ac648c07c3f1bf9f5389b2dfd9f4f6c5722 Mon Sep 17 00:00:00 2001
From: Yashi Kamboj <yashi.kamboj@gmail.com>
Date: Wed, 12 Oct 2016 18:22:24 -0700
Subject: [PATCH] Fixes Issue 181 hyperlinks added, a=chris

---
 src/controllers/components/SocialComponent.php | 27 +++++++++++++++++++
 src/locale/en_US/configure.ini                 |  6 +++++
 src/models/GroupModel.php                      | 37 ++++++++++++++++++++++++++
 src/views/WikiView.php                         |  6 ++++-
 src/views/elements/WikiElement.php             | 35 ++++++++++++++++++++++--
 5 files changed, 108 insertions(+), 3 deletions(-)

diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index 6b50b30..4feefe3 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -1996,6 +1996,33 @@ class SocialComponent extends Component implements CrawlConstants
                     }
                     $data['page_id'] = $page_id;
                     break;
+                case 'links':
+                    $data["MODE"] = "links";
+                    $data["PAGE_NAME"] = "linked";
+                    if (!isset($page_id) || !$page_id) {
+                        continue;
+                    }
+                    $page_info = $group_model->getPageInfoByPageId(
+                        $page_id);
+                    if (!isset($page_name)) {
+                        $page_name = empty($page_info['PAGE_NAME'])
+                            ? "links" : $page_info['PAGE_NAME'];
+                    }
+                    $limit = isset($limit) ? $limit : 0;
+                    $num = (isset($_SESSION["MAX_PAGES_TO_SHOW"])) ?
+                        $_SESSION["MAX_PAGES_TO_SHOW"] :
+                        C\DEFAULT_ADMIN_PAGING_NUM;
+                    $data["PAGE_ID"] = $page_id;
+                    $data["PAGE_NAME"] = $page_name;
+                    $data["DISCUSS_THREAD"] = empty($page_info["DISCUSS_THREAD"]
+                        ) ? -1 : $page_info['DISCUSS_THREAD'];
+                    $data["GROUP_ID"]=$page_info["GROUP_ID"];
+                    $data["LIMIT"] = $limit;
+                    $data["RESULTS_PER_PAGE"] = $num;
+                    list($data["TOTAL_ROWS"], $data["PAGES"]) =
+                        $group_model->getPagesLinkedToList($page_id, $limit,
+                        $num);
+                    break;
                 case "media":
                     $this->mediaWiki($data, $group_id, $page_id, $sub_path);
                     $is_media_list = true;
diff --git a/src/locale/en_US/configure.ini b/src/locale/en_US/configure.ini
index 2edac7c..57f08a2 100644
--- a/src/locale/en_US/configure.ini
+++ b/src/locale/en_US/configure.ini
@@ -4952,6 +4952,9 @@ wiki_element_edit_page = "Edit %s Page"
 ; WikiView.php line: 109
 wiki_element_page_history = "%s Page History"
 ;
+; WikiView.php line: 111
+wiki_element_page_links = "What links to %s Page"
+;
 ; WikiView.php line: 114
 wiki_element_read_page = "%s Page at %s Group"
 ;
@@ -4961,6 +4964,9 @@ wiki_element_page_source = "%s Page Source"
 ; WikiView.php line: 119
 wiki_element_page_history = "%s Page History"
 ;
+; WikiView.php line: 111
+wiki_element_page_links = "What links to %s Page"
+;
 ; WikiView.php line: 125
 wiki_element_page_discuss = "%s Page Discussion"
 ;
diff --git a/src/models/GroupModel.php b/src/models/GroupModel.php
index 03dff17..46d3eb8 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -1116,6 +1116,43 @@ class GroupModel extends Model implements MediaConstants
         return false;
     }
     /**
+     * Gets all the pages that are linked to a particular wiki page
+     * by providing that page id from the table Group_Page_Link.
+     *
+     * @param int $page_id identifier for the current page
+     * @param string $limit first row we want from the result set
+     * @param string $num number of rows we want starting from the
+     *     first row in the result set
+     * @return array of elements which represent all pages linked
+     *     to the given wiki page
+     */
+    public function getPagesLinkedToList($page_id, $limit, $num)
+    {
+        $db = $this->db;
+        $sql = "SELECT COUNT(*) AS NUM FROM GROUP_PAGE_LINK
+            WHERE TO_ID = ?";
+        $result = $db->execute($sql, [$page_id]);
+        if ($result) {
+            $row = $db->fetchArray($result);
+            $total = $row['NUM'];
+        }
+        $i = 0;
+        $pages = [];
+        $sql = "SELECT G.TITLE AS PAGES_THAT_LINK
+            FROM GROUP_PAGE_LINK L, GROUP_PAGE G
+            where L.TO_ID = ? AND (L.FROM_ID = G.ID)"
+            .$db->limitOffset($limit, $num);
+        $result = $db->execute($sql, [$page_id]);
+        if ($result) {
+            while ($pages[$i] = $db->fetchArray($result)) {
+                $i++;
+            }
+            unset($pages[$i]);
+            $i--;
+        }
+        return [$total,$pages];
+    }
+    /**
      * Return the page id, page string, and discussion thread id of the
      * most recent revision of a wiki page
      *
diff --git a/src/views/WikiView.php b/src/views/WikiView.php
index a0d2a39..1003035 100644
--- a/src/views/WikiView.php
+++ b/src/views/WikiView.php
@@ -109,6 +109,8 @@ class WikiView extends View
                         "edit" => tl('wiki_element_edit_page',$human_page_name),
                         "history" => tl('wiki_element_page_history',
                             $human_page_name),
+                        "links" => t1('wiki_element_page_links',
+                            $human_page_name),
                     ];
                 } else {
                     $modes = [
@@ -119,6 +121,8 @@ class WikiView extends View
                             $human_page_name),
                         "history" => tl('wiki_element_page_history',
                             $human_page_name),
+                        "links" => t1('wiki_element_page_links',
+                            $human_page_name),
                     ];
                 }
                 $modes[htmlentities(B\feedsUrl("thread",
@@ -138,7 +142,7 @@ class WikiView extends View
                 $page_name = ($name == 'pages') ?
                     'pages' : $data['PAGE_NAME'];
                 $arg = '&amp;arg=' . $name ;
-                if ($name == 'history') {
+                if ($name == 'history'||$name == 'links') {
                     $page_id = (empty($data['PAGE_ID'])) ? "" :
                         $data['PAGE_ID'];
                     $append .= "&amp;page_id=" . $page_id;
diff --git a/src/views/elements/WikiElement.php b/src/views/elements/WikiElement.php
index f7bda91..a4f0319 100644
--- a/src/views/elements/WikiElement.php
+++ b/src/views/elements/WikiElement.php
@@ -189,6 +189,8 @@ class WikiElement extends Element implements CrawlConstants
                             $full_human_page_name),
                         "history" => tl('wiki_element_page_history',
                             $human_page_name),
+                        "links" => tl('wiki_element_page_links',
+                            $human_page_name),
                     ];
                 } else {
                     $modes = [
@@ -199,6 +201,8 @@ class WikiElement extends Element implements CrawlConstants
                             $full_human_page_name),
                         "history" => tl('wiki_element_page_history',
                             $human_page_name),
+                        "links" => tl('wiki_element_page_links',
+                            $human_page_name),
                     ];
                 }
                 $modes[htmlentities(B\feedsUrl("thread",
@@ -218,7 +222,7 @@ class WikiElement extends Element implements CrawlConstants
                 $page_name = ($name == 'pages') ?
                     'pages' : $data['PAGE_NAME'];
                 $arg = '&amp;arg=' . $name ;
-                if ($name == 'history') {
+                if ($name == 'history'||$name == 'links') {
                     $page_id = (empty($data['PAGE_ID'])) ? "" :
                         $data['PAGE_ID'];
                     $append .= "&amp;page_id=" . $page_id;
@@ -265,6 +269,9 @@ class WikiElement extends Element implements CrawlConstants
             case "resources":
                 $this->renderResources($data);
                 break;
+            case "links":
+                $this->renderLinkPage($data);
+                break;
             case "read":
                 // no break
             case "show":
@@ -1210,7 +1217,31 @@ class WikiElement extends Element implements CrawlConstants
             </div><?php
         }
     }
-
+    /**
+     * Used to draw the page which displays all other pages 
+     *    that link to a wiki page
+     *
+     * @param array $data fields contain info about all linked pages
+     */
+    public function renderLinkPage($data)
+    {
+        $logged_in = isset($data["ADMIN"]) && $data["ADMIN"];
+        if ($logged_in) {
+            $csrf_token = C\CSRF_TOKEN."=".$data[C\CSRF_TOKEN];
+        }
+        if($data["PAGES"]==null){
+            e("No page has been linked to this page");
+            return;
+        }
+        foreach ($data["PAGES"] as $key => $value) {
+            $var = $data["PAGES"][$key]["PAGES_THAT_LINK"];
+            $url = htmlentities(B\wikiUrl($var, true,
+                        $data['CONTROLLER'], $data['GROUP_ID']));
+            $url .= $csrf_token;
+            e("<td><a href='$url'>$var</a></td>");
+            e("<br />");
+        }
+    }
     /**
      * Used to draw the revision history page for a wiki document
      * Has a form that can be used to draw the diff of two revisions
-- 
1.9.5.msysgit.0

