[dokuwiki] [patch] add support for OpenSearch

the attached patch adds support for OpenSearch:
http://en.wikipedia.org/wiki/OpenSearch
http://www.opensearch.org/

this allows web browsers to easily detect and add the search engine to their 
local search list ... FireFox-2.0 and IE7 both support this ... here's a 
little action shot:
http://wh0rd.org/firefox-blackfin-docs.png

while i originally added support to the last release, i had updated it to the 
latest code in darcs ... i havent fully tested it as i dont have a darcs 
installation, but ive tested these snippets in the last release and it's 
working nicely :)

here's a summary of the changes ... please be nice as i've never hacked on 
dokuwiki before

inc/template.php:
 - add new <link> tag to inform browser of the search plugin

lib/exe/ajax.php:
 - allow the qsearch function to be called from either GET or POST
 - implement ajax_suggestions() so that people can get suggestions if they 
want ... there are two issues here:
        - should the cap of 10 results be configurable ?
        - is there a function already that does what my little wiki_url() does ?

lib/exe/opensearch.php:
 - generate the actual xml search plugin file based upon the server config

cheers
-mike
diff -rN -u old-dokuwiki/inc/template.php new-dokuwiki/inc/template.php
--- old-dokuwiki/inc/template.php       2006-10-27 03:54:22.000000000 -0400
+++ new-dokuwiki/inc/template.php       2006-10-27 03:54:23.000000000 -0400
@@ -179,6 +179,8 @@
 
   // the usual stuff
   $head['meta'][] = array( 'name'=>'generator', 'content'=>'DokuWiki 
'.getVersion() );
+  $head['link'][] = array( 'name'=>'search', 
'type'=>'application/opensearchdescription+xml',
+                           'href'=>DOKU_BASE.'lib/exe/opensearch.php', 
'title'=>$conf['title'] );
   $head['link'][] = array( 'rel'=>'start', 'href'=>DOKU_BASE );
   $head['link'][] = array( 'rel'=>'contents', 'href'=> 
wl($ID,'do=index',false,'&'),
                            'title'=>$lang['btn_index'] );
diff -rN -u old-dokuwiki/lib/exe/ajax.php new-dokuwiki/lib/exe/ajax.php
--- old-dokuwiki/lib/exe/ajax.php       2006-10-27 03:54:23.000000000 -0400
+++ new-dokuwiki/lib/exe/ajax.php       2006-10-27 03:54:26.000000000 -0400
@@ -23,8 +23,12 @@
 
 
 //call the requested function
-if (!isset($_POST['call'])) { return; }
-$call = 'ajax_'.$_POST['call'];
+if(!isset($_POST['call']))
+  $call = 'ajax_'.$_POST['call'];
+else if(!isset($_GET['call']))
+  $call = 'ajax_'.$_GET['call'];
+else
+  return;
 if(function_exists($call)){
   $call();
 }else{
@@ -47,6 +51,7 @@
   global $lang;
 
   $query = cleanID($_POST['q']);
+  if(empty($query)) $query = cleanID($_GET['q']);
   if(empty($query)) return;
 
   require_once(DOKU_INC.'inc/html.php');
@@ -68,6 +73,46 @@
 }
 
 /**
+ * Support OpenSearch suggestions
+ * 
http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
+ *
+ * @author Mike Frysinger <vapier@xxxxxxxxxx>
+ */
+function ajax_suggestions() {
+  global $conf;
+  global $lang;
+
+  $query = cleanID($_POST['q']);
+  if(empty($query)) $query = cleanID($_GET['q']);
+  if(empty($query)) return;
+
+  require_once(DOKU_INC.'inc/html.php');
+  require_once(DOKU_INC.'inc/fulltext.php');
+  require_once(DOKU_INC.'inc/JSON.php');
+
+  $data = array();
+  $data = ft_pageLookup($query);
+
+  if(!count($data)) return;
+
+  /* limit results to 10 hits */
+  $data = array_slice($data, 0, 10);
+
+  /* now construct a json */
+  function wiki_url($article) { return DOKU_URL.DOKU_SCRIPT.'?id='.$article; }
+  $data = array_map(chop, $data);
+  $suggestions = array(
+    $query,
+    $data,
+    array(), /* page's lack descriptions so don't bother providing any */
+    array_map(wiki_url, $data)
+  );
+
+  $json = new JSON();
+  print $json->encode($suggestions);
+}
+
+/**
  * Refresh a page lock and save draft
  *
  * Andreas Gohr <andi@xxxxxxxxxxxxxx>
diff -rN -u old-dokuwiki/lib/exe/opensearch.php 
new-dokuwiki/lib/exe/opensearch.php
--- old-dokuwiki/lib/exe/opensearch.php 1969-12-31 19:00:00.000000000 -0500
+++ new-dokuwiki/lib/exe/opensearch.php 2006-10-27 03:54:26.000000000 -0400
@@ -0,0 +1,36 @@
+<?php
+/**
+ * DokuWiki OpenSearch creator
+ *
+ * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
+ * @author     Mike Frysinger <vapier@xxxxxxxxxx>
+ */
+
+/* You can find the OpenSearch spec/website here:
+ * http://www.opensearch.org/
+ */
+
+if(!defined('DOKU_INC')) 
define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
+if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session 
or authentication here (better caching)
+
+require_once(DOKU_INC.'inc/init.php');
+
+/* Need to echo this so php doesn't try to use the <?...?> */
+echo '<?xml version="1.0"?>';
+
+/* Doesn't seem to be any sort of "Description" configuration option,
+ * so we'll skip this field for now ...
+<Description><?php echo $conf['description']?></Description>
+ */
+?>
+
+<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/";>
+<ShortName><?php echo $conf['title']?></ShortName>
+<Image type="image/x-icon"><?php echo DOKU_URL?>lib/images/favicon.ico</Image>
+<Url type="text/html" template="<?php echo 
DOKU_URL.DOKU_SCRIPT?>?do=search&amp;id={searchTerms}"/>
+<Url type="application/x-suggestions+json" template="<?php echo 
DOKU_URL?>lib/exe/ajax.php?call=suggestions&amp;q={searchTerms}"/>
+</OpenSearchDescription>
+
+<?php
+//Setup VIM: ex: et ts=4 enc=utf-8 :
+?>

Other related posts: