]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UnfoldSubpages.php
Replace tabs by spaces; remove EOL spaces
[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
32 require_once("lib/PageList.php");
33 require_once("lib/TextSearchQuery.php");
34 require_once("lib/plugin/IncludePage.php");
35
36 class WikiPlugin_UnfoldSubpages
37 extends WikiPlugin_IncludePage
38 {
39     function getName() {
40         return _("UnfoldSubpages");
41     }
42
43     function getDescription () {
44         return _("Includes the content of all SubPages of the current page.");
45     }
46
47     function getVersion() {
48         return preg_replace("/[Revision: $]/", '',
49                             "\$Revision$");
50     }
51
52     function getDefaultArguments() {
53         return array_merge
54             (
55              PageList::supportedArgs(),
56              array(
57                    'pagename' => '[pagename]', // default: current page
58                    //'header'  => '',  // expandable string
59                    'quiet'   => false, // print no header
60                    'sortby'   => '',    // [+|-]pagename, [+|-]mtime, [+|-]hits
61                    'maxpages' => false, // maximum number of pages to include (== limit)
62                    'smalltitle' => false, // if set, hide transclusion-title,
63                                            //  just have a small link at the start of
64                                             //  the page.
65                    'words'   => false,         // maximum number of words
66                                         //  per page to include
67                    'lines'   => false,         // maximum number of lines
68                                         //  per page to include
69                    'bytes'   => false,         // maximum number of bytes
70                                         //  per page to include
71                    'sections' => false, // maximum number of sections per page to include
72                    'section' => false,         // this named section per page only
73                    'sectionhead' => false // when including a named
74                                            //  section show the heading
75                    ));
76     }
77
78     function run($dbi, $argstr, &$request, $basepage) {
79         static $included_pages = false;
80         if (!$included_pages) $included_pages = array($basepage);
81
82         $args = $this->getArgs($argstr, $request);
83         extract($args);
84         $query = new TextSearchQuery($pagename . SUBPAGE_SEPARATOR . '*', true, 'glob');
85         $subpages = $dbi->titleSearch($query, $sortby, $limit, $exclude);
86         //if ($sortby)
87         //    $subpages = $subpages->applyFilters(array('sortby' => $sortby, 'limit' => $limit, 'exclude' => $exclude));
88         //$subpages = explodePageList($pagename . SUBPAGE_SEPARATOR . '*', false,
89         //                            $sortby, $limit, $exclude);
90         if (is_string($exclude) and !is_array($exclude))
91             $exclude = PageList::explodePageList($exclude, false, false, $limit);
92         $content = HTML();
93
94         include_once('lib/BlockParser.php');
95         $i = 0;
96         while ($page = $subpages->next()) {
97             $cpagename = $page->getName();
98                  if ($maxpages and ($i++ > $maxpages)) {
99                 return $content;
100             }
101             if (in_array($cpagename, $exclude))
102                     continue;
103             // A page cannot include itself. Avoid doublettes.
104             if (in_array($cpagename, $included_pages)) {
105                 $content->pushContent(HTML::p(sprintf(_("recursive inclusion of page %s ignored"),
106                                                       $cpagename)));
107                 continue;
108             }
109
110             // Check if user is allowed to get the Page.
111             if (!mayAccessPage ('view', $cpagename)) {
112                     return $this->error(sprintf(_("Illegal inclusion of page %s: no read access"),
113                     $cpagename));
114             }
115
116             // trap any remaining nonexistant subpages
117             if ($page->exists()) {
118                 $r = $page->getCurrentRevision();
119                 $c = $r->getContent();   // array of lines
120                 // follow redirects
121                 if ((preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\S+)\s*\?'.'>/', implode("\n", $c), $m))
122                   or (preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(.*?)\s*\?'.'>/', implode("\n", $c), $m))
123                   or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $c), $m))
124                   or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $c), $m)))
125                 {
126                     // Strip quotes (simple or double) from page name if any
127                     if ((string_starts_with($m[1], "'"))
128                       or (string_starts_with($m[1], "\""))) {
129                         $m[1] = substr($m[1], 1, -1);
130                     }
131                     // trap recursive redirects
132                     if (in_array($m[1], $included_pages)) {
133                             if (!$quiet)
134                             $content->pushContent(
135                                 HTML::p(sprintf(_("recursive inclusion of page %s ignored"),
136                                                 $cpagename.' => '.$m[1])));
137                         continue;
138                     }
139                         $cpagename = $m[1];
140
141                             // Check if user is allowed to get the Page.
142                             if (!mayAccessPage ('view', $cpagename)) {
143                                     return $this->error(sprintf(_("Illegal inclusion of page %s: no read access"),
144                                     $cpagename));
145                             }
146
147                         $page = $dbi->getPage($cpagename);
148                     $r = $page->getCurrentRevision();
149                     $c = $r->getContent();   // array of lines
150                 }
151
152                 // moved to IncludePage
153                 $ct = $this->extractParts ($c, $cpagename, $args);
154
155                 array_push($included_pages, $cpagename);
156                 if ($smalltitle) {
157                     $pname = array_pop(explode(SUBPAGE_SEPARATOR, $cpagename)); // get last subpage name
158                     // Use _("%s: %s") instead of .": ". for French punctuation
159                     $ct = TransformText(sprintf(_("%s: %s"), "[$pname|$cpagename]",
160                                                 $ct),
161                                         $r->get('markup'), $cpagename);
162                 }
163                 else {
164                     $ct = TransformText($ct, $r->get('markup'), $cpagename);
165                 }
166                 array_pop($included_pages);
167                 if (! $smalltitle) {
168                     $content->pushContent(HTML::p(array('class' => $quiet ?
169                                                         '' : 'transclusion-title'),
170                                                   fmt("Included from %s:",
171                                                       WikiLink($cpagename))));
172                 }
173                 $content->pushContent(HTML(HTML::div(array('class' => $quiet ?
174                                                            '' : 'transclusion'),
175                                                      false, $ct)));
176             }
177         }
178         if (! isset($cpagename)) {
179             return $this->error(sprintf(_("%s has no subpages defined."), $pagename));
180         }
181         return $content;
182     }
183 };
184
185 // For emacs users
186 // Local Variables:
187 // mode: php
188 // tab-width: 8
189 // c-basic-offset: 4
190 // c-hanging-comment-ender-p: nil
191 // indent-tabs-mode: nil
192 // End:
193 ?>