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