]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UnfoldSubpages.php
fixed explodePageList: wrong sortby argument order in UnfoldSubpages
[SourceForge/phpwiki.git] / lib / plugin / UnfoldSubpages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UnfoldSubpages.php,v 1.13 2004-03-12 15:48:08 rurban Exp $');
3 /*
4  Copyright 2002 $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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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: 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>
31  */
32 class WikiPlugin_UnfoldSubpages
33 extends WikiPlugin
34 {
35     function getName() {
36         return _("UnfoldSubpages");
37     }
38
39     function getDescription () {
40         return _("Includes the content of all SubPages of the current page.");
41     }
42
43     function getVersion() {
44         return preg_replace("/[Revision: $]/", '',
45                             "\$Revision: 1.13 $");
46     }
47
48     function getDefaultArguments() {
49         return array(
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
56             'limit'    => 0,    
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
60                                 //  include
61             'smalltitle' => false, // if set, hide transclusion-title,
62                                 //  just have a small link at the start of 
63                                 //  the page.
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
73             );
74     }
75
76     // from IncludePage
77     function firstNWordsOfContent($n, $content) {
78         $wordcount = 0;
79         $new = array( );
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);
85                 return $new;
86             }
87             else {
88                 $wordcount += count($words);
89                 $new[] = $line;
90             }
91         }
92         return $new;
93     }
94
95     //TODO: move this to stdlib.php
96     function extractSection ($section, $content, $page, $quiet, $sectionhead) {
97         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
98
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
103                                                  //  or higher level)
104                                                  //  (or EOF)
105                        implode("\n", $content),
106                        $match)) {
107             // Strip trailing blanks lines and ---- <hr>s
108             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
109             if ($sectionhead)
110                 $text = $match[1] . $section ."\n". $text;
111             return explode("\n", $text);
112         }
113         if ($quiet)
114             $mesg = $page ." ". $section;
115         else
116             $mesg = $section;
117         return array(sprintf(_("<%s: no such section>"), $mesg));
118     }
119
120     function run($dbi, $argstr, &$request, $basepage) {
121         include_once('lib/BlockParser.php');
122
123         $args = $this->getArgs($argstr, $request);
124         extract($args);
125         $subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*',false,$sortby,$limit);
126         if (! $subpages ) {
127             return $this->error(_("The current page has no subpages defined."));
128         }           
129         $content = HTML();
130         if ($maxpages) {
131           $subpages = array_slice ($subpages, 0, $maxpages);
132         }
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"),
138                                                       $page)));
139                 continue;
140             }
141             // trap any remaining nonexistant subpages
142             if ($dbi->isWikiPage($page)) {
143                 $p = $dbi->getPage($page);
144                 $r = $p->getCurrentRevision();
145                 $c = $r->getContent();
146
147                 if ($section)
148                     $c = $this->extractSection($section, $c, $page, $quiet,
149                                                $sectionhead);
150                 if ($lines)
151                     $c = array_slice($c, 0, $lines)
152                         . sprintf(_(" ... first %d lines"), $bytes);
153                 if ($words)
154                     $c = $this->firstNWordsOfContent($words, $c);
155                 if ($bytes) {
156                     if (strlen($c) > $bytes)
157                         $c = substr($c, 0, $bytes)
158                             . sprintf(_(" ... first %d bytes"), $bytes);
159                 }
160
161                 array_push($included_pages, $page);
162                 if ($smalltitle) {
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]",
166                                                 implode("\n", $c)),
167                                         $r->get('markup'), $page);
168                 }
169                 else {
170                     $ct = TransformText(implode("\n", $c), $r->get('markup'), $page);
171                 }
172                 array_pop($included_pages);
173                 if (! $smalltitle) {
174                     $content->pushContent(HTML::p(array('class' => $quiet ?
175                                                         '' : 'transclusion-title'),
176                                                   fmt("Included from %s:",
177                                                       WikiLink($page))));
178                 }
179                 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
180                                                            '' : 'transclusion'),
181                                                      false, $ct)));
182             }
183         }
184         return $content;
185     }
186 };
187
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)
195 //
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, ...)
198 //
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
204 //
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
224 //
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)
228 //
229 // Revision 1.7  2003/02/21 04:12:06  dairiki
230 // Minor fixes for new cached markup.
231 //
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
234 //
235 // Revision 1.5  2003/01/18 22:11:44  carstenklapp
236 // Code cleanup:
237 // Reformatting & tabs to spaces;
238 // Added copyleft, getVersion, getDescription, rcs_id.
239 //
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;
243 // code rewrapping.
244 //
245 // Revision 1.3  2003/01/04 22:46:07  carstenklapp
246 // Workaround: when page has no subpages avoid include of nonexistant pages.
247 //
248
249 // KNOWN ISSUES:
250 // - line & word limit doesn't work if the included page itself
251 //   includes a plugin
252
253 // For emacs users
254 // Local Variables:
255 // mode: php
256 // tab-width: 8
257 // c-basic-offset: 4
258 // c-hanging-comment-ender-p: nil
259 // indent-tabs-mode: nil
260 // End:
261 ?>