]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LinkSearch.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / LinkSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
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
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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         return _("LinkSearch");
36     }
37     function getDescription() {
38         return _("Search page and link names");
39     }
40     function getVersion() {
41         return preg_replace("/[Revision: $]/", '',
42                             "\$Revision$");
43     }
44     function getDefaultArguments() {
45         return array_merge
46             (
47              PageList::supportedArgs(), // paging and more.
48              array(
49                    's'          => "", // linkvalue query string
50                    'page'       => "*", // which pages (glob allowed), default: all
51                    'direction'  => "out", // or in
52                    'case_exact' => false,
53                    'regex'      => 'auto',
54                    'noform'     => false, // don't show form with results.
55                    'noheader'   => false  // no caption
56                    ));
57     }
58
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'
71                                         ), '');
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'
80                                    ), '');
81         $dirsign_switch = JavaScript("
82 function dirsign_switch() {
83   var d = document.getElementById('dirsign')
84   d.innerHTML = (d.innerHTML == ' =&gt; ') ? ' &lt;= ' : ' =&gt; '
85 }
86 ");
87         $dirsign = " => ";
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';
92         $in['value'] = 'in';
93         $in['id'] = 'dir_in';
94         if ($args['direction']=='in') {
95             $in['checked'] = 'checked';
96             $dirsign = " <= ";
97         }
98         $direction = HTML(HTML::input($out), HTML::label(array('for'=>'dir_out'),_("outgoing")),
99                           HTML::input($in), HTML::label(array('for'=>'dir_in'),_("incoming")));
100         /*
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';
108             $dirsign = " <= ";
109         }
110         $direction->pushContent(HTML::option($out, _("outgoing")));
111         $direction->pushContent(HTML::option($in, _("incoming")));
112         */
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,
116                                  'method' => 'GET',
117                                  'accept-charset' => $GLOBALS['charset']),
118                            $dirsign_switch,
119                            $hiddenfield,
120                            $instructions, HTML::br(),
121                            $pagefilter,
122                            HTML::strong(HTML::tt(array('id'=>'dirsign'), $dirsign)),
123                            $query,
124                            HTML::raw('&nbsp;'), $direction,
125                            HTML::raw('&nbsp;'), $submit);
126         return $form;
127     }
128
129     function run ($dbi, $argstr, &$request, $basepage) {
130         global $WikiTheme;
131         $args = $this->getArgs($argstr, $request);
132
133         if (empty($args['page']))
134             $args['page'] = "*";
135         $form = $this->showForm($dbi, $request, $args);
136         extract($args);
137         if (empty($s))
138             return $form;
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;
147         }
148         $pagelist->addColumnObject
149             (new _PageList_Column_LinkSearch_link('link', _("Link"), $pagelist));
150
151         if (!$noheader) {
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)));
160         }
161         return $pagelist;
162     }
163 };
164
165 // FIXME: sortby errors with this column
166 class _PageList_Column_LinkSearch_link
167 extends _PageList_Column
168 {
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;
175     }
176     function _getValue(&$page, $revision_handle) {
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:
191 ?>