2 rcs_id('$Id: UnfoldSubpages.php,v 1.13 2004-03-12 15:48:08 rurban Exp $');
4 Copyright 2002 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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: Don't use it with subpages where the RedirectTo plugin is used
27 * or with non-existant sections!
28 * The section extractor is currently quite unstable.
29 * Usage: <?plugin UnfoldSubpages sortby=-mtime words=50 maxpages=5 ?>
30 * Author: Reini Urban <rurban@x-ray.at>
32 class WikiPlugin_UnfoldSubpages
36 return _("UnfoldSubpages");
39 function getDescription () {
40 return _("Includes the content of all SubPages of the current page.");
43 function getVersion() {
44 return preg_replace("/[Revision: $]/", '',
45 "\$Revision: 1.13 $");
48 function getDefaultArguments() {
50 'pagename' => '[pagename]', // default: current page
51 //'header' => '', // expandable string
52 'quiet' => false, // print no header
53 //'sort' => 'asc', // deprecated: use sortby=+pagename or
54 // sortby=-mtime instead,
55 'sortby' => 'pagename', // [+|-]pagename, [+|-]mtime, [+|-]hits
57 'pages' => false, // deprecated. use maxpages instead
58 'maxpages' => false, // maximum number of pages to include
59 'sections' => false,// maximum number of sections per page to
61 'smalltitle' => false, // if set, hide transclusion-title,
62 // just have a small link at the start of
64 'words' => false, // maximum number of words
65 // per page to include
66 'lines' => false, // maximum number of lines
67 // per page to include
68 'bytes' => false, // maximum number of bytes
69 // per page to include
70 'section' => false, // this named section per page only
71 'sectionhead' => false // when including a named
72 // section show the heading
77 function firstNWordsOfContent($n, $content) {
80 foreach ($content as $line) {
81 $words = explode(' ', $line);
82 if ($wordcount + count($words) > $n) {
83 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
84 . sprintf(_("... first %d words"), $n);
88 $wordcount += count($words);
95 //TODO: move this to stdlib.php
96 function extractSection ($section, $content, $page, $quiet, $sectionhead) {
97 $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
99 if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
100 . " \\s*$\\n?" // possible blank lines
101 . " ( (?: ^.*\\n? )*? )" // some lines
102 . " (?= ^\\1 | \\Z)/xm", // sec header (same
105 implode("\n", $content),
107 // Strip trailing blanks lines and ---- <hr>s
108 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
110 $text = $match[1] . $section ."\n". $text;
111 return explode("\n", $text);
114 $mesg = $page ." ". $section;
117 return array(sprintf(_("<%s: no such section>"), $mesg));
120 function run($dbi, $argstr, &$request, $basepage) {
121 include_once('lib/BlockParser.php');
123 $args = $this->getArgs($argstr, $request);
125 $subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*',false,$sortby,$limit);
127 return $this->error(_("The current page has no subpages defined."));
131 $subpages = array_slice ($subpages, 0, $maxpages);
133 foreach ($subpages as $page) {
134 // A page cannot include itself. Avoid doublettes.
135 static $included_pages = array();
136 if (in_array($page, $included_pages)) {
137 $content->pushContent(HTML::p(sprintf(_("recursive inclusion of page %s ignored"),
141 // trap any remaining nonexistant subpages
142 if ($dbi->isWikiPage($page)) {
143 $p = $dbi->getPage($page);
144 $r = $p->getCurrentRevision();
145 $c = $r->getContent();
148 $c = $this->extractSection($section, $c, $page, $quiet,
151 $c = array_slice($c, 0, $lines)
152 . sprintf(_(" ... first %d lines"), $bytes);
154 $c = $this->firstNWordsOfContent($words, $c);
156 if (strlen($c) > $bytes)
157 $c = substr($c, 0, $bytes)
158 . sprintf(_(" ... first %d bytes"), $bytes);
161 array_push($included_pages, $page);
163 $pname = array_pop(explode("/", $page)); // get last subpage name
164 // Use _("%s: %s") instead of .": ". for French punctuation
165 $ct = TransformText(sprintf(_("%s: %s"), "[$pname|$page]",
167 $r->get('markup'), $page);
170 $ct = TransformText(implode("\n", $c), $r->get('markup'), $page);
172 array_pop($included_pages);
174 $content->pushContent(HTML::p(array('class' => $quiet ?
175 '' : 'transclusion-title'),
176 fmt("Included from %s:",
179 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
180 '' : 'transclusion'),
188 // $Log: not supported by cvs2svn $
189 // Revision 1.12 2004/02/22 23:20:33 rurban
190 // fixed DumpHtmlToDir,
191 // enhanced sortby handling in PageList
192 // new button_heading th style (enabled),
193 // added sortby and limit support to the db backends and plugins
194 // for paging support (<<prev, next>> links on long lists)
196 // Revision 1.11 2004/02/17 12:11:36 rurban
197 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
199 // Revision 1.10 2004/01/27 12:10:45 rurban
200 // fixed UnfoldSubpages and added docs.
201 // new arguments: pagename, maxpages
202 // some arguments are deprecated: sort (use sortby), pages (use maxpages)
203 // fixed sortby, added docs
205 // Revision 1.9 2004/01/26 09:18:00 rurban
206 // * changed stored pref representation as before.
207 // the array of objects is 1) bigger and 2)
208 // less portable. If we would import packed pref
209 // objects and the object definition was changed, PHP would fail.
210 // This doesn't happen with an simple array of non-default values.
211 // * use $prefs->retrieve and $prefs->store methods, where retrieve
212 // understands the interim format of array of objects also.
213 // * simplified $prefs->get() and fixed $prefs->set()
214 // * added $user->_userid and class '_WikiUser' portability functions
215 // * fixed $user object ->_level upgrading, mostly using sessions.
216 // this fixes yesterdays problems with loosing authorization level.
217 // * fixed WikiUserNew::checkPass to return the _level
218 // * fixed WikiUserNew::isSignedIn
219 // * added explodePageList to class PageList, support sortby arg
220 // * fixed UserPreferences for WikiUserNew
221 // * fixed WikiPlugin for empty defaults array
222 // * UnfoldSubpages: added pagename arg, renamed pages arg,
223 // removed sort arg, support sortby arg
225 // Revision 1.8 2004/01/25 10:52:16 rurban
226 // added sortby support to explodePageList() and UnfoldSubpages
227 // fixes [ 758044 ] Plugin UnfoldSubpages does not sort (includes fix)
229 // Revision 1.7 2003/02/21 04:12:06 dairiki
230 // Minor fixes for new cached markup.
232 // Revision 1.6 2003/02/11 09:34:34 rurban
233 // fix by Steven D. Brewer <sbrewer@bio.umass.edu> to respect the $pages argument
235 // Revision 1.5 2003/01/18 22:11:44 carstenklapp
237 // Reformatting & tabs to spaces;
238 // Added copyleft, getVersion, getDescription, rcs_id.
240 // Revision 1.4 2003/01/05 02:37:30 carstenklapp
241 // New: Implemented 'smalltitle' argument and date sorting fix from
242 // Cuthbert Cat's sf patch 655095. Added getVersion & getDescription;
245 // Revision 1.3 2003/01/04 22:46:07 carstenklapp
246 // Workaround: when page has no subpages avoid include of nonexistant pages.
250 // - line & word limit doesn't work if the included page itself
258 // c-hanging-comment-ender-p: nil
259 // indent-tabs-mode: nil