4 * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
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.
23 require_once 'lib/TextSearchQuery.php';
24 require_once 'lib/PageList.php';
26 class WikiPlugin_LikePages
30 return _("LikePages");
33 function getDescription() {
34 return sprintf(_("List page names which share an initial or final title word with '%s'."),
38 function getDefaultArguments() {
41 PageList::supportedArgs(),
42 array('page' => '[pagename]',
48 // info arg allows multiple columns
49 // info=mtime,hits,summary,version,author,locked,minor
50 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
52 function run($dbi, $argstr, &$request, $basepage) {
53 $args = $this->getArgs($argstr, $request);
56 if (empty($page) && empty($prefix) && empty($suffix))
61 $descrip = fmt("Page names with prefix '%s'", $prefix);
64 $descrip = fmt("Page names with suffix '%s'", $suffix);
67 $words = preg_split('/[\s:-;.,]+/',
68 SplitPagename($page));
69 $words = preg_grep('/\S/', $words);
71 $prefix = reset($words);
72 $suffix = end($words);
74 $descrip = fmt("These pages share an initial or final title word with '%s'",
75 WikiLink($page, 'auto'));
78 // Search for pages containing either the suffix or the prefix.
79 $search = $match = array();
80 if (!empty($prefix)) {
81 $search[] = $this->_quote($prefix);
82 $match[] = '^' . preg_quote($prefix, '/');
84 if (!empty($suffix)) {
85 $search[] = $this->_quote($suffix);
86 $match[] = preg_quote($suffix, '/') . '$';
90 $query = new TextSearchQuery(join(' OR ', $search));
92 $query = new NullTextSearchQuery; // matches nothing
94 $match_re = '/' . join('|', $match) . '/';
96 $pagelist = new PageList($info, $exclude, $args);
98 $pagelist->setCaption($descrip);
99 $pages = $dbi->titleSearch($query);
100 while ($page = $pages->next()) {
101 $name = $page->getName();
102 if (!preg_match($match_re, $name))
104 $pagelist->addPage($page);
110 function _quote($str) {
111 return "'" . str_replace("'", "''", $str) . "'";
119 // c-hanging-comment-ender-p: nil
120 // indent-tabs-mode: nil