]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LinkSearch.php
getName should not translate
[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::tt(array('id' => 'dirsign'), $dirsign)),
120             $query,
121             HTML::raw('&nbsp;'), $direction,
122             HTML::raw('&nbsp;'), $submit);
123         return $form;
124     }
125
126     function run($dbi, $argstr, &$request, $basepage)
127     {
128         $args = $this->getArgs($argstr, $request);
129
130         if (empty($args['page']))
131             $args['page'] = "*";
132         $form = $this->showForm($dbi, $request, $args);
133         extract($args);
134         if (empty($s))
135             return $form;
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;
144         }
145         $pagelist->addColumnObject
146         (new _PageList_Column_LinkSearch_link('link', _("Link"), $pagelist));
147
148         if (!$noheader) {
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)));
157         }
158         return $pagelist;
159     }
160 }
161
162 // FIXME: sortby errors with this column
163 class _PageList_Column_LinkSearch_link
164     extends _PageList_Column
165 {
166     function _PageList_Column_LinkSearch_link($field, $heading, &$pagelist)
167     {
168         $this->_field = $field;
169         $this->_heading = $heading;
170         $this->_need_rev = false;
171         $this->_iscustom = true;
172         $this->_pagelist =& $pagelist;
173     }
174
175     function _getValue(&$page, $revision_handle)
176     {
177         if (is_object($page)) $text = $page->getName();
178         else $text = $page;
179         $link = $this->_pagelist->_links[$this->current_row];
180         return WikiLink($link['linkvalue'], 'if_known');
181     }
182 }
183
184 // Local Variables:
185 // mode: php
186 // tab-width: 8
187 // c-basic-offset: 4
188 // c-hanging-comment-ender-p: nil
189 // indent-tabs-mode: nil
190 // End: