]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UnfoldSubpages.php
first rough versions of new plugins:
[SourceForge/phpwiki.git] / lib / plugin / UnfoldSubpages.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UnfoldSubpages.php,v 1.1 2002-09-01 16:33:19 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 /**
25  * UnfoldSubpages:  Lists the content of all SubPages of the current page. 
26  *   This is e.g. useful for the CalendarPlugin, to see all entries at once. 
27  * Usage:   <?plugin UnfoldSubpages words=50 ?>
28  * Author:  Reini Urban <rurban@x-ray.at>
29  */
30
31 class WikiPlugin_UnfoldSubpages
32 extends WikiPlugin
33 {
34     function getName() {
35         return _("UnfoldSubpages");
36     }
37
38     function getDefaultArguments() {
39         return array( //'header'  => '',  // expandable string
40                       'quiet'   => false, // no header
41                       'sort'    => 'asc',
42                       'sortby'  => 'pagename',
43                       'pages'   => '',      // maximum number of pages to include
44                       'sections' => false,  // maximum number of sections per page to include
45                       'words'   => false,   // maximum number of words per page to include
46                       'lines'   => false,   // maximum number of lines per page to include
47                       'bytes'   => false,   // maximum number of bytes per page to include
48                       'section' => false,   // named section per page only
49                       'sectionhead' => false // when including a named section show the heading
50                       );
51     }
52
53     // from IncludePage
54     function firstNWordsOfContent( $n, $content ) {
55         $wordcount = 0;
56         $new = array( );
57         foreach ($content as $line) {
58             $words = explode(' ', $line);
59             if ($wordcount + count($words) > $n) {
60                 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
61                          . sprintf(_("... first $d words"), $n);
62                 return $new;
63             } else {
64                 $wordcount += count($words);
65                 $new[] = $line;
66             }
67         }
68         return $new;
69     }
70
71     function extractSection ($section, $content, $page, $quiet, $sectionhead) {
72         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
73
74         if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
75                        . "  \\s*$\\n?"           // possible blank lines
76                        . "  ( (?: ^.*\\n? )*? )" // some lines
77                        . "  (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
78                        implode("\n", $content),
79                        $match)) {
80             // Strip trailing blanks lines and ---- <hr>s
81             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
82             if ($sectionhead)
83                 $text = $match[1] . $section ."\n". $text;
84             return explode("\n", $text);
85         }
86         if ($quiet)
87             $mesg = $page ." ". $section;
88         else
89             $mesg = $section;
90         return array(sprintf(_("<%s: no such section>"), $mesg));
91     }
92
93     function run($dbi, $argstr, $request) {
94         $pagename = $request->getArg('pagename');
95         $subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*');
96         if (! $subpages) {
97             return $this->error(_("The current page has no subpages defined."));
98         }
99         include_once('lib/BlockParser.php');
100         extract($this->getArgs($argstr, $request));
101         $content = HTML();
102         foreach ($subpages as $page) {
103             // A page cannot include itself. Avoid doublettes.
104             static $included_pages = array();
105             if (in_array($page, $included_pages)) {
106                 $content->pushContent(HTML::p(sprintf(_("recursive inclusion of page %s ignored"), $page)));
107                 continue;
108             }
109             
110             $p = $dbi->getPage($page);
111             $r = $p->getCurrentRevision();
112             $c = $r->getContent();
113
114             if ($section)
115                 $c = $this->extractSection($section, $c, $page, $quiet, $sectionhead);
116             if ($lines)
117                 $c = array_slice($c, 0, $lines) . sprintf(_(" ... first $d lines"), $bytes);
118             if ($words)
119                 $c = $this->firstNWordsOfContent($words, $c);
120             if ($bytes) {
121                 if (strlen($c) > $bytes)
122                     $c = substr($c,0,$bytes) . sprintf(_(" ... first $d bytes"), $bytes);
123             }
124
125             array_push($included_pages, $page);
126             $ct = TransformText(implode("\n", $c), $r->get('markup'));
127             array_pop($included_pages);
128             $content->pushContent(HTML(HTML::p(array('class' => $quiet ? '' : 'transclusion-title'),
129                                                fmt("Included from %s:",WikiLink($page))),
130                                        HTML::div(array('class' => $quiet ? '' : 'transclusion'),
131                                                  false, $ct)));
132         }
133         return $content;
134     }
135 };
136
137 // KNOWN ISSUES:
138 // - line & word limit doesn't work if the included page itself
139 //   includes a plugin
140
141 // For emacs users
142 // Local Variables:
143 // mode: php
144 // tab-width: 8
145 // c-basic-offset: 4
146 // c-hanging-comment-ender-p: nil
147 // indent-tabs-mode: nil
148 // End:
149 ?>