]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/IncludePage.php
Added argument to display the section heading when including a named section from...
[SourceForge/phpwiki.git] / lib / plugin / IncludePage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: IncludePage.php,v 1.19 2002-03-02 05:40:33 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 getDefaultArguments() {
38         return array( 'page'    => false, // the page to include
39                       'rev'     => false, // the revision (defaults to most recent)
40                       'quiet'   => false, // if set, inclusion appears as normal content
41                       'words'   => false, // maximum number of words to include
42                       'lines'   => false, // maximum number of lines to include
43                       'section' => false, // include a named section
44                       'sectionhead' => false // when including a named section show the heading
45                       );
46     }
47
48     function firstNWordsOfContent( $n, $content ) {
49         $wordcount = 0;
50         $new = array( );
51         foreach ($content as $line) {
52             $words = explode(' ', $line);
53             if ($wordcount + count($words) > $n) {
54                 $new[] = implode(' ', array_slice($words, 0, $n - $wordcount))
55                          . "... (first $n words)";
56                 return $new;
57             } else {
58                 $wordcount += count($words);
59                 $new[] = $line;
60             }
61         }
62         return $new;
63     }
64
65     function extractSection ($section, $content, $page, $quiet, $sectionhead) {
66         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
67
68         if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
69                        . "  \\s*$\\n?"           // possible blank lines
70                        . "  ( (?: ^.*\\n? )*? )" // some lines
71                        . "  (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
72                        implode("\n", $content),
73                        $match)) {
74             // Strip trailing blanks lines and ---- <hr>s
75             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
76             if ($sectionhead)
77                 $text = $match[1] . $section ."\n". $text;
78             return explode("\n", $text);
79         }
80         if ($quiet)
81             $mesg = $page ." ". $section;
82         else
83             $mesg = $section;
84         return array(sprintf(_("<%s: no such section>"), $mesg));
85     }
86
87     function run($dbi, $argstr, $request) {
88
89         extract($this->getArgs($argstr, $request));
90
91         if (!$page) {
92             return $this->error(_("no page specified"));
93         }
94
95         // A page can include itself once (this is needed, e.g.,  when editing
96         // TextFormattingRules).
97         static $included_pages = array();
98         if (in_array($page, $included_pages)) {
99             return $this->error(sprintf(_("recursive inclusion of page %s"), $page));
100         }
101
102         $p = $dbi->getPage($page);
103
104         if ($rev) {
105             $r = $p->getRevision($rev);
106             if (!$r) {
107                 return $this->error(sprintf(_("%s(%d): no such revision"), $page,
108                                             $rev));
109             }
110         } else {
111             $r = $p->getCurrentRevision();
112         }
113
114         $c = $r->getContent();
115
116         if ($section)
117             $c = $this->extractSection($section, $c, $page, $quiet, $sectionhead);
118         if ($lines)
119             $c = array_slice($c, 0, $lines);
120         if ($words)
121             $c = $this->firstNWordsOfContent($words, $c);
122
123
124         array_push($included_pages, $page);
125
126         include_once('lib/BlockParser.php');
127         $content = TransformText(implode("\n", $c), $r->get('markup'));
128
129         array_pop($included_pages);
130
131         if ($quiet) return $content;
132
133         return HTML(HTML::p(array('class' => 'transclusion-title'),
134                             fmt("Included from %s",
135                                 WikiLink($page))),
136
137                     HTML::div(array('class' => 'transclusion'),
138                               false, $content));
139     }
140 };
141
142 // This is an excerpt from the css file I use:
143 //
144 // .transclusion-title {
145 //   font-style: oblique;
146 //   font-size: 0.75em;
147 //   text-decoration: underline;
148 //   text-align: right;
149 // }
150 //
151 // DIV.transclusion {
152 //   background: lightgreen;
153 //   border: thin;
154 //   border-style: solid;
155 //   padding-left: 0.8em;
156 //   padding-right: 0.8em;
157 //   padding-top: 0px;
158 //   padding-bottom: 0px;
159 //   margin: 0.5ex 0px;
160 // }
161
162 // KNOWN ISSUES:
163 // - line & word limit doesn't work if the included page itself
164 //   includes a plugin
165
166 // For emacs users
167 // Local Variables:
168 // mode: php
169 // tab-width: 8
170 // c-basic-offset: 4
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil
173 // End:
174 ?>