2 rcs_id('$Id: PageType.php,v 1.41 2005-02-02 19:34:09 rurban Exp $');
4 Copyright 1999,2000,2001,2002,2003,2004,2005 $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) {
39 $pagetype = $type_override;
40 elseif (isset($meta['pagetype']))
41 $pagetype = $meta['pagetype'];
42 $this->_type = PageType::GetPageType($pagetype);
43 $this->CacheableMarkup($this->_type->transform($page, $text, $meta),
53 * A page type descriptor.
55 * Encapsulate information about page types.
57 * Currently the only information encapsulated is how to format
58 * the specific page type. In the future or capabilities may be
59 * added, e.g. the abilities to edit different page types (differently.)
60 * e.g. Support for the javascript htmlarea editor, which can only edit
63 * IMPORTANT NOTE: Since the whole PageType class gets stored (serialized)
64 * as of the cached marked-up page, it is important that the PageType classes
65 * not have large amounts of class data. (No class data is even better.)
69 * Get a page type descriptor.
71 * This is a static member function.
73 * @param string $pagetype Name of the page type.
74 * @return PageType An object which is a subclass of PageType.
76 function GetPageType ($name=false) {
79 $class = "PageType_" . (string)$name;
80 if (class_exists($class))
82 trigger_error(sprintf("PageType '%s' unknown", (string)$name),
84 return new PageType_wikitext;
88 * Get the name of this page type.
90 * @return string Page type name.
93 if (!preg_match('/^PageType_(.+)$/i', get_class($this), $m))
94 trigger_error("Bad class name for formatter(?)", E_USER_ERROR);
99 * Transform page text.
101 * @param WikiDB_Page $page
102 * @param string $text
103 * @param hash $meta Version meta-data
104 * @return XmlContent The transformed page text.
106 function transform(&$page, &$text, $meta) {
107 $fmt_class = 'PageFormatter_' . $this->getName();
108 $formatter = new $fmt_class($page, $meta);
109 return $formatter->format($text);
113 class PageType_wikitext extends PageType {}
114 class PageType_html extends PageType {}
115 class PageType_pdf extends PageType {}
117 class PageType_wikiblog extends PageType {}
118 class PageType_comment extends PageType {}
119 class PageType_wikiforum extends PageType {}
121 /* To prevent from PHP5 Fatal error: Using $this when not in object context */
122 function getInterwikiMap ($pagetext = false) {
123 $map = new PageType_interwikimap($pagetext);
127 class PageType_interwikimap extends PageType
129 function PageType_interwikimap($pagetext = false) {
131 $dbi = $GLOBALS['request']->getDbh();
132 $page = $dbi->getPage(_("InterWikiMap"));
133 if ($page->get('locked')) {
134 $current = $page->getCurrentRevision();
135 $pagetext = $current->getPackedContent();
136 $intermap = $this->_getMapFromWikiText($pagetext);
137 } elseif ($page->exists()) {
138 trigger_error(_("WARNING: InterWikiMap page is unlocked, so not using those links."));
144 $intermap = $this->_getMapFromWikiText($pagetext);
146 if (!$intermap && defined('INTERWIKI_MAP_FILE'))
147 $intermap = $this->_getMapFromFile(INTERWIKI_MAP_FILE);
149 $this->_map = $this->_parseMap($intermap);
150 $this->_regexp = $this->_getRegexp();
153 function GetMap ($pagetext = false) {
154 /*PHP5 Fatal error: Using $this when not in object context */
155 if (empty($this->_map)) {
156 $map = new PageType_interwikimap($pagetext);
163 function getRegexp() {
164 return $this->_regexp;
167 function link ($link, $linktext = false) {
168 list ($moniker, $page) = split (":", $link, 2);
170 if (!isset($this->_map[$moniker])) {
171 return HTML::span(array('class' => 'bad-interwiki'),
172 $linktext ? $linktext : $link);
175 $url = $this->_map[$moniker];
177 // Urlencode page only if it's a query arg.
178 // FIXME: this is a somewhat broken heuristic.
179 $page_enc = strstr($url, '?') ? rawurlencode($page) : $page;
181 if (strstr($url, '%s'))
182 $url = sprintf($url, $page_enc);
186 $link = HTML::a(array('href' => $url));
189 $link->pushContent(PossiblyGlueIconToText('interwiki', "$moniker:"),
190 HTML::span(array('class' => 'wikipage'), $page));
191 $link->setAttr('class', 'interwiki');
194 $link->pushContent(PossiblyGlueIconToText('interwiki', $linktext));
195 $link->setAttr('class', 'named-interwiki');
202 function _parseMap ($text) {
203 if (!preg_match_all("/^\s*(\S+)\s+(\S+)/m",
204 $text, $matches, PREG_SET_ORDER))
207 foreach ($matches as $m) {
211 // Add virtual monikers: Upload:, Talk:, User:
212 if (empty($map['Upload']))
213 $map['Upload'] = getUploadDataPath();
214 if (empty($map["Talk"])) {
215 $pagename = $GLOBALS['request']->getArg('pagename');
216 if (string_ends_with($pagename, SUBPAGE_SEPARATOR._("Discussion")))
217 $map["Talk"] = WikiURL($pagename);
219 $map["Talk"] = WikiURL($pagename.SUBPAGE_SEPARATOR._("Discussion"));
221 // User:ReiniUrban => ReiniUrban or Users/ReiniUrban
222 // Can be easily overriden by a customized InterWikiMap:
224 if (empty($map["User"])) {
228 // Maybe add other monikers also (SemanticWeb link predicates?)
229 // Should they be defined in a RDF? (strict mode)
230 // Or should the SemanticWeb lib add it by itself?
231 // (adding only a subset dependent on the context = model)
235 function _getMapFromWikiText ($pagetext) {
236 if (preg_match('|^<verbatim>\n(.*)^</verbatim>|ms', $pagetext, $m)) {
242 function _getMapFromFile ($filename) {
243 if (defined('WARN_NONPUBLIC_INTERWIKIMAP') and WARN_NONPUBLIC_INTERWIKIMAP) {
244 $error_html = sprintf(_("Loading InterWikiMap from external file %s."), $filename);
245 trigger_error( $error_html, E_USER_NOTICE );
247 if (!file_exists($filename)) {
248 $finder = new FileFinder();
249 $filename = $finder->findFile(INTERWIKI_MAP_FILE);
251 @$fd = fopen ($filename, "rb");
252 @$data = fread ($fd, filesize($filename));
258 function _getRegexp () {
260 return '(?:(?!a)a)'; // Never matches.
262 foreach (array_keys($this->_map) as $moniker)
263 $qkeys[] = preg_quote($moniker, '/');
264 return "(?:" . join("|", $qkeys) . ")";
269 /** How to transform text.
271 class PageFormatter {
274 * @param WikiDB_Page $page
275 * @param hash $meta Version meta-data.
277 function PageFormatter(&$page, $meta) {
278 $this->_page = $page;
279 $this->_meta = $meta;
280 if (!empty($meta['markup']))
281 $this->_markup = $meta['markup'];
283 $this->_markup = 1; // dump used old-markup as empty.
284 // to be able to restore it we must keep markup 1 as default.
285 // new policy: default = new markup (old crashes quite often)
288 function _transform(&$text) {
289 include_once('lib/BlockParser.php');
290 return TransformText($text, $this->_markup);
293 /** Transform the page text.
295 * @param string $text The raw page content (e.g. wiki-text).
296 * @return XmlContent Transformed content.
298 function format($text) {
299 trigger_error("pure virtual", E_USER_ERROR);
303 class PageFormatter_wikitext extends PageFormatter
305 function format(&$text) {
306 return HTML::div(array('class' => 'wikitext'),
307 $this->_transform($text));
311 class PageFormatter_interwikimap extends PageFormatter
313 function format($text) {
314 return HTML::div(array('class' => 'wikitext'),
315 $this->_transform($this->_getHeader($text)),
316 $this->_formatMap($text),
317 $this->_transform($this->_getFooter($text)));
320 function _getHeader($text) {
321 return preg_replace('/<verbatim>.*/s', '', $text);
324 function _getFooter($text) {
325 return preg_replace('@.*?(</verbatim>|\Z)@s', '', $text, 1);
328 function _getMap($pagetext) {
329 $map = getInterwikiMap($pagetext);
333 function _formatMap($pagetext) {
334 $map = $this->_getMap($pagetext);
336 return HTML::p("<No interwiki map found>"); // Shouldn't happen.
338 $mon_attr = array('class' => 'interwiki-moniker');
339 $url_attr = array('class' => 'interwiki-url');
341 $thead = HTML::thead(HTML::tr(HTML::th($mon_attr, _("Moniker")),
342 HTML::th($url_attr, _("InterWiki Address"))));
343 foreach ($map as $moniker => $interurl) {
344 $rows[] = HTML::tr(HTML::td($mon_attr, new Cached_WikiLinkIfKnown($moniker)),
345 HTML::td($url_attr, HTML::tt($interurl)));
348 return HTML::table(array('class' => 'interwiki-map'),
350 HTML::tbody(false, $rows));
354 class FakePageRevision {
355 function FakePageRevision($meta) {
356 $this->_meta = $meta;
360 if (empty($this->_meta[$key]))
362 return $this->_meta[$key];
366 // abstract base class
367 class PageFormatter_attach extends PageFormatter
371 // Display templated contents for wikiblog, comment and wikiforum
372 function format($text) {
373 if (empty($this->type))
374 trigger_error('PageFormatter_attach->format: $type missing');
375 include_once('lib/Template.php');
377 $tokens['CONTENT'] = $this->_transform($text);
378 $tokens['page'] = $this->_page;
379 $tokens['rev'] = new FakePageRevision($this->_meta);
381 $name = new WikiPageName($this->_page->getName());
382 $tokens[$this->prefix."_PARENT"] = $name->getParent();
384 $meta = $this->_meta[$this->type];
385 foreach(array('ctime', 'creator', 'creator_id') as $key)
386 $tokens[$this->prefix . "_" . strtoupper($key)] = $meta[$key];
388 return new Template($this->type, $request, $tokens);
392 class PageFormatter_wikiblog extends PageFormatter_attach {
393 var $type = 'wikiblog', $prefix = "BLOG";
395 class PageFormatter_comment extends PageFormatter_attach {
396 var $type = 'comment', $prefix = "COMMENT";
398 class PageFormatter_wikiforum extends PageFormatter_attach {
399 var $type = 'wikiforum', $prefix = "FORUM";
402 /** wikiabuse for htmlarea editing. not yet used.
404 * Warning! Once a page is edited with a htmlarea like control it is
405 * stored in HTML and cannot be converted back to WikiText as long as
406 * we have no HTML => WikiText or any other interim format (WikiExchangeFormat e.g. XML)
407 * converter. See lib/HtmlParser.php for ongoing work on that.
408 * So it has a viral effect and certain plugins will not work anymore.
409 * But a lot of wikiusers seem to like it.
411 class PageFormatter_html extends PageFormatter
413 function _transform($text) {
416 function format($text) {
422 * FIXME. not yet used
424 class PageFormatter_pdf extends PageFormatter
427 function _transform($text) {
428 include_once('lib/BlockParser.php');
429 return TransformText($text, $this->_markup);
432 // one page or set of pages?
433 // here we try to format only a single page
434 function format($text) {
435 include_once('lib/Template.php');
437 $tokens['page'] = $this->_page;
438 $tokens['CONTENT'] = $this->_transform($text);
439 $pagename = $this->_page->getName();
441 // This is a XmlElement tree, which must be converted to PDF
443 // We can make use of several pdf extensions. This one - fpdf
444 // - is pure php and very easy, but looks quite ugly and has a
445 // terrible interface, as terrible as most of the othes.
446 // The closest to HTML is htmldoc which needs an external cgi
448 // We use a custom HTML->PDF class converter from PHPWebthings
449 // to be able to use templates for PDF.
450 require_once('lib/fpdf.php');
451 require_once('lib/pdf.php');
454 $pdf->SetTitle($pagename);
455 $pdf->SetAuthor($this->_page->get('author'));
456 $pdf->SetCreator(WikiURL($pagename,false,1));
457 $pdf->AliasNbPages();
460 $pdf->SetFont('Times','',12);
461 //$pdf->SetFont('Arial','B',16);
463 // PDF pagelayout from a special template
464 $template = new Template('pdf', $request, $tokens);
465 $pdf->ConvertFromHTML($template);
467 // specify filename, destination
468 $pdf->Output($pagename.".pdf",'I'); // I for stdin or D for download
470 // Output([string name [, string dest]])
474 // $Log: not supported by cvs2svn $
475 // Revision 1.40 2005/01/31 12:15:08 rurban
476 // avoid some cornercase intermap warning. Thanks to Stefan <sonstiges@bayern-mail.de>
478 // Revision 1.39 2005/01/25 06:59:35 rurban
479 // fix bogus InterWikiMap warning
481 // Revision 1.38 2004/12/26 17:10:44 rurban
482 // just docs or whitespace
484 // Revision 1.37 2004/12/06 19:49:55 rurban
485 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
486 // renamed delete_page to purge_page.
487 // enable action=edit&version=-1 to force creation of a new version.
488 // added BABYCART_PATH config
489 // fixed magiqc in adodb.inc.php
490 // and some more docs
497 // c-hanging-comment-ender-p: nil
498 // indent-tabs-mode: nil