4 * Copyright 2007 Reini Urban
6 * This file is part of PhpWiki.
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.
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.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 require_once('lib/plugin/SemanticSearch.php');
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
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.
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"
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))"
45 * "works_at::_organization
46 * and (for _organization
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)))
53 * Relation links may contain wildcards. For relation and attribute names I'm not sure yet.
55 * @author: Reini Urban
58 class WikiPlugin_SemanticSearchAdvanced
59 extends WikiPlugin_SemanticSearch
62 return _("SemanticSearchAdvanced");
64 function getDescription() {
65 return _("Parse and execute a full query expression");
67 function getDefaultArguments() {
70 PageList::supportedArgs(), // paging and more.
72 's' => "", // query expression
73 'page' => "*", // which pages (glob allowed), default: all
74 'case_exact' => false,
75 'regex' => 'auto', // hmm
76 'noform' => false, // don't show form with results.
77 'noheader' => false // no caption
81 function showForm (&$dbi, &$request, $args, $allrelations) {
83 $action = $request->getPostURL();
84 $hiddenfield = HiddenInputs($request->getArgs(),'',
85 array('action','page','s'));
86 $pagefilter = HTML::input(array('name' => 'page',
87 'value' => $args['page'],
88 'title' => _("Search only in these pages. With autocompletion."),
89 'class' => 'dropdown',
90 'acdropdown' => 'true',
91 'autocomplete_complete' => 'true',
92 'autocomplete_matchsubstring' => 'false',
93 'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
95 $help = Button('submit:semsearch[help]', "?", false);
96 $svalues = empty($allrelations) ? "" : join("','", $allrelations);
97 $reldef = JavaScript("var semsearch_relations = new Array('".$svalues."')");
98 $querybox = HTML::textarea(array('name' => 's',
99 'title' => _("Enter a valid query expression"),
101 'acdropdown' => 'true',
102 'autocomplete_complete' => 'true',
103 'autocomplete_assoc' => 'false',
104 'autocomplete_matchsubstring' => 'true',
105 'autocomplete_list' => 'array:semsearch_relations'
107 $submit = Button('submit:semsearch[relations]', _("Search"), false,
108 array('title' => 'Move to help page. No seperate window'));
109 $instructions = _("Search in all specified pages for the expression.");
110 $form = HTML::form(array('action' => $action,
112 'accept-charset' => $GLOBALS['charset']),
114 $hiddenfield, HiddenInputs(array('attribute'=>'')),
115 $instructions, HTML::br(),
116 HTML::table(array('border'=>'0','width' =>'100%'),
117 HTML::tr(HTML::td(_("Pagename(s): "), $pagefilter),
118 HTML::td(array('align' => 'right'),
120 HTML::tr(HTML::td(array('colspan' => 2),
123 HTML::div(array('align'=>'center'),$submit));
127 function run ($dbi, $argstr, &$request, $basepage) {
130 $this->_supported_operators = array(':=','<','<=','>','>=','!=','==','=~');
131 $args = $this->getArgs($argstr, $request);
132 $posted = $request->getArg('semsearch');
133 $request->setArg('semsearch', false);
134 if ($request->isPost() and isset($posted['help'])) {
135 $request->redirect(WikiURL(_("Help/SemanticSearchAdvancedPlugin"),
136 array('redirectfrom' => $basepage), true));
138 $allrelations = $dbi->listRelations();
139 $form = $this->showForm($dbi, $request, $args, $allrelations);
140 if (isset($this->_norelations_warning))
141 $form->pushContent(HTML::div(array('class' => 'warning'),
142 _("Warning:").$this->_norelations_warning));
144 // For convenience, peace and harmony we allow GET requests also.
145 if (!$args['s']) // check for good GET request
146 return $form; // nobody called us, so just display our form
148 // In reality we have to iterate over all found pages.
149 // To makes things shorter extract the next AND required expr and
150 // iterate only over this, then recurse into the next AND expr.
151 // => Split into an AND and OR expression tree.
153 $parsed_relations = $this->detectRelationsAndAttributes($args['s']);
155 if ($parsed_relations)
156 $regex = preg_grep("/[\*\?]/", $parsed_relations);
157 // Check that all those do exist.
159 $this->error("Invalid query: No relations or attributes in the query $s found");
160 $pagelist = new PageList($args['info'], $args['exclude'], $args);
162 $pagelist->setCaption
163 (HTML($noform ? '' : HTML($form,HTML::hr()),
164 fmt("Semantic %s Search Result for \"%s\" in pages \"%s\"",
167 if (!$regex and $missing = array_diff($parsed_relations, $allrelations))
169 $relquery = new TextSearchQuery(join(" ",$parsed_relations));
170 if (!$relquery->match(join(" ",$allrelations)))
172 $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
173 // if we have only numeric or text ops we can optimize.
174 //$parsed_attr_ops = $this->detectAttrOps($args['s']);
177 $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
178 $links = $dbi->linkSearch($pagequery, $linkquery, 'relation', $relquery);
179 $pagelist->_links = array();
180 while ($link = $links->next()) {
181 $pagelist->addPage($link['pagename']);
182 $pagelist->_links[] = $link;
184 $pagelist->addColumnObject
185 (new _PageList_Column_SemanticSearch_relation('relation', _("Relation"), $pagelist));
186 $pagelist->addColumnObject
187 (new _PageList_Column_SemanticSearch_link('link', _("Link"), $pagelist));
192 // ... (for _variable subquery) ...
193 function bindSubquery($query) {
196 // is_a::city* and (population < 1.000.000 or population > 10.000.000)
197 // => is_a population
198 // Do we support wildcards in relation names also? is_*::city
199 function detectRelationsAndAttributes($subquery) {
200 $relations = array();
201 // relations are easy
202 //$reltoken = preg_grep("/::/", preg_split("/\s+/", $query));
203 //$relations = array_map(create_function('$a','list($f,$b)=split("::",$a); return $f'),
205 foreach (preg_split("/\s+/", $query) as $whitetok) {
206 if (preg_match("/^([\w\*\?]+)::/", $whitetok))
207 $relations[] = $m[1];
210 // for attributes we might use the tokenizer. All non-numerics excl. units and non-ops
218 // c-hanging-comment-ender-p: nil
219 // indent-tabs-mode: nil