]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UnfoldSubpages.php
Minor fixes for new cached markup.
[SourceForge/phpwiki.git] / lib / plugin / UnfoldSubpages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UnfoldSubpages.php,v 1.7 2003-02-21 04:12:06 dairiki 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  * Usage:   <?plugin UnfoldSubpages words=50 ?>
27  * Author:  Reini Urban <rurban@x-ray.at>
28  */
29 class WikiPlugin_UnfoldSubpages
30 extends WikiPlugin
31 {
32     function getName() {
33         return _("UnfoldSubpages");
34     }
35
36     function getDescription () {
37         return _("Includes the content of all SubPages of the current page.");
38     }
39
40     function getVersion() {
41         return preg_replace("/[Revision: $]/", '',
42                             "\$Revision: 1.7 $");
43     }
44
45     function getDefaultArguments() {
46         return array(//'header'  => '',  // expandable string
47                      'quiet'   => false, // no header
48                      'sort'    => 'asc',
49                      'sortby'  => 'pagename',
50                      'pages'   => '',       // maximum number of pages
51                                             //  to include
52                      'sections' => false,   // maximum number of
53                                             //  sections per page to
54                                             //  include
55                      'smalltitle' => false, // if set, hide
56                                             //  transclusion-title,
57                                             //  just have a small link
58                                             //  at the start of the
59                                             //  page.
60                      'words'   => false,    // maximum number of words
61                                             //  per page to include
62                      'lines'   => false,    // maximum number of lines
63                                             //  per page to include
64                      'bytes'   => false,    // maximum number of bytes
65                                             //  per page to include
66                      'section' => false,    // named section per page
67                                             //  only
68                      'sectionhead' => false // when including a named
69                                             //  section show the
70                                             //  heading
71                      );
72     }
73
74     // from IncludePage
75     function firstNWordsOfContent($n, $content) {
76         $wordcount = 0;
77         $new = array( );
78         foreach ($content as $line) {
79             $words = explode(' ', $line);
80             if ($wordcount + count($words) > $n) {
81                 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
82                          . sprintf(_("... first %d words"), $n);
83                 return $new;
84             }
85             else {
86                 $wordcount += count($words);
87                 $new[] = $line;
88             }
89         }
90         return $new;
91     }
92
93     function extractSection ($section, $content, $page, $quiet, $sectionhead) {
94         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
95
96         if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
97                        . "  \\s*$\\n?"           // possible blank lines
98                        . "  ( (?: ^.*\\n? )*? )" // some lines
99                        . "  (?= ^\\1 | \\Z)/xm", // sec header (same
100                                                  //  or higher level)
101                                                  //  (or EOF)
102                        implode("\n", $content),
103                        $match)) {
104             // Strip trailing blanks lines and ---- <hr>s
105             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
106             if ($sectionhead)
107                 $text = $match[1] . $section ."\n". $text;
108             return explode("\n", $text);
109         }
110         if ($quiet)
111             $mesg = $page ." ". $section;
112         else
113             $mesg = $section;
114         return array(sprintf(_("<%s: no such section>"), $mesg));
115     }
116
117     function run($dbi, $argstr, $request) {
118         include_once('lib/BlockParser.php');
119         
120         $pagename = $request->getArg('pagename');
121         $subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*');
122         if (! $subpages) {
123             return $this->error(_("The current page has no subpages defined."));
124         }           
125         include_once('lib/BlockParser.php');
126         extract($this->getArgs($argstr, $request));
127         $content = HTML();
128         $subpages = array_reverse($subpages);
129         if($pages) {
130           $subpages = array_slice ($subpages, 0, $pages);        
131         }
132         foreach ($subpages as $page) {
133             // A page cannot include itself. Avoid doublettes.
134             static $included_pages = array();
135             if (in_array($page, $included_pages)) {
136                 $content->pushContent(HTML::p(sprintf(_("recursive inclusion of page %s ignored"),
137                                                       $page)));
138                 continue;
139             }
140             // trap any remaining nonexistant subpages
141             if ($dbi->isWikiPage($page)) {
142                 $p = $dbi->getPage($page);
143                 $r = $p->getCurrentRevision();
144                 $c = $r->getContent();
145
146                 if ($section)
147                     $c = $this->extractSection($section, $c, $page, $quiet,
148                                                $sectionhead);
149                 if ($lines)
150                     $c = array_slice($c, 0, $lines)
151                         . sprintf(_(" ... first %d lines"), $bytes);
152                 if ($words)
153                     $c = $this->firstNWordsOfContent($words, $c);
154                 if ($bytes) {
155                     if (strlen($c) > $bytes)
156                         $c = substr($c, 0, $bytes)
157                             . sprintf(_(" ... first %d bytes"), $bytes);
158                 }
159
160                 array_push($included_pages, $page);
161                 if ($smalltitle) {
162                     $pname = array_pop(explode("/", $page)); // get last subpage name
163                     // Use _("%s: %s") instead of .": ". for French punctuation
164                     $ct = TransformText(sprintf(_("%s: %s"), "[$pname|$page]",
165                                                 implode("\n", $c)),
166                                         $r->get('markup'), $page);
167                 }
168                 else {
169                     $ct = TransformText(implode("\n", $c), $r->get('markup'), $page);
170                 }
171                 array_pop($included_pages);
172                 if (! $smalltitle) {
173                     $content->pushContent(HTML::p(array('class' => $quiet ?
174                                                         '' : 'transclusion-title'),
175                                                   fmt("Included from %s:",
176                                                       WikiLink($page))));
177                 }
178                 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
179                                                            '' : 'transclusion'),
180                                                      false, $ct)));
181             }
182         }
183         return $content;
184     }
185 };
186
187 // $Log: not supported by cvs2svn $
188 // Revision 1.6  2003/02/11 09:34:34  rurban
189 // fix by Steven D. Brewer <sbrewer@bio.umass.edu> to respect the $pages argument
190 //
191 // Revision 1.5  2003/01/18 22:11:44  carstenklapp
192 // Code cleanup:
193 // Reformatting & tabs to spaces;
194 // Added copyleft, getVersion, getDescription, rcs_id.
195 //
196 // Revision 1.4  2003/01/05 02:37:30  carstenklapp
197 // New: Implemented 'smalltitle' argument and date sorting fix from
198 // Cuthbert Cat's sf patch 655095. Added getVersion & getDescription;
199 // code rewrapping.
200 //
201 // Revision 1.3  2003/01/04 22:46:07  carstenklapp
202 // Workaround: when page has no subpages avoid include of nonexistant pages.
203 //
204
205 // KNOWN ISSUES:
206 // - line & word limit doesn't work if the included page itself
207 //   includes a plugin
208
209 // For emacs users
210 // Local Variables:
211 // mode: php
212 // tab-width: 8
213 // c-basic-offset: 4
214 // c-hanging-comment-ender-p: nil
215 // indent-tabs-mode: nil
216 // End:
217 ?>