]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LikePages.php
More infiltration of new object-based HTML generation.
[SourceForge/phpwiki.git] / lib / plugin / LikePages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: LikePages.php,v 1.7 2002-01-21 06:55:47 dairiki 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         
36         if ($prefix) {
37             $suffix = false;
38             $descrip = fmt("Page names with prefix '%s'", $prefix);
39         }
40         elseif ($suffix) {
41             $descrip = fmt("Page names with suffix '%s'", $suffix);
42         }
43         elseif ($page) {
44             $words = preg_split('/[\s:-;.,]+/',
45                                 split_pagename($page));
46             $words = preg_grep('/\S/', $words);
47             
48             $prefix = reset($words);
49             $suffix = end($words);
50             $exclude = $page;
51             
52             $descrip = fmt("These pages share an initial or final title word with '%s'",
53                            _LinkWikiWord($page));
54         }
55
56         // Search for pages containing either the suffix or the prefix.
57         $search = $match = array();
58         if (!empty($prefix)) {
59             $search[] = $this->_quote($prefix);
60             $match[]  = '^' . preg_quote($prefix, '/');
61         }
62         if (!empty($suffix)) {
63             $search[] = $this->_quote($suffix);
64             $match[]  = preg_quote($suffix, '/') . '$';
65         }
66
67         if ($search)
68             $query = new TextSearchQuery(join(' OR ', $search));
69         else
70             $query = new NullTextSearchQuery; // matches nothing
71         
72         $match_re = '/' . join('|', $match) . '/';
73
74         $pages = $dbi->titleSearch($query);
75         $list = HTML::ul();
76         while ($page = $pages->next()) {
77             $name = $page->getName();
78             if (!preg_match($match_re, $name))
79                 continue;
80             if (!empty($exclude) && $name == $exclude)
81                 continue;
82             $list->pushContent(HTML::li(_LinkWikiWord($name)));
83         }
84         if (!$list->getContent())
85             $list = HTML::blockquote(_("<none>"));
86
87         if ($noheader)
88             return $list;
89         return array(HTML::p($descrip), $list);
90     }
91
92     function _quote($str) {
93         return "'" . str_replace("'", "''", $str) . "'";
94     }
95 };
96         
97 // Local Variables:
98 // mode: php
99 // tab-width: 8
100 // c-basic-offset: 4
101 // c-hanging-comment-ender-p: nil
102 // indent-tabs-mode: nil
103 // End:   
104 ?>