]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Use new WikiPlugin::error() method to report errors.
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: IncludePage.php,v 1.18 2002-02-09 20:20:38 dairiki Exp $');
3 /**
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>
7  */
8
9 class WikiPlugin_IncludePage
10 extends WikiPlugin
11 {
12     function getName() {
13         return _("IncludePage");
14     }
15
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
23                       );
24     }
25
26     function firstNWordsOfContent( $n, $content ) {
27         $wordcount = 0;
28         $new = array( );
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)";
34                 return $new;
35             } else {
36                 $wordcount += count($words);
37                 $new[] = $line;
38             }
39         }
40         return $new;
41     }
42
43     function extractSection ($section, $content, $page, $quiet) {
44         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
45
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),
51                        $match)) {
52             // Strip trailing blanks lines and ---- <hr>s
53             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
54             return explode("\n", $text);
55         }
56         if ($quiet)
57             $mesg = $page ." ". $section;
58         else
59             $mesg = $section;
60         return array(sprintf(_("<%s: no such section>"), $mesg));
61     }
62
63     function run($dbi, $argstr, $request) {
64
65         extract($this->getArgs($argstr, $request));
66
67         if (!$page) {
68             return $this->error(_("no page specified"));
69         }
70
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));
76         }
77
78         $p = $dbi->getPage($page);
79
80         if ($rev) {
81             $r = $p->getRevision($rev);
82             if (!$r) {
83                 return $this->error(sprintf(_("%s(%d): no such revision"), $page,
84                                             $rev));
85             }
86         } else {
87             $r = $p->getCurrentRevision();
88         }
89
90         $c = $r->getContent();
91
92         if ($section)
93             $c = $this->extractSection($section, $c, $page, $quiet);
94         if ($lines)
95             $c = array_slice($c, 0, $lines);
96         if ($words)
97             $c = $this->firstNWordsOfContent($words, $c);
98
99         
100         array_push($included_pages, $page);
101
102         include_once('lib/BlockParser.php');
103         $content = TransformText(implode("\n", $c), $r->get('markup'));
104         
105         array_pop($included_pages);
106
107         if ($quiet) return $content;
108
109         return HTML(HTML::p(array('class' => 'transclusion-title'),
110                             fmt("Included from %s",
111                                 WikiLink($page))),
112                     
113                     HTML::div(array('class' => 'transclusion'),
114                               false, $content));
115     }
116 };
117
118 // This is an excerpt from the css file I use:
119 //
120 // .transclusion-title {
121 //   font-style: oblique;
122 //   font-size: 0.75em;
123 //   text-decoration: underline;
124 //   text-align: right;
125 // }
126 //
127 // DIV.transclusion {
128 //   background: lightgreen;
129 //   border: thin;
130 //   border-style: solid;
131 //   padding-left: 0.8em;
132 //   padding-right: 0.8em;
133 //   padding-top: 0px;
134 //   padding-bottom: 0px;
135 //   margin: 0.5ex 0px;
136 // }
137
138 // KNOWN ISSUES:
139 // - line & word limit doesn't work if the included page itself
140 //   includes a plugin
141
142 // For emacs users
143 // Local Variables:
144 // mode: php
145 // tab-width: 8
146 // c-basic-offset: 4
147 // c-hanging-comment-ender-p: nil
148 // indent-tabs-mode: nil
149 // End:
150 ?>