4 * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2011 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
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.
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>
30 class WikiPlugin_IncludePage
33 function getDescription()
35 return _("Include text from another wiki page.");
38 function getDefaultArguments()
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
52 function getWikiPageLinks($argstr, $basepage)
54 $args = $this->getArgs($argstr);
55 $page = $args['page'];
58 // Expand relative page names.
59 $page = new WikiPageName($page, $basepage);
61 if (!$page or !$page->name)
65 if (empty($backlinks)) {
67 $this->run($request->_dbi, $argstr, $request, $basepage);
70 $backlinks[] = array('linkto' => $page->name, 'relation' => 0);
75 // <<IncludePages pages=<!plugin-list BackLinks page=CategoryWikiPlugin !> >>
76 function handle_plugin_args_cruft($argstr, $args)
83 * @param string $argstr
84 * @param WikiRequest $request
85 * @param string $basepage
88 function run($dbi, $argstr, &$request, $basepage)
90 $args = $this->getArgs($argstr, $request);
91 $page = $args['page'];
93 $quiet = $args['quiet'];
96 // Expand relative page names.
97 $page = new WikiPageName($page, $basepage);
101 return $this->error(sprintf(_("A required argument ā%sā is missing."), 'page'));
104 // A page can include itself once (this is needed, e.g., when editing
105 // TextFormattingRules).
106 // Protect from recursive inclusion.
108 if ($rootpage == '') {
109 $rootpage = $basepage;
111 if ($page == $rootpage) {
112 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
116 // Check if page exists
117 if (!($dbi->isWikiPage($page))) {
118 return $this->error(sprintf(_("Page ā%sā does not exist."), $page));
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."),
127 $p = $dbi->getPage($page);
129 if (!is_whole_number($rev) or !($rev > 0)) {
130 return $this->error(_("Error: rev must be a positive integer."));
132 $r = $p->getRevision($rev);
133 if ((!$r) || ($r->hasDefaultContents())) {
134 return $this->error(sprintf(_("%s: no such revision %d."),
138 $r = $p->getCurrentRevision();
140 $c = $r->getContent();
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))
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], "\""))
152 $m[1] = substr($m[1], 1, -1);
154 // trap recursive redirects
155 if ($m[1] == $rootpage) {
156 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
157 $page . ' => ' . $m[1]));
160 $p = $dbi->getPage($page);
161 $r = $p->getCurrentRevision();
162 $c = $r->getContent(); // array of lines
165 $ct = $this->extractParts($c, $page, $args);
167 // exclude from expansion
168 if (preg_match('/<noinclude>.+<\/noinclude>/s', $ct)) {
169 $ct = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", $ct);
172 $ct = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1", $ct);
174 include_once 'lib/BlockParser.php';
175 $content = TransformText($ct, $page);
181 $transclusion_title = fmt("Included from %s (revision %d)", WikiLink($page), $rev);
183 $transclusion_title = fmt("Included from %s", WikiLink($page));
185 return HTML(HTML::p(array('class' => 'transclusion-title'), $transclusion_title),
186 HTML::div(array('class' => 'transclusion'), false, $content));
190 * handles the arguments: section, sectionhead, lines, words, bytes,
191 * for UnfoldSubpages, IncludePage, ...
193 protected function extractParts($c, $pagename, $args)
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'];
205 $c = extractSection($section, $c, $pagename, $quiet, 1);
207 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
211 $c = extractSections($sections, $c, $pagename, $quiet, 1);
214 $c = array_slice($c, 0, $lines);
215 $c[] = sprintf(_(" ... first %d lines"), $lines);
218 $c = firstNWordsOfContent($words, $c);
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));
227 $ct = implode("\n", $c); // one string
236 // c-hanging-comment-ender-p: nil
237 // indent-tabs-mode: nil