]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Consistent messages for missing required arguments
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5  * Copyright 2008-2011 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * IncludePage:  include text from another wiki page in this one
26  * usage:   <<IncludePage page=OtherPage rev=6 quiet=1 words=50 lines=6>>
27  * author:  Joe Edelman <joe@orbis-tertius.net>
28  */
29
30 class WikiPlugin_IncludePage
31 extends WikiPlugin
32 {
33     function getName() {
34         return _("IncludePage");
35     }
36
37     function getDescription() {
38         return _("Include text from another wiki page.");
39     }
40
41     function getDefaultArguments() {
42         return array( 'page'    => false, // the page to include
43                       'rev'     => false, // the revision (defaults to most recent)
44                       'quiet'   => false, // if set, inclusion appears as normal content
45                       'bytes'   => false, // maximum number of bytes to include
46                       'words'   => false, // maximum number of words to include
47                       'lines'   => false, // maximum number of lines to include
48                       'sections' => false, // maximum number of sections to include
49                       'section' => false, // include a named section
50                       'sectionhead' => false // when including a named section show the heading
51                       );
52     }
53
54     function getWikiPageLinks($argstr, $basepage) {
55         extract($this->getArgs($argstr));
56
57         if (!isset($page))
58             return false;
59         if ($page) {
60             // Expand relative page names.
61             $page = new WikiPageName($page, $basepage);
62         }
63         if (!$page or !$page->name)
64             return false;
65         return array(array('linkto' => $page->name, 'relation' => 0));
66     }
67
68     function run($dbi, $argstr, &$request, $basepage) {
69         $args = $this->getArgs($argstr, $request);
70         extract($args);
71
72         if ($page) {
73             // Expand relative page names.
74             $page = new WikiPageName($page, $basepage);
75             $page = $page->name;
76         }
77         if (!$page) {
78             return $this->error(sprintf(_("A required argument '%s' is missing."), 'page'));
79         }
80
81         // A page can include itself once (this is needed, e.g.,  when editing
82         // TextFormattingRules).
83         static $included_pages = array();
84         if (in_array($page, $included_pages)) {
85             return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
86                                         $page));
87         }
88
89         // Check if page exists
90         if (!($dbi->isWikiPage($page))) {
91             return $this->error(sprintf(_("Page '%s' does not exist"), $page));
92         }
93
94         // Check if user is allowed to get the Page.
95         if (!mayAccessPage ('view', $page)) {
96             return $this->error(sprintf(_("Illegal inclusion of page %s: no read access"),
97                                         $page));
98         }
99
100         $p = $dbi->getPage($page);
101         if ($rev) {
102             if (!is_whole_number($rev) or !($rev>0)) {
103                 return $this->error(_("Error: rev must be a positive integer."));
104             }
105             $r = $p->getRevision($rev);
106             if ((!$r) || ($r->hasDefaultContents())) {
107                 return $this->error(sprintf(_("%s: no such revision %d."),
108                                             $page, $rev));
109             }
110         } else {
111             $r = $p->getCurrentRevision();
112         }
113         $c = $r->getContent();
114
115         // follow redirects
116         if ((preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\S+)\s*\?'.'>/', implode("\n", $c), $m))
117           or (preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(.*?)\s*\?'.'>/', implode("\n", $c), $m))
118           or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $c), $m))
119           or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $c), $m)))
120         {
121             // Strip quotes (simple or double) from page name if any
122             if ((string_starts_with($m[1], "'"))
123               or (string_starts_with($m[1], "\""))) {
124                 $m[1] = substr($m[1], 1, -1);
125             }
126             // trap recursive redirects
127             if (in_array($m[1], $included_pages)) {
128                 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
129                                                 $page.' => '.$m[1]));
130             }
131             $page = $m[1];
132             $p = $dbi->getPage($page);
133             $r = $p->getCurrentRevision();
134             $c = $r->getContent();   // array of lines
135         }
136
137         $ct = $this->extractParts ($c, $page, $args);
138
139         // exclude from expansion
140         if (preg_match('/<noinclude>.+<\/noinclude>/s', $ct)) {
141             $ct = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", $ct);
142         }
143         // only in expansion
144         $ct = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1", $ct);
145
146         array_push($included_pages, $page);
147
148         include_once('lib/BlockParser.php');
149         $content = TransformText($ct, $r->get('markup'), $page);
150
151         array_pop($included_pages);
152
153         if ($quiet)
154             return $content;
155
156         if ($rev) {
157             $transclusion_title = fmt("Included from %s (revision %d)", WikiLink($page), $rev);
158         } else {
159             $transclusion_title = fmt("Included from %s", WikiLink($page));
160         }
161         return HTML(HTML::p(array('class' => 'transclusion-title'), $transclusion_title),
162                     HTML::div(array('class' => 'transclusion'), false, $content));
163     }
164
165     /**
166      * handles the arguments: section, sectionhead, lines, words, bytes,
167      * for UnfoldSubpages, IncludePage, ...
168      */
169     function extractParts ($c, $pagename, $args) {
170         extract($args);
171
172         if ($section) {
173             if ($sections) {
174                 $c = extractSection($section, $c, $pagename, $quiet, 1);
175             } else {
176                 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
177             }
178         }
179         if ($sections) {
180             $c = extractSections($sections, $c, $pagename, $quiet, 1);
181         }
182         if ($lines) {
183             $c = array_slice($c, 0, $lines);
184             $c[] = sprintf(_(" ... first %d lines"), $lines);
185         }
186         if ($words) {
187             $c = firstNWordsOfContent($words, $c);
188         }
189         if ($bytes) {
190             $ct = implode("\n", $c); // one string
191             if (strlen($ct) > $bytes) {
192                 $ct = substr($c, 0, $bytes);
193                 $c = array($ct, sprintf(_(" ... first %d bytes"), $bytes));
194             }
195         }
196         $ct = implode("\n", $c); // one string
197         return $ct;
198     }
199 };
200
201 // Local Variables:
202 // mode: php
203 // tab-width: 8
204 // c-basic-offset: 4
205 // c-hanging-comment-ender-p: nil
206 // indent-tabs-mode: nil
207 // End:
208 ?>