From 8d22f1e96be5aa2c65ecb6ee934debbfe0f8f4cc Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sat, 26 Jun 2010 13:38:10 +0200
Subject: [PATCH] Changes to the ft_pageLookup and related event FS#1978

This patch changes the ft_pageLookup function to always return the
title of pages with the result. This makes it easier to work with the
array, as it no longer changes between numeric and key indexes depending
on useheading.
This also means that action plugins subscribed to
SEARCH_QUERY_PAGELOOKUP need to be adjusted. The event contains a new
data field called 'has_titles' which plugins can use to check for
backwards compatibility.
---
 inc/fulltext.php | 18 +++++++++---------
 inc/html.php     | 16 ++++++++++------
 lib/exe/ajax.php | 20 +++++++++++---------
 3 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/inc/fulltext.php b/inc/fulltext.php
index 950e7f7d3..1c9981812 100644
--- a/inc/fulltext.php
+++ b/inc/fulltext.php
@@ -215,29 +215,29 @@ function ft_mediause($id,$max){
  * By default it only matches the pagename and ignores the
  * namespace. This can be changed with the second parameter.
  * The third parameter allows to search in titles as well.
- * If the function should search in titles as well, the return array
- * has the ids as key and the titles as value.
  *
- * refactored into ft_pageLookup(), _ft_pageLookup() and trigger_event()
+ * The function always returns titles as well
  *
+ * @triggers SEARCH_QUERY_PAGELOOKUP
  * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Adrian Lang <lang@cosmocode.de>
  */
-function ft_pageLookup($id, $not_in_ns=true, $not_in_title=true){
-    $data = compact('id', 'not_in_ns', 'not_in_title');
+function ft_pageLookup($id, $in_ns=false, $in_title=false){
+    $data = compact('id', 'in_ns', 'in_title');
+    $data['has_titles'] = true; // for plugin backward compatibility check
     return trigger_event('SEARCH_QUERY_PAGELOOKUP', $data, '_ft_pageLookup');
 }
 
 function _ft_pageLookup(&$data){
     // split out original parameters
-
     $id = $data['id'];
     if (preg_match('/(?:^| )@(\w+)/', $id, $matches)) {
         $ns = cleanID($matches[1]) . ':';
         $id = str_replace($matches[0], '', $id);
     }
 
-    $in_ns = !$data['not_in_ns'];
-    $in_title = !$data['not_in_title'];
+    $in_ns    = $data['in_ns'];
+    $in_title = $data['in_title'];
 
     $pages  = array_map('rtrim', idx_getIndex('page', ''));
     $titles = array_map('rtrim', idx_getIndex('title', ''));
@@ -267,7 +267,7 @@ function _ft_pageLookup(&$data){
     }
 
     uasort($pages,'ft_pagesorter');
-    return $in_title ? $pages : array_keys($pages);
+    return $pages;
 }
 
 /**
diff --git a/inc/html.php b/inc/html.php
index 01823449e..7e842e052 100644
--- a/inc/html.php
+++ b/inc/html.php
@@ -336,18 +336,22 @@ function html_search(){
     //do quick pagesearch
     $data = array();
 
-    if($id) $data = ft_pageLookup($id);
+    if($id) $data = ft_pageLookup($id,true,useHeading('navigation'));
     if(count($data)){
         print '<div class="search_quickresult">';
         print '<h3>'.$lang['quickhits'].':</h3>';
         print '<ul class="search_quickhits">';
-        foreach($data as $id){
+        foreach($data as $id => $title){
             print '<li> ';
-            $ns = getNS($id);
-            if($ns){
-                $name = shorten(noNS($id), ' ('.$ns.')',30);
+            if (useHeading('navigation')) {
+                $name = $title;
             }else{
-                $name = $id;
+                $ns = getNS($id);
+                if($ns){
+                    $name = shorten(noNS($id), ' ('.$ns.')',30);
+                }else{
+                    $name = $id;
+                }
             }
             print html_wikilink(':'.$id,$name);
             print '</li> ';
diff --git a/lib/exe/ajax.php b/lib/exe/ajax.php
index 9c10ca548..8b03bb07c 100644
--- a/lib/exe/ajax.php
+++ b/lib/exe/ajax.php
@@ -54,7 +54,7 @@ function ajax_qsearch(){
   if(empty($query)) $query = $_GET['q'];
   if(empty($query)) return;
 
-  $data = ft_pageLookup($query, true, false);
+  $data = ft_pageLookup($query, true, useHeading('navigation'));
 
   if(!count($data)) return;
 
@@ -93,6 +93,7 @@ function ajax_suggestions() {
   $data = array();
   $data = ft_pageLookup($query);
   if(!count($data)) return;
+  $data = array_keys($data);
 
   // limit results to 15 hits
   $data = array_slice($data, 0, 15);
@@ -252,26 +253,27 @@ function ajax_linkwiz(){
 
     // use index to lookup matching pages
     $pages = array();
-    $pages = ft_pageLookup($id,false);
+    $pages = ft_pageLookup($id,true);
 
     // result contains matches in pages and namespaces
     // we now extract the matching namespaces to show
     // them seperately
     $dirs  = array();
-    $count = count($pages);
-    for($i=0; $i<$count; $i++){
-      if(strpos(noNS($pages[$i]),$id) === false){
+
+
+    foreach($pages as $pid => $title){
+      if(strpos(noNS($pid),$id) === false){
         // match was in the namespace
-        $dirs[getNS($pages[$i])] = 1; // assoc array avoids dupes
+        $dirs[getNS($pid)] = 1; // assoc array avoids dupes
       }else{
         // it is a matching page, add it to the result
         $data[] = array(
-          'id'    => $pages[$i],
-          'title' => p_get_first_heading($pages[$i],false),
+          'id'    => $pid,
+          'title' => $title,
           'type'  => 'f',
         );
       }
-      unset($pages[$i]);
+      unset($pages[$pid]);
     }
     foreach($dirs as $dir => $junk){
       $data[] = array(
-- 
GitLab