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