2 rcs_id('$Id: PageType.php,v 1.18 2003-02-21 04:18:06 dairiki Exp $');
4 Copyright 1999, 2000, 2001, 2002, 2003 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
23 require_once('lib/CachedMarkup.php');
25 /** A cacheable formatted wiki page.
27 class TransformedText extends CacheableMarkup {
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.
36 function TransformedText($page, $text, $meta, $type_override=false) {
37 @$pagetype = $meta['pagetype'];
39 $pagetype = $type_override;
40 $this->_type = PageType::GetPageType($pagetype);
41 $this->CacheableMarkup($this->_type->transform($page, $text, $meta),
51 * A page type descriptor.
53 * Encapsulate information about page types.
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.)
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.)
65 * Get a page type descriptor.
67 * This is a static member function.
69 * @param string $pagetype Name of the page type.
70 * @return PageType An object which is a subclass of PageType.
72 function GetPageType ($name=false) {
76 $class = "PageType_" . (string)$name;
77 if (class_exists($class))
79 trigger_error(sprintf("PageType '%s' unknown", (string)$name),
82 return new PageType_wikitext;
86 * Get the name of this page type.
88 * @return string Page type name.
91 if (!preg_match('/^PageType_(.+)$/i', get_class($this), $m))
92 trigger_error("Bad class name for formatter(?)", E_USER_ERROR);
97 * Transform page text.
99 * @param WikiDB_Page $page
100 * @param string $text
101 * @param hash $meta Version meta-data
102 * @return XmlContent The transformed page text.
104 function transform($page, $text, $meta) {
105 $fmt_class = 'PageFormatter_' . $this->getName();
106 $formatter = new $fmt_class($page, $meta);
107 return $formatter->format($text);
111 class PageType_wikitext extends PageType {}
112 class PageType_wikiblog extends PageType {}
113 class PageType_interwikimap extends PageType
115 // FIXME: move code from interwikimap into here.(?)
119 /** How to transform text.
121 class PageFormatter {
124 * @param WikiDB_Page $page
125 * @param hash $meta Version meta-data.
127 function PageFormatter($page, $meta) {
128 $this->_page = $page;
129 $this->_meta = $meta;
130 if (!empty($meta['markup']))
131 $this->_markup = $meta['markup'];
136 function _transform($text) {
137 include_once('lib/BlockParser.php');
138 return TransformText($text, $this->_markup);
141 /** Transform the page text.
143 * @param string $text The raw page content (e.g. wiki-text).
144 * @return XmlContent Transformed content.
146 function format($text) {
147 trigger_error("pure virtual", E_USER_ERROR);
151 class PageFormatter_wikitext extends PageFormatter
153 function format($text) {
154 return HTML::div(array('class' => 'wikitext'),
155 $this->_transform($text));
159 class PageFormatter_interwikimap extends PageFormatter
161 function format($text) {
162 return HTML::div(array('class' => 'wikitext'),
163 $this->_transform($this->_getHeader($text)),
165 $this->_transform($this->_getFooter($text)));
168 function _getHeader($text) {
169 return preg_replace('/<verbatim>.*/s', '', $text);
171 function _getFooter($text) {
172 return preg_replace('@.*?(</verbatim>|\Z)@s', '', $text, 1);
177 // let interwiki.php get the map
178 include_once("lib/interwiki.php");
179 $map = InterWikiMap::GetMap($request);
183 function _formatMap() {
184 $map = $this->_getMap();
186 return HTML::p("<No map found>"); // Shouldn't happen.
189 $dbi = $request->getDbh();
191 $mon_attr = array('class' => 'interwiki-moniker');
192 $url_attr = array('class' => 'interwiki-url');
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)));
201 return HTML::table(array('class' => 'interwiki-map'),
203 HTML::tbody(false, $rows));
207 class FakePageRevision {
208 function FakePageRevision($meta) {
209 $this->_meta = $meta;
213 if (empty($this->_meta[$key]))
215 return $this->_meta[$key];
220 class PageFormatter_wikiblog extends PageFormatter
223 function format($text) {
224 include_once('lib/Template.php');
226 $tokens['CONTENT'] = $this->_transform($text);
227 $tokens['page'] = $this->_page;
228 $tokens['rev'] = new FakePageRevision($this->_meta);
230 $name = new WikiPageName($this->_page->getName());
231 $tokens['BLOG_PARENT'] = $name->getParent();
233 $blog_meta = $this->_meta['wikiblog'];
234 foreach(array('ctime', 'creator', 'creator_id') as $key)
235 $tokens["BLOG_" . strtoupper($key)] = $blog_meta[$key];
237 return new Template('wikiblog', $request, $tokens);
245 // c-hanging-comment-ender-p: nil
246 // indent-tabs-mode: nil