]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LikePages.php
Reverted pagename= arg back to page=. Begin refactor.
[SourceForge/phpwiki.git] / lib / plugin / LikePages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: LikePages.php,v 1.15 2002-01-30 22:47:08 carstenklapp 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                            LinkWikiWord($page));
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;
77         $this->_init($page, &$pagelist, $info, $exclude);
78
79         $pages = $dbi->titleSearch($query);
80
81         while ($page = $pages->next()) {
82             $name = $page->getName();
83             if (!preg_match($match_re, $name))
84                 continue;
85
86             $pagelist->addPage($page);
87         }
88
89         if (!$noheader)
90             $pagelist->setCaption($descrip);
91
92         return $pagelist;
93     }
94
95     function _init(&$page, &$pagelist, $info = '', $exclude = '', $include_self = '') {
96         if ($info)
97             foreach (explode(",", $info) as $col)
98                 $pagelist->insertColumn($col);
99
100         if ($exclude)
101             foreach (explode(",", $exclude) as $excludepage)
102                 $pagelist->excludePageName($excludepage);
103         if (!$include_self)
104             $pagelist->excludePageName($page);
105    }
106
107     function _quote($str) {
108         return "'" . str_replace("'", "''", $str) . "'";
109     }
110 };
111         
112 // Local Variables:
113 // mode: php
114 // tab-width: 8
115 // c-basic-offset: 4
116 // c-hanging-comment-ender-p: nil
117 // indent-tabs-mode: nil
118 // End:   
119 ?>