]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Check if page to include exists
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5  * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with PhpWiki; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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$");
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 (!isset($page))
63             return false;
64         if ($page) {
65             // Expand relative page names.
66             $page = new WikiPageName($page, $basepage);
67         }
68         if (!$page or !$page->name)
69             return false;
70         return array(array('linkto' => $page->name, 'relation' => 0));
71     }
72                 
73     function run($dbi, $argstr, &$request, $basepage) {
74         $args = $this->getArgs($argstr, $request);
75         extract($args);
76         if ($page) {
77             // Expand relative page names.
78             $page = new WikiPageName($page, $basepage);
79             $page = $page->name;
80         }
81         if (!$page) {
82             return $this->error(_("no page specified"));
83         }
84
85         // A page can include itself once (this is needed, e.g.,  when editing
86         // TextFormattingRules).
87         static $included_pages = array();
88         if (in_array($page, $included_pages)) {
89             return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
90                                         $page));
91         }
92
93         // Check if page exists
94         if (!($dbi->isWikiPage($page))) {
95             return $this->error(sprintf(_("Page '%s' does not exist"), $page));
96         }
97
98         // Check if user is allowed to get the Page.
99         if (!mayAccessPage ('view', $page)) {
100             return $this->error(sprintf(_("Illegal inclusion of page %s: no read access"),
101                                         $page));
102         }
103         
104         $p = $dbi->getPage($page);
105         if ($rev) {
106             $r = $p->getRevision($rev);
107             if (!$r) {
108                 return $this->error(sprintf(_("%s(%d): no such revision"),
109                                             $page, $rev));
110             }
111         } else {
112             $r = $p->getCurrentRevision();
113         }
114         $c = $r->getContent();
115         
116         // follow redirects
117         if ((preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(\S+)\s*\?'.'>/', implode("\n", $c), $m))
118           or (preg_match('/<'.'\?plugin\s+RedirectTo\s+page=(.*?)\s*\?'.'>/', implode("\n", $c), $m))
119           or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $c), $m))
120           or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $c), $m)))
121         {
122             // Strip quotes (simple or double) from page name if any
123             if ((string_starts_with($m[1], "'"))
124               or (string_starts_with($m[1], "\""))) {
125                 $m[1] = substr($m[1], 1, -1);
126             }
127             // trap recursive redirects
128             if (in_array($m[1], $included_pages)) {
129                 return $this->error(sprintf(_("recursive inclusion of page %s ignored"),
130                                                 $page.' => '.$m[1]));
131             }
132             $page = $m[1];
133             $p = $dbi->getPage($page);
134             $r = $p->getCurrentRevision();
135             $c = $r->getContent();   // array of lines
136         }
137
138         $ct = $this->extractParts ($c, $page, $args);
139
140         // exclude from expansion
141         if (preg_match('/<noinclude>.+<\/noinclude>/s', $ct)) {
142             $ct = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", $ct);
143         }
144         // only in expansion
145         $ct = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1", $ct);
146
147         array_push($included_pages, $page);
148
149         include_once('lib/BlockParser.php');
150         $content = TransformText($ct, $r->get('markup'), $page);
151
152         array_pop($included_pages);
153
154         if ($quiet)
155             return $content;
156
157         return HTML(HTML::p(array('class' => 'transclusion-title'),
158                             fmt("Included from %s", WikiLink($page))),
159
160                     HTML::div(array('class' => 'transclusion'),
161                               false, $content));
162     }
163     
164     /** 
165      * handles the arguments: section, sectionhead, lines, words, bytes,
166      * for UnfoldSubpages, IncludePage, ...
167      */
168     function extractParts ($c, $pagename, $args) {
169         extract($args);
170
171         if ($section) {
172             if ($sections) { 
173                 $c = extractSection($section, $c, $pagename, $quiet, 1);
174             } else {
175                 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
176             }
177         }
178         if ($sections) {
179             $c = extractSections($sections, $c, $pagename, $quiet, 1);
180         }
181         if ($lines) {
182             $c = array_slice($c, 0, $lines);
183             $c[] = sprintf(_(" ... first %d lines"), $lines);
184         }
185         if ($words) {
186             $c = firstNWordsOfContent($words, $c);
187         }
188         if ($bytes) {
189             $ct = implode("\n", $c); // one string
190             if (strlen($ct) > $bytes) {
191                 $ct = substr($c, 0, $bytes);
192                 $c = array($ct, sprintf(_(" ... first %d bytes"), $bytes));
193             }
194         }
195         $ct = implode("\n", $c); // one string
196         return $ct;
197     }
198 };
199
200 // This is an excerpt from the css file I use:
201 //
202 // .transclusion-title {
203 //   font-style: oblique;
204 //   font-size: 0.75em;
205 //   text-decoration: underline;
206 //   text-align: right;
207 // }
208 //
209 // DIV.transclusion {
210 //   background: lightgreen;
211 //   border: thin;
212 //   border-style: solid;
213 //   padding-left: 0.8em;
214 //   padding-right: 0.8em;
215 //   padding-top: 0px;
216 //   padding-bottom: 0px;
217 //   margin: 0.5ex 0px;
218 // }
219
220 // For emacs users
221 // Local Variables:
222 // mode: php
223 // tab-width: 8
224 // c-basic-offset: 4
225 // c-hanging-comment-ender-p: nil
226 // indent-tabs-mode: nil
227 // End:
228 ?>