]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LinkSearch.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / LinkSearch.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/TextSearchQuery.php';
24 require_once 'lib/PageList.php';
25
26 /**
27  * Similar to SemanticSearch, just for ordinary in- or outlinks.
28  *
29  * @author: Reini Urban
30  */
31 class WikiPlugin_LinkSearch
32     extends WikiPlugin
33 {
34     function getDescription()
35     {
36         return _("Search page and link names.");
37     }
38
39     function getDefaultArguments()
40     {
41         return array_merge
42         (
43             PageList::supportedArgs(), // paging and more.
44             array(
45                 's' => "", // linkvalue query string
46                 'page' => "*", // which pages (glob allowed), default: all
47                 'direction' => "out", // or in
48                 'case_exact' => false,
49                 'regex' => 'auto',
50                 'noform' => false, // don't show form with results.
51                 'noheader' => false // no caption
52             ));
53     }
54
55     function showForm(&$dbi, &$request, $args)
56     {
57         $action = $request->getPostURL();
58         $hiddenfield = HiddenInputs($request->getArgs(), '',
59             array('action', 'page', 's', 'direction'));
60         $pagefilter = HTML::input(array('name' => 'page',
61             'value' => $args['page'],
62             'title' => _("Search only in these pages. With autocompletion."),
63             'class' => 'dropdown',
64             'acdropdown' => 'true',
65             'autocomplete_complete' => 'true',
66             'autocomplete_matchsubstring' => 'false',
67             'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
68         ), '');
69         $query = HTML::input(array('name' => 's',
70             'value' => $args['s'],
71             'title' => _("Filter by this link. These are pagenames. With autocompletion."),
72             'class' => 'dropdown',
73             'acdropdown' => 'true',
74             'autocomplete_complete' => 'true',
75             'autocomplete_matchsubstring' => 'true',
76             'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
77         ), '');
78         $dirsign_switch = JavaScript("
79 function dirsign_switch() {
80   var d = document.getElementById('dirsign')
81   d.innerHTML = (d.innerHTML == ' =&gt; ') ? ' &lt;= ' : ' =&gt; '
82 }
83 ");
84         $dirsign = " => ";
85         $in = $out = array('name' => 'direction', 'type' => 'radio', 'onChange' => 'dirsign_switch()');
86         $out['value'] = 'out';
87         $out['id'] = 'dir_out';
88         if ($args['direction'] == 'out') $out['checked'] = 'checked';
89         $in['value'] = 'in';
90         $in['id'] = 'dir_in';
91         if ($args['direction'] == 'in') {
92             $in['checked'] = 'checked';
93             $dirsign = " <= ";
94         }
95         $direction = HTML(HTML::input($out), HTML::label(array('for' => 'dir_out'), _("outgoing")),
96             HTML::input($in), HTML::label(array('for' => 'dir_in'), _("incoming")));
97         /*
98         $direction = HTML::select(array('name'=>'direction',
99                                         'onChange' => 'dirsign_switch()'));
100         $out = array('value' => 'out');
101         if ($args['direction']=='out') $out['selected'] = 'selected';
102         $in = array('value' => 'in');
103         if ($args['direction']=='in') {
104             $in['selected'] = 'selected';
105             $dirsign = " <= ";
106         }
107         $direction->pushContent(HTML::option($out, _("outgoing")));
108         $direction->pushContent(HTML::option($in, _("incoming")));
109         */
110         $submit = Button('submit:search', _("LinkSearch"), false);
111         $instructions = _("Search in pages for links with the matching name.");
112         $form = HTML::form(array('action' => $action,
113                 'method' => 'GET',
114                 'accept-charset' => 'UTF-8'),
115             $dirsign_switch,
116             $hiddenfield,
117             $instructions, HTML::br(),
118             $pagefilter,
119             HTML::strong(HTML::samp(array('id' => 'dirsign'), $dirsign)),
120             $query,
121             HTML::raw('&nbsp;'), $direction,
122             HTML::raw('&nbsp;'), $submit);
123         return $form;
124     }
125
126     /**
127      * @param WikiDB $dbi
128      * @param string $argstr
129      * @param WikiRequest $request
130      * @param string $basepage
131      * @return mixed
132      */
133     function run($dbi, $argstr, &$request, $basepage)
134     {
135         $args = $this->getArgs($argstr, $request);
136
137         if (empty($args['page']))
138             $args['page'] = "*";
139         $form = $this->showForm($dbi, $request, $args);
140         extract($args);
141         if (empty($s))
142             return $form;
143         $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
144         $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
145         $links = $dbi->linkSearch($pagequery, $linkquery, $direction == 'in' ? 'linkfrom' : 'linkto');
146         $pagelist = new PageList($args['info'], $args['exclude'], $args);
147         $pagelist->_links = array();
148         while ($link = $links->next()) {
149             $pagelist->addPage($link['pagename']);
150             $pagelist->_links[] = $link;
151         }
152         $pagelist->addColumnObject
153         (new _PageList_Column_LinkSearch_link('link', _("Link"), $pagelist));
154
155         if (!$noheader) {
156             // We put the form into the caption just to be able to return one pagelist object,
157             // and to still have the convenience form at the top. we could workaround this by
158             // putting the form as WikiFormRich into the actionpage. but thid doesnt look as
159             // nice as this here.
160             $pagelist->setCaption
161             ( // on mozilla the form doesn't fit into the caption very well.
162                 HTML($noform ? '' : HTML($form, HTML::hr()),
163                     fmt("LinkSearch result for \"%s\" in pages \"%s\", direction %s", $s, $page, $direction)));
164         }
165         return $pagelist;
166     }
167 }
168
169 // FIXME: sortby errors with this column
170 class _PageList_Column_LinkSearch_link
171     extends _PageList_Column
172 {
173     function _PageList_Column_LinkSearch_link($field, $heading, &$pagelist)
174     {
175         $this->_field = $field;
176         $this->_heading = $heading;
177         $this->_need_rev = false;
178         $this->_iscustom = true;
179         $this->_pagelist =& $pagelist;
180     }
181
182     function _getValue(&$page, $revision_handle)
183     {
184         if (is_object($page)) $text = $page->getName();
185         else $text = $page;
186         $link = $this->_pagelist->_links[$this->current_row];
187         return WikiLink($link['linkvalue'], 'if_known');
188     }
189 }
190
191 // Local Variables:
192 // mode: php
193 // tab-width: 8
194 // c-basic-offset: 4
195 // c-hanging-comment-ender-p: nil
196 // indent-tabs-mode: nil
197 // End: