2 rcs_id('$Id: IncludePage.php,v 1.17 2002-02-08 03:01:11 dairiki 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 class WikiPlugin_IncludePage
13 return _("IncludePage");
16 function getDefaultArguments() {
17 return array( 'page' => false, // the page to include
18 'rev' => false, // the revision (defaults to most recent)
19 'quiet' => false, // if set, inclusion appears as normal content
20 'words' => false, // maximum number of words to include
21 'lines' => false, // maximum number of lines to include
22 'section' => false // include a named section
26 function firstNWordsOfContent( $n, $content ) {
29 foreach ($content as $line) {
30 $words = explode(' ', $line);
31 if ($wordcount + count($words) > $n) {
32 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
33 . "... (first $n words)";
36 $wordcount += count($words);
43 function extractSection ($section, $content, $page, $quiet) {
44 $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
46 if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
47 . " \\s*$\\n?" // possible blank lines
48 . " ( (?: ^.*\\n? )*? )" // some lines
49 . " (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
50 implode("\n", $content),
52 // Strip trailing blanks lines and ---- <hr>s
53 $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
54 return explode("\n", $text);
57 $mesg = $page ." ". $section;
60 return array(sprintf(_("<%s: no such section>"), $mesg));
63 function error($msg) {
64 // FIXME: better error reporting?
65 trigger_error($msg, E_USER_NOTICE);
68 function run($dbi, $argstr, $request) {
70 extract($this->getArgs($argstr, $request));
73 $this->error(_("no page specified"));
77 // A page can include itself once (this is needed, e.g., when editing
78 // TextFormattingRules).
79 static $included_pages = array();
80 if (in_array($page, $included_pages)) {
81 $this->error(sprintf(_("recursive inclusion of page %s"), $page));
85 $p = $dbi->getPage($page);
88 $r = $p->getRevision($rev);
90 $this->error(sprintf(_("%s(%d): no such revision"), $page,
95 $r = $p->getCurrentRevision();
98 $c = $r->getContent();
101 $c = $this->extractSection($section, $c, $page, $quiet);
103 $c = array_slice($c, 0, $lines);
105 $c = $this->firstNWordsOfContent($words, $c);
108 array_push($included_pages, $page);
110 include_once('lib/BlockParser.php');
111 $content = TransformText(implode("\n", $c), $r->get('markup'));
113 array_pop($included_pages);
115 if ($quiet) return $content;
117 return HTML(HTML::p(array('class' => 'transclusion-title'),
118 fmt("Included from %s",
121 HTML::div(array('class' => 'transclusion'),
126 // This is an excerpt from the css file I use:
128 // .transclusion-title {
129 // font-style: oblique;
130 // font-size: 0.75em;
131 // text-decoration: underline;
132 // text-align: right;
135 // DIV.transclusion {
136 // background: lightgreen;
138 // border-style: solid;
139 // padding-left: 0.8em;
140 // padding-right: 0.8em;
142 // padding-bottom: 0px;
143 // margin: 0.5ex 0px;
147 // - line & word limit doesn't work if the included page itself
149 // - we need an error reporting scheme
156 // c-hanging-comment-ender-p: nil
157 // indent-tabs-mode: nil