]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php
2
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 getDescription()
34     {
35         return _("Include text from another wiki page.");
36     }
37
38     function getDefaultArguments()
39     {
40         return array('page' => false, // the page to include
41             'rev' => false, // the revision (defaults to most recent)
42             'quiet' => false, // if set, inclusion appears as normal content
43             'bytes' => false, // maximum number of bytes to include
44             'words' => false, // maximum number of words to include
45             'lines' => false, // maximum number of lines to include
46             'sections' => false, // maximum number of sections to include
47             'section' => false, // include a named section
48             'sectionhead' => false // when including a named section show the heading
49         );
50     }
51
52     function getWikiPageLinks($argstr, $basepage)
53     {
54         $args = $this->getArgs($argstr);
55         $page = $args['page'];
56
57         if ($page) {
58             // Expand relative page names.
59             $page = new WikiPageName($page, $basepage);
60         }
61         if (!$page or !$page->name)
62             return false;
63
64         global $backlinks;
65         if (empty($backlinks)) {
66             global $request;
67             $this->run($request->_dbi, $argstr, $request, $basepage);
68         }
69
70         $backlinks[] = array('linkto' => $page->name, 'relation' => 0); 
71         return $backlinks;
72     }
73
74     // Avoid warning in:
75     // <<IncludePages pages=<!plugin-list BackLinks page=CategoryWikiPlugin !> >>
76     function handle_plugin_args_cruft($argstr, $args)
77     {
78         return;
79     }
80
81     /**
82      * @param WikiDB $dbi
83      * @param string $argstr
84      * @param WikiRequest $request
85      * @param string $basepage
86      * @return mixed
87      */
88     function run($dbi, $argstr, &$request, $basepage)
89     {
90         $args = $this->getArgs($argstr, $request);
91         $page = $args['page'];
92         $rev = $args['rev'];
93         $quiet = $args['quiet'];
94
95         if ($page) {
96             // Expand relative page names.
97             $page = new WikiPageName($page, $basepage);
98             $page = $page->name;
99         }
100         if (!$page) {
101             return $this->error(sprintf(_("A required argument ā€œ%sā€ is missing."), 'page'));
102         }
103
104         // A page can include itself once (this is needed, e.g.,  when editing
105         // TextFormattingRules).
106         // Protect from recursive inclusion.
107         global $rootpage;
108         if ($rootpage == '') {
109             $rootpage = $basepage;
110         }
111         if ($page == $rootpage) {
112             return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
113                 $page));
114         }
115
116         // Check if page exists
117         if (!($dbi->isWikiPage($page))) {
118             return $this->error(sprintf(_("Page ā€œ%sā€ does not exist."), $page));
119         }
120
121         // Check if user is allowed to get the Page.
122         if (!mayAccessPage('view', $page)) {
123             return $this->error(sprintf(_("Illegal inclusion of page %s: no read access."),
124                 $page));
125         }
126
127         $p = $dbi->getPage($page);
128         if ($rev) {
129             if (!is_whole_number($rev) or !($rev > 0)) {
130                 return $this->error(_("Error: rev must be a positive integer."));
131             }
132             $r = $p->getRevision($rev);
133             if ((!$r) || ($r->hasDefaultContents())) {
134                 return $this->error(sprintf(_("%s: no such revision %d."),
135                     $page, $rev));
136             }
137         } else {
138             $r = $p->getCurrentRevision();
139         }
140         $c = $r->getContent();
141
142         // follow redirects
143         if ((preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(\S+)\s*\?' . '>/', implode("\n", $c), $m))
144             or (preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(.*?)\s*\?' . '>/', implode("\n", $c), $m))
145             or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $c), $m))
146             or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $c), $m))
147         ) {
148             // Strip quotes (simple or double) from page name if any
149             if ((string_starts_with($m[1], "'"))
150                 or (string_starts_with($m[1], "\""))
151             ) {
152                 $m[1] = substr($m[1], 1, -1);
153             }
154             // trap recursive redirects
155             if ($m[1] == $rootpage) {
156                 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
157                     $page . ' => ' . $m[1]));
158             }
159             $page = $m[1];
160             $p = $dbi->getPage($page);
161             $r = $p->getCurrentRevision();
162             $c = $r->getContent(); // array of lines
163         }
164
165         $ct = $this->extractParts($c, $page, $args);
166
167         // exclude from expansion
168         if (preg_match('/<noinclude>.+<\/noinclude>/s', $ct)) {
169             $ct = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", $ct);
170         }
171         // only in expansion
172         $ct = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1", $ct);
173
174         include_once 'lib/BlockParser.php';
175         $content = TransformText($ct, $page);
176
177         if ($quiet)
178             return $content;
179
180         if ($rev) {
181             $transclusion_title = fmt("Included from %s (revision %d)", WikiLink($page), $rev);
182         } else {
183             $transclusion_title = fmt("Included from %s", WikiLink($page));
184         }
185         return HTML(HTML::p(array('class' => 'transclusion-title'), $transclusion_title),
186             HTML::div(array('class' => 'transclusion'), false, $content));
187     }
188
189     /**
190      * handles the arguments: section, sectionhead, lines, words, bytes,
191      * for UnfoldSubpages, IncludePage, ...
192      */
193     protected function extractParts($c, $pagename, $args)
194     {
195         $section = $args['section'];
196         $sections = $args['sections'];
197         $lines = $args['lines'];
198         $words = $args['words'];
199         $bytes = $args['bytes'];
200         $quiet = $args['quiet'];
201         $sectionhead = $args['sectionhead'];
202
203         if ($section) {
204             if ($sections) {
205                 $c = extractSection($section, $c, $pagename, $quiet, 1);
206             } else {
207                 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
208             }
209         }
210         if ($sections) {
211             $c = extractSections($sections, $c, $pagename, $quiet, 1);
212         }
213         if ($lines) {
214             $c = array_slice($c, 0, $lines);
215             $c[] = sprintf(_(" ... first %d lines"), $lines);
216         }
217         if ($words) {
218             $c = firstNWordsOfContent($words, $c);
219         }
220         if ($bytes) {
221             $ct = implode("\n", $c); // one string
222             if (strlen($ct) > $bytes) {
223                 $ct = substr($c, 0, $bytes);
224                 $c = array($ct, sprintf(_(" ... first %d bytes"), $bytes));
225             }
226         }
227         $ct = implode("\n", $c); // one string
228         return $ct;
229     }
230 }
231
232 // Local Variables:
233 // mode: php
234 // tab-width: 8
235 // c-basic-offset: 4
236 // c-hanging-comment-ender-p: nil
237 // indent-tabs-mode: nil
238 // End: