]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UnfoldSubpages.php
Merge OldTextFormattingRules into TextFormattingRules; rename underscore plugins
[SourceForge/phpwiki.git] / lib / plugin / UnfoldSubpages.php
1 <?php
2
3 /*
4  * Copyright 2002,2004,2005 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * UnfoldSubpages:  Lists the content of all SubPages of the current page.
25  *   This is e.g. useful for the CalendarPlugin, to see all entries at once.
26  *   Warning: Better don't use it with non-existant sections!
27  *              The section extractor is currently quite unstable.
28  * Usage:   <<UnfoldSubpages sortby=-mtime words=50 maxpages=5 >>
29  * Author:  Reini Urban <rurban@x-ray.at>
30  */
31
32 require_once 'lib/PageList.php';
33 require_once 'lib/TextSearchQuery.php';
34 require_once 'lib/plugin/IncludePage.php';
35
36 class WikiPlugin_UnfoldSubpages
37     extends WikiPlugin_IncludePage
38 {
39     function getDescription()
40     {
41         return _("Include the content of all SubPages of the current page.");
42     }
43
44     function getDefaultArguments()
45     {
46         return array_merge
47         (
48             PageList::supportedArgs(),
49             array(
50                 'pagename' => '[pagename]', // default: current page
51                 //'header'  => '',  // expandable string
52                 'quiet' => false, // print no header
53                 'sortby' => '', // [+|-]pagename, [+|-]mtime, [+|-]hits
54                 'maxpages' => false, // maximum number of pages to include (== limit)
55                 'smalltitle' => false, // if set, hide transclusion-title,
56                 //  just have a small link at the start of
57                 //  the page.
58                 'words' => false, // maximum number of words
59                 //  per page to include
60                 'lines' => false, // maximum number of lines
61                 //  per page to include
62                 'bytes' => false, // maximum number of bytes
63                 //  per page to include
64                 'sections' => false, // maximum number of sections per page to include
65                 'section' => false, // this named section per page only
66                 'sectionhead' => false // when including a named
67                 //  section show the heading
68             ));
69     }
70
71     function run($dbi, $argstr, &$request, $basepage)
72     {
73         static $included_pages = false;
74         if (!$included_pages) $included_pages = array($basepage);
75
76         $args = $this->getArgs($argstr, $request);
77         extract($args);
78         $query = new TextSearchQuery($pagename . SUBPAGE_SEPARATOR . '*', true, 'glob');
79         $subpages = $dbi->titleSearch($query, $sortby, $limit, $exclude);
80         //if ($sortby)
81         //    $subpages = $subpages->applyFilters(array('sortby' => $sortby, 'limit' => $limit, 'exclude' => $exclude));
82         //$subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*', false,
83         //                            $sortby, $limit, $exclude);
84         if (is_string($exclude) and !is_array($exclude))
85             $exclude = PageList::explodePageList($exclude, false, false, $limit);
86         $content = HTML();
87
88         include_once 'lib/BlockParser.php';
89         $i = 0;
90         while ($page = $subpages->next()) {
91             $cpagename = $page->getName();
92             if ($maxpages and ($i++ > $maxpages)) {
93                 return $content;
94             }
95             if (in_array($cpagename, $exclude))
96                 continue;
97             // A page cannot include itself. Avoid doublettes.
98             if (in_array($cpagename, $included_pages)) {
99                 $content->pushContent(HTML::p(sprintf(_("Recursive inclusion of page %s ignored"),
100                     $cpagename)));
101                 continue;
102             }
103
104             // Check if user is allowed to get the Page.
105             if (!mayAccessPage('view', $cpagename)) {
106                 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access."),
107                     $cpagename));
108             }
109
110             // trap any remaining nonexistant subpages
111             if ($page->exists()) {
112                 $r = $page->getCurrentRevision();
113                 $c = $r->getContent(); // array of lines
114                 // follow redirects
115                 if ((preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(\S+)\s*\?' . '>/', implode("\n", $c), $m))
116                     or (preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(.*?)\s*\?' . '>/', implode("\n", $c), $m))
117                     or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $c), $m))
118                     or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $c), $m))
119                 ) {
120                     // Strip quotes (simple or double) from page name if any
121                     if ((string_starts_with($m[1], "'"))
122                         or (string_starts_with($m[1], "\""))
123                     ) {
124                         $m[1] = substr($m[1], 1, -1);
125                     }
126                     // trap recursive redirects
127                     if (in_array($m[1], $included_pages)) {
128                         if (!$quiet)
129                             $content->pushContent(
130                                 HTML::p(sprintf(_("Recursive inclusion of page %s ignored"),
131                                     $cpagename . ' => ' . $m[1])));
132                         continue;
133                     }
134                     $cpagename = $m[1];
135
136                     // Check if user is allowed to get the Page.
137                     if (!mayAccessPage('view', $cpagename)) {
138                         return $this->error(sprintf(_("Illegal inclusion of page %s: no read access."),
139                             $cpagename));
140                     }
141
142                     $page = $dbi->getPage($cpagename);
143                     $r = $page->getCurrentRevision();
144                     $c = $r->getContent(); // array of lines
145                 }
146
147                 // moved to IncludePage
148                 $ct = $this->extractParts($c, $cpagename, $args);
149
150                 array_push($included_pages, $cpagename);
151                 if ($smalltitle) {
152                     $pname = array_pop(explode(SUBPAGE_SEPARATOR, $cpagename)); // get last subpage name
153                     // Use _("%s: %s") instead of .": ". for French punctuation
154                     $ct = TransformText(sprintf(_("%s: %s"), "[$pname|$cpagename]", $ct),
155                                         $cpagename);
156                 } else {
157                     $ct = TransformText($ct, $cpagename);
158                 }
159                 array_pop($included_pages);
160                 if (!$smalltitle) {
161                     $content->pushContent(HTML::p(array('class' => $quiet ?
162                             '' : 'transclusion-title'),
163                         fmt("Included from %s", WikiLink($cpagename))));
164                 }
165                 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
166                         '' : 'transclusion'),
167                     false, $ct)));
168             }
169         }
170         if (!isset($cpagename)) {
171             return $this->error(sprintf(_("%s has no subpages defined."), $pagename));
172         }
173         return $content;
174     }
175 }
176
177 // Local Variables:
178 // mode: php
179 // tab-width: 8
180 // c-basic-offset: 4
181 // c-hanging-comment-ender-p: nil
182 // indent-tabs-mode: nil
183 // End: