fixtags hack slightly cleaned up
[lhc/web/wiklou.git] / includes / SkinPHPTal.php
1 <?php
2 # Generic PHPTal (http://phptal.sourceforge.net/) skin
3 # Based on Brion's smarty skin
4 # Copyright (C) Gabriel Wicke -- http://www.aulinx.de/
5 #
6 # Todo: Needs some serious refactoring into functions that correspond
7 # to the computations individual esi snippets need. Most importantly no body
8 # parsing for most of those of course.
9 #
10 # Set this in LocalSettings to enable phptal:
11 # set_include_path(get_include_path() . ":" . $IP.'/PHPTAL-NP-0.7.0/libs');
12 # $wgUsePHPTal = true;
13 #
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along
25 # with this program; if not, write to the Free Software Foundation, Inc.,
26 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 # http://www.gnu.org/copyleft/gpl.html
28
29 require_once "PHPTAL.php";
30
31 class MediaWiki_I18N extends PHPTAL_I18N
32 {
33 var $_context = array();
34
35 function set($varName, $value)
36 {
37 $this->_context[$varName] = $value;
38 }
39
40 function translate($value)
41 {
42 $value = wfMsg( $value );
43
44 // interpolate variables
45 while (preg_match('/\$([0-9]*?)/sm', $value, $m)) {
46 list($src, $var) = $m;
47 $varValue = $this->_context[$var];
48 $value = str_replace($src, $varValue, $value);
49 }
50 return $value;
51 }
52 }
53
54 class SkinPHPTal extends Skin {
55 var $template;
56
57 function initPage() {
58 $this->skinname = "davinci";
59 $this->template = "xhtml_slim";
60 }
61
62 function outputPage( &$out ) {
63 global $wgTitle, $wgArticle, $wgUser, $wgLang, $wgOut;
64 global $wgScriptPath, $wgStyleSheetPath, $wgLanguageCode, $wgUseNewInterlanguage;
65 global $wgOutputEncoding, $wgUseDatabaseMessages, $wgRequest;
66
67 $this->thispage = $wgTitle->getPrefixedDbKey();
68 $this->loggedin = $wgUser->getID() != 0;
69 $this->username = $wgUser->getName();
70 $this->userpage = $wgLang->getNsText( Namespace::getUser() ) . ":" . $wgUser->getName();
71 $this->titletxt = $wgTitle->getPrefixedText();
72
73
74 $this->initPage();
75 $tpl = new PHPTAL($this->template . '.pt', 'templates');
76
77 #if ( $wgUseDatabaseMessages ) { // uncomment this to fall back to GetText
78 $tpl->setTranslator(new MediaWiki_I18N());
79 #}
80
81 $tpl->setRef( "title", &$this->titletxt ); // ?
82 $tpl->setRef( "thispage", &$this->thispage );
83 $tpl->set( "subtitle", $out->getSubtitle() );
84 $tpl->setRef( 'charset', $wgOutputEncoding);
85 $tpl->setRef( 'skinname', $this->skinname );
86 $tpl->setRef( "loggedin", &$this->loggedin );
87 $tpl->set( "editable", ($wgTitle->getNamespace != Namespace::getSpecial() ) );
88 $tpl->set( "exists", $wgTitle->getArticleID() != 0 );
89 $tpl->set( "watch", $wgTitle->userIsWatching() ? "unwatch" : "watch" );
90 $tpl->set( "protect", count($wgTitle->getRestrictions()) ? "unprotect" : "protect" );
91 $tpl->set( "helppage", wfMsg('helppage'));
92 $tpl->setRef( "searchaction", &$wgScriptPath );
93 $tpl->setRef( "stylepath", &$wgStyleSheetPath );
94 $tpl->setRef( "lang", &$wgLanguageCode );
95 $tpl->set( "langname", $wgLang->getLanguageName( $wgLanguageCode ) );
96 $tpl->setRef( "username", &$this->username );
97 $tpl->setRef( "userpage", &$this->userpage);
98 $tpl->set( "sysop", $wgUser->isSysop() );
99 if( $wgUser->getNewtalk() ) {
100 $ntl = wfMsg( "newmessages",
101 $this->makeKnownLink(
102 $wgLang->getNsText( Namespace::getTalk( Namespace::getUser() ) )
103 . ":" . $wgUser->getName(),
104 wfMsg("newmessageslink") )
105 );
106 } else {
107 $ntl = "";
108 }
109 $tpl->setRef( "newtalk", &$ntl );
110 $tpl->setRef( "skin", &$this);
111 $tpl->set( "logo", $this->logoText() );
112 $tpl->set( "pagestats", $this->pageStats() );
113 $tpl->set( "disclaimer", $this->disclaimerLink() );
114 $tpl->set( "about", $this->aboutLink() );
115
116 $tpl->setRef( "debug", &$out->mDebugtext );
117 $tpl->set( "reporttime", $out->reportTime() );
118
119 $tpl->setRef( "bodytext", &$out->mBodytext );
120
121 $language_urls = array();
122 foreach( $wgOut->getLanguageLinks() as $l ) {
123 $nt = Title::newFromText( $l );
124 $language_urls[] = array('href' => $nt->getFullURL(),
125 'text' => ($wgLang->getLanguageName( $nt->getInterwiki()) != ''?$wgLang->getLanguageName( $nt->getInterwiki()) : $l),
126 'class' => $wgLang->isRTL() ? 'rtl' : 'ltr');
127 }
128 if(count($language_urls) != 0 ) {
129 $tpl->setRef( 'language_urls', &$language_urls);
130 } else {
131 $tpl->set('language_urls', false);
132 }
133 $tpl->set('personal_urls', $this->buildPersonalUrls());
134 $tpl->set('content_actions', $this->buildContentActionUrls());
135 $tpl->set( "nav_urls", $this->buildNavUrls() );
136
137 // execute template
138 $res = $tpl->execute();
139 // result may be an error
140 if (PEAR::isError($res)) {
141 echo $res->toString(), "\n";
142 } else {
143 echo $res;
144 }
145
146 }
147
148 # build array of urls for personal toolbar
149 function buildPersonalUrls() {
150 /* set up the default links for the personal toolbar */
151 $personal_urls = array();
152 if ($this->loggedin) {
153 $personal_urls['userpage'] = array('text' => $this->username,
154 'href' => $this->makeUrl($this->userpage),
155 'ttip' => wfMsg('tooltip-userpage'),
156 'akey' => wfMsg('accesskey-userpage'));
157 $personal_urls['mytalk'] = array('text' => wfMsg('mytalk'),
158 'href' => $this->makeTalkUrl($this->userpage),
159 'ttip' => wfMsg('tooltip-mytalk'),
160 'akey' => wfMsg('accesskey-mytalk'));
161 $personal_urls['preferences'] = array('text' => wfMsg('preferences'),
162 'href' => $this->makeSpecialUrl('Preferences'),
163 'ttip' => wfMsg('tooltip-preferences'),
164 'akey' => wfMsg('accesskey-preferences'));
165 $personal_urls['watchlist'] = array('text' => wfMsg('watchlist'),
166 'href' => $this->makeSpecialUrl('Watchlist'),
167 'ttip' => wfMsg('tooltip-watchlist'),
168 'akey' => wfMsg('accesskey-watchlist'));
169 $personal_urls['mycontris'] = array('text' => wfMsg('mycontris'),
170 'href' => $this->makeSpecialUrl('Contributions','target=' . $this->username),
171 'ttip' => wfMsg('tooltip-mycontris'),
172 'akey' => wfMsg('accesskey-mycontris'));
173 $personal_urls['logout'] = array('text' => wfMsg('userlogout'),
174 'href' => $this->makeSpecialUrl('Userlogout','returnpage=' . $this->thispage),
175 'ttip' => wfMsg('tooltip-logout'),
176 'akey' => wfMsg('accesskey-logout'));
177 } else {
178 $personal_urls['login'] = array('text' => wfMsg('userlogin'),
179 'href' => $this->makeSpecialUrl('Userlogin'),
180 'ttip' => wfMsg('tooltip-login'),
181 'akey' => wfMsg('accesskey-login'));
182 }
183 return $personal_urls;
184 }
185
186 # an array of edit links by default used for the tabs
187 function buildContentActionUrls () {
188 global $wgTitle, $wgUser, $wgRequest;
189 $action = $wgRequest->getText( 'action' );
190 $oldid = $wgRequest->getVal( 'oldid' );
191 $diff = $wgRequest->getVal( 'diff' );
192 $content_actions = array();
193
194 $iscontent = ($wgTitle->getNamespace() != Namespace::getSpecial() );
195 if( $iscontent) {
196
197 $content_actions['article'] = array('class' => (!Namespace::isTalk( $wgTitle->getNamespace())) ? 'selected' : '',
198 'text' => wfMsg('article'),
199 'href' => $this->makeArticleUrl($this->thispage),
200 'ttip' => wfMsg('tooltip-article'),
201 'akey' => wfMsg('accesskey-article'));
202
203 $content_actions['talk'] = array('class' => (Namespace::isTalk( $wgTitle->getNamespace()) ? 'selected' : ''),
204 'text' => wfMsg('talk'),
205 'href' => $this->makeTalkUrl($this->titletxt),
206 'ttip' => wfMsg('tooltip-talk'),
207 'akey' => wfMsg('accesskey-talk'));
208
209 if ( $wgTitle->userCanEdit() ) {
210 if ( $oldid && ! isset( $diff ) ) {
211 $oid = "&oldid={$oldid}";
212 }
213 $content_actions['edit'] = array('class' => ($action == 'edit' or $action == 'submit') ? 'selected' : '',
214 'text' => wfMsg('edit'),
215 'href' => $this->makeUrl($this->thispage, 'action=edit'.$oid),
216 'ttip' => wfMsg('tooltip-edit'),
217 'akey' => wfMsg('accesskey-edit'));
218 } else {
219 if ( $oldid && ! isset( $diff ) ) {
220 $oid = "&oldid={$oldid}";
221 }
222 $content_actions['edit'] = array('class' => ($action == 'edit') ? 'selected' : '',
223 'text' => wfMsg('viewsource'),
224 'href' => $this->makeUrl($this->thispage, 'action=edit'.$oid),
225 'ttip' => wfMsg('tooltip-edit'),
226 'akey' => wfMsg('accesskey-edit'));
227 }
228
229 if ( $wgTitle->getArticleId() ) {
230
231 $content_actions['history'] = array('class' => ($action == 'history') ? 'selected' : '',
232 'text' => wfMsg('history_short'),
233 'href' => $this->makeUrl($this->thispage, 'action=history'),
234 'ttip' => wfMsg('tooltip-history'),
235 'akey' => wfMsg('accesskey-history'));
236
237 # XXX: is there a rollback action anywhere or is it planned?
238 # Don't recall where i got this from...
239 /*if( $wgUser->getNewtalk() ) {
240 $content_actions['rollback'] = array('class' => ($action == 'rollback') ? 'selected' : '',
241 'text' => wfMsg('rollback_short'),
242 'href' => $this->makeUrl($this->thispage, 'action=rollback'),
243 'ttip' => wfMsg('tooltip-rollback'),
244 'akey' => wfMsg('accesskey-rollback'));
245 }*/
246
247 if($wgUser->isSysop()){
248 if(!$wgTitle->isProtected()){
249 $content_actions['protect'] = array('class' => ($action == 'protect') ? 'selected' : '',
250 'text' => wfMsg('protect'),
251 'href' => $this->makeUrl($this->thispage, 'action=protect'),
252 'ttip' => wfMsg('tooltip-protect'),
253 'akey' => wfMsg('accesskey-protect'));
254
255 } else {
256 $content_actions['unprotect'] = array('class' => ($action == 'unprotect') ? 'selected' : '',
257 'text' => wfMsg('unprotect'),
258 'href' => $this->makeUrl($this->thispage, 'action=unprotect'),
259 'ttip' => wfMsg('tooltip-protect'),
260 'akey' => wfMsg('accesskey-protect'));
261 }
262 $content_actions['delete'] = array('class' => ($action == 'delete') ? 'selected' : '',
263 'text' => wfMsg('delete'),
264 'href' => $this->makeUrl($this->thispage, 'action=delete'),
265 'ttip' => wfMsg('tooltip-delete'),
266 'akey' => wfMsg('accesskey-delete'));
267 }
268 if ( $wgUser->getID() != 0 ) {
269 if ( $wgTitle->userCanEdit()) {
270 $content_actions['move'] = array('class' => ($wgTitle->getDbKey() == 'Movepage' and $wgTitle->getNamespace == Namespace::getSpecial()) ? 'selected' : '',
271 'text' => wfMsg('move'),
272 'href' => $this->makeSpecialUrl('Movepage', 'target='.$this->thispage),
273 'ttip' => wfMsg('tooltip-move'),
274 'akey' => wfMsg('accesskey-move'));
275 } else {
276 $content_actions['move'] = array('class' => 'inactive',
277 'text' => wfMsg('move'),
278 'href' => false,
279 'ttip' => wfMsg('tooltip-nomove'),
280 'akey' => false);
281
282 }
283 }
284 }
285
286 if ( $wgUser->getID() != 0 and $action != 'edit' and $action != 'submit' ) {
287 if( !$wgTitle->userIsWatching()) {
288 $content_actions['watch'] = array('class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : '',
289 'text' => wfMsg('watch'),
290 'href' => $this->makeUrl($this->thispage, 'action=watch'),
291 'ttip' => wfMsg('tooltip-watch'),
292 'akey' => wfMsg('accesskey-watch'));
293 } else {
294 $content_actions['watch'] = array('class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : '',
295 'text' => wfMsg('unwatch'),
296 'href' => $this->makeUrl($this->thispage, 'action=unwatch'),
297 'ttip' => wfMsg('tooltip-unwatch'),
298 'akey' => wfMsg('accesskey-unwatch'));
299
300 }
301 }
302 } else {
303 /* show special page tab */
304
305 $content_actions['article'] = array('class' => 'selected',
306 'text' => wfMsg('specialpage'),
307 'href' => false,
308 'ttip' => wfMsg('tooltip-specialpage'),
309 'akey' => false);
310 }
311
312 return $content_actions;
313 }
314
315 # build array of common navigation links
316 function buildNavUrls () {
317 global $wgTitle, $wgUser, $wgRequest;
318 global $wgSiteSupportPage;
319
320 $action = $wgRequest->getText( 'action' );
321 $oldid = $wgRequest->getVal( 'oldid' );
322 $diff = $wgRequest->getVal( 'diff' );
323
324 $nav_urls = array();
325 $nav_urls['mainpage'] = array('href' => $this->makeI18nUrl('mainpage'));
326 $nav_urls['randompage'] = array('href' => $this->makeSpecialUrl('Randompage'));
327 $nav_urls['recentchanges'] = array('href' => $this->makeSpecialUrl('Recentchanges'));
328 $nav_urls['whatlinkshere'] = array('href' => $this->makeSpecialUrl('Whatlinkshere', 'target='.$this->thispage));
329 if(wfMsg('currentevents') != '-') {
330 $nav_urls['currentevents'] = array('href' => $this->makeI18nUrl('currentevents'));
331 }
332 $nav_urls['recentchangeslinked'] = array('href' => $this->makeSpecialUrl('Recentchangeslinked', 'target='.$this->thispage));
333 $nav_urls['bugreports'] = array('href' => $this->makeI18nUrl('bugreportspage'));
334 // $nav_urls['sitesupport'] = array('href' => $this->makeI18nUrl('sitesupportpage'));
335 $nav_urls['sitesupport'] = array('href' => $wgSiteSupportPage);
336 $nav_urls['help'] = array('href' => $this->makeI18nUrl('helppage'));
337 $nav_urls['upload'] = array('href' => $this->makeSpecialUrl('Upload'));
338 $nav_urls['specialpages'] = array('href' => $this->makeSpecialUrl('Specialpages'));
339
340 return $nav_urls;
341 }
342
343 /*static*/ function makeSpecialUrl( $name, $urlaction='' ) {
344 $title = Title::makeTitle( NS_SPECIAL, $name );
345 $this->checkTitle(&$title, &$name);
346 return $title->getLocalURL( $urlaction );
347 }
348 /*static*/ function makeTalkUrl ( $name, $urlaction='' ) {
349 $title = Title::newFromText( $name );
350 $title = $title->getTalkPage();
351 $this->checkTitle(&$title, &$name);
352 return $title->getLocalURL( $urlaction );
353 }
354 /*static*/ function makeArticleUrl ( $name, $urlaction='' ) {
355 $title = Title::newFromText( $name );
356 $title= $title->getSubjectPage();
357 $this->checkTitle(&$title, &$name);
358 return $title->getLocalURL( $urlaction );
359 }
360 /*static*/ function makeI18nUrl ( $name, $urlaction='' ) {
361 $title = Title::newFromText( wfMsg($name) );
362 $this->checkTitle(&$title, &$name);
363 return $title->getLocalURL( $urlaction );
364 }
365 /*static*/ function makeUrl ( $name, $urlaction='' ) {
366 $title = Title::newFromText( $name );
367 $this->checkTitle(&$title, &$name);
368 return $title->getLocalURL( $urlaction );
369 }
370
371 # make sure we have some title to operate on, mind the '&'
372 /*static*/ function checkTitle ( &$title, &$name ) {
373 if(!is_object($title)) {
374 $title = Title::newFromText( $name );
375 if(!is_object($title)) {
376 $title = Title::newFromText( '<error: link target missing>' );
377 }
378 }
379 }
380
381
382 }
383
384 class SkinDaVinci extends SkinPHPTal {
385 function initPage() {
386 SkinPHPTal::initPage();
387 $this->skinname = "davinci";
388 $this->template = "xhtml_slim";
389 }
390 }
391
392 class SkinMono extends SkinPHPTal {
393 function initPage() {
394 SkinPHPTal::initPage();
395 $this->skinname = "mono";
396 $this->template = "xhtml_slim";
397 }
398 }
399
400 class SkinMonoBook extends SkinPHPTal {
401 function initPage() {
402 SkinPHPTal::initPage();
403 $this->skinname = "monobook";
404 $this->template = "xhtml_slim";
405 }
406 }
407
408 ?>