2 rcs_id('$Id: IncludePage.php,v 1.18 2002-02-09 20:20:38 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 run($dbi, $argstr, $request) {
65 extract($this->getArgs($argstr, $request));
68 return $this->error(_("no page specified"));
71 // A page can include itself once (this is needed, e.g., when editing
72 // TextFormattingRules).
73 static $included_pages = array();
74 if (in_array($page, $included_pages)) {
75 return $this->error(sprintf(_("recursive inclusion of page %s"), $page));
78 $p = $dbi->getPage($page);
81 $r = $p->getRevision($rev);
83 return $this->error(sprintf(_("%s(%d): no such revision"), $page,
87 $r = $p->getCurrentRevision();
90 $c = $r->getContent();
93 $c = $this->extractSection($section, $c, $page, $quiet);
95 $c = array_slice($c, 0, $lines);
97 $c = $this->firstNWordsOfContent($words, $c);
100 array_push($included_pages, $page);
102 include_once('lib/BlockParser.php');
103 $content = TransformText(implode("\n", $c), $r->get('markup'));
105 array_pop($included_pages);
107 if ($quiet) return $content;
109 return HTML(HTML::p(array('class' => 'transclusion-title'),
110 fmt("Included from %s",
113 HTML::div(array('class' => 'transclusion'),
118 // This is an excerpt from the css file I use:
120 // .transclusion-title {
121 // font-style: oblique;
122 // font-size: 0.75em;
123 // text-decoration: underline;
124 // text-align: right;
127 // DIV.transclusion {
128 // background: lightgreen;
130 // border-style: solid;
131 // padding-left: 0.8em;
132 // padding-right: 0.8em;
134 // padding-bottom: 0px;
135 // margin: 0.5ex 0px;
139 // - line & word limit doesn't work if the included page itself
147 // c-hanging-comment-ender-p: nil
148 // indent-tabs-mode: nil