]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
protected function extractParts
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php
2
3 /*
4  * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5  * Copyright 2008-2011 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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * IncludePage:  include text from another wiki page in this one
26  * usage:   <<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     {
35         return _("IncludePage");
36     }
37
38     function getDescription()
39     {
40         return _("Include text from another wiki page.");
41     }
42
43     function getDefaultArguments()
44     {
45         return array('page' => false, // the page to include
46             'rev' => false, // the revision (defaults to most recent)
47             'quiet' => false, // if set, inclusion appears as normal content
48             'bytes' => false, // maximum number of bytes to include
49             'words' => false, // maximum number of words to include
50             'lines' => false, // maximum number of lines to include
51             'sections' => false, // maximum number of sections 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 getWikiPageLinks($argstr, $basepage)
58     {
59         extract($this->getArgs($argstr));
60
61         if (!isset($page))
62             return false;
63         if ($page) {
64             // Expand relative page names.
65             $page = new WikiPageName($page, $basepage);
66         }
67         if (!$page or !$page->name)
68             return false;
69         return array(array('linkto' => $page->name, 'relation' => 0));
70     }
71
72     // Avoid warning in:
73     // <<IncludePages pages=<!plugin-list BackLinks page=CategoryWikiPlugin !> >>
74     function handle_plugin_args_cruft($argstr, $args)
75     {
76         return;
77     }
78
79     function run($dbi, $argstr, &$request, $basepage)
80     {
81         $args = $this->getArgs($argstr, $request);
82         extract($args);
83
84         if ($page) {
85             // Expand relative page names.
86             $page = new WikiPageName($page, $basepage);
87             $page = $page->name;
88         }
89         if (!$page) {
90             return $this->error(sprintf(_("A required argument ā€œ%sā€ is missing."), 'page'));
91         }
92
93         // A page can include itself once (this is needed, e.g.,  when editing
94         // TextFormattingRules).
95         static $included_pages = array();
96         if (in_array($page, $included_pages)) {
97             return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
98                 $page));
99         }
100
101         // Check if page exists
102         if (!($dbi->isWikiPage($page))) {
103             return $this->error(sprintf(_("Page ā€œ%sā€ does not exist."), $page));
104         }
105
106         // Check if user is allowed to get the Page.
107         if (!mayAccessPage('view', $page)) {
108             return $this->error(sprintf(_("Illegal inclusion of page %s: no read access."),
109                 $page));
110         }
111
112         $p = $dbi->getPage($page);
113         if ($rev) {
114             if (!is_whole_number($rev) or !($rev > 0)) {
115                 return $this->error(_("Error: rev must be a positive integer."));
116             }
117             $r = $p->getRevision($rev);
118             if ((!$r) || ($r->hasDefaultContents())) {
119                 return $this->error(sprintf(_("%s: no such revision %d."),
120                     $page, $rev));
121             }
122         } else {
123             $r = $p->getCurrentRevision();
124         }
125         $c = $r->getContent();
126
127         // follow redirects
128         if ((preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(\S+)\s*\?' . '>/', implode("\n", $c), $m))
129             or (preg_match('/<' . '\?plugin\s+RedirectTo\s+page=(.*?)\s*\?' . '>/', implode("\n", $c), $m))
130             or (preg_match('/<<\s*RedirectTo\s+page=(\S+)\s*>>/', implode("\n", $c), $m))
131             or (preg_match('/<<\s*RedirectTo\s+page="(.*?)"\s*>>/', implode("\n", $c), $m))
132         ) {
133             // Strip quotes (simple or double) from page name if any
134             if ((string_starts_with($m[1], "'"))
135                 or (string_starts_with($m[1], "\""))
136             ) {
137                 $m[1] = substr($m[1], 1, -1);
138             }
139             // trap recursive redirects
140             if (in_array($m[1], $included_pages)) {
141                 return $this->error(sprintf(_("Recursive inclusion of page %s ignored"),
142                     $page . ' => ' . $m[1]));
143             }
144             $page = $m[1];
145             $p = $dbi->getPage($page);
146             $r = $p->getCurrentRevision();
147             $c = $r->getContent(); // array of lines
148         }
149
150         $ct = $this->extractParts($c, $page, $args);
151
152         // exclude from expansion
153         if (preg_match('/<noinclude>.+<\/noinclude>/s', $ct)) {
154             $ct = preg_replace("/<noinclude>.+?<\/noinclude>/s", "", $ct);
155         }
156         // only in expansion
157         $ct = preg_replace("/<includeonly>(.+)<\/includeonly>/s", "\\1", $ct);
158
159         array_push($included_pages, $page);
160
161         include_once 'lib/BlockParser.php';
162         $content = TransformText($ct, $r->get('markup'), $page);
163
164         array_pop($included_pages);
165
166         if ($quiet)
167             return $content;
168
169         if ($rev) {
170             $transclusion_title = fmt("Included from %s (revision %d)", WikiLink($page), $rev);
171         } else {
172             $transclusion_title = fmt("Included from %s", WikiLink($page));
173         }
174         return HTML(HTML::p(array('class' => 'transclusion-title'), $transclusion_title),
175             HTML::div(array('class' => 'transclusion'), false, $content));
176     }
177
178     /**
179      * handles the arguments: section, sectionhead, lines, words, bytes,
180      * for UnfoldSubpages, IncludePage, ...
181      */
182     protected function extractParts($c, $pagename, $args)
183     {
184         extract($args);
185
186         if ($section) {
187             if ($sections) {
188                 $c = extractSection($section, $c, $pagename, $quiet, 1);
189             } else {
190                 $c = extractSection($section, $c, $pagename, $quiet, $sectionhead);
191             }
192         }
193         if ($sections) {
194             $c = extractSections($sections, $c, $pagename, $quiet, 1);
195         }
196         if ($lines) {
197             $c = array_slice($c, 0, $lines);
198             $c[] = sprintf(_(" ... first %d lines"), $lines);
199         }
200         if ($words) {
201             $c = firstNWordsOfContent($words, $c);
202         }
203         if ($bytes) {
204             $ct = implode("\n", $c); // one string
205             if (strlen($ct) > $bytes) {
206                 $ct = substr($c, 0, $bytes);
207                 $c = array($ct, sprintf(_(" ... first %d bytes"), $bytes));
208             }
209         }
210         $ct = implode("\n", $c); // one string
211         return $ct;
212     }
213 }
214
215 // Local Variables:
216 // mode: php
217 // tab-width: 8
218 // c-basic-offset: 4
219 // c-hanging-comment-ender-p: nil
220 // indent-tabs-mode: nil
221 // End: