]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Refactor $WikiPlugin::name and $WikiPlugin::description stuff.
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: IncludePage.php,v 1.4 2001-12-16 18:33:25 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                                         );
25     }
26
27     function firstNWordsOfContent( $n, $content ) {
28         $wordcount = 0; 
29         $new = array( );
30         foreach ($content as $line) {
31             $words = explode(' ', $line);
32             if ($wordcount + count($words) > $n) {
33                 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount)) 
34                     . "... (first $n words)";
35                 return $new;
36             } else {
37                 $wordcount += count($words);
38                 $new[] = $line;
39             }
40         }
41         return $new;
42     }
43
44     function error($msg) {
45         // FIXME: better error reporting?
46         trigger_error($msg, E_USER_NOTICE);
47     }
48     
49     function run($dbi, $argstr, $request) {
50         
51         extract($this->getArgs($argstr, $request));
52
53         if (!$page) {
54             $this->error(_("no page specified"));
55             return '';
56         }
57
58         static $included_pages = array();
59         if ($page == $request->getArg('pagename')
60             || in_array($page, $included_pages)) {
61             $this->error(sprintf(_("recursive inclusion of page %s"), $page));
62             return '';
63         }
64         
65         $p = $dbi->getPage($page); 
66         
67         if ($rev) {
68             $r = $p->getRevision($rev);
69             if (!$r) {
70                 $this->error(sprintf(_("%s(%d): no such revision"), $page, $rev));
71                 return '';
72             }
73         } else {
74             $r = $p->getCurrentRevision();
75         }
76
77         $c = $r->getContent();
78         
79         if ($lines)
80             $c = array_slice($c, 0, $lines);
81         if ($words) 
82             $c = $this->firstNWordsOfContent($words, $c);
83
84         array_push($included_pages, $page);
85         $content = do_transform($c);
86         array_pop($included_pages);
87         
88         if ($quiet) return $content;
89
90         return Element('p', array('class' => 'transclusion-title'),
91                        sprintf(_("Included from %s"), LinkExistingWikiWord($page)))
92             . Element('div', array('class' => 'transclusion'),
93                       $content);
94     }
95 };
96
97 // This is an excerpt from the css file I use:
98 //
99 // .transclusion-title {
100 //   font-style: oblique;
101 //   font-size: 0.75em;
102 //   text-decoration: underline;
103 //   text-align: right;
104 // } 
105 //
106 // DIV.transclusion { 
107 //   background: lightgreen;
108 //   border: thin;
109 //   border-style: solid;
110 //   padding-left: 0.8em; 
111 //   padding-right: 0.8em; 
112 //   padding-top: 0px;
113 //   padding-bottom: 0px;
114 //   margin: 0.5ex 0px;
115 // }
116
117 // KNOWN ISSUES:
118 //   - line & word limit doesn't work if the included page itself includes a plugin
119 //   - we need an error reporting scheme
120
121 // For emacs users
122 // Local Variables:
123 // mode: php
124 // tab-width: 8
125 // c-basic-offset: 4
126 // c-hanging-comment-ender-p: nil
127 // indent-tabs-mode: nil
128 // End:
129         
130 ?>