2 rcs_id('$Id: IncludePage.php,v 1.7 2002-01-20 08:20:28 carstenklapp Exp $');
4 * IncludePage: include text from another wiki page in this one
5 * usage: <?plugin IncludePage page=OtherPage rev=6 quiet=1 words=50 lines=6?>
6 * author: Joe Edelman <joe@orbis-tertius.net>
9 require_once('lib/transform.php');
11 class WikiPlugin_IncludePage
15 return _("IncludePage");
18 function getDefaultArguments() {
19 return array( 'page' => false, // the page to include
20 'rev' => false, // the revision (defaults to most recent)
21 'quiet' => false, // if set, inclusion appears as normal content
22 'words' => false, // maximum number of words to include
23 'lines' => false, // maximum number of lines to include
24 'section' => false // include a named section
28 function firstNWordsOfContent( $n, $content ) {
31 foreach ($content as $line) {
32 $words = explode(' ', $line);
33 if ($wordcount + count($words) > $n) {
34 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
35 . "... (first $n words)";
38 $wordcount += count($words);
45 function extractSection ($section, $content) {
46 $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
48 if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
49 . " \\s*$\\n?" // possible blank lines
50 . " ( (?: ^.*\\n? )*? )" // some lines
51 . " (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
52 implode("\n", $content),
54 // Strip trailing blanks lines and ---- <hr>s
55 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
56 return explode("\n", $text);
58 return array(sprintf(_("<%s: no such section>"), $section));
61 function error($msg) {
62 // FIXME: better error reporting?
63 trigger_error($msg, E_USER_NOTICE);
66 function run($dbi, $argstr, $request) {
68 extract($this->getArgs($argstr, $request));
71 $this->error(_("no page specified"));
75 // Bypass recursion error message for the special case when
76 // editing the TextFormattingRules page itself.
77 static $included_pages = array();
78 if ($page == $request->getArg('pagename')
79 || in_array($page, $included_pages)) {
80 if (! ($page == _("TextFormattingRules") && $quiet) ) {
81 $this->error(sprintf(_("recursive inclusion of page %s"), $page));
86 $p = $dbi->getPage($page);
89 $r = $p->getRevision($rev);
91 $this->error(sprintf(_("%s(%d): no such revision"), $page,
96 $r = $p->getCurrentRevision();
99 $c = $r->getContent();
102 $c = $this->extractSection($section, $c);
104 $c = array_slice($c, 0, $lines);
106 $c = $this->firstNWordsOfContent($words, $c);
108 array_push($included_pages, $page);
109 $content = do_transform($c);
110 array_pop($included_pages);
112 if ($quiet) return $content;
114 return Element('p', array('class' => 'transclusion-title'),
115 sprintf(_("Included from %s"),
116 LinkExistingWikiWord($page)))
117 . Element('div', array('class' => 'transclusion'),
122 // This is an excerpt from the css file I use:
124 // .transclusion-title {
125 // font-style: oblique;
126 // font-size: 0.75em;
127 // text-decoration: underline;
128 // text-align: right;
131 // DIV.transclusion {
132 // background: lightgreen;
134 // border-style: solid;
135 // padding-left: 0.8em;
136 // padding-right: 0.8em;
138 // padding-bottom: 0px;
139 // margin: 0.5ex 0px;
143 // - line & word limit doesn't work if the included page itself
145 // - we need an error reporting scheme
152 // c-hanging-comment-ender-p: nil
153 // indent-tabs-mode: nil