*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once(DOKU_INC.'inc/fulltext.php');
class syntax_plugin_searchresults extends DokuWiki_Syntax_Plugin {
function getInfo(){
return array(
'author' => 'Todd Augsburger',
'email' => 'todd@rollerorgans.com',
'date' => '2007-08-06',
'name' => 'SearchResults Plugin',
'desc' => "returns search results as bulleted list:\n{{search>the words}} for normal [[page]] links\n{{search>the words|section}} for [[page#section|pagename]] links",
'url' => 'http://www.dokuwiki.org/plugin:searchresults',
);
}
function getType() {
return 'substition';
}
function getSort() {
return 300;
}
function connectTo($mode) {
$this->Lexer->addSpecialPattern("{{search>.*?}}", $mode, 'plugin_searchresults');
}
function handle($match, $state, $pos, &$handler) {
if ($state == DOKU_LEXER_SPECIAL) {
// strip / from start and / from end
$match = substr($match,9,-2);
return array($state, $match);
}
return array();
}
//natsort an array of pagenames
function _addSorted(&$target,$names){
global $conf;
if ($conf['useheading']) {
// sort by headings
$title_array = array();
foreach($names as $key=>$value) {
if ($title = p_get_first_heading($value))
$title_array[$key] = $title;
else
$title_array[$key] = $value;
}
natsort($title_array);
foreach($title_array as $key=>$value)
$target[] = $names[$key];
} else {
// sort by pagenames
natsort($names);
foreach($names as $value)
$target[] = $value;
}
}
function render($mode, &$renderer, $data) {
if ($mode == 'xhtml') {
list($state, $match) = $data;
if ($state == DOKU_LEXER_SPECIAL) {
$matches = array();
if(preg_match('/(.*)\|(.*)/',$match,$matches))
$search = ft_pageSearch($matches[1],$poswords);
else
$search = ft_pageSearch($match,$poswords);
if(count($search)){
$renderer->doc .= "\n";
$key_array = array();
$this->_addSorted($key_array,array_keys($search));
foreach($key_array as $value) {
$renderer->doc .= '- ';
if(empty($matches[2]))
$renderer->doc .= html_wikilink(':'.$value);
else
$renderer->doc .= html_wikilink(':'.$value.'#'.$matches[2],($title = p_get_first_heading($value)) ? $title : $value);
$renderer->doc .= "\n";
}
$renderer->doc .= "
\n";
}
}
return true;
}
return false;
}
}
===== Tips =====
*You may see the searchresults plugin in use (in a closed wiki) [[http://cobs.rollerorgans.com/cobs/2/home|here]] where a [[plugin:folded|folded]] list is presented.\\ (Click on the "Composer"--the wiki syntax is %%{{search>"Lowell Mason" @cobs}}%%)
*Or in use (in an open wiki) [[http://wiki.mechanicalmusic.info/scales/home|here]]
===== Discussion =====
Thanks for making this available. I got it installed and it mostly does what I want, but...
It seems to be missing:
require_once(DOKU_INC.'inc/fulltext.php');
> Correct! I'll update the code.
Also, a few other things would be nice:
* the search results don't update without setting the cache time to something short for the entire wiki; it would be nice if that wasn't necessary
> I use the %%~~NOCACHE~~%% tag in pages which need to be updated "realtime"
* it would be nice to be able to include/exclude namespaces
> I use "@namespace" within my searches to limit them to a single namespace ...
> and do multiple searches for multiple criteria--that way it's not limited to just namespaces
* it would be nice to be able to display the namespace in the search results
> So, using the above, I put the namespaces or descriptions on the page. Tables work well, too.
* it would be nice to be able to exclude specific pages
> Agreed. My "work-around" is that I use another plugin which allows tags in the page, so my searches exclude certain tags
* the page containing the search should probably always be excluded
> Well, not always--I want my "lists" to be identical everywhere. But it might make a good option
* it would be nice to be able to display context optionally
> Good suggestion
----
\\
Hi Todd. Superb plugin - it makes multiple relationships between pages really easy to do.
However, I WOULD like the section search facility. Why?
My site has lists of tunes, and I would like to link to tune authors.
But I do sometimes refer to the same people as //playing// those tunes. Obviously, this would cause doubles where I don't want them.
However, since I will put tune authors in a separate section, this would work.
Could you maybe just quote the necessary code here? I could then just paste it in and you would not need to do a new release.
Cheers
[[http://www.tunemason.org|Ed Bradburn]]
> The original code (which included the ability to go to a section) is what is still shown above
> Note that it did not "search" for the section, it simply allowed the section text to be specified and passed to the resultant HTML. i.e. ...
> {{search>the words|section}}
returned a link like
> [[page#section|pagename]]
[[todd@rollerorgans.com|Todd Augsburger 2008-10-17]]