]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UnfoldSubpages.php
* changed stored pref representation as before.
[SourceForge/phpwiki.git] / lib / plugin / UnfoldSubpages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UnfoldSubpages.php,v 1.9 2004-01-26 09:18:00 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.9 $");
46     }
47
48     function getDefaultArguments() {
49         return array(
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
59                                 //  include
60             'smalltitle' => false, // if set, hide transclusion-title,
61                                 //  just have a small link at the start of 
62                                 //  the page.
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
72             );
73     }
74
75     // from IncludePage
76     function firstNWordsOfContent($n, $content) {
77         $wordcount = 0;
78         $new = array( );
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);
84                 return $new;
85             }
86             else {
87                 $wordcount += count($words);
88                 $new[] = $line;
89             }
90         }
91         return $new;
92     }
93
94     //TODO: move this to stdlib.php
95     function extractSection ($section, $content, $page, $quiet, $sectionhead) {
96         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
97
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
102                                                  //  or higher level)
103                                                  //  (or EOF)
104                        implode("\n", $content),
105                        $match)) {
106             // Strip trailing blanks lines and ---- <hr>s
107             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
108             if ($sectionhead)
109                 $text = $match[1] . $section ."\n". $text;
110             return explode("\n", $text);
111         }
112         if ($quiet)
113             $mesg = $page ." ". $section;
114         else
115             $mesg = $section;
116         return array(sprintf(_("<%s: no such section>"), $mesg));
117     }
118
119     function run($dbi, $argstr, $request) {
120         include_once('lib/BlockParser.php');
121         
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');
127         }
128         extract($this->getArgs($argstr, $request));
129         //TODO: explodePageList should be a PageList method.
130         $subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*',$sortby);
131         if (! $subpages ) {
132             return $this->error(_("The current page has no subpages defined."));
133         }           
134         extract($this->getArgs($argstr, $request));
135         $content = HTML();
136         // ignore the sort argument, use sortby with the + or - prefix instead
137         // default: +
138         //if ($sort != 'asc') {
139         //    $subpages = array_reverse($subpages);
140         //}
141         if ($numpages) {
142           $subpages = array_slice ($subpages, 0, $numpages);
143         }
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"),
149                                                       $page)));
150                 continue;
151             }
152             // trap any remaining nonexistant subpages
153             if ($dbi->isWikiPage($page)) {
154                 $p = $dbi->getPage($page);
155                 $r = $p->getCurrentRevision();
156                 $c = $r->getContent();
157
158                 if ($section)
159                     $c = $this->extractSection($section, $c, $page, $quiet,
160                                                $sectionhead);
161                 if ($lines)
162                     $c = array_slice($c, 0, $lines)
163                         . sprintf(_(" ... first %d lines"), $bytes);
164                 if ($words)
165                     $c = $this->firstNWordsOfContent($words, $c);
166                 if ($bytes) {
167                     if (strlen($c) > $bytes)
168                         $c = substr($c, 0, $bytes)
169                             . sprintf(_(" ... first %d bytes"), $bytes);
170                 }
171
172                 array_push($included_pages, $page);
173                 if ($smalltitle) {
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]",
177                                                 implode("\n", $c)),
178                                         $r->get('markup'), $page);
179                 }
180                 else {
181                     $ct = TransformText(implode("\n", $c), $r->get('markup'), $page);
182                 }
183                 array_pop($included_pages);
184                 if (! $smalltitle) {
185                     $content->pushContent(HTML::p(array('class' => $quiet ?
186                                                         '' : 'transclusion-title'),
187                                                   fmt("Included from %s:",
188                                                       WikiLink($page))));
189                 }
190                 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
191                                                            '' : 'transclusion'),
192                                                      false, $ct)));
193             }
194         }
195         return $content;
196     }
197 };
198
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)
203 //
204 // Revision 1.7  2003/02/21 04:12:06  dairiki
205 // Minor fixes for new cached markup.
206 //
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
209 //
210 // Revision 1.5  2003/01/18 22:11:44  carstenklapp
211 // Code cleanup:
212 // Reformatting & tabs to spaces;
213 // Added copyleft, getVersion, getDescription, rcs_id.
214 //
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;
218 // code rewrapping.
219 //
220 // Revision 1.3  2003/01/04 22:46:07  carstenklapp
221 // Workaround: when page has no subpages avoid include of nonexistant pages.
222 //
223
224 // KNOWN ISSUES:
225 // - line & word limit doesn't work if the included page itself
226 //   includes a plugin
227
228 // For emacs users
229 // Local Variables:
230 // mode: php
231 // tab-width: 8
232 // c-basic-offset: 4
233 // c-hanging-comment-ender-p: nil
234 // indent-tabs-mode: nil
235 // End:
236 ?>