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
35 return _("IncludePage");
38 function getDescription()
40 return _("Include text from another wiki page.");
43 function getDefaultArguments()
45 return array('page' => false, // the page to include
46 'rev' => false, // the revision (defaults to most recent)
47 'quiet' => false, // if set, inclusion appears as normal content
48 'bytes' => false, // maximum number of bytes to include
49 'words' => false, // maximum number of words to include
50 'lines' => false, // maximum number of lines to include
51 'sections' => false, // maximum number of sections to include
52 'section' => false, // include a named section
53 'sectionhead' => false // when including a named section show the heading
57 function getWikiPageLinks($argstr, $basepage)
59 extract($this->getArgs($argstr));
64 // Expand relative page names.
65 $page = new WikiPageName($page, $basepage);
67 if (!$page or !$page->name)
69 return array(array('linkto' => $page->name, 'relation' => 0));
72 function run($dbi, $argstr, &$request, $basepage)
74 $args = $this->getArgs($argstr, $request);
78 // Expand relative page names.
79 $page = new WikiPageName($page, $basepage);
83 return $this->error(sprintf(_("A required argument '%s' is missing."), 'page'));
86 // A page can include itself once (this is needed, e.g., when editing
87 // TextFormattingRules).
88 static $included_pages = array();
89 if (in_array($page, $included_pages)) {
90 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
94 // Check if page exists
95 if (!($dbi->isWikiPage($page))) {
96 return $this->error(sprintf(_("Page '%s' does not exist"), $page));
99 // Check if user is allowed to get the Page.
100 if (!mayAccessPage('view', $page)) {
101 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access"),
105 $p = $dbi->getPage($page);
107 if (!is_whole_number($rev) or !($rev > 0)) {
108 return $this->error(_("Error: rev must be a positive integer."));
110 $r = $p->getRevision($rev);
111 if ((!$r) || ($r->hasDefaultContents())) {
112 return $this->error(sprintf(_("%s: no such revision %d."),
116 $r = $p->getCurrentRevision();
118 $c = $r->getContent();
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))
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], "\""))
130 $m[1] = substr($m[1], 1, -1);
132 // trap recursive redirects
133 if (in_array($m[1], $included_pages)) {
134 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
135 $page . ' => ' . $m[1]));
138 $p = $dbi->getPage($page);
139 $r = $p->getCurrentRevision();
140 $c = $r->getContent(); // array of lines
143 $ct = $this->extractParts($c, $page, $args);
145 // exclude from expansion
146 if (preg_match('/<noinclude>.+<\/noinclude>/s', $ct)) {
147 $ct = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", $ct);
150 $ct = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1", $ct);
152 array_push($included_pages, $page);
154 include_once 'lib/BlockParser.php';
155 $content = TransformText($ct, $r->get('markup'), $page);
157 array_pop($included_pages);
163 $transclusion_title = fmt("Included from %s (revision %d)", WikiLink($page), $rev);
165 $transclusion_title = fmt("Included from %s", WikiLink($page));
167 return HTML(HTML::p(array('class' => 'transclusion-title'), $transclusion_title),
168 HTML::div(array('class' => 'transclusion'), false, $content));
172 * handles the arguments: section, sectionhead, lines, words, bytes,
173 * for UnfoldSubpages, IncludePage, ...
175 function extractParts($c, $pagename, $args)
181 $c = extractSection($section, $c, $pagename, $quiet, 1);
183 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
187 $c = extractSections($sections, $c, $pagename, $quiet, 1);
190 $c = array_slice($c, 0, $lines);
191 $c[] = sprintf(_(" ... first %d lines"), $lines);
194 $c = firstNWordsOfContent($words, $c);
197 $ct = implode("\n", $c); // one string
198 if (strlen($ct) > $bytes) {
199 $ct = substr($c, 0, $bytes);
200 $c = array($ct, sprintf(_(" ... first %d bytes"), $bytes));
203 $ct = implode("\n", $c); // one string
214 // c-hanging-comment-ender-p: nil
215 // indent-tabs-mode: nil