]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SemanticSearchAdvanced.php
Update getDescription
[SourceForge/phpwiki.git] / lib / plugin / SemanticSearchAdvanced.php
1 <?php
2
3 /*
4  * Copyright 2007 Reini Urban
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 require_once 'lib/plugin/SemanticSearch.php';
24
25 /**
26  * Advanced search for relations/attributes and its values.
27  * Parse the query string, which can contain full mathematical expressions
28  * and various logical and mathematical functions and operators.
29  * Support subqueries (for _pagename ...) and temporary variables
30  * starting with _
31  *
32  * Are multiple variables valid for one page only, or is the result
33  * constructed as list of all matches? We'll stick with one page for now.
34  * This the only way I can see semantic meaning for now.
35  *
36  * Simple queries, with no variables, only against the pagename (implicit):
37  *    "is_a::city and (population < 1.000.000 or population > 10.000.000)"
38  *    "(is_a::city or is_a::country) and population < 10.000.000"
39  *
40  * Subqueries, with variables bound to the matching pagename, with (for ...):
41  *    "works_at::_organization
42  *      and (for _organization located_in::_city
43  *             and (for _city population>1000000))"
44  *
45  *    "works_at::_organization
46  *       and (for _organization
47  *             (located_in::_city
48  *             and (for _city is_a::City
49  *                   and population>1000000))
50  *          or (located_in::_country
51  *               and (for _country is_a::Country and population>5000000)))
52  *
53  * Relation links may contain wildcards. For relation and attribute names I'm not sure yet.
54  *
55  * @author: Reini Urban
56  */
57
58 class WikiPlugin_SemanticSearchAdvanced
59     extends WikiPlugin_SemanticSearch
60 {
61     function getName()
62     {
63         return _("SemanticSearchAdvanced");
64     }
65
66     function getDescription()
67     {
68         return _("Parse and execute a full query expression.");
69     }
70
71     function getDefaultArguments()
72     {
73         return array_merge
74         (
75             PageList::supportedArgs(), // paging and more.
76             array(
77                 's' => "", // query expression
78                 'page' => "*", // which pages (glob allowed), default: all
79                 'case_exact' => false,
80                 'regex' => 'auto', // hmm
81                 'noform' => false, // don't show form with results.
82                 'noheader' => false // no caption
83             ));
84     }
85
86     function showForm(&$dbi, &$request, $args, $allrelations)
87     {
88         global $WikiTheme;
89         $action = $request->getPostURL();
90         $hiddenfield = HiddenInputs($request->getArgs(), '',
91             array('action', 'page', 's'));
92         $pagefilter = HTML::input(array('name' => 'page',
93             'value' => $args['page'],
94             'title' => _("Search only in these pages. With autocompletion."),
95             'class' => 'dropdown',
96             'acdropdown' => 'true',
97             'autocomplete_complete' => 'true',
98             'autocomplete_matchsubstring' => 'false',
99             'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
100         ), '');
101         $help = Button('submit:semsearch[help]', "?", false);
102         $svalues = empty($allrelations) ? "" : join("','", $allrelations);
103         $reldef = JavaScript("var semsearch_relations = new Array('" . $svalues . "')");
104         $querybox = HTML::textarea(array('name' => 's',
105             'title' => _("Enter a valid query expression"),
106             'rows' => 4,
107             'acdropdown' => 'true',
108             'autocomplete_complete' => 'true',
109             'autocomplete_assoc' => 'false',
110             'autocomplete_matchsubstring' => 'true',
111             'autocomplete_list' => 'array:semsearch_relations'
112         ), $args['s']);
113         $submit = Button('submit:semsearch[relations]', _("Search"), false,
114             array('title' => 'Move to help page. No seperate window'));
115         $instructions = _("Search in all specified pages for the expression.");
116         $form = HTML::form(array('action' => $action,
117                 'method' => 'post',
118                 'accept-charset' => $GLOBALS['charset']),
119             $reldef,
120             $hiddenfield, HiddenInputs(array('attribute' => '')),
121             $instructions, HTML::br(),
122             HTML::table(array('border' => '0', 'width' => '100%'),
123                 HTML::tr(HTML::td(_("Pagename(s): "), $pagefilter),
124                     HTML::td(array('align' => 'right'),
125                         $help)),
126                 HTML::tr(HTML::td(array('colspan' => 2),
127                     $querybox))),
128             HTML::br(),
129             HTML::div(array('align' => 'center'), $submit));
130         return $form;
131     }
132
133     function run($dbi, $argstr, &$request, $basepage)
134     {
135         global $WikiTheme;
136
137         $this->_supported_operators = array(':=', '<', '<=', '>', '>=', '!=', '==', '=~');
138         $args = $this->getArgs($argstr, $request);
139         $posted = $request->getArg('semsearch');
140         $request->setArg('semsearch', false);
141         if ($request->isPost() and isset($posted['help'])) {
142             $request->redirect(WikiURL(_("Help/SemanticSearchAdvancedPlugin"),
143                 array('redirectfrom' => $basepage), true));
144         }
145         $allrelations = $dbi->listRelations();
146         $form = $this->showForm($dbi, $request, $args, $allrelations);
147         if (isset($this->_norelations_warning))
148             $form->pushContent(HTML::div(array('class' => 'warning'),
149                 _("Warning:") . $this->_norelations_warning));
150         extract($args);
151         // For convenience, peace and harmony we allow GET requests also.
152         if (!$args['s']) // check for good GET request
153             return $form; // nobody called us, so just display our form
154
155         // In reality we have to iterate over all found pages.
156         // To makes things shorter extract the next AND required expr and
157         // iterate only over this, then recurse into the next AND expr.
158         // => Split into an AND and OR expression tree.
159
160         $parsed_relations = $this->detectRelationsAndAttributes($args['s']);
161         $regex = '';
162         if ($parsed_relations)
163             $regex = preg_grep("/[\*\?]/", $parsed_relations);
164         // Check that all those do exist.
165         else
166             $this->error("Invalid query: No relations or attributes in the query $s found");
167         $pagelist = new PageList($args['info'], $args['exclude'], $args);
168         if (!$noheader) {
169             $pagelist->setCaption
170             (HTML($noform ? '' : HTML($form, HTML::hr()),
171                 fmt("Semantic %s Search Result for \"%s\" in pages \"%s\"",
172                     '', $s, $page)));
173         }
174         if (!$regex and $missing = array_diff($parsed_relations, $allrelations))
175             return $pagelist;
176         $relquery = new TextSearchQuery(join(" ", $parsed_relations));
177         if (!$relquery->match(join(" ", $allrelations)))
178             return $pagelist;
179         $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
180         // if we have only numeric or text ops we can optimize.
181         //$parsed_attr_ops = $this->detectAttrOps($args['s']);
182
183         //TODO: writeme
184         $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
185         $links = $dbi->linkSearch($pagequery, $linkquery, 'relation', $relquery);
186         $pagelist->_links = array();
187         while ($link = $links->next()) {
188             $pagelist->addPage($link['pagename']);
189             $pagelist->_links[] = $link;
190         }
191         $pagelist->addColumnObject
192         (new _PageList_Column_SemanticSearch_relation('relation', _("Relation"), $pagelist));
193         $pagelist->addColumnObject
194         (new _PageList_Column_SemanticSearch_link('link', _("Link"), $pagelist));
195
196         return $pagelist;
197     }
198
199     // ... (for _variable subquery) ...
200     function bindSubquery($query)
201     {
202     }
203
204     // is_a::city* and (population < 1.000.000 or population > 10.000.000)
205     // => is_a population
206     // Do we support wildcards in relation names also? is_*::city
207     function detectRelationsAndAttributes($subquery)
208     {
209         $relations = array();
210         // relations are easy
211         //$reltoken = preg_grep("/::/", preg_split("/\s+/", $query));
212         //$relations = array_map(create_function('$a','list($f,$b)=split("::",$a); return $f'),
213         //                       $reltoken);
214         foreach (preg_split("/\s+/", $query) as $whitetok) {
215             if (preg_match("/^([\w\*\?]+)::/", $whitetok))
216                 $relations[] = $m[1];
217         }
218         return $relations;
219         // for attributes we might use the tokenizer. All non-numerics excl. units and non-ops
220     }
221 }
222
223 // Local Variables:
224 // mode: php
225 // tab-width: 8
226 // c-basic-offset: 4
227 // c-hanging-comment-ender-p: nil
228 // indent-tabs-mode: nil
229 // End: