]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageType.php
The enclosing <div> for the content is now generated automatically according to the...
[SourceForge/phpwiki.git] / lib / PageType.php
1 <?php rcs_id('$Id: PageType.php,v 1.4 2002-02-18 23:17:47 carstenklapp Exp $');
2 /*
3 Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
4
5 This file is part of PhpWiki.
6
7 PhpWiki is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 PhpWiki is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with PhpWiki; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21
22
23 include_once('lib/BlockParser.php');
24
25
26 /**
27  * Get a PageType
28  * 
29  * usage:
30  *
31  * require_once('lib/PageType.php');
32  * $transformedContent = PageType($pagerevisionhandle, $pagename, $markup);
33  *
34  * See http://phpwiki.sourceforge.net/phpwiki/PageType
35  */
36 function PageType(&$rev, $pagename = false, $markup = false) {
37
38     if (isa($rev, 'WikiDB_PageRevision')) {
39         $text = $rev->getPackedContent();
40         $pagename = $rev->_pagename; //is this _ok?
41         $markup = $rev->get('markup');
42
43     } else {
44         // Hopefully only an edit preview gets us here, else we might be screwed.
45         if ($pagename == false || $markup = false) {
46             $error_text = "DEBUG: \$rev was not a 'WikiDB_PageRevision'. (Are you not previewing a page edit?)"; //debugging message only
47             trigger_error($error_text, E_USER_NOTICE);
48         }
49         $text = $rev;
50     }
51
52     // PageType currently only works with InterWikiMap.
53     // Once a contentType field has been implemented in the
54     // database then that can be used instead of this pagename check.
55     $i = _("InterWikiMap");
56     switch($pagename) {
57         case $i:
58             $ContentTemplateName = 'interwikimap';
59             break;
60         default:
61             $ContentTemplateName = 'wikitext';
62     }
63
64     $_ContentTemplates = array('wikitext' => new PageType($text, $markup),
65                                'interwikimap' => new interWikiMapPageType($text, $markup));
66
67     // Start making the actual content
68     $content_template = $_ContentTemplates[$ContentTemplateName];
69     return $content_template->getContent();
70 }
71
72
73 /**
74  *
75  */
76 class PageType {
77     /**
78      * This is a simple WikiPage
79      */
80     var $_content = "";
81     var $_markup = false;
82     var $_divs = array();
83
84     function PageType (&$content, $markup) {
85         $this->_content = $content;
86         $this->_markup = $markup;
87         $this->_html = HTML();
88
89         $this->_defineSections();
90         $this->_populateSections();
91     }
92
93     function _defineSections() {
94         // section_id => ('css_class', $this->_section_function)
95         $this->_divs = array('wikitext' => array('wikitext', $this->_extractText()));
96     }
97
98     function _populateSections() {
99         foreach ($this->_divs as $section => $data) {
100             list($class, $function) = $data;
101             $this->_html->pushContent(HTML::div(array('class' => $class), $function));
102         }
103
104     }
105
106     function _extractText() {
107         return TransformText($this->_content, $this->_markup);
108     }
109
110     function getContent() {
111         return $this->_html;
112     }
113 };
114
115
116 class interWikiMapPageType extends PageType {
117
118     function _defineSections() {
119         // section_id => ('css_class', $this->_section_function)
120         $this->_divs = array('interwikimap-header' => array('wikitext', $this->_extractStartText()),
121                              'interwikimap'        => array('wikitext', $this->_getMap()),
122                              'interwikimap-footer' => array('wikitext', $this->_extractEndText()));
123     }
124
125     function _getMap() {
126         // plain text
127         // return TransformText("<verbatim>" . $this->_extractMap() . "</verbatim>", $this->markup);
128         global $request;
129         // table with links
130         //return $this->_arrayToTable($this->_extractMap(), $request);
131
132         // let interwiki.php get the map
133         include_once("lib/interwiki.php");
134         $map = InterWikiMap::GetMap($request);
135         return $this->_arrayToTable($map->_map, $request);
136     }
137
138     function _arrayToTable ($array, &$request) {
139         $dbi = $request->getDbh();
140         $table = HTML::table();
141         foreach ($array as $moniker => $url) {
142             if ($dbi->isWikiPage($moniker)) {
143                 $moniker = WikiLink($moniker);
144             }
145             $table->pushContent(HTML::tr(HTML::td($moniker), HTML::td(HTML::pre($url))));
146         }
147         return $table;
148     }
149
150     function _extractStartText() {
151         // cut the map out of the text
152         $v = strpos($this->_content, "<verbatim>");
153         if ($v) {
154             list($wikitext, $cruft) = explode("<verbatim>", $this->_content);
155         } else {
156             $wikitext = $this->_content;
157         }
158         return TransformText($wikitext, $this->_markup);
159     }
160
161     function _extractEndText() {
162         // cut the map out of the text
163         $v = strpos($this->_content, "</verbatim>");
164         if ($v) {
165             list($cruft, $endtext) = explode("</verbatim>", $this->_content);
166             return TransformText($endtext, $this->_markup);
167         } else {
168             return "";
169         }
170     }
171
172     /*
173     function _extractMap() {
174         if (preg_match('|^<verbatim>\n(.*)^</verbatim>|ms',
175                     $this->_content['rawmarkup'], $m)) {
176             $maptext = $m[1];
177         }
178         //return $maptext;
179         global $AllowedProtocols;
180         if (!preg_match_all("/^\s*(\S+)\s+(\S+)/m",
181                             $maptext, $matches, PREG_SET_ORDER))
182             return false;
183         foreach ($matches as $m) {
184             $map[$m[1]] = $m[2];
185         }
186         return $map;
187     }
188     */
189 };
190
191
192 // Local Variables:
193 // mode: php
194 // tab-width: 8
195 // c-basic-offset: 4
196 // c-hanging-comment-ender-p: nil
197 // indent-tabs-mode: nil
198 // End:   
199 ?>