2 rcs_id('$Id: IncludePage.php,v 1.25 2004-07-08 20:30:07 rurban Exp $');
4 Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 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: $]/", '',
43 "\$Revision: 1.25 $");
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 'words' => false, // maximum number of words to include
51 'lines' => false, // maximum number of lines to include
52 'section' => false, // include a named section
53 'sectionhead' => false // when including a named section show the heading
57 function firstNWordsOfContent( $n, $content ) {
60 foreach ($content as $line) {
61 $words = explode(' ', $line);
62 if ($wordcount + count($words) > $n) {
63 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
64 . "... (first $n words)";
67 $wordcount += count($words);
74 function extractSection ($section, $content, $page, $quiet, $sectionhead) {
75 $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
77 if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
78 . " \\s*$\\n?" // possible blank lines
79 . " ( (?: ^.*\\n? )*? )" // some lines
80 . " (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
81 implode("\n", $content),
83 // Strip trailing blanks lines and ---- <hr>s
84 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
86 $text = $match[1] . $section ."\n". $text;
87 return explode("\n", $text);
90 $mesg = $page ." ". $section;
93 return array(sprintf(_("<%s: no such section>"), $mesg));
96 function getWikiPageLinks($argstr, $basepage) {
97 extract($this->getArgs($argstr));
100 // Expand relative page names.
101 $page = new WikiPageName($page, $basepage);
103 if (!$page or !$page->name)
105 return array($page->name);
108 function run($dbi, $argstr, &$request, $basepage) {
109 extract($this->getArgs($argstr, $request));
111 // Expand relative page names.
112 $page = new WikiPageName($page, $basepage);
116 return $this->error(_("no page specified"));
119 // A page can include itself once (this is needed, e.g., when editing
120 // TextFormattingRules).
121 static $included_pages = array();
122 if (in_array($page, $included_pages)) {
123 return $this->error(sprintf(_("recursive inclusion of page %s"),
127 $p = $dbi->getPage($page);
130 $r = $p->getRevision($rev);
132 return $this->error(sprintf(_("%s(%d): no such revision"),
136 $r = $p->getCurrentRevision();
139 $c = $r->getContent();
142 $c = $this->extractSection($section, $c, $page, $quiet,
145 $c = array_slice($c, 0, $lines);
147 $c = $this->firstNWordsOfContent($words, $c);
150 array_push($included_pages, $page);
152 include_once('lib/BlockParser.php');
153 $content = TransformText(implode("\n", $c), $r->get('markup'), $page);
155 array_pop($included_pages);
160 return HTML(HTML::p(array('class' => 'transclusion-title'),
161 fmt("Included from %s", WikiLink($page))),
163 HTML::div(array('class' => 'transclusion'),
168 // This is an excerpt from the css file I use:
170 // .transclusion-title {
171 // font-style: oblique;
172 // font-size: 0.75em;
173 // text-decoration: underline;
174 // text-align: right;
177 // DIV.transclusion {
178 // background: lightgreen;
180 // border-style: solid;
181 // padding-left: 0.8em;
182 // padding-right: 0.8em;
184 // padding-bottom: 0px;
185 // margin: 0.5ex 0px;
189 // - line & word limit doesn't work if the included page itself
193 // $Log: not supported by cvs2svn $
194 // Revision 1.24 2004/02/17 12:11:36 rurban
195 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
197 // Revision 1.23 2003/03/25 21:01:52 dairiki
198 // Remove debugging cruft.
200 // Revision 1.22 2003/03/13 18:57:56 dairiki
201 // Hack so that (when using the IncludePage plugin) the including page shows
202 // up in the BackLinks of the included page.
204 // Revision 1.21 2003/02/21 04:12:06 dairiki
205 // Minor fixes for new cached markup.
207 // Revision 1.20 2003/01/18 21:41:02 carstenklapp
209 // Reformatting & tabs to spaces;
210 // Added copyleft, getVersion, getDescription, rcs_id.
218 // c-hanging-comment-ender-p: nil
219 // indent-tabs-mode: nil