2 rcs_id('$Id: PageType.php,v 1.46 2005-08-06 13:09:33 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) {
125 $map = new PageType_interwikimap($pagetext);
129 class PageType_interwikimap extends PageType
131 function PageType_interwikimap($pagetext = false) {
133 $dbi = $GLOBALS['request']->getDbh();
134 $page = $dbi->getPage(_("InterWikiMap"));
135 if ($page->get('locked')) {
136 $current = $page->getCurrentRevision();
137 $pagetext = $current->getPackedContent();
138 $intermap = $this->_getMapFromWikiText($pagetext);
139 } elseif ($page->exists()) {
140 trigger_error(_("WARNING: InterWikiMap page is unlocked, so not using those links."));
146 $intermap = $this->_getMapFromWikiText($pagetext);
148 if (!$intermap && defined('INTERWIKI_MAP_FILE'))
149 $intermap = $this->_getMapFromFile(INTERWIKI_MAP_FILE);
151 $this->_map = $this->_parseMap($intermap);
152 $this->_regexp = $this->_getRegexp();
155 function GetMap ($pagetext = false) {
156 /*PHP5 Fatal error: Using $this when not in object context */
157 if (empty($this->_map)) {
158 $map = new PageType_interwikimap($pagetext);
165 function getRegexp() {
166 return $this->_regexp;
169 function link ($link, $linktext = false) {
170 list ($moniker, $page) = split (":", $link, 2);
172 if (!isset($this->_map[$moniker])) {
173 return HTML::span(array('class' => 'bad-interwiki'),
174 $linktext ? $linktext : $link);
177 $url = $this->_map[$moniker];
179 // Urlencode page only if it's a query arg.
180 // FIXME: this is a somewhat broken heuristic.
181 $page_enc = strstr($url, '?') ? rawurlencode($page) : $page;
183 if (strstr($url, '%s'))
184 $url = sprintf($url, $page_enc);
188 $link = HTML::a(array('href' => $url));
191 $link->pushContent(PossiblyGlueIconToText('interwiki', "$moniker:"),
192 HTML::span(array('class' => 'wikipage'), $page));
193 $link->setAttr('class', 'interwiki');
196 $link->pushContent(PossiblyGlueIconToText('interwiki', $linktext));
197 $link->setAttr('class', 'named-interwiki');
204 function _parseMap ($text) {
205 if (!preg_match_all("/^\s*(\S+)\s+(.+)$/m",
206 $text, $matches, PREG_SET_ORDER))
209 foreach ($matches as $m) {
213 // Add virtual monikers: Upload:, Talk:, User:
214 // Upload: Should be expanded later to user-specific upload dirs.
215 // In the Upload plugin, not here: Upload:ReiniUrban/uploaded-file.png
216 if (empty($map['Upload'])) {
217 $map['Upload'] = getUploadDataPath();
219 // User:ReiniUrban => ReiniUrban or Users/ReiniUrban
220 // Can be easily overriden by a customized InterWikiMap:
222 if (empty($map["User"])) {
225 if (empty($map["Talk"])) {
226 $pagename = $GLOBALS['request']->getArg('pagename');
227 if (string_ends_with($pagename, SUBPAGE_SEPARATOR._("Discussion")))
230 $map["Talk"] = "%s".SUBPAGE_SEPARATOR._("Discussion");
233 foreach (array('Upload','User','Talk') as $special) {
234 // Expand special variables:
237 // %d => iso8601 DateTime
238 // %s is expanded later to the pagename
239 if (strstr($map[$special], '%u'))
240 $map[$special] = str_replace($map[$special],
242 $GLOBALS['request']->_user->_userid);
243 if (strstr($map[$special], '%b'))
244 $map[$special] = str_replace($map[$special],
247 if (strstr($map[$special], '%d'))
248 $map[$special] = str_replace($map[$special],
250 // such as 2003-01-11T14:03:02+00:00
254 // Maybe add other monikers also (SemanticWeb link predicates?)
255 // Should they be defined in a RDF? (strict mode)
256 // Or should the SemanticWeb lib add it by itself?
257 // (adding only a subset dependent on the context = model)
261 function _getMapFromWikiText ($pagetext) {
262 if (preg_match('|^<verbatim>\n(.*)^</verbatim>|ms', $pagetext, $m)) {
268 function _getMapFromFile ($filename) {
269 if (defined('WARN_NONPUBLIC_INTERWIKIMAP') and WARN_NONPUBLIC_INTERWIKIMAP) {
270 $error_html = sprintf(_("Loading InterWikiMap from external file %s."),
272 trigger_error( $error_html, E_USER_NOTICE );
274 if (!file_exists($filename)) {
275 $finder = new FileFinder();
276 $filename = $finder->findFile(INTERWIKI_MAP_FILE);
278 @$fd = fopen ($filename, "rb");
279 @$data = fread ($fd, filesize($filename));
285 function _getRegexp () {
287 return '(?:(?!a)a)'; // Never matches.
289 foreach (array_keys($this->_map) as $moniker)
290 $qkeys[] = preg_quote($moniker, '/');
291 return "(?:" . join("|", $qkeys) . ")";
296 /** How to transform text.
298 class PageFormatter {
301 * @param WikiDB_Page $page
302 * @param hash $meta Version meta-data.
304 function PageFormatter(&$page, $meta) {
305 $this->_page = $page;
306 $this->_meta = $meta;
307 if (!empty($meta['markup']))
308 $this->_markup = $meta['markup'];
310 $this->_markup = 2; // dump used old-markup as empty.
311 // FIXME: To be able to restore old plain-backups we should keep markup 1 as default.
312 // New policy: default = new markup (old crashes quite often)
315 function _transform(&$text) {
316 include_once('lib/BlockParser.php');
317 return TransformText($text, $this->_markup);
320 /** Transform the page text.
322 * @param string $text The raw page content (e.g. wiki-text).
323 * @return XmlContent Transformed content.
325 function format($text) {
326 trigger_error("pure virtual", E_USER_ERROR);
330 class PageFormatter_wikitext extends PageFormatter
332 function format(&$text) {
333 return HTML::div(array('class' => 'wikitext'),
334 $this->_transform($text));
338 class PageFormatter_interwikimap extends PageFormatter
340 function format($text) {
341 return HTML::div(array('class' => 'wikitext'),
342 $this->_transform($this->_getHeader($text)),
343 $this->_formatMap($text),
344 $this->_transform($this->_getFooter($text)));
347 function _getHeader($text) {
348 return preg_replace('/<verbatim>.*/s', '', $text);
351 function _getFooter($text) {
352 return preg_replace('@.*?(</verbatim>|\Z)@s', '', $text, 1);
355 function _getMap($pagetext) {
356 $map = getInterwikiMap($pagetext);
360 function _formatMap($pagetext) {
361 $map = $this->_getMap($pagetext);
363 return HTML::p("<No interwiki map found>"); // Shouldn't happen.
365 $mon_attr = array('class' => 'interwiki-moniker');
366 $url_attr = array('class' => 'interwiki-url');
368 $thead = HTML::thead(HTML::tr(HTML::th($mon_attr, _("Moniker")),
369 HTML::th($url_attr, _("InterWiki Address"))));
370 foreach ($map as $moniker => $interurl) {
371 $rows[] = HTML::tr(HTML::td($mon_attr, new Cached_WikiLinkIfKnown($moniker)),
372 HTML::td($url_attr, HTML::tt($interurl)));
375 return HTML::table(array('class' => 'interwiki-map'),
377 HTML::tbody(false, $rows));
381 class FakePageRevision {
382 function FakePageRevision($meta) {
383 $this->_meta = $meta;
387 if (empty($this->_meta[$key]))
389 return $this->_meta[$key];
393 // abstract base class
394 class PageFormatter_attach extends PageFormatter
398 // Display templated contents for wikiblog, comment and wikiforum
399 function format($text) {
400 if (empty($this->type))
401 trigger_error('PageFormatter_attach->format: $type missing');
402 include_once('lib/Template.php');
404 $tokens['CONTENT'] = $this->_transform($text);
405 $tokens['page'] = $this->_page;
406 $tokens['rev'] = new FakePageRevision($this->_meta);
408 $name = new WikiPageName($this->_page->getName());
409 $tokens[$this->prefix."_PARENT"] = $name->getParent();
411 $meta = $this->_meta[$this->type];
412 foreach(array('ctime', 'creator', 'creator_id') as $key)
413 $tokens[$this->prefix . "_" . strtoupper($key)] = $meta[$key];
415 return new Template($this->type, $request, $tokens);
419 class PageFormatter_wikiblog extends PageFormatter_attach {
420 var $type = 'wikiblog', $prefix = "BLOG";
422 class PageFormatter_comment extends PageFormatter_attach {
423 var $type = 'comment', $prefix = "COMMENT";
425 class PageFormatter_wikiforum extends PageFormatter_attach {
426 var $type = 'wikiforum', $prefix = "FORUM";
429 /** wikiabuse for htmlarea editing. not yet used.
431 * Warning! Once a page is edited with a htmlarea like control it is
432 * stored in HTML and cannot be converted back to WikiText as long as
433 * we have no HTML => WikiText or any other interim format (WikiExchangeFormat e.g. XML)
434 * converter. See lib/HtmlParser.php for ongoing work on that.
435 * So it has a viral effect and certain plugins will not work anymore.
436 * But a lot of wikiusers seem to like it.
438 class PageFormatter_html extends PageFormatter
440 function _transform($text) {
443 function format($text) {
449 * FIXME. not yet used
451 class PageFormatter_pdf extends PageFormatter
454 function _transform($text) {
455 include_once('lib/BlockParser.php');
456 return TransformText($text, $this->_markup);
459 // one page or set of pages?
460 // here we try to format only a single page
461 function format($text) {
462 include_once('lib/Template.php');
464 $tokens['page'] = $this->_page;
465 $tokens['CONTENT'] = $this->_transform($text);
466 $pagename = $this->_page->getName();
468 // This is a XmlElement tree, which must be converted to PDF
470 // We can make use of several pdf extensions. This one - fpdf
471 // - is pure php and very easy, but looks quite ugly and has a
472 // terrible interface, as terrible as most of the othes.
473 // The closest to HTML is htmldoc which needs an external cgi
475 // We use a custom HTML->PDF class converter from PHPWebthings
476 // to be able to use templates for PDF.
477 require_once('lib/fpdf.php');
478 require_once('lib/pdf.php');
481 $pdf->SetTitle($pagename);
482 $pdf->SetAuthor($this->_page->get('author'));
483 $pdf->SetCreator(WikiURL($pagename,false,1));
484 $pdf->AliasNbPages();
487 $pdf->SetFont('Times','',12);
488 //$pdf->SetFont('Arial','B',16);
490 // PDF pagelayout from a special template
491 $template = new Template('pdf', $request, $tokens);
492 $pdf->ConvertFromHTML($template);
494 // specify filename, destination
495 $pdf->Output($pagename.".pdf",'I'); // I for stdin or D for download
497 // Output([string name [, string dest]])
501 // $Log: not supported by cvs2svn $
502 // Revision 1.45 2005/05/06 16:48:41 rurban
503 // support %u, %b, %d expansion for Upload: User: and Talk: interwiki monikers
505 // Revision 1.44 2005/04/23 11:07:34 rurban
508 // Revision 1.43 2005/02/02 20:40:12 rurban
509 // fix Talk: and User: names and links
511 // Revision 1.42 2005/02/02 19:36:56 rurban
514 // Revision 1.41 2005/02/02 19:34:09 rurban
515 // more maps: Talk, User
517 // Revision 1.40 2005/01/31 12:15:08 rurban
518 // avoid some cornercase intermap warning. Thanks to Stefan <sonstiges@bayern-mail.de>
520 // Revision 1.39 2005/01/25 06:59:35 rurban
521 // fix bogus InterWikiMap warning
523 // Revision 1.38 2004/12/26 17:10:44 rurban
524 // just docs or whitespace
526 // Revision 1.37 2004/12/06 19:49:55 rurban
527 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
528 // renamed delete_page to purge_page.
529 // enable action=edit&version=-1 to force creation of a new version.
530 // added BABYCART_PATH config
531 // fixed magiqc in adodb.inc.php
532 // and some more docs
539 // c-hanging-comment-ender-p: nil
540 // indent-tabs-mode: nil