]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LikePages.php
Jeff's hacks II, continued:
[SourceForge/phpwiki.git] / lib / plugin / LikePages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: LikePages.php,v 1.1 2001-09-18 19:19:05 dairiki Exp $');
3
4 require_once('lib/TextSearchQuery.php');
5
6 /**
7  */
8 class WikiPlugin_LikePages
9 extends WikiPlugin
10 {
11     var $name = 'LikePages';
12     
13     function getDefaultArguments() {
14         // FIXME: how to exclude multiple pages?
15         return array('page'             => false,
16                      'prefix'           => false,
17                      'suffix'           => false,
18                      'exclude'          => false,
19                      'noheader'         => false
20                      );
21     }
22
23     function run($dbi, $argstr, $request) {
24         $args = $this->getArgs($argstr, $request);
25         extract($args);
26         if (empty($page) && empty($prefix) && empty($suffix))
27             return '';
28
29         $html = '';
30         if ($prefix) {
31             $suffix = false;
32             if (!$noheader)
33                 $html .= QElement('p', sprintf(gettext("Page names with prefix '%s'"),
34                                                $prefix));
35         }
36         elseif ($suffix) {
37             if (!$noheader)
38                 $html .= QElement('p', sprintf(gettext("Page names with suffix '%s'"),
39                                                $suffix));
40         }
41         elseif ($page) {
42             $words = explode(" ", split_pagename($page));
43             assert($words);
44             $prefix = $words[0];
45             list($suffix) = array_reverse($words);
46             $exclude = $page;
47
48             $fs = gettext("These pages share an initial or final title word with '%s'");
49             if (!$noheader)
50                 $html .= Element('p', sprintf(htmlspecialchars($fs), LinkWikiWord($page)));
51         }
52
53         // Search for pages containing either the suffix or the prefix.
54         $search = array();
55         if (!empty($prefix)) {
56             $search[] = $this->_quote($prefix);
57             $match[] = '^' . preg_quote($prefix, '/');
58         }
59         if (!empty($suffix)) {
60             $search[] = $this->_quote($suffix);
61             $match[] = preg_quote($suffix, '/') . '$';
62         }
63         $query = new TextSearchQuery(join(' OR ', $search));
64         $match_re = '/' . join('|', $match) . '/';
65
66         $pages = $dbi->titleSearch($query);
67         $lines = array();
68         while ($page = $pages->next()) {
69             $name = $page->getName();
70             if (!preg_match($match_re, $name))
71                 continue;
72             if (!empty($exclude) && $name == $exclude)
73                 continue;
74             $lines[] = Element('li', LinkWikiWord($name));
75         }
76         if ($lines)
77             $html .= Element('ul', join("\n", $lines));
78         else
79             $html .= QElement('blockquote', gettext("<none>"));
80         
81         return $html;
82     }
83
84     function _quote($str) {
85         return "'" . str_replace("'", "''", $str) . "'";
86     }
87 };
88         
89 // Local Variables:
90 // mode: php
91 // tab-width: 8
92 // c-basic-offset: 4
93 // c-hanging-comment-ender-p: nil
94 // indent-tabs-mode: nil
95 // End:   
96 ?>