2 rcs_id('$Id: SemanticSearch.php,v 1.2 2007-01-02 13:23:06 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/TextSearchQuery.php');
24 require_once('lib/PageList.php');
27 * Search for relations/attributes and its values.
28 * page - relation::object. e.g list all cities: is_a::city => relation=is_a&s=city
30 * An attribute has just a value, which is a number, and which is for sure no pagename,
31 * and its value goes through some units unification. (not yet)
32 * We can also do numerical comparison (e.g. range searching) with attributes.
33 * population>1000000 (not yet)
35 * A more generic <ask> feature will use multiple comparison and nesting.
36 * <ask is_a::city and population > 1.000.000 and population < 10.000.000>
37 * <ask (is_a::city or is_a::country) and population < 10.000.000>
39 * @author: Reini Urban
41 class WikiPlugin_SemanticSearch
45 return _("SemanticSearch");
47 function getDescription() {
48 return _("Search relations and attributes");
50 function getVersion() {
51 return preg_replace("/[Revision: $]/", '',
54 function getDefaultArguments() {
57 PageList::supportedArgs(), // paging and more.
59 's' => "*", // linkvalue query string
60 'page' => "*", // which pages (glob allowed), default: all
61 'relation' => '', // linkname. which relations. default all
62 'attribute' => '', // linkname. which attributes. default all
63 'attr_op' => ':=', // a funny written way for equality for pure aesthetic pleasure
64 // "All attributes which have this value set"
66 'case_exact' => false,
68 'noform' => false, // don't show form with results.
69 'noheader' => false // no caption
73 function showForm (&$dbi, &$request, $args) {
75 $action = $request->getPostURL();
76 $hiddenfield = HiddenInputs($request->getArgs(),'',
77 array('action','page','s','relation','attribute'));
78 $pagefilter = HTML::input(array('name' => 'page',
79 'value' => $args['page'],
80 'title' => _("Search only in these pages. With autocompletion."),
81 'class' => 'dropdown',
82 'acdropdown' => 'true',
83 'autocomplete_complete' => 'true',
84 'autocomplete_matchsubstring' => 'false',
85 'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
87 $allrelations = $dbi->listRelations(false,false,true);
88 $svalues = empty($allrelations) ? "" : join("','", $allrelations);
89 $reldef = JavaScript("var semsearch_relations = new Array('".$svalues."')");
90 $relation = HTML::input(array('name' => 'relation',
91 'value' => $args['relation'],
92 'title' => _("Filter by this relation. With autocompletion."),
93 'class' => 'dropdown',
94 'style' => 'width:10em',
95 'acdropdown' => 'true',
96 'autocomplete_assoc' => 'false',
97 'autocomplete_complete' => 'true',
98 'autocomplete_matchsubstring' => 'true',
99 'autocomplete_list' => 'array:semsearch_relations'
101 $queryrel = HTML::input(array('name' => 's',
102 'value' => $args['s'],
103 'title' => _("Filter by this link. These are pagenames. With autocompletion."),
104 'class' => 'dropdown',
105 'acdropdown' => 'true',
106 'autocomplete_complete' => 'true',
107 'autocomplete_matchsubstring' => 'true',
108 'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
110 $relsubmit = Button('submit:semsearch[relations]', _("Relations"), false);
111 // just testing some dhtml... not yet done
112 $enhancements = HTML();
113 $nbsp = HTML::raw(' ');
114 $this_uri = $_SERVER['REQUEST_URI'].'#';
115 $andbutton = new Button(_("AND"),$this_uri,'wikiaction',
117 'onclick' => "addquery('rel', 'and')",
118 'title' => _("Add an AND query")));
119 $orbutton = new Button(_("OR"),$this_uri,'wikiaction',
121 'onclick' => "addquery('rel', 'or')",
122 'title' => _("Add an OR query")));
124 $enhancements = HTML::span($andbutton, $nbsp, $orbutton);
125 $instructions = _("Search in pages for a relation with that value, which is a pagename.");
126 $form1 = HTML::form(array('action' => $action,
128 'accept-charset' => $GLOBALS['charset']),
130 $hiddenfield, HiddenInputs(array('attribute'=>'')),
131 $instructions, HTML::br(),
133 (array('border' => 0,'cellspacing' => 2),
134 HTML::colgroup(array('span' => 6)),
136 (HTML::th(''),HTML::th('Pagefilter'),HTML::th('Relation'),
137 HTML::th(''),HTML::th(array('span' => 2),'Links')),
140 HTML::td($nbsp,$nbsp,$nbsp),
141 HTML::td($pagefilter, ": "),
143 HTML::td(HTML::strong(HTML::tt(' :: '))),
145 HTML::td($nbsp, $relsubmit,
146 $nbsp, $enhancements)))));
148 $allattrs = $dbi->listRelations(false,true,true);
149 if (empty($allrelations) and empty($allattrs)) // be nice to the dummy.
150 $this->_norelations_warning = _("No relations nor attributes in the whole wikidb defined!");
151 $svalues = empty($allattrs) ? "" : join("','", $allattrs);
152 $attdef = JavaScript("var semsearch_attributes = new Array('".$svalues."')\n"
153 ."var semsearch_op = new Array('"
154 .join("','", $this->_supported_operators)
156 $attribute = HTML::input(array('name' => 'attribute',
157 'value' => $args['attribute'],
158 'title' => _("Filter by this attribute name. With autocompletion."),
159 'class' => 'dropdown',
160 'style' => 'width:10em',
161 'acdropdown' => 'true',
162 'autocomplete_complete' => 'true',
163 'autocomplete_matchsubstring' => 'true',
164 'autocomplete_assoc' => 'false',
165 'autocomplete_list' => 'array:semsearch_attributes'
167 $attr_op = HTML::input(array('name' => 'attr_op',
168 'value' => $args['attr_op'],
169 'title' => _("Logical operator. With autocompletion."),
170 'class' => 'dropdown',
171 'style' => 'width:2em',
172 'acdropdown' => 'true',
173 'autocomplete_complete' => 'true',
174 'autocomplete_matchsubstring' => 'true',
175 'autocomplete_assoc' => 'false',
176 'autocomplete_list' => 'array:semsearch_op'
178 $queryatt = HTML::input(array('name' => 's',
179 'value' => $args['s'],
180 'title' => _("Filter by this numeric attribute value. With autocompletion."), //?
181 'class' => 'dropdown',
182 'acdropdown' => 'false',
183 'autocomplete_complete' => 'true',
184 'autocomplete_matchsubstring' => 'false',
185 'autocomplete_assoc' => 'false',
186 'autocomplete_list' => 'plugin:SemanticSearch page='.$args['page'].' attribute=^[S]'
188 $andbutton = new Button(_("AND"),$this_uri,'wikiaction',
190 'onclick' => "addquery('attr', 'and')",
191 'title' => _("Add an AND query")));
192 $orbutton = new Button(_("OR"),$this_uri,'wikiaction',
194 'onclick' => "addquery('attr', 'or')",
195 'title' => _("Add an OR query")));
197 $enhancements = HTML::span($andbutton, $nbsp, $orbutton);
198 $attsubmit = Button('submit:semsearch[attributes]', _("Attributes"), false);
199 $instructions = HTML::span(_("Search in pages for an attribute with that numeric value."),"\n");
201 $instructions->pushContent
202 (HTML(" ", new Button(_("Advanced..."),$this_uri)));
203 $form2 = HTML::form(array('action' => $action,
205 'accept-charset' => $GLOBALS['charset']),
207 $hiddenfield, HiddenInputs(array('relation'=>'')),
208 $instructions, HTML::br(),
210 (array('border' => 0,'cellspacing' => 2),
211 HTML::colgroup(array('span' => 6)),
213 (HTML::th(''),HTML::th('Pagefilter'),HTML::th('Attribute'),
214 HTML::th('Op'),HTML::th(array('span' => 2),'Value')),
217 HTML::td($nbsp,$nbsp,$nbsp),
218 HTML::td($pagefilter, ": "),
219 HTML::td($attribute),
222 HTML::td($nbsp, $attsubmit,
223 $nbsp, $enhancements)))));
225 return HTML($form1, $form2);
228 function run ($dbi, $argstr, &$request, $basepage) {
231 $this->_supported_operators = array(':=','<','<=','>','>=','!=','==','=~');
232 $args = $this->getArgs($argstr, $request);
233 if (empty($args['page']))
235 if (!isset($args['s'])) // it might be (integer) 0
237 $form = $this->showForm($dbi, $request, $args);
238 if (isset($this->_norelations_warning))
239 $form->pushContent(HTML::div(array('class' => 'warning'),
240 _("Warning:").$this->_norelations_warning));
242 // for convenience and harmony we allow GET requests also.
243 if (!$request->isPost()) {
244 if ($relation or $attribute) // check for good GET request
247 return $form; // nobody called us, so just display our supadupa form
249 $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
250 // we might want to check for semsearch['relations'] and semsearch['attributes'] also
251 if (empty($relation) and empty($attribute)) {
252 // so we just clicked without selecting any relation.
253 // hmm. check which button we clicked, before we do the massive alltogether search.
254 $posted = $request->getArg("semsearch");
255 if (isset($semsearch['relations']))
257 elseif (isset($posted['attributes']))
260 $searchtype = "Text";
261 if (!empty($relation)) {
262 $querydesc = $relation."::".$s;
263 $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
264 $relquery = new TextSearchQuery($relation, $args['case_exact'], $args['regex']);
265 $links = $dbi->linkSearch($pagequery, $linkquery, 'relation', $relquery);
266 $pagelist = new PageList($args['info'], $args['exclude'], $args);
267 $pagelist->_links = array();
268 while ($link = $links->next()) {
269 $pagelist->addPage($link['pagename']);
270 $pagelist->_links[] = $link;
272 $pagelist->addColumnObject
273 (new _PageList_Column_SemanticSearch_relation('relation', _("Relation"), $pagelist));
274 $pagelist->addColumnObject
275 (new _PageList_Column_SemanticSearch_link('link', _("Link"), $pagelist));
277 // can we merge two different pagelist?
278 if (!empty($attribute)) {
279 $relquery = new TextSearchQuery($attribute, $args['case_exact'], $args['regex']);
280 if (!in_array($attr_op, $this->_supported_operators)) {
281 return HTML($form,$this->error(fmt("Illegal operator: %s",
282 HTML::tt($attr_op))));
284 // TODO: support unit suffixes
285 if (preg_match('/^\d+$/', $s)) { // do comparison only with numbers
286 //include_once("lib/SemanticWeb.php");
287 // TODO: Unify units to some format before comparison.
288 /* Sooner or later we want logical expressions also:
289 * population < 1million AND area > 50km2
290 * Here we check only for one attribute per page.
292 // it might not be the best idea to use '*' as variable to expand
293 if ($attribute == '*') $attribute = '_x';
294 $querydesc = $attribute." ".$attr_op." ".$s;
295 $searchtype = "Numeric";
296 $linkquery = new NumericSearchQuery($querydesc, $attribute);
297 if ($attribute == '_x') $attribute = '*';
298 $querydesc = $attribute." ".$attr_op." ".$s;
299 // text matcher or '*' MATCH_ALL
300 } elseif (in_array($attr_op, array(':=','==','=~'))) {
301 if ($attr_op == '=~') {
302 if ($s == '*') $s = '.*'; // help the poor user. we need pcre syntax.
303 $linkquery = new TextSearchQuery("$s", $args['case_exact'], 'pcre');
306 $linkquery = new TextSearchQuery("$s", $args['case_exact'], $args['regex']);
307 $querydesc = "$attribute $attr_op $s";
309 $querydesc = $attribute." ".$attr_op." ".$s;
310 return HTML($form, $this->error(fmt("Only text operators can used with strings: %s",
311 HTML::tt($querydesc))));
313 $links = $dbi->linkSearch($pagequery, $linkquery, 'attribute', $relquery);
314 if (empty($relation)) {
315 $pagelist = new PageList($args['info'], $args['exclude'], $args);
316 $pagelist->_links = array();
318 while ($link = $links->next()) {
319 $pagelist->addPage($link['pagename']);
320 $pagelist->_links[] = $link;
322 $pagelist->addColumnObject
323 (new _PageList_Column_SemanticSearch_relation('attribute', _("Attribute"), $pagelist));
324 $pagelist->addColumnObject
325 (new _PageList_Column_SemanticSearch_link('value', _("Value"), $pagelist));
327 if (!isset($pagelist)) {
328 $querydesc = _("<empty>");
329 $pagelist = new PageList();
332 // We put the form into the caption just to be able to return one pagelist object,
333 // and to still have the convenience form at the top. we could workaround this by
334 // putting the form as WikiFormRich into the actionpage. but thid doesnt look as
335 // nice as this here.
336 $pagelist->setCaption
337 ( // on mozilla the form doesn't fit into the caption very well.
338 HTML($noform ? '' : HTML($form,HTML::hr()),
339 fmt("Semantic %s Search Result for \"%s\" in pages \"%s\"",$searchtype,$querydesc,$page)));
345 class _PageList_Column_SemanticSearch_relation
346 extends _PageList_Column
348 function _PageList_Column_SemanticSearch_relation ($field, $heading, &$pagelist) {
349 $this->_field = $field;
350 $this->_heading = $heading;
351 $this->_need_rev = false;
352 $this->_iscustom = true;
353 $this->_pagelist =& $pagelist;
355 function _getValue(&$page, $revision_handle) {
356 if (is_object($page)) $text = $page->getName();
358 $link = $this->_pagelist->_links[$this->current_row];
359 return WikiLink($link['linkname'],'if_known');
362 class _PageList_Column_SemanticSearch_link
363 extends _PageList_Column_SemanticSearch_relation
365 function _getValue(&$page, $revision_handle) {
366 if (is_object($page)) $text = $page->getName();
368 $link = $this->_pagelist->_links[$this->current_row];
369 if ($this->_field != 'value')
370 return WikiLink($link['linkvalue'],'if_known');
372 return $link['linkvalue'];
376 // $Log: not supported by cvs2svn $
377 // Revision 1.1 2006/03/07 20:52:01 rurban
378 // not yet working good enough
385 // c-hanging-comment-ender-p: nil
386 // indent-tabs-mode: nil