2 rcs_id('$Id: UnfoldSubpages.php,v 1.9 2004-01-26 09:18:00 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: $]/", '',
48 function getDefaultArguments() {
50 'pagename' => '', // not the current page
51 //'header' => '', // expandable string
52 'quiet' => false, // no header
53 'sort' => 'asc', // deprecated: use sortby=+pagename or
54 // sortby=-mtime instead,
55 'sortby' => 'pagename', // [+|-]pagename, [+|-]mtime, [+|-]hits
56 'pages' => '', // deprecated. use maxpages instead
57 'maxpages' => '', // maximum number of pages to include
58 'sections' => false,// maximum number of sections per page to
60 'smalltitle' => false, // if set, hide transclusion-title,
61 // just have a small link at the start of
63 'words' => false, // maximum number of words
64 // per page to include
65 'lines' => false, // maximum number of lines
66 // per page to include
67 'bytes' => false, // maximum number of bytes
68 // per page to include
69 'section' => '', // this named section per page only
70 'sectionhead' => false // when including a named
71 // section show the heading
76 function firstNWordsOfContent($n, $content) {
79 foreach ($content as $line) {
80 $words = explode(' ', $line);
81 if ($wordcount + count($words) > $n) {
82 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
83 . sprintf(_("... first %d words"), $n);
87 $wordcount += count($words);
94 //TODO: move this to stdlib.php
95 function extractSection ($section, $content, $page, $quiet, $sectionhead) {
96 $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
98 if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
99 . " \\s*$\\n?" // possible blank lines
100 . " ( (?: ^.*\\n? )*? )" // some lines
101 . " (?= ^\\1 | \\Z)/xm", // sec header (same
104 implode("\n", $content),
106 // Strip trailing blanks lines and ---- <hr>s
107 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
109 $text = $match[1] . $section ."\n". $text;
110 return explode("\n", $text);
113 $mesg = $page ." ". $section;
116 return array(sprintf(_("<%s: no such section>"), $mesg));
119 function run($dbi, $argstr, $request) {
120 include_once('lib/BlockParser.php');
122 if (!$this->getArg('pagename'))
123 $pagename = $request->getArg('pagename');
124 $sortby = 'pagename';
125 if ($request->getArg('sortby')) {
126 $sortby = PageList::sortby($request->getArg('sortby'),'db');
128 extract($this->getArgs($argstr, $request));
129 //TODO: explodePageList should be a PageList method.
130 $subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*',$sortby);
132 return $this->error(_("The current page has no subpages defined."));
134 extract($this->getArgs($argstr, $request));
136 // ignore the sort argument, use sortby with the + or - prefix instead
138 //if ($sort != 'asc') {
139 // $subpages = array_reverse($subpages);
142 $subpages = array_slice ($subpages, 0, $numpages);
144 foreach ($subpages as $page) {
145 // A page cannot include itself. Avoid doublettes.
146 static $included_pages = array();
147 if (in_array($page, $included_pages)) {
148 $content->pushContent(HTML::p(sprintf(_("recursive inclusion of page %s ignored"),
152 // trap any remaining nonexistant subpages
153 if ($dbi->isWikiPage($page)) {
154 $p = $dbi->getPage($page);
155 $r = $p->getCurrentRevision();
156 $c = $r->getContent();
159 $c = $this->extractSection($section, $c, $page, $quiet,
162 $c = array_slice($c, 0, $lines)
163 . sprintf(_(" ... first %d lines"), $bytes);
165 $c = $this->firstNWordsOfContent($words, $c);
167 if (strlen($c) > $bytes)
168 $c = substr($c, 0, $bytes)
169 . sprintf(_(" ... first %d bytes"), $bytes);
172 array_push($included_pages, $page);
174 $pname = array_pop(explode("/", $page)); // get last subpage name
175 // Use _("%s: %s") instead of .": ". for French punctuation
176 $ct = TransformText(sprintf(_("%s: %s"), "[$pname|$page]",
178 $r->get('markup'), $page);
181 $ct = TransformText(implode("\n", $c), $r->get('markup'), $page);
183 array_pop($included_pages);
185 $content->pushContent(HTML::p(array('class' => $quiet ?
186 '' : 'transclusion-title'),
187 fmt("Included from %s:",
190 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
191 '' : 'transclusion'),
199 // $Log: not supported by cvs2svn $
200 // Revision 1.8 2004/01/25 10:52:16 rurban
201 // added sortby support to explodePageList() and UnfoldSubpages
202 // fixes [ 758044 ] Plugin UnfoldSubpages does not sort (includes fix)
204 // Revision 1.7 2003/02/21 04:12:06 dairiki
205 // Minor fixes for new cached markup.
207 // Revision 1.6 2003/02/11 09:34:34 rurban
208 // fix by Steven D. Brewer <sbrewer@bio.umass.edu> to respect the $pages argument
210 // Revision 1.5 2003/01/18 22:11:44 carstenklapp
212 // Reformatting & tabs to spaces;
213 // Added copyleft, getVersion, getDescription, rcs_id.
215 // Revision 1.4 2003/01/05 02:37:30 carstenklapp
216 // New: Implemented 'smalltitle' argument and date sorting fix from
217 // Cuthbert Cat's sf patch 655095. Added getVersion & getDescription;
220 // Revision 1.3 2003/01/04 22:46:07 carstenklapp
221 // Workaround: when page has no subpages avoid include of nonexistant pages.
225 // - line & word limit doesn't work if the included page itself
233 // c-hanging-comment-ender-p: nil
234 // indent-tabs-mode: nil