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
36 return _("LinkSearch");
39 function getDescription()
41 return _("Search page and link names.");
44 function getDefaultArguments()
48 PageList::supportedArgs(), // paging and more.
50 's' => "", // linkvalue query string
51 'page' => "*", // which pages (glob allowed), default: all
52 'direction' => "out", // or in
53 'case_exact' => false,
55 'noform' => false, // don't show form with results.
56 'noheader' => false // no caption
60 function showForm(&$dbi, &$request, $args)
62 $action = $request->getPostURL();
63 $hiddenfield = HiddenInputs($request->getArgs(), '',
64 array('action', 'page', 's', 'direction'));
65 $pagefilter = HTML::input(array('name' => 'page',
66 'value' => $args['page'],
67 'title' => _("Search only in these pages. With autocompletion."),
68 'class' => 'dropdown',
69 'acdropdown' => 'true',
70 'autocomplete_complete' => 'true',
71 'autocomplete_matchsubstring' => 'false',
72 'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
74 $query = HTML::input(array('name' => 's',
75 'value' => $args['s'],
76 'title' => _("Filter by this link. These are pagenames. With autocompletion."),
77 'class' => 'dropdown',
78 'acdropdown' => 'true',
79 'autocomplete_complete' => 'true',
80 'autocomplete_matchsubstring' => 'true',
81 'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
83 $dirsign_switch = JavaScript("
84 function dirsign_switch() {
85 var d = document.getElementById('dirsign')
86 d.innerHTML = (d.innerHTML == ' => ') ? ' <= ' : ' => '
90 $in = $out = array('name' => 'direction', 'type' => 'radio', 'onChange' => 'dirsign_switch()');
91 $out['value'] = 'out';
92 $out['id'] = 'dir_out';
93 if ($args['direction'] == 'out') $out['checked'] = 'checked';
96 if ($args['direction'] == 'in') {
97 $in['checked'] = 'checked';
100 $direction = HTML(HTML::input($out), HTML::label(array('for' => 'dir_out'), _("outgoing")),
101 HTML::input($in), HTML::label(array('for' => 'dir_in'), _("incoming")));
103 $direction = HTML::select(array('name'=>'direction',
104 'onChange' => 'dirsign_switch()'));
105 $out = array('value' => 'out');
106 if ($args['direction']=='out') $out['selected'] = 'selected';
107 $in = array('value' => 'in');
108 if ($args['direction']=='in') {
109 $in['selected'] = 'selected';
112 $direction->pushContent(HTML::option($out, _("outgoing")));
113 $direction->pushContent(HTML::option($in, _("incoming")));
115 $submit = Button('submit:search', _("LinkSearch"), false);
116 $instructions = _("Search in pages for links with the matching name.");
117 $form = HTML::form(array('action' => $action,
119 'accept-charset' => $GLOBALS['charset']),
122 $instructions, HTML::br(),
124 HTML::strong(HTML::tt(array('id' => 'dirsign'), $dirsign)),
126 HTML::raw(' '), $direction,
127 HTML::raw(' '), $submit);
131 function run($dbi, $argstr, &$request, $basepage)
134 $args = $this->getArgs($argstr, $request);
136 if (empty($args['page']))
138 $form = $this->showForm($dbi, $request, $args);
142 $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
143 $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
144 $links = $dbi->linkSearch($pagequery, $linkquery, $direction == 'in' ? 'linkfrom' : 'linkto');
145 $pagelist = new PageList($args['info'], $args['exclude'], $args);
146 $pagelist->_links = array();
147 while ($link = $links->next()) {
148 $pagelist->addPage($link['pagename']);
149 $pagelist->_links[] = $link;
151 $pagelist->addColumnObject
152 (new _PageList_Column_LinkSearch_link('link', _("Link"), $pagelist));
155 // We put the form into the caption just to be able to return one pagelist object,
156 // and to still have the convenience form at the top. we could workaround this by
157 // putting the form as WikiFormRich into the actionpage. but thid doesnt look as
158 // nice as this here.
159 $pagelist->setCaption
160 ( // on mozilla the form doesn't fit into the caption very well.
161 HTML($noform ? '' : HTML($form, HTML::hr()),
162 fmt("LinkSearch result for \"%s\" in pages \"%s\", direction %s", $s, $page, $direction)));
168 // FIXME: sortby errors with this column
169 class _PageList_Column_LinkSearch_link
170 extends _PageList_Column
172 function _PageList_Column_LinkSearch_link($field, $heading, &$pagelist)
174 $this->_field = $field;
175 $this->_heading = $heading;
176 $this->_need_rev = false;
177 $this->_iscustom = true;
178 $this->_pagelist =& $pagelist;
181 function _getValue(&$page, $revision_handle)
183 if (is_object($page)) $text = $page->getName();
185 $link = $this->_pagelist->_links[$this->current_row];
186 return WikiLink($link['linkvalue'], 'if_known');
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil