]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
My IncludePage plugin. Docs at phpwiki:IncludePage. A cheap substitute for
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: IncludePage.php,v 1.1 2001-12-02 02:12:21 joe_edelman 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 getDefaultArguments() {
15         return array( 'page'  => false,    // the page to include
16                       'rev'   => false,    // the revision (defaults to most recent)
17                       'quiet' => false,    // if set, inclusion appears as normal content
18                       'words' => false,    // maximum number of words to include
19                       'lines' => false     // maximum number of lines to include
20                                         );
21     }
22
23     function firstNWordsOfContent( $n, $content ) {
24         $wordcount = 0; 
25         $new = array( );
26         foreach ($content as $line) {
27             $words = explode(' ', $line);
28             if ($wordcount + count($words) > $n) {
29                 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount)) 
30                     . "... (first $n words)";
31                 return $new;
32             } else {
33                 $wordcount += count($words);
34                 $new[] = $line;
35             }
36         }
37         return $new;
38     }
39     
40     function run($dbi, $argstr, $request) {
41         extract($this->getArgs($argstr, $request));
42         if (!$page) return '';         // FIXME:  error reporting?
43         $p = $dbi->getPage($page); 
44         
45         if ($rev) {
46             $r = $p->getRevision($rev);
47             if (!$r) return '';        // FIXME:  error reporting?
48         } else {
49             $r = $p->getCurrentRevision();
50         }
51
52         $c = $r->getContent();
53         
54         if ($lines)
55             $c = array_slice($c, 0, $lines);
56         if ($words) 
57             $c = $this->firstNWordsOfContent($words, $c);
58                     
59         $content = do_transform($c);
60         if ($quiet) return $content;
61         return 
62                  '<p class="transclusion-title">Included from '
63                . LinkExistingWikiWord($page)
64                . '</p>'
65                . '<div class="transclusion">'  . $content 
66                . '</div>';
67     }
68 };
69
70 // This is an excerpt from the css file I use:
71 //
72 // .transclusion-title {
73 //   font-style: oblique;
74 //   font-size: 0.75em;
75 //   text-decoration: underline;
76 //   text-align: right;
77 // } 
78 //
79 // DIV.transclusion { 
80 //   background: lightgreen;
81 //   border: thin;
82 //   border-style: solid;
83 //   padding-left: 0.8em; 
84 //   padding-right: 0.8em; 
85 //   padding-top: 0px;
86 //   padding-bottom: 0px;
87 //   margin: 0.5ex 0px;
88 // }
89
90 // KNOWN ISSUES:
91 //   - line & word limit doesn't work if the included page itself includes a plugin
92 //   - we need an error reporting scheme
93
94 // For emacs users
95 // Local Variables:
96 // mode: php
97 // tab-width: 8
98 // c-basic-offset: 4
99 // c-hanging-comment-ender-p: nil
100 // indent-tabs-mode: nil
101 // End:
102         
103 ?>