]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UnfoldSubpages.php
Activated Revision substitution for Subversion
[SourceForge/phpwiki.git] / lib / plugin / UnfoldSubpages.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
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
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: Better don't use it with non-existant sections!
27  *            The section extractor is currently quite unstable.
28  * Usage:   <?plugin UnfoldSubpages sortby=-mtime words=50 maxpages=5 ?>
29  * Author:  Reini Urban <rurban@x-ray.at>
30  * 
31  * Todo: follow RedirectTo
32  */
33
34 require_once("lib/PageList.php");
35 require_once("lib/TextSearchQuery.php");
36 require_once("lib/plugin/IncludePage.php");
37
38 class WikiPlugin_UnfoldSubpages
39 extends WikiPlugin_IncludePage
40 {
41     function getName() {
42         return _("UnfoldSubpages");
43     }
44
45     function getDescription () {
46         return _("Includes the content of all SubPages of the current page.");
47     }
48
49     function getVersion() {
50         return preg_replace("/[Revision: $]/", '',
51                             "\$Revision$");
52     }
53
54     function getDefaultArguments() {
55         return array_merge
56             (
57              PageList::supportedArgs(),
58              array(
59                    'pagename' => '[pagename]', // default: current page
60                    //'header'  => '',  // expandable string
61                    'quiet'   => false, // print no header
62                    'sortby'   => '',    // [+|-]pagename, [+|-]mtime, [+|-]hits
63                    'maxpages' => false, // maximum number of pages to include (== limit)
64                    'smalltitle' => false, // if set, hide transclusion-title,
65                                         //  just have a small link at the start of 
66                                         //  the page.
67                    'words'   => false,  // maximum number of words
68                                         //  per page to include
69                    'lines'   => false,  // maximum number of lines
70                                         //  per page to include
71                    'bytes'   => false,  // maximum number of bytes
72                                         //  per page to include
73                    'sections' => false, // maximum number of sections per page to include
74                    'section' => false,  // this named section per page only
75                    'sectionhead' => false // when including a named
76                                         //  section show the heading
77                    ));
78     }
79
80     function run($dbi, $argstr, &$request, $basepage) {
81         static $included_pages = false;
82         if (!$included_pages) $included_pages = array($basepage);
83         
84         $args = $this->getArgs($argstr, $request);
85         extract($args);
86         $query = new TextSearchQuery($pagename . SUBPAGE_SEPARATOR . '*', true, 'glob');
87         $subpages = $dbi->titleSearch($query, $sortby, $limit, $exclude);
88         //if ($sortby)
89         //    $subpages = $subpages->applyFilters(array('sortby' => $sortby, 'limit' => $limit, 'exclude' => $exclude));
90         //$subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*', false, 
91         //                            $sortby, $limit, $exclude);
92         if (is_string($exclude) and !is_array($exclude))
93             $exclude = PageList::explodePageList($exclude, false, false, $limit);
94         $content = HTML();
95
96         include_once('lib/BlockParser.php');
97         $i = 0;
98         while ($page = $subpages->next()) {
99             $cpagename = $page->getName();
100             if ($maxpages and ($i++ > $maxpages)) {
101                 return $content;
102             }
103             if (in_array($cpagename, $exclude))
104                 continue;
105             // A page cannot include itself. Avoid doublettes.
106             if (in_array($cpagename, $included_pages)) {
107                 $content->pushContent(HTML::p(sprintf(_("recursive inclusion of page %s ignored"),
108                                                       $cpagename)));
109                 continue;
110             }
111             // trap any remaining nonexistant subpages
112             if ($page->exists()) {
113                 $r = $page->getCurrentRevision();
114                 $c = $r->getContent();   // array of lines
115                 // follow redirects
116                 if (preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\w+)\s+\?'.'>/', 
117                                implode("\n", $c), $m)) 
118                 {
119                     // trap recursive redirects
120                     if (in_array($m[1], $included_pages)) {
121                         if (!$quiet)
122                             $content->pushContent(
123                                 HTML::p(sprintf(_("recursive inclusion of page %s ignored"),
124                                                 $cpagename.' => '.$m[1])));
125                         continue;
126                     }
127                     $cpagename = $m[1];
128                     $page = $dbi->getPage($cpagename);
129                     $r = $page->getCurrentRevision();
130                     $c = $r->getContent();   // array of lines
131                 }
132
133                 // moved to IncludePage
134                 $ct = $this->extractParts ($c, $cpagename, $args);
135
136                 array_push($included_pages, $cpagename);
137                 if ($smalltitle) {
138                     $pname = array_pop(explode(SUBPAGE_SEPARATOR, $cpagename)); // get last subpage name
139                     // Use _("%s: %s") instead of .": ". for French punctuation
140                     $ct = TransformText(sprintf(_("%s: %s"), "[$pname|$cpagename]",
141                                                 $ct),
142                                         $r->get('markup'), $cpagename);
143                 }
144                 else {
145                     $ct = TransformText($ct, $r->get('markup'), $cpagename);
146                 }
147                 array_pop($included_pages);
148                 if (! $smalltitle) {
149                     $content->pushContent(HTML::p(array('class' => $quiet ?
150                                                         '' : 'transclusion-title'),
151                                                   fmt("Included from %s:",
152                                                       WikiLink($cpagename))));
153                 }
154                 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
155                                                            '' : 'transclusion'),
156                                                      false, $ct)));
157             }
158         }
159         if (! $cpagename ) {
160             return $this->error(sprintf(_("%s has no subpages defined."), $pagename));
161         }
162         return $content;
163     }
164 };
165
166 // $Log: not supported by cvs2svn $
167 // Revision 1.21  2005/09/11 13:20:07  rurban
168 // use TitleSearch and iterators instead of get_all_pages
169 //
170 // Revision 1.20  2005/04/11 19:45:17  rurban
171 // proper linebreaks
172 //
173 // Revision 1.19  2005/01/21 14:12:48  rurban
174 // clarify $ct
175 //
176 // Revision 1.18  2004/12/06 19:50:05  rurban
177 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
178 // renamed delete_page to purge_page.
179 // enable action=edit&version=-1 to force creation of a new version.
180 // added BABYCART_PATH config
181 // fixed magiqc in adodb.inc.php
182 // and some more docs
183 //
184 // Revision 1.17  2004/11/23 15:17:19  rurban
185 // better support for case_exact search (not caseexact for consistency),
186 // plugin args simplification:
187 //   handle and explode exclude and pages argument in WikiPlugin::getArgs
188 //     and exclude in advance (at the sql level if possible)
189 //   handle sortby and limit from request override in WikiPlugin::getArgs
190 // ListSubpages: renamed pages to maxpages
191 //
192 // Revision 1.16  2004/09/25 16:35:09  rurban
193 // use stdlib firstNWordsOfContent, extractSection
194 //
195 // Revision 1.15  2004/07/03 14:48:18  rurban
196 // Tested new mysql 4.1.3-beta: binary search bug as fixed.
197 // => fixed action=upgrade,
198 // => version check in PearDB also (as in ADODB)
199 //
200 // Revision 1.14  2004/07/03 08:19:40  rurban
201 // trap recursive redirects
202 //
203 // Revision 1.13  2004/03/12 15:48:08  rurban
204 // fixed explodePageList: wrong sortby argument order in UnfoldSubpages
205 // simplified lib/stdlib.php:explodePageList
206 //
207 // Revision 1.12  2004/02/22 23:20:33  rurban
208 // fixed DumpHtmlToDir,
209 // enhanced sortby handling in PageList
210 //   new button_heading th style (enabled),
211 // added sortby and limit support to the db backends and plugins
212 //   for paging support (<<prev, next>> links on long lists)
213 //
214 // Revision 1.11  2004/02/17 12:11:36  rurban
215 // 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, ...)
216 //
217 // Revision 1.10  2004/01/27 12:10:45  rurban
218 // fixed UnfoldSubpages and added docs.
219 // new arguments: pagename, maxpages
220 // some arguments are deprecated: sort (use sortby), pages (use maxpages)
221 // fixed sortby, added docs
222 //
223 // Revision 1.9  2004/01/26 09:18:00  rurban
224 // * changed stored pref representation as before.
225 //   the array of objects is 1) bigger and 2)
226 //   less portable. If we would import packed pref
227 //   objects and the object definition was changed, PHP would fail.
228 //   This doesn't happen with an simple array of non-default values.
229 // * use $prefs->retrieve and $prefs->store methods, where retrieve
230 //   understands the interim format of array of objects also.
231 // * simplified $prefs->get() and fixed $prefs->set()
232 // * added $user->_userid and class '_WikiUser' portability functions
233 // * fixed $user object ->_level upgrading, mostly using sessions.
234 //   this fixes yesterdays problems with loosing authorization level.
235 // * fixed WikiUserNew::checkPass to return the _level
236 // * fixed WikiUserNew::isSignedIn
237 // * added explodePageList to class PageList, support sortby arg
238 // * fixed UserPreferences for WikiUserNew
239 // * fixed WikiPlugin for empty defaults array
240 // * UnfoldSubpages: added pagename arg, renamed pages arg,
241 //   removed sort arg, support sortby arg
242 //
243 // Revision 1.8  2004/01/25 10:52:16  rurban
244 // added sortby support to explodePageList() and UnfoldSubpages
245 // fixes [ 758044 ] Plugin UnfoldSubpages does not sort (includes fix)
246 //
247 // Revision 1.7  2003/02/21 04:12:06  dairiki
248 // Minor fixes for new cached markup.
249 //
250 // Revision 1.6  2003/02/11 09:34:34  rurban
251 // fix by Steven D. Brewer <sbrewer@bio.umass.edu> to respect the $pages argument
252 //
253 // Revision 1.5  2003/01/18 22:11:44  carstenklapp
254 // Code cleanup:
255 // Reformatting & tabs to spaces;
256 // Added copyleft, getVersion, getDescription, rcs_id.
257 //
258 // Revision 1.4  2003/01/05 02:37:30  carstenklapp
259 // New: Implemented 'smalltitle' argument and date sorting fix from
260 // Cuthbert Cat's sf patch 655095. Added getVersion & getDescription;
261 // code rewrapping.
262 //
263 // Revision 1.3  2003/01/04 22:46:07  carstenklapp
264 // Workaround: when page has no subpages avoid include of nonexistant pages.
265 //
266
267 // KNOWN ISSUES:
268 // - line & word limit doesn't work if the included page itself
269 //   includes a plugin
270
271 // For emacs users
272 // Local Variables:
273 // mode: php
274 // tab-width: 8
275 // c-basic-offset: 4
276 // c-hanging-comment-ender-p: nil
277 // indent-tabs-mode: nil
278 // End:
279 ?>