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