2 rcs_id('$Id: LikePages.php,v 1.16 2002-01-30 23:41:54 dairiki Exp $');
4 require_once('lib/TextSearchQuery.php');
5 require_once('lib/PageList.php');
9 class WikiPlugin_LikePages
13 return _("LikePages");
16 function getDescription() {
17 return sprintf(_("List LikePages for %s"), '[pagename]');
20 function getDefaultArguments() {
21 return array('page' => '[pagename]',
29 // info arg allows multiple columns info=mtime,hits,summary,version,author,locked,minor
30 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
32 function run($dbi, $argstr, $request) {
33 $args = $this->getArgs($argstr, $request);
35 if (empty($page) && empty($prefix) && empty($suffix))
41 $descrip = fmt("Page names with prefix '%s'", $prefix);
44 $descrip = fmt("Page names with suffix '%s'", $suffix);
47 $words = preg_split('/[\s:-;.,]+/',
48 split_pagename($page));
49 $words = preg_grep('/\S/', $words);
51 $prefix = reset($words);
52 $suffix = end($words);
54 $descrip = fmt("These pages share an initial or final title word with '%s'",
55 WikiLink($page, 'auto'));
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, '/');
64 if (!empty($suffix)) {
65 $search[] = $this->_quote($suffix);
66 $match[] = preg_quote($suffix, '/') . '$';
70 $query = new TextSearchQuery(join(' OR ', $search));
72 $query = new NullTextSearchQuery; // matches nothing
74 $match_re = '/' . join('|', $match) . '/';
76 $pagelist = new PageList;
77 $this->_init($page, &$pagelist, $info, $exclude);
79 $pages = $dbi->titleSearch($query);
81 while ($page = $pages->next()) {
82 $name = $page->getName();
83 if (!preg_match($match_re, $name))
86 $pagelist->addPage($page);
90 $pagelist->setCaption($descrip);
95 function _init(&$page, &$pagelist, $info = '', $exclude = '', $include_self = '') {
97 foreach (explode(",", $info) as $col)
98 $pagelist->insertColumn($col);
101 foreach (explode(",", $exclude) as $excludepage)
102 $pagelist->excludePageName($excludepage);
104 $pagelist->excludePageName($page);
107 function _quote($str) {
108 return "'" . str_replace("'", "''", $str) . "'";
116 // c-hanging-comment-ender-p: nil
117 // indent-tabs-mode: nil