3 * Copyright © 2002,2004,2005 $ThePhpWikiProgrammingTeam
5 * This file is part of PhpWiki.
7 * PhpWiki is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * PhpWiki is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * SPDX-License-Identifier: GPL-2.0-or-later
26 * UnfoldSubpages: Lists the content of all SubPages of the current page.
27 * This is e.g. useful for the CalendarPlugin, to see all entries at once.
28 * Warning: Better don't use it with nonexistent sections!
29 * The section extractor is currently quite unstable.
30 * Usage: <<UnfoldSubpages sortby=-mtime words=50 maxpages=5 >>
31 * Author: Reini Urban <rurban@x-ray.at>
34 require_once 'lib/PageList.php';
35 require_once 'lib/TextSearchQuery.php';
36 require_once 'lib/plugin/IncludePage.php';
38 class WikiPlugin_UnfoldSubpages
39 extends WikiPlugin_IncludePage
41 function getDescription()
43 return _("Include the content of all SubPages of the current page.");
46 function getDefaultArguments()
50 PageList::supportedArgs(),
52 'pagename' => '[pagename]', // default: current page
53 'quiet' => false, // print no header
54 'sortby' => '', // [+|-]pagename, [+|-]mtime, [+|-]hits
55 'maxpages' => false, // maximum number of pages to include (== limit)
56 'smalltitle' => false, // if set, hide transclusion-title,
57 // just have a small link at the start of the page.
58 'words' => false, // maximum number of words per page to include
59 'lines' => false, // maximum number of lines per page to include
60 'bytes' => false, // maximum number of bytes per page to include
61 'sections' => false, // maximum number of sections per page to include
62 'section' => false, // this named section per page only
63 'sectionhead' => false // when including a named section show the heading
69 * @param string $argstr
70 * @param WikiRequest $request
71 * @param string $basepage
74 function run($dbi, $argstr, &$request, $basepage)
76 static $included_pages = false;
77 if (!$included_pages) $included_pages = array($basepage);
79 $args = $this->getArgs($argstr, $request);
81 $query = new TextSearchQuery($pagename . '/' . '*', true, 'glob');
82 $subpages = $dbi->titleSearch($query, $sortby, $limit, $exclude);
83 if (is_string($exclude) and !is_array($exclude))
84 $exclude = PageList::explodePageList($exclude, false, false, $limit);
87 include_once 'lib/BlockParser.php';
89 while ($page = $subpages->next()) {
90 $cpagename = $page->getName();
91 if ($maxpages and ($i++ > $maxpages)) {
94 if (in_array($cpagename, $exclude))
96 // A page cannot include itself. Avoid doublettes.
97 if (in_array($cpagename, $included_pages)) {
98 $content->pushContent(HTML::p(sprintf(_("Recursive inclusion of page %s ignored"),
103 // Check if user is allowed to get the Page.
104 if (!mayAccessPage('view', $cpagename)) {
105 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access."),
109 // Trap any remaining nonexistent subpages
110 if ($page->exists()) {
111 $r = $page->getCurrentRevision();
112 $c = $r->getContent(); // array of lines
114 if ((preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(\S+)\s*\?' . '>/', implode("\n", $c), $m))
115 or (preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(.*?)\s*\?' . '>/', implode("\n", $c), $m))
116 or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $c), $m))
117 or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $c), $m))
119 // Strip quotes (simple or double) from page name if any
120 if ((string_starts_with($m[1], "'"))
121 or (string_starts_with($m[1], "\""))
123 $m[1] = substr($m[1], 1, -1);
125 // trap recursive redirects
126 if (in_array($m[1], $included_pages)) {
128 $content->pushContent(
129 HTML::p(sprintf(_("Recursive inclusion of page %s ignored"),
130 $cpagename . ' => ' . $m[1])));
135 // Check if user is allowed to get the Page.
136 if (!mayAccessPage('view', $cpagename)) {
137 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access."),
141 $page = $dbi->getPage($cpagename);
142 $r = $page->getCurrentRevision();
143 $c = $r->getContent(); // array of lines
146 // moved to IncludePage
147 $ct = $this->extractParts($c, $cpagename, $args);
149 array_push($included_pages, $cpagename);
151 $pname = array_pop(explode('/', $cpagename)); // get last subpage name
152 // Use _("%s: %s") instead of .": ". for French punctuation
153 $ct = TransformText(sprintf(_("%s: %s"), "[$pname|$cpagename]", $ct),
156 $ct = TransformText($ct, $cpagename);
158 array_pop($included_pages);
160 $content->pushContent(HTML::p(array('class' => $quiet ?
161 '' : 'transclusion-title'),
162 fmt("Included from %s", WikiLink($cpagename))));
164 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
165 '' : 'transclusion'),
169 if (!isset($cpagename)) {
170 return HTML::p(array('class' => 'warning'),
171 sprintf(_("%s has no subpages defined."), $pagename));