1<?php
2/**
3 * Search with Scopes
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10 // must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once(DOKU_PLUGIN.'syntax.php');
15
16class syntax_plugin_namespacesearch extends DokuWiki_Syntax_Plugin {
17
18    function getType() { return 'substition'; }
19    function getPType() { return 'block'; }
20    function getSort() { return 98; }
21
22    function connectTo($mode) {
23        $this->Lexer->addSpecialPattern('{{namespacesearch>.*?}}', $mode, 'plugin_namespacesearch');
24    }
25
26    function handle($match, $state, $pos, Doku_Handler $handler) {
27        global $ID;
28
29        $match = substr($match, 14, -2); // strip markup
30
31        $spaces = array();
32        foreach( explode(' ', $match) as $ns ) {
33            $ns = cleanID($ns);
34
35            if (($ns == '*') || ($ns == ':')) { $ns = ''; }
36            elseif ($ns == '.') { $ns = getNS($ID); }
37
38            $spaces[] = $ns;
39        }
40
41        return array($state, $spaces);
42
43    }
44
45    function render($mode, Doku_Renderer $renderer, $data) {
46        global $conf, $ID, $FANCYDATA;
47
48        list($state, $spaces) = $data;
49
50        if ($mode == 'xhtml' && $state = DOKU_LEXER_SPECIAL ) {
51
52            $renderer->nocache();
53
54            $helper = &plugin_load('helper', 'namespacesearch');
55
56            $form = new Doku_Form('namespacesearchForm', wl($ID));
57            $form->addElement(form_makeTextField('namespacesearchField', $FANCYDATA['namespacesearchField'], $this->getLang('keywords') . ':', 'namespacesearchField', null, array('autocomplete'=>'off')));
58
59            /* List of Namespaces for the scope */
60            $names = array();
61
62            $allScopes = implode(' ', $spaces);
63            $names[] = array("$allScopes", $this->getLang('all_scopes'), 'allscopes');
64
65            foreach ( $spaces as $dir ) {
66
67                $namespaces = array();
68                list($dir, $class) = explode('|', $dir);
69                search($namespaces,$conf['datadir'],'search_namespaces',null,$dir);
70
71                foreach ($namespaces as $namespace) {
72                    list($id, $type, $lvl) = array_values($namespace);
73                    $names[] = array($id, ucwords(preg_replace('/[-_:]/', ' ', str_replace("$ID:", '', $id))), $class);
74                }
75            }
76
77            $renderer->doc .= $helper->tpl_searchform($names, true);
78            // $this->namespacesearchSuggestionInserter($renderer);
79
80            return true;
81        }
82        return false;
83    }
84
85    function _capture_form_output($form) {
86
87        if ( !$form ) { return ''; }
88        ob_start();
89        $form->printForm();
90        $output = ob_get_contents();
91        ob_end_clean();
92
93        return trim($output);
94    }
95
96    function namespacesearchSuggestionInserter(&$renderer) {
97        global $FANCYDATA, $lang, $conf, $ID;
98
99        if ( !empty($FANCYDATA['query']) ) {
100
101            //do fulltext search
102            $ns = explode('@', $FANCYDATA['query']);
103
104            $query = array();
105            foreach ( explode(' ', array_shift($ns)) as $part) {
106                if (empty($part)) { continue; }
107                if ( substr($part, 0, 1) != '"' && substr($part, 0, 1) != '*' ) $part = '*' . $part;
108                if ( substr($part, -1) != '"' && substr($part, -1) != '*' ) $part = $part. '*';
109                $query[] = $part;
110            }
111
112            $query = implode(' ', $query);
113            array_unshift($ns, $query);
114            $query2 = array();
115            foreach( $ns as $namespace ) {
116                if (empty($namespace)) { continue; }
117                $query2[] = trim(cleanID($namespace));
118            }
119
120            $query = implode('@', $query2);
121            $data = ft_pageSearch($query,$regex);
122            $renderer->doc .= '<h2>Search Result</h2>';
123            if(count($data)){
124                $num = 1;
125                foreach($data as $id => $cnt){
126                    if ( $id == $ID ) { continue; }
127                    $renderer->doc .= '<div class="search_result">';
128                    $renderer->doc .= html_wikilink(':'.$id, $conf['useheading']?NULL:$id, $regex);
129                    $renderer->doc .= ': <span class="search_cnt">'.$cnt.' '.$lang['hits'].'</span><br />';
130
131                    if($num < 15){ // create snippets for the first number of matches only #FIXME add to conf ?
132                        $renderer->doc .= '<div class="search_snippet">'.ft_snippet($id,$regex).'</div>';
133                    }
134                    $renderer->doc .= '</div>';
135                    $num++;
136                }
137            }else{
138                $renderer->doc .= '<div class="nothing">'.$lang['nothingfound'].'</div>';
139            }
140
141        }
142    }
143}
144// vim:ts=4:sw=4:et:enc=utf-8:
145