]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/LikePages.php
Fix to split words at certain punctuation, so that e.g. LikePages
[SourceForge/phpwiki.git] / lib / plugin / LikePages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: LikePages.php,v 1.4 2001-12-16 19:35:35 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 _("List LikePages for [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', sprintf(_("Page names with prefix '%s'"),
40                                                $prefix));
41         }
42         elseif ($suffix) {
43             if (!$noheader)
44                 $html .= QElement('p', sprintf(_("Page names with suffix '%s'"),
45                                                $suffix));
46         }
47         elseif ($page) {
48             $words = preg_split('/[\s:-;.,]+/',
49                                 split_pagename($page));
50             $words = preg_grep('/\S/', $words);
51             
52             $prefix = reset($words);
53             $suffix = end($words);
54             $exclude = $page;
55             
56             if (!$noheader) {
57                 $fs = _("These pages share an initial or final title word with '%s'");
58                 $html .= Element('p', sprintf(htmlspecialchars($fs), LinkWikiWord($page)));
59             }
60         }
61
62         // Search for pages containing either the suffix or the prefix.
63         $search = $match = array();
64         if (!empty($prefix)) {
65             $search[] = $this->_quote($prefix);
66             $match[] = '^' . preg_quote($prefix, '/');
67         }
68         if (!empty($suffix)) {
69             $search[] = $this->_quote($suffix);
70             $match[] = preg_quote($suffix, '/') . '$';
71         }
72
73         if ($search)
74             $query = new TextSearchQuery(join(' OR ', $search));
75         else
76             $query = new NullTextSearchQuery; // matches nothing
77         
78         $match_re = '/' . join('|', $match) . '/';
79
80         $pages = $dbi->titleSearch($query);
81         $lines = array();
82         while ($page = $pages->next()) {
83             $name = $page->getName();
84             if (!preg_match($match_re, $name))
85                 continue;
86             if (!empty($exclude) && $name == $exclude)
87                 continue;
88             $lines[] = Element('li', LinkWikiWord($name));
89         }
90         if ($lines)
91             $html .= Element('ul', join("\n", $lines));
92         else
93             $html .= QElement('blockquote', gettext("<none>"));
94         
95         return $html;
96     }
97
98     function _quote($str) {
99         return "'" . str_replace("'", "''", $str) . "'";
100     }
101 };
102         
103 // Local Variables:
104 // mode: php
105 // tab-width: 8
106 // c-basic-offset: 4
107 // c-hanging-comment-ender-p: nil
108 // indent-tabs-mode: nil
109 // End:   
110 ?>