]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LikePages.php
PageList cleanup. Move the Plugin::_init functionality into the
[SourceForge/phpwiki.git] / lib / plugin / LikePages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: LikePages.php,v 1.17 2002-01-31 01:14:14 dairiki Exp $');
3
4 require_once('lib/TextSearchQuery.php');
5 require_once('lib/PageList.php');
6
7 /**
8  */
9 class WikiPlugin_LikePages
10 extends WikiPlugin
11 {
12     function getName() {
13         return _("LikePages");
14     }
15     
16     function getDescription() {
17         return sprintf(_("List LikePages for %s"), '[pagename]');
18     }
19     
20     function getDefaultArguments() {
21         return array('page'     => '[pagename]',
22                      'prefix'   => false,
23                      'suffix'   => false,
24                      'exclude'  => '',
25                      'noheader' => false,
26                      'info'     => ''
27                      );
28     }
29     // info arg allows multiple columns info=mtime,hits,summary,version,author,locked,minor
30     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
31
32     function run($dbi, $argstr, $request) {
33         $args = $this->getArgs($argstr, $request);
34         extract($args);
35         if (empty($page) && empty($prefix) && empty($suffix))
36             return '';
37
38         
39         if ($prefix) {
40             $suffix = false;
41             $descrip = fmt("Page names with prefix '%s'", $prefix);
42         }
43         elseif ($suffix) {
44             $descrip = fmt("Page names with suffix '%s'", $suffix);
45         }
46         elseif ($page) {
47             $words = preg_split('/[\s:-;.,]+/',
48                                 split_pagename($page));
49             $words = preg_grep('/\S/', $words);
50             
51             $prefix = reset($words);
52             $suffix = end($words);
53
54             $descrip = fmt("These pages share an initial or final title word with '%s'",
55                            WikiLink($page, 'auto'));
56         }
57
58         // Search for pages containing either the suffix or the prefix.
59         $search = $match = array();
60         if (!empty($prefix)) {
61             $search[] = $this->_quote($prefix);
62             $match[]  = '^' . preg_quote($prefix, '/');
63         }
64         if (!empty($suffix)) {
65             $search[] = $this->_quote($suffix);
66             $match[]  = preg_quote($suffix, '/') . '$';
67         }
68
69         if ($search)
70             $query = new TextSearchQuery(join(' OR ', $search));
71         else
72             $query = new NullTextSearchQuery; // matches nothing
73         
74         $match_re = '/' . join('|', $match) . '/';
75
76         $pagelist = new PageList($info, $exclude);
77         if (!$noheader)
78             $pagelist->setCaption($descrip);
79
80         $pages = $dbi->titleSearch($query);
81
82         while ($page = $pages->next()) {
83             $name = $page->getName();
84             if (!preg_match($match_re, $name))
85                 continue;
86             $pagelist->addPage($page);
87         }
88
89         return $pagelist;
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 ?>