]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/OrphanedPages.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / OrphanedPages.php
1 <?php
2
3 /**
4  * This file is part of PhpWiki.
5  *
6  * PhpWiki is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PhpWiki is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 /**
22  * A plugin which returns a list of pages which are not linked to by
23  * any other page
24  *
25  * Initial version by Lawrence Akka
26  *
27  **/
28 require_once 'lib/PageList.php';
29
30 class WikiPlugin_OrphanedPages
31     extends WikiPlugin
32 {
33     function getDescription()
34     {
35         return _("List pages which are not linked to by any other page.");
36     }
37
38     function getDefaultArguments()
39     {
40         return array('noheader' => false,
41             'include_empty' => false,
42             'exclude' => '',
43             'info' => '',
44             'sortby' => false,
45             'limit' => 0,
46             'paging' => 'auto',
47         );
48     }
49
50     // info arg allows multiple columns
51     // info=mtime,hits,summary,version,author,locked,minor,markup or all
52     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
53
54     /**
55      * @param WikiDB $dbi
56      * @param string $argstr
57      * @param WikiRequest $request
58      * @param string $basepage
59      * @return mixed
60      */
61     function run($dbi, $argstr, &$request, $basepage)
62     {
63         $args = $this->getArgs($argstr, $request);
64         extract($args);
65
66         // There's probably a more efficient way to do this (eg a
67         // tailored SQL query via the backend, but this does the job
68
69         $allpages_iter = $dbi->getAllPages($include_empty);
70         $pages = array();
71         while ($page = $allpages_iter->next()) {
72             $links_iter = $page->getBackLinks();
73             // Test for absence of backlinks. If a page is linked to
74             // only by itself, it is still an orphan
75             $parent = $links_iter->next();
76             if (!$parent // page has no parents
77                 or (($parent->getName() == $page->getName())
78                     and !$links_iter->next())
79             ) // or page has only itself as a parent
80             {
81                 $pages[] = $page;
82             }
83         }
84         $args['count'] = count($pages);
85         $pagelist = new PageList($info, $exclude, $args);
86         if (!$noheader)
87             $pagelist->setCaption(_("Orphaned Pages in this wiki (%d total):"));
88         // deleted pages show up as version 0.
89         if ($include_empty)
90             $pagelist->_addColumn('version');
91         list($offset, $pagesize) = $pagelist->limit($args['limit']);
92         if (!$pagesize) $pagelist->addPageList($pages);
93         else {
94             for ($i = $offset; $i < $offset + $pagesize - 1; $i++) {
95                 if ($i >= $args['count']) break;
96                 $pagelist->addPage($pages[$i]);
97             }
98         }
99         return $pagelist;
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: