4 Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5 Copyright 2008-2009 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
20 along with PhpWiki; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * IncludePage: include text from another wiki page in this one
26 * usage: <?plugin IncludePage page=OtherPage rev=6 quiet=1 words=50 lines=6?>
27 * author: Joe Edelman <joe@orbis-tertius.net>
30 class WikiPlugin_IncludePage
34 return _("IncludePage");
37 function getDescription() {
38 return _("Include text from another wiki page.");
41 function getVersion() {
42 return preg_replace("/[Revision: $]/", '',
46 function getDefaultArguments() {
47 return array( 'page' => false, // the page to include
48 'rev' => false, // the revision (defaults to most recent)
49 'quiet' => false, // if set, inclusion appears as normal content
50 'bytes' => false, // maximum number of bytes to include
51 'words' => false, // maximum number of words to include
52 'lines' => false, // maximum number of lines to include
53 'sections' => false, // maximum number of sections to include
54 'section' => false, // include a named section
55 'sectionhead' => false // when including a named section show the heading
59 function getWikiPageLinks($argstr, $basepage) {
60 extract($this->getArgs($argstr));
65 // Expand relative page names.
66 $page = new WikiPageName($page, $basepage);
68 if (!$page or !$page->name)
70 return array(array('linkto' => $page->name, 'relation' => 0));
73 function run($dbi, $argstr, &$request, $basepage) {
74 $args = $this->getArgs($argstr, $request);
77 // Expand relative page names.
78 $page = new WikiPageName($page, $basepage);
82 return $this->error(_("no page specified"));
85 // A page can include itself once (this is needed, e.g., when editing
86 // TextFormattingRules).
87 static $included_pages = array();
88 if (in_array($page, $included_pages)) {
89 return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
93 // Check if user is allowed to get the Page.
94 if (!mayAccessPage ('view', $page)) {
95 return $this->error(sprintf(_("Illegal inclusion of page %s: no read access"),
99 $p = $dbi->getPage($page);
101 $r = $p->getRevision($rev);
103 return $this->error(sprintf(_("%s(%d): no such revision"),
107 $r = $p->getCurrentRevision();
109 $c = $r->getContent();
112 if (preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\w+)\s+\?'.'>/',
113 implode("\n", $c), $m))
115 // trap recursive redirects
116 if (in_array($m[1], $included_pages)) {
117 return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
118 $page.' => '.$m[1]));
121 $p = $dbi->getPage($page);
122 $r = $p->getCurrentRevision();
123 $c = $r->getContent(); // array of lines
126 $ct = $this->extractParts ($c, $page, $args);
128 // exclude from expansion
129 if (preg_match('/<noinclude>.+<\/noinclude>/s', $ct)) {
130 $ct = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", $ct);
133 $ct = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1", $ct);
135 array_push($included_pages, $page);
137 include_once('lib/BlockParser.php');
138 $content = TransformText($ct, $r->get('markup'), $page);
140 array_pop($included_pages);
145 return HTML(HTML::p(array('class' => 'transclusion-title'),
146 fmt("Included from %s", WikiLink($page))),
148 HTML::div(array('class' => 'transclusion'),
153 * handles the arguments: section, sectionhead, lines, words, bytes,
154 * for UnfoldSubpages, IncludePage, ...
156 function extractParts ($c, $pagename, $args) {
161 $c = extractSection($section, $c, $pagename, $quiet, 1);
163 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
167 $c = extractSections($sections, $c, $pagename, $quiet, 1);
170 $c = array_slice($c, 0, $lines);
171 $c[] = sprintf(_(" ... first %d lines"), $lines);
174 $c = firstNWordsOfContent($words, $c);
177 $ct = implode("\n", $c); // one string
178 if (strlen($ct) > $bytes) {
179 $ct = substr($c, 0, $bytes);
180 $c = array($ct, sprintf(_(" ... first %d bytes"), $bytes));
183 $ct = implode("\n", $c); // one string
188 // This is an excerpt from the css file I use:
190 // .transclusion-title {
191 // font-style: oblique;
192 // font-size: 0.75em;
193 // text-decoration: underline;
194 // text-align: right;
197 // DIV.transclusion {
198 // background: lightgreen;
200 // border-style: solid;
201 // padding-left: 0.8em;
202 // padding-right: 0.8em;
204 // padding-bottom: 0px;
205 // margin: 0.5ex 0px;
213 // c-hanging-comment-ender-p: nil
214 // indent-tabs-mode: nil