]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageType.php
Commented-out debugging message.
[SourceForge/phpwiki.git] / lib / PageType.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageType.php,v 1.17 2003-02-01 03:12:51 carstenklapp Exp $');
3 /*
4  Copyright 1999, 2000, 2001, 2002 $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 include_once('lib/BlockParser.php');
24
25 /**
26  * Get a PageType
27  *
28  * usage:
29  *
30  * require_once('lib/PageType.php');
31  * $transformedContent = PageType($pagerevisionhandle, $pagename, $markup);
32  *
33  * The pagename and markup args are only required when displaying the
34  * content for an edit preview, otherwise they will be extracted from
35  * the page $revision instance.
36  *
37  * See http://phpwiki.sourceforge.net/phpwiki/PageType
38  */
39 function PageType(&$rev, $pagename = false, $markup = false, $overridePageType = false) {
40
41     if (isa($rev, 'WikiDB_PageRevision')) {
42         $text = $rev->getPackedContent();
43         $pagename = $rev->getPageName();
44         $markup = $rev->get('markup');
45
46     }
47     else {
48         // Hopefully only an edit preview gets us here, else we might
49         // be screwed.
50         if ($pagename == false) {
51             //debugging message only
52             $error_text = "DEBUG: \$rev was not a 'WikiDB_PageRevision'. (Are you not previewing a page edit?)";
53             trigger_error($error_text, E_USER_NOTICE);
54         }
55         $text = $rev;
56     }
57
58     // PageType currently only works with InterWikiMap and WikiBlog.
59     // Once a contentType field has been implemented in the database
60     // then that can be used instead of this pagename check.
61
62     /**
63      * Hook to allow plugins to provide their own or override a PageType.
64      * See InterWikiSearch plugin.
65      */
66     if ($overridePageType) {
67         $ContentTemplateName = '$overridePageType';
68         $content_template = new $overridePageType($text, $markup);
69         //trigger_error(sprintf("DEBUG: PageType overridden as %s", $overridePageType));
70     }
71     else {
72        /**
73         * Check whether pagename is InterWikiMap.
74         */
75         $isInterWikiMap = _("InterWikiMap");
76
77         /**
78          * Check whether pagename indicates a blog. Adapted from the
79          * WikiBlog plugin.
80          */
81         // get subpage basename, don't forget subpages of subpages
82         $basename = explode(SUBPAGE_SEPARATOR, $pagename);
83         array_pop($basename);
84         $escpage = implode(SUBPAGE_SEPARATOR, $basename);
85         // from WikiBlog plugin:
86         // If page contains '/', we must escape them
87         // FIXME: only works for '/' SUBPAGE_SEPARATOR
88         $escpage = preg_replace ("/\//", '\/', $escpage);
89         if (preg_match("/^$escpage\/Blog-([[:digit:]]{14})$/", $pagename, $matches))
90             $isWikiBlog = $pagename;
91         else
92             $isWikiBlog = "";
93     
94         switch($pagename) {
95             case $isInterWikiMap:
96                 $ContentTemplateName = 'interwikimap';
97                 $content_template = new interWikiMapPageType($text, $markup);
98                 //trigger_error("DEBUG: PageType is an InterWikiMap");
99                 break;
100             case $isWikiBlog:
101                 $ContentTemplateName = 'wikiblog';
102                 $content_template = new wikiBlogPageType($text, $markup);
103                 $content_template->_summary = $rev->get('summary');
104                 //trigger_error("DEBUG: PageType is a WikiBlog");
105                 break;
106             default:
107                 $ContentTemplateName = 'wikitext';
108                 $content_template = new PageType($text, $markup);
109                 //trigger_error("DEBUG: PageType is default");
110         }
111     }
112     return $content_template->getContent();
113 }
114
115 /**
116  * The basic PageType formats a standard WikiPage with one section:
117  *
118  * - wikitext
119  */
120 class PageType
121 {
122     /**
123      * This is a simple WikiPage
124      */
125     var $_content = "";
126     var $_markup = false;
127     var $_divs = array();
128
129     function PageType (&$content, $markup) {
130         $this->_content = $content;
131         $this->_markup = $markup;
132         $this->_html = HTML();
133
134     }
135
136     function _defineSections() {
137         /**
138          * ... section_id => ('css_class', $this->_section_function)
139          */
140         $this->_divs = array('wikitext' => array('wikitext',
141                                                  $this->_extractText()));
142     }
143
144     function _populateSections() {
145         foreach ($this->_divs as $section => $data) {
146             list($class, $function) = $data;
147             if (!empty($function))
148                 $this->_html->pushContent(HTML::div(array('class' => $class),
149                                                     $function));
150         }
151     }
152
153     function _extractText() {
154         /**
155          * Custom text extractions might want to check if the section
156          * contains any text using trim() before returning any
157          * transformed text, to avoid displaying blank boxes.
158          *
159          * See interWikiMapPageType->_extractStartText()
160          * and interWikiMapPageType->_extractEndText() for examples.
161          */
162         return TransformText($this->_content, $this->_markup);
163     }
164
165     function getContent() {
166         // _defineSections & _populateSections execution moved to here
167         // from constructor, to allow custom vars (used for WikiBlog
168         // page summary field)
169         $this->_defineSections();
170         $this->_populateSections();
171         return $this->_html;
172     }
173 };
174
175 /**
176  * wikiBlogPageType formats a Wiki page as a blog, with two sections:
177  *
178  * - wikiblog-summary
179  * - wikitext
180  */
181 class wikiBlogPageType
182 extends PageType
183 {
184     var $_summary = "";
185
186     function _defineSections() {
187         /**
188          * section_id => ('css_class', $this->_section_function)
189          */
190         // FIXME: Create new css styles
191         $this->_divs = array('wikiblog-summary' => array('wikitext', $this->_extractSummary()),
192                              'wikitext' => array('wikitext', $this->_extractText())
193                              );
194     }
195
196     function _extractSummary() {
197         return HTML(HTML::strong(array('class' => 'wikiblog-label'),
198                                  _("Summary:")),
199                     " ", $this->_summary);
200     }
201 };
202
203 /**
204  * interWikiMapPageType formats a Wiki page as an InterWikiMap, with
205  * up to three sections:
206  *
207  * - interwikimap-header
208  * - interwikimap
209  * - interwikimap-footer
210  */
211 class interWikiMapPageType
212 extends PageType
213 {
214     function _defineSections() {
215         /**
216          * section_id => ('css_class', $this->_section_function)
217          */
218         $this->_divs = array('interwikimap-header' => array('wikitext', $this->_extractStartText()),
219                              'interwikimap'        => array('wikitext', $this->_getMap()),
220                              'interwikimap-footer' => array('wikitext', $this->_extractEndText()));
221     }
222
223     function _getMap() {
224         global $request;
225         // let interwiki.php get the map
226         include_once("lib/interwiki.php");
227         $map = InterWikiMap::GetMap($request);
228         return $this->_arrayToTable($map->_map, $request);
229     }
230
231     function _arrayToTable ($array, &$request) {
232         $thead = HTML::thead();
233         $label[0] = _("Name");
234         $label[1] = _("InterWiki Address");
235         $thead->pushContent(HTML::tr(HTML::td($label[0]),
236                                      HTML::td($label[1])));
237
238         $tbody = HTML::tbody();
239         $dbi = $request->getDbh();
240         if ($array) {
241             foreach ($array as $moniker => $interurl) {
242                 if ($dbi->isWikiPage($moniker)) {
243                     $moniker = WikiLink($moniker);
244                 }
245                 $moniker = HTML::td(array('class' => 'interwiki-moniker'),
246                                     $moniker);
247                 $interurl = HTML::td(array('class' =>'interwiki-url'),
248                                      HTML::tt($interurl));
249
250                 $tbody->pushContent(HTML::tr($moniker, $interurl));
251             }
252         }
253         $table = HTML::table();
254         $table->setAttr('class', 'interwiki-map');
255         $table->pushContent($thead);
256         $table->pushContent($tbody);
257
258         return $table;
259     }
260
261     function _extractStartText() {
262         // get the start block of text
263         $v = strpos($this->_content, "<verbatim>");
264         if ($v)
265             list($wikitext, $cruft) = explode("<verbatim>", $this->_content);
266         else
267             $wikitext = $this->_content;
268
269         if (trim($wikitext))
270             return TransformText($wikitext, $this->_markup);
271
272         return "";
273     }
274
275     function _extractEndText() {
276         // get the ending block of text
277         $v = strpos($this->_content, "</verbatim>");
278         if ($v) {
279             list($cruft, $endtext) = explode("</verbatim>", $this->_content);
280             if (trim($endtext))
281                 return TransformText($endtext, $this->_markup);
282         }
283         return "";
284     }
285 };
286
287 // $Log: not supported by cvs2svn $
288 // Revision 1.16  2003/01/31 22:53:39  carstenklapp
289 // Added hook/hack to allow plugins to provide their own or override a PageType.
290 //
291 // Revision 1.15  2003/01/06 01:46:31  carstenklapp
292 // Bugfix: Also identify any WikiBlogs which are subpages of subpages.
293 //
294 // Revision 1.14  2003/01/06 00:08:08  carstenklapp
295 // Added a basic WikiBlog page type, takes advantage of editpage summary field.
296 //
297
298 // Local Variables:
299 // mode: php
300 // tab-width: 8
301 // c-basic-offset: 4
302 // c-hanging-comment-ender-p: nil
303 // indent-tabs-mode: nil
304 // End:
305 ?>