]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SemanticSearchAdvanced.php
Generate valid XHTML code
[SourceForge/phpwiki.git] / lib / plugin / SemanticSearchAdvanced.php
1 <?php // -*-php-*-
2 rcs_id('$Id: SemanticSearchAdvanced.php,v 1.1 2007-05-24 18:40:43 rurban Exp $');
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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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  *
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.
33  *
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"
36  *
37  * Relation links may contain wildcards. For relation and attribute names I'm not sure yet. 
38  *
39  * @author: Reini Urban
40  */
41
42 class WikiPlugin_SemanticSearchAdvanced
43 extends WikiPlugin_SemanticSearch
44 {
45     function getName() {
46         return _("SemanticSearchAdvanced");
47     }
48     function getDescription() {
49         return _("Parse and execute a full query expression");
50     }
51     function getVersion() {
52         return preg_replace("/[Revision: $]/", '',
53                             "\$Revision: 1.1 $");
54     }
55     function getDefaultArguments() { 
56         return array_merge
57             (
58              PageList::supportedArgs(),  // paging and more.
59              array(
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
66                    ));
67     }
68
69     function showForm (&$dbi, &$request, $args, $allrelations) {
70         global $WikiTheme;
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'
82                                         ), '');
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"),
88                                          'rows' => 4,
89                                          'acdropdown' => 'true', 
90                                          'autocomplete_complete' => 'true', 
91                                          'autocomplete_assoc' => 'false', 
92                                          'autocomplete_matchsubstring' => 'true', 
93                                          'autocomplete_list' => 'array:semsearch_relations'
94                                       ), $args['s']);
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,
99                                   'method' => 'post',
100                                   'accept-charset' => $GLOBALS['charset']),
101                            $reldef,
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'), 
107                                                          $help)),
108                                        HTML::tr(HTML::td(array('colspan' => 2), 
109                                                          $querybox))), 
110                            HTML::br(),
111                            HTML::div(array('align'=>'center'),$submit));
112         return $form;
113     }
114  
115     function run ($dbi, $argstr, &$request, $basepage) { 
116         global $WikiTheme;
117
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));
125         }
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));
131         extract($args);
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
135
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.
140
141         $parsed_relations = $this->detectRelationsAndAttributes($args['s']);
142         $regex = '';
143         if ($parsed_relations)
144             $regex = preg_grep("/[\*\?]/", $parsed_relations);
145         // Check that all those do exist.
146         else
147             $this->error("Invalid query: No relations or attributes in the query $s found");
148         $pagelist = new PageList($args['info'], $args['exclude'], $args);
149         if (!$noheader) {
150             $pagelist->setCaption
151                 (HTML($noform ? '' : HTML($form,HTML::hr()),
152                       fmt("Semantic %s Search Result for \"%s\" in pages \"%s\"",
153                           '',$s,$page)));
154         }
155         if (!$regex and $missing = array_diff($parsed_relations, $allrelations))
156             return $pagelist;
157         $relquery = new TextSearchQuery(join(" ",$parsed_relations));
158         if (!$relquery->match(join(" ",$allrelations)))
159             return $pagelist;
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']);
163
164         //TODO: writeme
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;
171         }
172         $pagelist->addColumnObject
173             (new _PageList_Column_SemanticSearch_relation('relation', _("Relation"), $pagelist));
174         $pagelist->addColumnObject
175             (new _PageList_Column_SemanticSearch_link('link', _("Link"), $pagelist));
176
177         return $pagelist;
178     }
179
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'), 
188         //                     $reltoken);
189         foreach (preg_split("/\s+/", $query) as $whitetok) {
190             if (preg_match("/^([\w\*\?]+)::/", $whitetok))
191                 $relations[] = $m[1];
192         }
193         return $relations;
194         // for attributes we might use the tokenizer. All non-numerics excl. units and non-ops
195     }
196 };
197
198 // $Log: not supported by cvs2svn $
199
200 // Local Variables:
201 // mode: php
202 // tab-width: 8
203 // c-basic-offset: 4
204 // c-hanging-comment-ender-p: nil
205 // indent-tabs-mode: nil
206 // End:
207 ?>