]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
add page name to error string if plugin invoked quietly
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: IncludePage.php,v 1.16 2002-02-01 04:02:13 carstenklapp 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 require_once('lib/transform.php');
10
11 class WikiPlugin_IncludePage
12 extends WikiPlugin
13 {
14     function getName() {
15         return _("IncludePage");
16     }
17
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
25                       );
26     }
27
28     function firstNWordsOfContent( $n, $content ) {
29         $wordcount = 0;
30         $new = array( );
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)";
36                 return $new;
37             } else {
38                 $wordcount += count($words);
39                 $new[] = $line;
40             }
41         }
42         return $new;
43     }
44
45     function extractSection ($section, $content, $page, $quiet) {
46         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
47
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),
53                        $match)) {
54             // Strip trailing blanks lines and ---- <hr>s
55             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
56             return explode("\n", $text);
57         }
58         if ($quiet)
59             $mesg = $page ." ". $section;
60         else
61             $mesg = $section;
62         return array(sprintf(_("<%s: no such section>"), $mesg));
63     }
64
65     function error($msg) {
66         // FIXME: better error reporting?
67         trigger_error($msg, E_USER_NOTICE);
68     }
69
70     function run($dbi, $argstr, $request) {
71
72         extract($this->getArgs($argstr, $request));
73
74         if (!$page) {
75             $this->error(_("no page specified"));
76             return '';
77         }
78
79         // A page can include itself once (this is needed, e.g.,  when editing
80         // TextFormattingRules).
81         static $included_pages = array();
82         if (in_array($page, $included_pages)) {
83             $this->error(sprintf(_("recursive inclusion of page %s"), $page));
84             return '';
85         }
86
87         $p = $dbi->getPage($page);
88
89         if ($rev) {
90             $r = $p->getRevision($rev);
91             if (!$r) {
92                 $this->error(sprintf(_("%s(%d): no such revision"), $page,
93                                      $rev));
94                 return '';
95             }
96         } else {
97             $r = $p->getCurrentRevision();
98         }
99
100         $c = $r->getContent();
101
102         if ($section)
103             $c = $this->extractSection($section, $c, $page, $quiet);
104         if ($lines)
105             $c = array_slice($c, 0, $lines);
106         if ($words)
107             $c = $this->firstNWordsOfContent($words, $c);
108
109         
110         array_push($included_pages, $page);
111
112         if ($r->get('markup') == 'new') {
113             include_once('lib/BlockParser.php');
114             $content = NewTransform(implode("\n", $c));
115         }
116         else {
117             $content = do_transform($c);
118         }
119         
120         array_pop($included_pages);
121
122         if ($quiet) return $content;
123
124         return HTML(HTML::p(array('class' => 'transclusion-title'),
125                             fmt("Included from %s",
126                                 WikiLink($page))),
127                     
128                     HTML::div(array('class' => 'transclusion'),
129                               false, $content));
130     }
131 };
132
133 // This is an excerpt from the css file I use:
134 //
135 // .transclusion-title {
136 //   font-style: oblique;
137 //   font-size: 0.75em;
138 //   text-decoration: underline;
139 //   text-align: right;
140 // }
141 //
142 // DIV.transclusion {
143 //   background: lightgreen;
144 //   border: thin;
145 //   border-style: solid;
146 //   padding-left: 0.8em;
147 //   padding-right: 0.8em;
148 //   padding-top: 0px;
149 //   padding-bottom: 0px;
150 //   margin: 0.5ex 0px;
151 // }
152
153 // KNOWN ISSUES:
154 // - line & word limit doesn't work if the included page itself
155 //   includes a plugin
156 // - we need an error reporting scheme
157
158 // For emacs users
159 // Local Variables:
160 // mode: php
161 // tab-width: 8
162 // c-basic-offset: 4
163 // c-hanging-comment-ender-p: nil
164 // indent-tabs-mode: nil
165 // End:
166 ?>