]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LinkSearch.php
function _getValue is not private
[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 getName()
35     {
36         return _("LinkSearch");
37     }
38
39     function getDescription()
40     {
41         return _("Search page and link names.");
42     }
43
44     function getDefaultArguments()
45     {
46         return array_merge
47         (
48             PageList::supportedArgs(), // paging and more.
49             array(
50                 's' => "", // linkvalue query string
51                 'page' => "*", // which pages (glob allowed), default: all
52                 'direction' => "out", // or in
53                 'case_exact' => false,
54                 'regex' => 'auto',
55                 'noform' => false, // don't show form with results.
56                 'noheader' => false // no caption
57             ));
58     }
59
60     function showForm(&$dbi, &$request, $args)
61     {
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'
73         ), '');
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'
82         ), '');
83         $dirsign_switch = JavaScript("
84 function dirsign_switch() {
85   var d = document.getElementById('dirsign')
86   d.innerHTML = (d.innerHTML == ' =&gt; ') ? ' &lt;= ' : ' =&gt; '
87 }
88 ");
89         $dirsign = " => ";
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';
94         $in['value'] = 'in';
95         $in['id'] = 'dir_in';
96         if ($args['direction'] == 'in') {
97             $in['checked'] = 'checked';
98             $dirsign = " <= ";
99         }
100         $direction = HTML(HTML::input($out), HTML::label(array('for' => 'dir_out'), _("outgoing")),
101             HTML::input($in), HTML::label(array('for' => 'dir_in'), _("incoming")));
102         /*
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';
110             $dirsign = " <= ";
111         }
112         $direction->pushContent(HTML::option($out, _("outgoing")));
113         $direction->pushContent(HTML::option($in, _("incoming")));
114         */
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,
118                 'method' => 'GET',
119                 'accept-charset' => 'UTF-8'),
120             $dirsign_switch,
121             $hiddenfield,
122             $instructions, HTML::br(),
123             $pagefilter,
124             HTML::strong(HTML::tt(array('id' => 'dirsign'), $dirsign)),
125             $query,
126             HTML::raw('&nbsp;'), $direction,
127             HTML::raw('&nbsp;'), $submit);
128         return $form;
129     }
130
131     function run($dbi, $argstr, &$request, $basepage)
132     {
133         $args = $this->getArgs($argstr, $request);
134
135         if (empty($args['page']))
136             $args['page'] = "*";
137         $form = $this->showForm($dbi, $request, $args);
138         extract($args);
139         if (empty($s))
140             return $form;
141         $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
142         $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
143         $links = $dbi->linkSearch($pagequery, $linkquery, $direction == 'in' ? 'linkfrom' : 'linkto');
144         $pagelist = new PageList($args['info'], $args['exclude'], $args);
145         $pagelist->_links = array();
146         while ($link = $links->next()) {
147             $pagelist->addPage($link['pagename']);
148             $pagelist->_links[] = $link;
149         }
150         $pagelist->addColumnObject
151         (new _PageList_Column_LinkSearch_link('link', _("Link"), $pagelist));
152
153         if (!$noheader) {
154             // We put the form into the caption just to be able to return one pagelist object,
155             // and to still have the convenience form at the top. we could workaround this by
156             // putting the form as WikiFormRich into the actionpage. but thid doesnt look as
157             // nice as this here.
158             $pagelist->setCaption
159             ( // on mozilla the form doesn't fit into the caption very well.
160                 HTML($noform ? '' : HTML($form, HTML::hr()),
161                     fmt("LinkSearch result for \"%s\" in pages \"%s\", direction %s", $s, $page, $direction)));
162         }
163         return $pagelist;
164     }
165 }
166
167 // FIXME: sortby errors with this column
168 class _PageList_Column_LinkSearch_link
169     extends _PageList_Column
170 {
171     private function _PageList_Column_LinkSearch_link($field, $heading, &$pagelist)
172     {
173         $this->_field = $field;
174         $this->_heading = $heading;
175         $this->_need_rev = false;
176         $this->_iscustom = true;
177         $this->_pagelist =& $pagelist;
178     }
179
180     function _getValue(&$page, $revision_handle)
181     {
182         if (is_object($page)) $text = $page->getName();
183         else $text = $page;
184         $link = $this->_pagelist->_links[$this->current_row];
185         return WikiLink($link['linkvalue'], 'if_known');
186     }
187 }
188
189 // Local Variables:
190 // mode: php
191 // tab-width: 8
192 // c-basic-offset: 4
193 // c-hanging-comment-ender-p: nil
194 // indent-tabs-mode: nil
195 // End: