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 * Similar to SemanticSearch, just for ordinary in- or outlinks.
29 * @author: Reini Urban
31 class WikiPlugin_LinkSearch
35 return _("LinkSearch");
37 function getDescription() {
38 return _("Search page and link names");
40 function getVersion() {
41 return preg_replace("/[Revision: $]/", '',
44 function getDefaultArguments() {
47 PageList::supportedArgs(), // paging and more.
49 's' => "", // linkvalue query string
50 'page' => "*", // which pages (glob allowed), default: all
51 'direction' => "out", // or in
52 'case_exact' => false,
54 'noform' => false, // don't show form with results.
55 'noheader' => false // no caption
59 function showForm (&$dbi, &$request, $args) {
60 $action = $request->getPostURL();
61 $hiddenfield = HiddenInputs($request->getArgs(),'',
62 array('action','page','s','direction'));
63 $pagefilter = HTML::input(array('name' => 'page',
64 'value' => $args['page'],
65 'title' => _("Search only in these pages. With autocompletion."),
66 'class' => 'dropdown',
67 'acdropdown' => 'true',
68 'autocomplete_complete' => 'true',
69 'autocomplete_matchsubstring' => 'false',
70 'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
72 $query = HTML::input(array('name' => 's',
73 'value' => $args['s'],
74 'title' => _("Filter by this link. These are pagenames. With autocompletion."),
75 'class' => 'dropdown',
76 'acdropdown' => 'true',
77 'autocomplete_complete' => 'true',
78 'autocomplete_matchsubstring' => 'true',
79 'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
81 $dirsign_switch = JavaScript("
82 function dirsign_switch() {
83 var d = document.getElementById('dirsign')
84 d.innerHTML = (d.innerHTML == ' => ') ? ' <= ' : ' => '
88 $in = $out = array('name' => 'direction', 'type'=>'radio', 'onChange' => 'dirsign_switch()');
89 $out['value'] = 'out';
90 $out['id'] = 'dir_out';
91 if ($args['direction']=='out') $out['checked'] = 'checked';
94 if ($args['direction']=='in') {
95 $in['checked'] = 'checked';
98 $direction = HTML(HTML::input($out), HTML::label(array('for'=>'dir_out'),_("outgoing")),
99 HTML::input($in), HTML::label(array('for'=>'dir_in'),_("incoming")));
101 $direction = HTML::select(array('name'=>'direction',
102 'onChange' => 'dirsign_switch()'));
103 $out = array('value' => 'out');
104 if ($args['direction']=='out') $out['selected'] = 'selected';
105 $in = array('value' => 'in');
106 if ($args['direction']=='in') {
107 $in['selected'] = 'selected';
110 $direction->pushContent(HTML::option($out, _("outgoing")));
111 $direction->pushContent(HTML::option($in, _("incoming")));
113 $submit = Button('submit:search', _("LinkSearch"), false);
114 $instructions = _("Search in pages for links with the matching name.");
115 $form = HTML::form(array('action' => $action,
117 'accept-charset' => $GLOBALS['charset']),
120 $instructions, HTML::br(),
122 HTML::strong(HTML::tt(array('id'=>'dirsign'), $dirsign)),
124 HTML::raw(' '), $direction,
125 HTML::raw(' '), $submit);
129 function run ($dbi, $argstr, &$request, $basepage) {
131 $args = $this->getArgs($argstr, $request);
133 if (empty($args['page']))
135 $form = $this->showForm($dbi, $request, $args);
139 $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
140 $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
141 $links = $dbi->linkSearch($pagequery, $linkquery, $direction == 'in' ? 'linkfrom' : 'linkto');
142 $pagelist = new PageList($args['info'], $args['exclude'], $args);
143 $pagelist->_links = array();
144 while ($link = $links->next()) {
145 $pagelist->addPage($link['pagename']);
146 $pagelist->_links[] = $link;
148 $pagelist->addColumnObject
149 (new _PageList_Column_LinkSearch_link('link', _("Link"), $pagelist));
152 // We put the form into the caption just to be able to return one pagelist object,
153 // and to still have the convenience form at the top. we could workaround this by
154 // putting the form as WikiFormRich into the actionpage. but thid doesnt look as
155 // nice as this here.
156 $pagelist->setCaption
157 ( // on mozilla the form doesn't fit into the caption very well.
158 HTML($noform ? '' : HTML($form,HTML::hr()),
159 fmt("LinkSearch result for \"%s\" in pages \"%s\", direction %s", $s, $page, $direction)));
165 // FIXME: sortby errors with this column
166 class _PageList_Column_LinkSearch_link
167 extends _PageList_Column
169 function _PageList_Column_LinkSearch_link ($field, $heading, &$pagelist) {
170 $this->_field = $field;
171 $this->_heading = $heading;
172 $this->_need_rev = false;
173 $this->_iscustom = true;
174 $this->_pagelist =& $pagelist;
176 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