]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Implemented "sections" parameter for IncludePagePlugin
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: IncludePage.php,v 1.31 2008-07-03 19:22:57 vargenau 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.31 $");
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                       'bytes'   => false, // maximum number of bytes to include
51                       'words'   => false, // maximum number of words to include
52                       'lines'   => false, // maximum number of lines to include
53                       'sections' => false, // maximum number of sections to include
54                       'section' => false, // include a named section
55                       'sectionhead' => false // when including a named section show the heading
56                       );
57     }
58
59     function getWikiPageLinks($argstr, $basepage) {
60         extract($this->getArgs($argstr));
61
62         if ($page) {
63             // Expand relative page names.
64             $page = new WikiPageName($page, $basepage);
65         }
66         if (!$page or !$page->name)
67             return false;
68         return array(array('linkto' => $page->name, 'relation' => 0));
69     }
70                 
71     function run($dbi, $argstr, &$request, $basepage) {
72         $args = $this->getArgs($argstr, $request);
73         extract($args);
74         if ($page) {
75             // Expand relative page names.
76             $page = new WikiPageName($page, $basepage);
77             $page = $page->name;
78         }
79         if (!$page) {
80             return $this->error(_("no page specified"));
81         }
82
83         // A page can include itself once (this is needed, e.g.,  when editing
84         // TextFormattingRules).
85         static $included_pages = array();
86         if (in_array($page, $included_pages)) {
87             return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
88                                         $page));
89         }
90
91         $p = $dbi->getPage($page);
92         if ($rev) {
93             $r = $p->getRevision($rev);
94             if (!$r) {
95                 return $this->error(sprintf(_("%s(%d): no such revision"),
96                                             $page, $rev));
97             }
98         } else {
99             $r = $p->getCurrentRevision();
100         }
101         $c = $r->getContent();
102         
103         // follow redirects
104         if (preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\w+)\s+\?'.'>/', 
105                        implode("\n", $c), $m))
106         {
107             // trap recursive redirects
108             if (in_array($m[1], $included_pages)) {
109                 return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
110                                                 $page.' => '.$m[1]));
111             }
112             $page = $m[1];
113             $p = $dbi->getPage($page);
114             $r = $p->getCurrentRevision();
115             $c = $r->getContent();   // array of lines
116         }
117         
118         $ct = $this->extractParts ($c, $page, $args);
119
120         array_push($included_pages, $page);
121
122         include_once('lib/BlockParser.php');
123         $content = TransformText($ct, $r->get('markup'), $page);
124
125         array_pop($included_pages);
126
127         if ($quiet)
128             return $content;
129
130         return HTML(HTML::p(array('class' => 'transclusion-title'),
131                             fmt("Included from %s", WikiLink($page))),
132
133                     HTML::div(array('class' => 'transclusion'),
134                               false, $content));
135     }
136     
137     /** 
138      * handles the arguments: section, sectionhead, lines, words, bytes,
139      * for UnfoldSubpages, IncludePage, ...
140      */
141     function extractParts ($c, $pagename, $args) {
142         extract($args);
143
144         if ($section) {
145             if ($sections) { 
146                 $c = extractSection($section, $c, $pagename, $quiet, 1);
147             } else {
148                 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
149             }
150         }
151         if ($sections) {
152             $c = extractSections($sections, $c, $pagename, $quiet, 1);
153         }
154         if ($lines) {
155             $c = array_slice($c, 0, $lines);
156             $c[] = sprintf(_(" ... first %d lines"), $lines);
157         }
158         if ($words) {
159             $c = firstNWordsOfContent($words, $c);
160         }
161         if ($bytes) {
162             $ct = implode("\n", $c); // one string
163             if (strlen($ct) > $bytes) {
164                 $ct = substr($c, 0, $bytes);
165                 $c = array($ct, sprintf(_(" ... first %d bytes"), $bytes));
166             }
167         }
168         $ct = implode("\n", $c); // one string
169         return $ct;
170     }
171 };
172
173 // This is an excerpt from the css file I use:
174 //
175 // .transclusion-title {
176 //   font-style: oblique;
177 //   font-size: 0.75em;
178 //   text-decoration: underline;
179 //   text-align: right;
180 // }
181 //
182 // DIV.transclusion {
183 //   background: lightgreen;
184 //   border: thin;
185 //   border-style: solid;
186 //   padding-left: 0.8em;
187 //   padding-right: 0.8em;
188 //   padding-top: 0px;
189 //   padding-bottom: 0px;
190 //   margin: 0.5ex 0px;
191 // }
192
193 // KNOWN ISSUES:
194 // - line & word limit doesn't work if the included page itself
195 //   includes a plugin
196
197
198 // $Log: not supported by cvs2svn $
199 // Revision 1.30  2008/07/02 17:48:01  vargenau
200 // Fix mix-up of bytes and lines
201 //
202 // Revision 1.29  2007/06/03 21:58:51  rurban
203 // Fix for Bug #1713784
204 // Includes this patch and a refactoring.
205 // RedirectTo is still not handled correctly.
206 //
207 // Revision 1.28  2006/04/17 17:28:21  rurban
208 // honor getWikiPageLinks change linkto=>relation
209 //
210 // Revision 1.27  2004/11/17 20:07:18  rurban
211 // just whitespace
212 //
213 // Revision 1.26  2004/09/25 16:35:09  rurban
214 // use stdlib firstNWordsOfContent, extractSection
215 //
216 // Revision 1.25  2004/07/08 20:30:07  rurban
217 // plugin->run consistency: request as reference, added basepage.
218 // encountered strange bug in AllPages (and the test) which destroys ->_dbi
219 //
220 // Revision 1.24  2004/02/17 12:11:36  rurban
221 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
222 //
223 // Revision 1.23  2003/03/25 21:01:52  dairiki
224 // Remove debugging cruft.
225 //
226 // Revision 1.22  2003/03/13 18:57:56  dairiki
227 // Hack so that (when using the IncludePage plugin) the including page shows
228 // up in the BackLinks of the included page.
229 //
230 // Revision 1.21  2003/02/21 04:12:06  dairiki
231 // Minor fixes for new cached markup.
232 //
233 // Revision 1.20  2003/01/18 21:41:02  carstenklapp
234 // Code cleanup:
235 // Reformatting & tabs to spaces;
236 // Added copyleft, getVersion, getDescription, rcs_id.
237 //
238
239 // For emacs users
240 // Local Variables:
241 // mode: php
242 // tab-width: 8
243 // c-basic-offset: 4
244 // c-hanging-comment-ender-p: nil
245 // indent-tabs-mode: nil
246 // End:
247 ?>