2 rcs_id('$Id: SemanticSearchAdvanced.php,v 1.1 2007-05-24 18:40:43 rurban Exp $');
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.
30 * Are multiple variables valid for one page only, or is the result
31 * constructed as list of all matches? We'll stick with one page for now.
32 * This the only way I can see semantic meaning for now.
34 * Queries: "is_a::city and (population < 1.000.000 or population > 10.000.000)"
35 * "(is_a::city or is_a::country) and population < 10.000.000"
37 * Relation links may contain wildcards. For relation and attribute names I'm not sure yet.
39 * @author: Reini Urban
42 class WikiPlugin_SemanticSearchAdvanced
43 extends WikiPlugin_SemanticSearch
46 return _("SemanticSearchAdvanced");
48 function getDescription() {
49 return _("Parse and execute a full query expression");
51 function getVersion() {
52 return preg_replace("/[Revision: $]/", '',
55 function getDefaultArguments() {
58 PageList::supportedArgs(), // paging and more.
60 's' => "", // query expression
61 'page' => "*", // which pages (glob allowed), default: all
62 'case_exact' => false,
63 'regex' => 'auto', // hmm
64 'noform' => false, // don't show form with results.
65 'noheader' => false // no caption
69 function showForm (&$dbi, &$request, $args, $allrelations) {
71 $action = $request->getPostURL();
72 $hiddenfield = HiddenInputs($request->getArgs(),'',
73 array('action','page','s'));
74 $pagefilter = HTML::input(array('name' => 'page',
75 'value' => $args['page'],
76 'title' => _("Search only in these pages. With autocompletion."),
77 'class' => 'dropdown',
78 'acdropdown' => 'true',
79 'autocomplete_complete' => 'true',
80 'autocomplete_matchsubstring' => 'false',
81 'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
83 $help = Button('submit:semsearch[help]', "?", false);
84 $svalues = empty($allrelations) ? "" : join("','", $allrelations);
85 $reldef = JavaScript("var semsearch_relations = new Array('".$svalues."')");
86 $querybox = HTML::textarea(array('name' => 's',
87 'title' => _("Enter a valid query expression"),
89 'acdropdown' => 'true',
90 'autocomplete_complete' => 'true',
91 'autocomplete_assoc' => 'false',
92 'autocomplete_matchsubstring' => 'true',
93 'autocomplete_list' => 'array:semsearch_relations'
95 $submit = Button('submit:semsearch[relations]', _("Search"), false,
96 array('title' => 'Move to help page. No seperate window'));
97 $instructions = _("Search in all specified pages for the expression.");
98 $form = HTML::form(array('action' => $action,
100 'accept-charset' => $GLOBALS['charset']),
102 $hiddenfield, HiddenInputs(array('attribute'=>'')),
103 $instructions, HTML::br(),
104 HTML::table(array('border'=>'0','width' =>'100%'),
105 HTML::tr(HTML::td(_("Pagename(s): "), $pagefilter),
106 HTML::td(array('align' => 'right'),
108 HTML::tr(HTML::td(array('colspan' => 2),
111 HTML::div(array('align'=>'center'),$submit));
115 function run ($dbi, $argstr, &$request, $basepage) {
118 $this->_supported_operators = array(':=','<','<=','>','>=','!=','==','=~');
119 $args = $this->getArgs($argstr, $request);
120 $posted = $request->getArg('semsearch');
121 $request->setArg('semsearch', false);
122 if ($request->isPost() and isset($posted['help'])) {
123 $request->redirect(WikiURL(_("Help/SemanticSearchAdvancedPlugin"),
124 array('redirectfrom' => $basepage), true));
126 $allrelations = $dbi->listRelations();
127 $form = $this->showForm($dbi, $request, $args, $allrelations);
128 if (isset($this->_norelations_warning))
129 $form->pushContent(HTML::div(array('class' => 'warning'),
130 _("Warning:").$this->_norelations_warning));
132 // For convenience, peace and harmony we allow GET requests also.
133 if (!$args['s']) // check for good GET request
134 return $form; // nobody called us, so just display our form
136 // In reality we have to iterate over all found pages.
137 // To makes things shorter extract the next AND required expr and
138 // iterate only over this, then recurse into the next AND expr.
139 // => Split into an AND and OR expression tree.
141 $parsed_relations = $this->detectRelationsAndAttributes($args['s']);
143 if ($parsed_relations)
144 $regex = preg_grep("/[\*\?]/", $parsed_relations);
145 // Check that all those do exist.
147 $this->error("Invalid query: No relations or attributes in the query $s found");
148 $pagelist = new PageList($args['info'], $args['exclude'], $args);
150 $pagelist->setCaption
151 (HTML($noform ? '' : HTML($form,HTML::hr()),
152 fmt("Semantic %s Search Result for \"%s\" in pages \"%s\"",
155 if (!$regex and $missing = array_diff($parsed_relations, $allrelations))
157 $relquery = new TextSearchQuery(join(" ",$parsed_relations));
158 if (!$relquery->match(join(" ",$allrelations)))
160 $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
161 // if we have only numeric or text ops we can optimize.
162 //$parsed_attr_ops = $this->detectAttrOps($args['s']);
165 $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
166 $links = $dbi->linkSearch($pagequery, $linkquery, 'relation', $relquery);
167 $pagelist->_links = array();
168 while ($link = $links->next()) {
169 $pagelist->addPage($link['pagename']);
170 $pagelist->_links[] = $link;
172 $pagelist->addColumnObject
173 (new _PageList_Column_SemanticSearch_relation('relation', _("Relation"), $pagelist));
174 $pagelist->addColumnObject
175 (new _PageList_Column_SemanticSearch_link('link', _("Link"), $pagelist));
180 // is_a::city* and (population < 1.000.000 or population > 10.000.000)
181 // => is_a population
182 // Do we support wildcards in relation names also? is_*::city
183 function detectRelationsAndAttributes($query) {
184 $relations = array();
185 // relations are easy
186 //$reltoken = preg_grep("/::/", preg_split("/\s+/", $query));
187 //$relations = array_map(create_function('$a','list($f,$b)=split("::",$a); return $f'),
189 foreach (preg_split("/\s+/", $query) as $whitetok) {
190 if (preg_match("/^([\w\*\?]+)::/", $whitetok))
191 $relations[] = $m[1];
194 // for attributes we might use the tokenizer. All non-numerics excl. units and non-ops
198 // $Log: not supported by cvs2svn $
204 // c-hanging-comment-ender-p: nil
205 // indent-tabs-mode: nil