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 along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 require_once 'lib/TextSearchQuery.php';
24 require_once 'lib/PageList.php';
27 * Similar to SemanticSearch, just for ordinary in- or outlinks.
29 * @author: Reini Urban
31 class WikiPlugin_LinkSearch
34 function getDescription()
36 return _("Search page and link names.");
39 function getDefaultArguments()
43 PageList::supportedArgs(), // paging and more.
45 's' => "", // linkvalue query string
46 'page' => "*", // which pages (glob allowed), default: all
47 'direction' => "out", // or in
48 'case_exact' => false,
50 'noform' => false, // don't show form with results.
51 'noheader' => false // no caption
55 function showForm(&$dbi, &$request, $args)
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'
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'
78 $dirsign_switch = JavaScript("
79 function dirsign_switch() {
80 var d = document.getElementById('dirsign')
81 d.innerHTML = (d.innerHTML == ' => ') ? ' <= ' : ' => '
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';
91 if ($args['direction'] == 'in') {
92 $in['checked'] = 'checked';
95 $direction = HTML(HTML::input($out), HTML::label(array('for' => 'dir_out'), _("outgoing")),
96 HTML::input($in), HTML::label(array('for' => 'dir_in'), _("incoming")));
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';
107 $direction->pushContent(HTML::option($out, _("outgoing")));
108 $direction->pushContent(HTML::option($in, _("incoming")));
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,
114 'accept-charset' => 'UTF-8'),
117 $instructions, HTML::br(),
119 HTML::strong(HTML::tt(array('id' => 'dirsign'), $dirsign)),
121 HTML::raw(' '), $direction,
122 HTML::raw(' '), $submit);
126 function run($dbi, $argstr, &$request, $basepage)
128 $args = $this->getArgs($argstr, $request);
130 if (empty($args['page']))
132 $form = $this->showForm($dbi, $request, $args);
136 $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
137 $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
138 $links = $dbi->linkSearch($pagequery, $linkquery, $direction == 'in' ? 'linkfrom' : 'linkto');
139 $pagelist = new PageList($args['info'], $args['exclude'], $args);
140 $pagelist->_links = array();
141 while ($link = $links->next()) {
142 $pagelist->addPage($link['pagename']);
143 $pagelist->_links[] = $link;
145 $pagelist->addColumnObject
146 (new _PageList_Column_LinkSearch_link('link', _("Link"), $pagelist));
149 // We put the form into the caption just to be able to return one pagelist object,
150 // and to still have the convenience form at the top. we could workaround this by
151 // putting the form as WikiFormRich into the actionpage. but thid doesnt look as
152 // nice as this here.
153 $pagelist->setCaption
154 ( // on mozilla the form doesn't fit into the caption very well.
155 HTML($noform ? '' : HTML($form, HTML::hr()),
156 fmt("LinkSearch result for \"%s\" in pages \"%s\", direction %s", $s, $page, $direction)));
162 // FIXME: sortby errors with this column
163 class _PageList_Column_LinkSearch_link
164 extends _PageList_Column
166 function _PageList_Column_LinkSearch_link($field, $heading, &$pagelist)
168 $this->_field = $field;
169 $this->_heading = $heading;
170 $this->_need_rev = false;
171 $this->_iscustom = true;
172 $this->_pagelist =& $pagelist;
175 function _getValue(&$page, $revision_handle)
177 if (is_object($page)) $text = $page->getName();
179 $link = $this->_pagelist->_links[$this->current_row];
180 return WikiLink($link['linkvalue'], 'if_known');
188 // c-hanging-comment-ender-p: nil
189 // indent-tabs-mode: nil