]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageType.php
Hack so that (when using the IncludePage plugin) the including page shows
[SourceForge/phpwiki.git] / lib / PageType.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageType.php,v 1.18 2003-02-21 04:18:06 dairiki Exp $');
3 /*
4  Copyright 1999, 2000, 2001, 2002, 2003 $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 require_once('lib/CachedMarkup.php');
24
25 /** A cacheable formatted wiki page.
26  */
27 class TransformedText extends CacheableMarkup {
28     /** Constructor.
29      *
30      * @param WikiDB_Page $page
31      * @param string $text  The packed page revision content.
32      * @param hash $meta    The version meta-data.
33      * @param string $type_override  For markup of page using a different
34      *        pagetype than that specified in its version meta-data.
35      */
36     function TransformedText($page, $text, $meta, $type_override=false) {
37         @$pagetype = $meta['pagetype'];
38         if ($type_override)
39             $pagetype = $type_override;
40         $this->_type = PageType::GetPageType($pagetype);
41         $this->CacheableMarkup($this->_type->transform($page, $text, $meta),
42                                $page->getName());
43     }
44
45     function getType() {
46         return $this->_type;
47     }
48 }
49
50 /**
51  * A page type descriptor.
52  *
53  * Encapsulate information about page types.
54  *
55  * Currently the only information encapsulated is how to format
56  * the specific page type.  In the future or capabilities may be
57  * added, e.g. the abilities to edit different page types (differently.)
58  *
59  * IMPORTANT NOTE: Since the whole PageType class gets stored (serialized)
60  * as of the cached marked-up page, it is important that the PageType classes
61  * not have large amounts of class data.  (No class data is even better.)
62  */
63 class PageType {
64     /**
65      * Get a page type descriptor.
66      *
67      * This is a static member function.
68      *
69      * @param string $pagetype  Name of the page type.
70      * @return PageType  An object which is a subclass of PageType.
71      */
72     function GetPageType ($name=false) {
73         if (!$name)
74             $name = 'wikitext';
75         if ($name) {
76             $class = "PageType_" . (string)$name;
77             if (class_exists($class))
78                 return new $class;
79             trigger_error(sprintf("PageType '%s' unknown", (string)$name),
80                           E_USER_WARNING);
81         }
82         return new PageType_wikitext;
83     }
84
85     /**
86      * Get the name of this page type.
87      *
88      * @return string  Page type name.
89      */
90     function getName() {
91         if (!preg_match('/^PageType_(.+)$/i', get_class($this), $m))
92             trigger_error("Bad class name for formatter(?)", E_USER_ERROR);
93         return $m[1];
94     }
95
96     /**
97      * Transform page text.
98      *
99      * @param WikiDB_Page $page
100      * @param string $text
101      * @param hash $meta Version meta-data
102      * @return XmlContent The transformed page text.
103      */
104     function transform($page, $text, $meta) {
105         $fmt_class = 'PageFormatter_' . $this->getName();
106         $formatter = new $fmt_class($page, $meta);
107         return $formatter->format($text);
108     }
109 }
110
111 class PageType_wikitext extends PageType {}
112 class PageType_wikiblog extends PageType {}
113 class PageType_interwikimap extends PageType
114 {
115     // FIXME: move code from interwikimap into here.(?)
116 }
117
118
119 /** How to transform text.
120  */
121 class PageFormatter {
122     /** Constructor.
123      *
124      * @param WikiDB_Page $page
125      * @param hash $meta Version meta-data.
126      */
127     function PageFormatter($page, $meta) {
128         $this->_page = $page;
129         $this->_meta = $meta;
130         if (!empty($meta['markup']))
131             $this->_markup = $meta['markup'];
132         else
133             $this->_markup = 1;
134     }
135
136     function _transform($text) {
137         include_once('lib/BlockParser.php');
138         return TransformText($text, $this->_markup);
139     }
140
141     /** Transform the page text.
142      *
143      * @param string $text  The raw page content (e.g. wiki-text).
144      * @return XmlContent   Transformed content.
145      */
146     function format($text) {
147         trigger_error("pure virtual", E_USER_ERROR);
148     }
149 }
150
151 class PageFormatter_wikitext extends PageFormatter 
152 {
153     function format($text) {
154         return HTML::div(array('class' => 'wikitext'),
155                          $this->_transform($text));
156     }
157 }
158
159 class PageFormatter_interwikimap extends PageFormatter
160 {
161     function format($text) {
162         return HTML::div(array('class' => 'wikitext'),
163                          $this->_transform($this->_getHeader($text)),
164                          $this->_formatMap(),
165                          $this->_transform($this->_getFooter($text)));
166     }
167
168     function _getHeader($text) {
169         return preg_replace('/<verbatim>.*/s', '', $text);
170     }
171     function _getFooter($text) {
172         return preg_replace('@.*?(</verbatim>|\Z)@s', '', $text, 1);
173     }
174
175     function _getMap() {
176         global $request;
177         // let interwiki.php get the map
178         include_once("lib/interwiki.php");
179         $map = InterWikiMap::GetMap($request);
180         return $map->_map;
181     }
182
183     function _formatMap() {
184         $map = $this->_getMap();
185         if (!$map)
186             return HTML::p("<No map found>"); // Shouldn't happen.
187
188         global $request;
189         $dbi = $request->getDbh();
190
191         $mon_attr = array('class' => 'interwiki-moniker');
192         $url_attr = array('class' => 'interwiki-url');
193         
194         $thead = HTML::thead(HTML::tr(HTML::th($mon_attr, _("Moniker")),
195                                       HTML::th($url_attr, _("InterWiki Address"))));
196         foreach ($map as $moniker => $interurl) {
197             $rows[] = HTML::tr(HTML::td($mon_attr, new Cached_WikiLinkIfKnown($moniker)),
198                                HTML::td($url_attr, HTML::tt($interurl)));
199         }
200         
201         return HTML::table(array('class' => 'interwiki-map'),
202                            $thead,
203                            HTML::tbody(false, $rows));
204     }
205 }
206
207 class FakePageRevision {
208     function FakePageRevision($meta) {
209         $this->_meta = $meta;
210     }
211
212     function get($key) {
213         if (empty($this->_meta[$key]))
214             return false;
215         return $this->_meta[$key];
216     }
217 }
218
219         
220 class PageFormatter_wikiblog extends PageFormatter
221 {
222     // Display contents:
223     function format($text) {
224         include_once('lib/Template.php');
225         global $request;
226         $tokens['CONTENT'] = $this->_transform($text);
227         $tokens['page'] = $this->_page;
228         $tokens['rev'] = new FakePageRevision($this->_meta);
229
230         $name = new WikiPageName($this->_page->getName());
231         $tokens['BLOG_PARENT'] = $name->getParent();
232
233         $blog_meta = $this->_meta['wikiblog'];
234         foreach(array('ctime', 'creator', 'creator_id') as $key)
235             $tokens["BLOG_" . strtoupper($key)] = $blog_meta[$key];
236         
237         return new Template('wikiblog', $request, $tokens);
238     }
239 }
240
241 // Local Variables:
242 // mode: php
243 // tab-width: 8
244 // c-basic-offset: 4
245 // c-hanging-comment-ender-p: nil
246 // indent-tabs-mode: nil
247 // End:
248 ?>