]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LikePages.php
Normalize header
[SourceForge/phpwiki.git] / lib / plugin / LikePages.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4  * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
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  */
28 class WikiPlugin_LikePages
29 extends WikiPlugin
30 {
31     function getName() {
32         return _("LikePages");
33     }
34
35     function getDescription() {
36         return sprintf(_("List page names which share an initial or final title word with '%s'."),
37                        '[pagename]');
38     }
39
40     function getVersion() {
41         return preg_replace("/[Revision: $]/", '',
42                             "\$Revision$");
43     }
44
45     function getDefaultArguments() {
46         return array_merge
47             (
48              PageList::supportedArgs(),
49              array('page'     => '[pagename]',
50                    'prefix'   => false,
51                    'suffix'   => false,
52                    'noheader' => false,
53                    ));
54     }
55     // info arg allows multiple columns
56     // info=mtime,hits,summary,version,author,locked,minor
57     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
58
59     function run($dbi, $argstr, &$request, $basepage) {
60         $args = $this->getArgs($argstr, $request);
61
62         extract($args);
63         if (empty($page) && empty($prefix) && empty($suffix))
64             return '';
65
66         if ($prefix) {
67             $suffix = false;
68             $descrip = fmt("Page names with prefix '%s'", $prefix);
69         }
70         elseif ($suffix) {
71             $descrip = fmt("Page names with suffix '%s'", $suffix);
72         }
73         elseif ($page) {
74             $words = preg_split('/[\s:-;.,]+/',
75                                 SplitPagename($page));
76             $words = preg_grep('/\S/', $words);
77
78             $prefix = reset($words);
79             $suffix = end($words);
80
81             $descrip = fmt("These pages share an initial or final title word with '%s'",
82                            WikiLink($page, 'auto'));
83         }
84
85         // Search for pages containing either the suffix or the prefix.
86         $search = $match = array();
87         if (!empty($prefix)) {
88             $search[] = $this->_quote($prefix);
89             $match[]  = '^' . preg_quote($prefix, '/');
90         }
91         if (!empty($suffix)) {
92             $search[] = $this->_quote($suffix);
93             $match[]  = preg_quote($suffix, '/') . '$';
94         }
95
96         if ($search)
97             $query = new TextSearchQuery(join(' OR ', $search));
98         else
99             $query = new NullTextSearchQuery; // matches nothing
100
101         $match_re = '/' . join('|', $match) . '/';
102
103         $pagelist = new PageList($info, $exclude, $args);
104         if (!$noheader)
105             $pagelist->setCaption($descrip);
106         $pages = $dbi->titleSearch($query);
107         while ($page = $pages->next()) {
108             $name = $page->getName();
109             if (!preg_match($match_re, $name))
110                 continue;
111             $pagelist->addPage($page);
112         }
113
114         return $pagelist;
115     }
116
117     function _quote($str) {
118         return "'" . str_replace("'", "''", $str) . "'";
119     }
120 };
121
122 // Local Variables:
123 // mode: php
124 // tab-width: 8
125 // c-basic-offset: 4
126 // c-hanging-comment-ender-p: nil
127 // indent-tabs-mode: nil
128 // End:
129 ?>