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