]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageType.php
View the InterWikiMap page to see the first PageType in action! New class: PageType...
[SourceForge/phpwiki.git] / lib / PageType.php
1 <?php rcs_id('$Id: PageType.php,v 1.1 2002-02-18 08:58:10 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     //echo $ContentTemplateName; //debugging
52     //echo $text; //debugging
53
54
55     // PageType currently only works with InterWikiMap.
56     // Once a contentType field has been implemented in the
57     // database then that can be used instead of this pagename check.
58     switch( _($pagename) ) {
59         case "InterWikiMap":
60             $ContentTemplateName = 'interwikimap';
61             break;
62         default:
63             $ContentTemplateName = 'wikitext';
64     }
65
66     $_ContentTemplates = array('wikitext' => new PageType($text, $markup),
67                                'interwikimap' => new interWikiMapPageType($text, $markup));
68
69     // Start making the actual content
70     $content_template = $_ContentTemplates[$ContentTemplateName];
71     return $content_template->getContent();
72 }
73
74
75 /**
76  *
77  */
78 class PageType {
79     /**
80      * This is a simple WikiPage
81      */
82     //var $_content = "";
83
84     function PageType (&$content, $markup) {
85         $this->_html = TransformText($content, $markup);
86     }
87
88     function getContent() {
89         return $this->_html;
90     }
91 };
92
93
94 class interWikiMapPageType extends PageType {
95     var $_content = "";
96
97     function interWikiMapPageType($content, $markup) {
98         //echo $content; //debugging
99         $this->_content = $content;
100         $this->markup = $markup;
101         $this->_html = HTML();
102
103 //        $this->_html->pushContent($this->_extractText());
104 //        $this->_html->pushContent($this->_getMap());
105
106         $divs = array('interwikimap-header' => $this->_extractStartText(),
107                       'interwikimap'        => $this->_getMap(),
108                       'interwikimap-footer' => $this->_extractEndText());
109
110         foreach ($divs as $class => $function)
111             $this->_html->pushContent(HTML::div(array('class' => $class), $function));
112
113     }
114
115     function getContent() {
116         return $this->_html;
117     }
118
119     function _getMap() {
120         // plain text
121         // return TransformText("<verbatim>" . $this->_extractMap() . "</verbatim>", $this->markup);
122         global $request;
123         // table with links
124         //return $this->_arrayToTable($this->_extractMap(), $request);
125
126         // let interwiki.php get the map
127         include_once("lib/interwiki.php");
128         $map = InterWikiMap::GetMap($request);
129         return $this->_arrayToTable($map->_map, $request);
130     }
131
132     function _arrayToTable ($array, &$request) {
133         $dbi = $request->getDbh();
134         $table = HTML::table();
135         foreach ($array as $moniker => $url) {
136             if ($dbi->isWikiPage($moniker)) {
137                 $moniker = WikiLink($moniker);
138             }
139             $table->pushContent(HTML::tr(HTML::td($moniker), HTML::td(HTML::pre($url))));
140         }
141         return $table;
142     }
143
144     function _extractStartText() {
145         // cut the map out of the text
146         $v = strpos($this->_content, "<verbatim>");
147         if ($v) {
148             list($wikitext, $cruft) = explode("<verbatim>", $this->_content);
149             //echo $cruft; //debugging
150         } else {
151             $wikitext = $this->_content;
152         }
153         return TransformText($wikitext, $this->markup);
154     }
155
156     function _extractEndText() {
157         // cut the map out of the text
158         $v = strpos($this->_content, "</verbatim>");
159         if ($v) {
160             list($cruft, $endtext) = explode("</verbatim>", $this->_content);
161             //echo $cruft; //debugging
162             return TransformText($endtext, $this->markup);
163         } else {
164             return "";
165         }
166     }
167
168     /*
169     function _extractMap() {
170         if (preg_match('|^<verbatim>\n(.*)^</verbatim>|ms',
171                     $this->_content['rawmarkup'], $m)) {
172             $maptext = $m[1];
173         }
174         //return $maptext;
175         global $AllowedProtocols;
176         if (!preg_match_all("/^\s*(\S+)\s+(\S+)/m",
177                             $maptext, $matches, PREG_SET_ORDER))
178             return false;
179         foreach ($matches as $m) {
180             $map[$m[1]] = $m[2];
181         }
182         return $map;
183     }
184     */
185 };
186
187
188 // Local Variables:
189 // mode: php
190 // tab-width: 8
191 // c-basic-offset: 4
192 // c-hanging-comment-ender-p: nil
193 // indent-tabs-mode: nil
194 // End:   
195 ?>