]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LinkSearch.php
include [all] Include and file path should be devided with single space. File path...
[SourceForge/phpwiki.git] / lib / plugin / LinkSearch.php
1 <?php // -*-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         return _("LinkSearch");
36     }
37     function getDescription() {
38         return _("Search page and link names");
39     }
40     function getDefaultArguments() {
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         $action = $request->getPostURL();
57         $hiddenfield = HiddenInputs($request->getArgs(),'',
58                                     array('action','page','s','direction'));
59         $pagefilter = HTML::input(array('name' => 'page',
60                                         'value' => $args['page'],
61                                         'title' => _("Search only in these pages. With autocompletion."),
62                                         'class' => 'dropdown',
63                                         'acdropdown' => 'true',
64                                         'autocomplete_complete' => 'true',
65                                         'autocomplete_matchsubstring' => 'false',
66                                         'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
67                                         ), '');
68         $query = HTML::input(array('name' => 's',
69                                    'value' => $args['s'],
70                                    'title' => _("Filter by this link. These are pagenames. With autocompletion."),
71                                    'class' => 'dropdown',
72                                    'acdropdown' => 'true',
73                                    'autocomplete_complete' => 'true',
74                                    'autocomplete_matchsubstring' => 'true',
75                                    'autocomplete_list' => 'xmlrpc:wiki.titleSearch ^[S] 4'
76                                    ), '');
77         $dirsign_switch = JavaScript("
78 function dirsign_switch() {
79   var d = document.getElementById('dirsign')
80   d.innerHTML = (d.innerHTML == ' =&gt; ') ? ' &lt;= ' : ' =&gt; '
81 }
82 ");
83         $dirsign = " => ";
84         $in = $out = array('name' => 'direction', 'type'=>'radio', 'onChange' => 'dirsign_switch()');
85         $out['value'] = 'out';
86         $out['id'] = 'dir_out';
87         if ($args['direction']=='out') $out['checked'] = 'checked';
88         $in['value'] = 'in';
89         $in['id'] = 'dir_in';
90         if ($args['direction']=='in') {
91             $in['checked'] = 'checked';
92             $dirsign = " <= ";
93         }
94         $direction = HTML(HTML::input($out), HTML::label(array('for'=>'dir_out'),_("outgoing")),
95                           HTML::input($in), HTML::label(array('for'=>'dir_in'),_("incoming")));
96         /*
97         $direction = HTML::select(array('name'=>'direction',
98                                         'onChange' => 'dirsign_switch()'));
99         $out = array('value' => 'out');
100         if ($args['direction']=='out') $out['selected'] = 'selected';
101         $in = array('value' => 'in');
102         if ($args['direction']=='in') {
103             $in['selected'] = 'selected';
104             $dirsign = " <= ";
105         }
106         $direction->pushContent(HTML::option($out, _("outgoing")));
107         $direction->pushContent(HTML::option($in, _("incoming")));
108         */
109         $submit = Button('submit:search',  _("LinkSearch"), false);
110         $instructions = _("Search in pages for links with the matching name.");
111         $form = HTML::form(array('action' => $action,
112                                  'method' => 'GET',
113                                  'accept-charset' => $GLOBALS['charset']),
114                            $dirsign_switch,
115                            $hiddenfield,
116                            $instructions, HTML::br(),
117                            $pagefilter,
118                            HTML::strong(HTML::tt(array('id'=>'dirsign'), $dirsign)),
119                            $query,
120                            HTML::raw('&nbsp;'), $direction,
121                            HTML::raw('&nbsp;'), $submit);
122         return $form;
123     }
124
125     function run ($dbi, $argstr, &$request, $basepage) {
126         global $WikiTheme;
127         $args = $this->getArgs($argstr, $request);
128
129         if (empty($args['page']))
130             $args['page'] = "*";
131         $form = $this->showForm($dbi, $request, $args);
132         extract($args);
133         if (empty($s))
134             return $form;
135         $pagequery = new TextSearchQuery($page, $args['case_exact'], $args['regex']);
136         $linkquery = new TextSearchQuery($s, $args['case_exact'], $args['regex']);
137         $links = $dbi->linkSearch($pagequery, $linkquery, $direction == 'in' ? 'linkfrom' : 'linkto');
138         $pagelist = new PageList($args['info'], $args['exclude'], $args);
139         $pagelist->_links = array();
140         while ($link = $links->next()) {
141             $pagelist->addPage($link['pagename']);
142             $pagelist->_links[] = $link;
143         }
144         $pagelist->addColumnObject
145             (new _PageList_Column_LinkSearch_link('link', _("Link"), $pagelist));
146
147         if (!$noheader) {
148             // We put the form into the caption just to be able to return one pagelist object,
149             // and to still have the convenience form at the top. we could workaround this by
150             // putting the form as WikiFormRich into the actionpage. but thid doesnt look as
151             // nice as this here.
152             $pagelist->setCaption
153             (   // on mozilla the form doesn't fit into the caption very well.
154                 HTML($noform ? '' : HTML($form,HTML::hr()),
155                      fmt("LinkSearch result for \"%s\" in pages \"%s\", direction %s", $s, $page, $direction)));
156         }
157         return $pagelist;
158     }
159 };
160
161 // FIXME: sortby errors with this column
162 class _PageList_Column_LinkSearch_link
163 extends _PageList_Column
164 {
165     function _PageList_Column_LinkSearch_link ($field, $heading, &$pagelist) {
166         $this->_field = $field;
167         $this->_heading = $heading;
168         $this->_need_rev = false;
169         $this->_iscustom = true;
170         $this->_pagelist =& $pagelist;
171     }
172     function _getValue(&$page, $revision_handle) {
173         if (is_object($page)) $text = $page->getName();
174         else $text = $page;
175         $link = $this->_pagelist->_links[$this->current_row];
176         return WikiLink($link['linkvalue'],'if_known');
177     }
178 }
179
180 // Local Variables:
181 // mode: php
182 // tab-width: 8
183 // c-basic-offset: 4
184 // c-hanging-comment-ender-p: nil
185 // indent-tabs-mode: nil
186 // End: