]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LikePages.php
info arg now allows multiple columns
[SourceForge/phpwiki.git] / lib / plugin / LikePages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: LikePages.php,v 1.11 2002-01-22 06:15:52 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         // FIXME: how to exclude multiple pages?
22         return array('page'     => false,
23                      'prefix'   => false,
24                      'suffix'   => false,
25                      'exclude'  => false,
26                      'noheader' => false,
27                      'info'     => false
28                      );
29     }
30     // info arg now allows multiple columns info=mtime,hits,summary,author,locked,minor
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             $exclude = $page;
54             
55             $descrip = fmt("These pages share an initial or final title word with '%s'",
56                            LinkWikiWord($page));
57         }
58
59         // Search for pages containing either the suffix or the prefix.
60         $search = $match = array();
61         if (!empty($prefix)) {
62             $search[] = $this->_quote($prefix);
63             $match[]  = '^' . preg_quote($prefix, '/');
64         }
65         if (!empty($suffix)) {
66             $search[] = $this->_quote($suffix);
67             $match[]  = preg_quote($suffix, '/') . '$';
68         }
69
70         if ($search)
71             $query = new TextSearchQuery(join(' OR ', $search));
72         else
73             $query = new NullTextSearchQuery; // matches nothing
74         
75         $match_re = '/' . join('|', $match) . '/';
76
77         $pages = $dbi->titleSearch($query);
78         $pagelist = new PageList;
79
80         if ($info)
81             foreach (explode(",", $info) as $col)
82                 $pagelist->insertColumn($col);
83
84         while ($page = $pages->next()) {
85             $name = $page->getName();
86             if (!preg_match($match_re, $name))
87                 continue;
88             if (!empty($exclude) && $name == $exclude)
89                 continue;
90
91             $pagelist->addPage($page);
92         }
93
94         if (!$noheader)
95             $pagelist->setCaption($descrip);
96
97         return $pagelist;
98     }
99
100     function _quote($str) {
101         return "'" . str_replace("'", "''", $str) . "'";
102     }
103 };
104         
105 // Local Variables:
106 // mode: php
107 // tab-width: 8
108 // c-basic-offset: 4
109 // c-hanging-comment-ender-p: nil
110 // indent-tabs-mode: nil
111 // End:   
112 ?>