]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Code cleanup:
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: IncludePage.php,v 1.20 2003-01-18 21:41:02 carstenklapp Exp $');
3 /*
4  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23
24 /**
25  * IncludePage:  include text from another wiki page in this one
26  * usage:   <?plugin IncludePage page=OtherPage rev=6 quiet=1 words=50 lines=6?>
27  * author:  Joe Edelman <joe@orbis-tertius.net>
28  */
29
30 class WikiPlugin_IncludePage
31 extends WikiPlugin
32 {
33     function getName() {
34         return _("IncludePage");
35     }
36
37     function getDescription() {
38         return _("Include text from another wiki page.");
39     }
40
41     function getVersion() {
42         return preg_replace("/[Revision: $]/", '',
43                             "\$Revision: 1.20 $");
44     }
45
46     function getDefaultArguments() {
47         return array( 'page'    => false, // the page to include
48                       'rev'     => false, // the revision (defaults to most recent)
49                       'quiet'   => false, // if set, inclusion appears as normal content
50                       'words'   => false, // maximum number of words to include
51                       'lines'   => false, // maximum number of lines to include
52                       'section' => false, // include a named section
53                       'sectionhead' => false // when including a named section show the heading
54                       );
55     }
56
57     function firstNWordsOfContent( $n, $content ) {
58         $wordcount = 0;
59         $new = array( );
60         foreach ($content as $line) {
61             $words = explode(' ', $line);
62             if ($wordcount + count($words) > $n) {
63                 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
64                     . "... (first $n words)";
65                 return $new;
66             } else {
67                 $wordcount += count($words);
68                 $new[] = $line;
69             }
70         }
71         return $new;
72     }
73
74     function extractSection ($section, $content, $page, $quiet, $sectionhead) {
75         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
76
77         if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
78                        . "  \\s*$\\n?"           // possible blank lines
79                        . "  ( (?: ^.*\\n? )*? )" // some lines
80                        . "  (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
81                        implode("\n", $content),
82                        $match)) {
83             // Strip trailing blanks lines and ---- <hr>s
84             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
85             if ($sectionhead)
86                 $text = $match[1] . $section ."\n". $text;
87             return explode("\n", $text);
88         }
89         if ($quiet)
90             $mesg = $page ." ". $section;
91         else
92             $mesg = $section;
93         return array(sprintf(_("<%s: no such section>"), $mesg));
94     }
95
96     function run($dbi, $argstr, $request) {
97
98         extract($this->getArgs($argstr, $request));
99
100         if (!$page) {
101             return $this->error(_("no page specified"));
102         }
103
104         // A page can include itself once (this is needed, e.g.,  when editing
105         // TextFormattingRules).
106         static $included_pages = array();
107         if (in_array($page, $included_pages)) {
108             return $this->error(sprintf(_("recursive inclusion of page %s"),
109                                         $page));
110         }
111
112         $p = $dbi->getPage($page);
113
114         if ($rev) {
115             $r = $p->getRevision($rev);
116             if (!$r) {
117                 return $this->error(sprintf(_("%s(%d): no such revision"),
118                                             $page, $rev));
119             }
120         } else {
121             $r = $p->getCurrentRevision();
122         }
123
124         $c = $r->getContent();
125
126         if ($section)
127             $c = $this->extractSection($section, $c, $page, $quiet,
128                                        $sectionhead);
129         if ($lines)
130             $c = array_slice($c, 0, $lines);
131         if ($words)
132             $c = $this->firstNWordsOfContent($words, $c);
133
134
135         array_push($included_pages, $page);
136
137         include_once('lib/BlockParser.php');
138         $content = TransformText(implode("\n", $c), $r->get('markup'));
139
140         array_pop($included_pages);
141
142         if ($quiet)
143             return $content;
144
145         return HTML(HTML::p(array('class' => 'transclusion-title'),
146                             fmt("Included from %s", WikiLink($page))),
147
148                     HTML::div(array('class' => 'transclusion'),
149                               false, $content));
150     }
151 };
152
153 // This is an excerpt from the css file I use:
154 //
155 // .transclusion-title {
156 //   font-style: oblique;
157 //   font-size: 0.75em;
158 //   text-decoration: underline;
159 //   text-align: right;
160 // }
161 //
162 // DIV.transclusion {
163 //   background: lightgreen;
164 //   border: thin;
165 //   border-style: solid;
166 //   padding-left: 0.8em;
167 //   padding-right: 0.8em;
168 //   padding-top: 0px;
169 //   padding-bottom: 0px;
170 //   margin: 0.5ex 0px;
171 // }
172
173 // KNOWN ISSUES:
174 // - line & word limit doesn't work if the included page itself
175 //   includes a plugin
176
177
178 // $Log: not supported by cvs2svn $
179
180 // For emacs users
181 // Local Variables:
182 // mode: php
183 // tab-width: 8
184 // c-basic-offset: 4
185 // c-hanging-comment-ender-p: nil
186 // indent-tabs-mode: nil
187 // End:
188 ?>