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