]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UnfoldSubpages.php
Code cleanup:
[SourceForge/phpwiki.git] / lib / plugin / UnfoldSubpages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UnfoldSubpages.php,v 1.5 2003-01-18 22:11:44 carstenklapp 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.5 $");
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         $pagename = $request->getArg('pagename');
119         $subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*');
120         if (! $subpages) {
121             return $this->error(_("The current page has no subpages defined."));
122         }
123         include_once('lib/BlockParser.php');
124         extract($this->getArgs($argstr, $request));
125         $content = HTML();
126         foreach (array_reverse($subpages) as $page) {
127             // A page cannot include itself. Avoid doublettes.
128             static $included_pages = array();
129             if (in_array($page, $included_pages)) {
130                 $content->pushContent(HTML::p(sprintf(_("recursive inclusion of page %s ignored"),
131                                                       $page)));
132                 continue;
133             }
134             // trap any remaining nonexistant subpages
135             if ($dbi->isWikiPage($page)) {
136                 $p = $dbi->getPage($page);
137                 $r = $p->getCurrentRevision();
138                 $c = $r->getContent();
139
140                 if ($section)
141                     $c = $this->extractSection($section, $c, $page, $quiet,
142                                                $sectionhead);
143                 if ($lines)
144                     $c = array_slice($c, 0, $lines)
145                         . sprintf(_(" ... first %d lines"), $bytes);
146                 if ($words)
147                     $c = $this->firstNWordsOfContent($words, $c);
148                 if ($bytes) {
149                     if (strlen($c) > $bytes)
150                         $c = substr($c, 0, $bytes)
151                             . sprintf(_(" ... first %d bytes"), $bytes);
152                 }
153
154                 array_push($included_pages, $page);
155                 if ($smalltitle) {
156                     $pname = array_pop(explode("/", $page)); // get last subpage name
157                     // Use _("%s: %s") instead of .": ". for French punctuation
158                     $ct = TransformText(sprintf(_("%s: %s"), "[$pname|$page]",
159                                                 implode("\n", $c)),
160                                         $r->get('markup'));
161                 }
162                 else {
163                     $ct = TransformText(implode("\n", $c), $r->get('markup'));
164                 }
165                 array_pop($included_pages);
166                 if (! $smalltitle) {
167                     $content->pushContent(HTML::p(array('class' => $quiet ?
168                                                         '' : 'transclusion-title'),
169                                                   fmt("Included from %s:",
170                                                       WikiLink($page))));
171                 }
172                 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
173                                                            '' : 'transclusion'),
174                                                      false, $ct)));
175             }
176         }
177         return $content;
178     }
179 };
180
181 // $Log: not supported by cvs2svn $
182 // Revision 1.4  2003/01/05 02:37:30  carstenklapp
183 // New: Implemented 'smalltitle' argument and date sorting fix from
184 // Cuthbert Cat's sf patch 655095. Added getVersion & getDescription;
185 // code rewrapping.
186 //
187 // Revision 1.3  2003/01/04 22:46:07  carstenklapp
188 // Workaround: when page has no subpages avoid include of nonexistant pages.
189 //
190
191 // KNOWN ISSUES:
192 // - line & word limit doesn't work if the included page itself
193 //   includes a plugin
194
195 // For emacs users
196 // Local Variables:
197 // mode: php
198 // tab-width: 8
199 // c-basic-offset: 4
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil
202 // End:
203 ?>