]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageType.php
new css classes for interwikimap
[SourceForge/phpwiki.git] / lib / PageType.php
1 <?php rcs_id('$Id: PageType.php,v 1.8 2002-02-19 03:42:36 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  * The pagename and markup args are only required when displaying the content
35  * for an edit preview, otherwise they will be extracted from the page $revision
36  * instance.
37  *
38  * See http://phpwiki.sourceforge.net/phpwiki/PageType
39  */
40 function PageType(&$rev, $pagename = false, $markup = false) {
41
42     if (isa($rev, 'WikiDB_PageRevision')) {
43         $text = $rev->getPackedContent();
44         $pagename = $rev->_pagename; //is this _ok?
45         $markup = $rev->get('markup');
46
47     } else {
48         // Hopefully only an edit preview gets us here, else we might be screwed.
49         if ($pagename == false) {
50             $error_text = "DEBUG: \$rev was not a 'WikiDB_PageRevision'. (Are you not previewing a page edit?)"; //debugging message only
51             trigger_error($error_text, E_USER_NOTICE);
52         }
53         $text = $rev;
54     }
55
56     // PageType currently only works with InterWikiMap.
57     // Once a contentType field has been implemented in the
58     // database then that can be used instead of this pagename check.
59     $i = _("InterWikiMap");
60     switch($pagename) {
61         case $i:
62             $ContentTemplateName = 'interwikimap';
63             break;
64         default:
65             $ContentTemplateName = 'wikitext';
66     }
67
68     $_ContentTemplates = array('wikitext' => new PageType($text, $markup),
69                                'interwikimap' => new interWikiMapPageType($text, $markup));
70
71     // Start making the actual content
72     $content_template = $_ContentTemplates[$ContentTemplateName];
73     return $content_template->getContent();
74 }
75
76
77 /**
78  *
79  */
80 class PageType {
81     /**
82      * This is a simple WikiPage
83      */
84     var $_content = "";
85     var $_markup = false;
86     var $_divs = array();
87
88     function PageType (&$content, $markup) {
89         $this->_content = $content;
90         $this->_markup = $markup;
91         $this->_html = HTML();
92
93         $this->_defineSections();
94         $this->_populateSections();
95     }
96
97     function _defineSections() {
98         // section_id => ('css_class', $this->_section_function)
99         $this->_divs = array('wikitext' => array('wikitext', $this->_extractText()));
100     }
101
102     function _populateSections() {
103         foreach ($this->_divs as $section => $data) {
104             list($class, $function) = $data;
105             if (!empty($function))
106                 $this->_html->pushContent(HTML::div(array('class' => $class), $function));
107         }
108     }
109
110     function _extractText() {
111         /**
112          * Custom text extractions might want to check if the section
113          * contains any text using trim() before returning any
114          * transformed text, to avoid displaying blank boxes.
115          * See interWikiMapPageType->_extractStartText()
116          * and interWikiMapPageType->_extractEndText() for examples.
117          */
118         return TransformText($this->_content, $this->_markup);
119     }
120
121     function getContent() {
122         return $this->_html;
123     }
124 };
125
126
127 class interWikiMapPageType extends PageType {
128
129     function _defineSections() {
130         // section_id => ('css_class', $this->_section_function)
131         $this->_divs = array('interwikimap-header' => array('wikitext', $this->_extractStartText()),
132                              'interwikimap'        => array('wikitext', $this->_getMap()),
133                              'interwikimap-footer' => array('wikitext', $this->_extractEndText()));
134     }
135
136     function _getMap() {
137         // plain text
138         // return TransformText("<verbatim>" . $this->_extractMap() . "</verbatim>", $this->markup);
139         global $request;
140         // table with links
141         //return $this->_arrayToTable($this->_extractMap(), $request);
142
143         // let interwiki.php get the map
144         include_once("lib/interwiki.php");
145         $map = InterWikiMap::GetMap($request);
146         return $this->_arrayToTable($map->_map, $request);
147     }
148
149     function _arrayToTable ($array, &$request) {
150         $thead = HTML::thead();
151         $label[0] = _("Name");
152         $label[1] = _("InterWiki Address");
153         $thead->pushContent(HTML::tr(HTML::td($label[0]),
154                                      HTML::td($label[1])));
155
156         $tbody = HTML::tbody();
157         $dbi = $request->getDbh();
158         foreach ($array as $moniker => $interurl) {
159             if ($dbi->isWikiPage($moniker)) {
160                 $moniker = WikiLink($moniker);
161             }
162             $moniker = HTML::td(array('class' => 'interwiki-moniker'), $moniker);
163             $interurl = HTML::td(array('class' =>'interwiki-url'), HTML::tt($interurl));
164     
165             $tbody->pushContent(HTML::tr($moniker, $interurl));
166         }
167         $table = HTML::table();
168         $table->setAttr('class', 'interwiki-map');
169         $table->pushContent($thead);
170         $table->pushContent($tbody);
171
172         return $table;
173     }
174
175     function _extractStartText() {
176         // cut the map out of the text
177         $v = strpos($this->_content, "<verbatim>");
178         if ($v)
179             list($wikitext, $cruft) = explode("<verbatim>", $this->_content);
180         else
181             $wikitext = $this->_content;
182
183         if (trim($wikitext))
184             return TransformText($wikitext, $this->_markup);
185
186         return "";
187     }
188
189     function _extractEndText() {
190         // cut the map out of the text
191         $v = strpos($this->_content, "</verbatim>");
192         if ($v) {
193             list($cruft, $endtext) = explode("</verbatim>", $this->_content);
194             if (trim($endtext))
195                 return TransformText($endtext, $this->_markup);
196         }
197         return "";
198     }
199
200     /*
201     function _extractMap() {
202         if (preg_match('|^<verbatim>\n(.*)^</verbatim>|ms',
203                     $this->_content['rawmarkup'], $m)) {
204             $maptext = $m[1];
205         }
206         //return $maptext;
207         global $AllowedProtocols;
208         if (!preg_match_all("/^\s*(\S+)\s+(\S+)/m",
209                             $maptext, $matches, PREG_SET_ORDER))
210             return false;
211         foreach ($matches as $m) {
212             $map[$m[1]] = $m[2];
213         }
214         return $map;
215     }
216     */
217 };
218
219
220 // Local Variables:
221 // mode: php
222 // tab-width: 8
223 // c-basic-offset: 4
224 // c-hanging-comment-ender-p: nil
225 // indent-tabs-mode: nil
226 // End:   
227 ?>