]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Added plugin argument 'section', which allows one to include a particular
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: IncludePage.php,v 1.5 2001-12-17 16:40:43 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 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) {
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         return array(sprintf(_("<%s: no such section>"), $section));
59     }
60     
61                 
62     function error($msg) {
63         // FIXME: better error reporting?
64         trigger_error($msg, E_USER_NOTICE);
65     }
66     
67     function run($dbi, $argstr, $request) {
68         
69         extract($this->getArgs($argstr, $request));
70
71         if (!$page) {
72             $this->error(_("no page specified"));
73             return '';
74         }
75
76         static $included_pages = array();
77         if ($page == $request->getArg('pagename')
78             || in_array($page, $included_pages)) {
79             $this->error(sprintf(_("recursive inclusion of page %s"), $page));
80             return '';
81         }
82         
83         $p = $dbi->getPage($page); 
84         
85         if ($rev) {
86             $r = $p->getRevision($rev);
87             if (!$r) {
88                 $this->error(sprintf(_("%s(%d): no such revision"), $page, $rev));
89                 return '';
90             }
91         } else {
92             $r = $p->getCurrentRevision();
93         }
94
95         $c = $r->getContent();
96
97         if ($section)
98             $c = $this->extractSection($section, $c);
99         if ($lines)
100             $c = array_slice($c, 0, $lines);
101         if ($words) 
102             $c = $this->firstNWordsOfContent($words, $c);
103
104         array_push($included_pages, $page);
105         $content = do_transform($c);
106         array_pop($included_pages);
107         
108         if ($quiet) return $content;
109
110         return Element('p', array('class' => 'transclusion-title'),
111                        sprintf(_("Included from %s"), LinkExistingWikiWord($page)))
112             . Element('div', array('class' => 'transclusion'),
113                       $content);
114     }
115 };
116
117 // This is an excerpt from the css file I use:
118 //
119 // .transclusion-title {
120 //   font-style: oblique;
121 //   font-size: 0.75em;
122 //   text-decoration: underline;
123 //   text-align: right;
124 // } 
125 //
126 // DIV.transclusion { 
127 //   background: lightgreen;
128 //   border: thin;
129 //   border-style: solid;
130 //   padding-left: 0.8em; 
131 //   padding-right: 0.8em; 
132 //   padding-top: 0px;
133 //   padding-bottom: 0px;
134 //   margin: 0.5ex 0px;
135 // }
136
137 // KNOWN ISSUES:
138 //   - line & word limit doesn't work if the included page itself includes a plugin
139 //   - we need an error reporting scheme
140
141 // For emacs users
142 // Local Variables:
143 // mode: php
144 // tab-width: 8
145 // c-basic-offset: 4
146 // c-hanging-comment-ender-p: nil
147 // indent-tabs-mode: nil
148 // End:
149         
150 ?>