* logo url specified in the xhtml src, uses $wgLogo
[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( &$out ) {
58 parent::initPage( $out );
59 $this->skinname = "davinci";
60 $this->template = "xhtml_slim";
61 }
62
63 function outputPage( &$out ) {
64 global $wgTitle, $wgArticle, $wgUser, $wgLang, $wgOut;
65 global $wgScript, $wgStyleSheetPath, $wgLanguageCode, $wgUseNewInterlanguage;
66 global $wgMimeType, $wgOutputEncoding, $wgUseDatabaseMessages, $wgRequest;
67 global $wgDisableCounters, $wgLogo;
68
69 extract( $wgRequest->getValues( 'oldid', 'diff' ) );
70
71 $this->thispage = $wgTitle->getPrefixedDbKey();
72 $this->loggedin = $wgUser->getID() != 0;
73 $this->username = $wgUser->getName();
74 $this->userpage = $wgLang->getNsText( Namespace::getUser() ) . ":" . $wgUser->getName();
75 $this->titletxt = $wgTitle->getPrefixedText();
76
77 $this->initPage( $out );
78 $tpl = new PHPTAL($this->template . '.pt', 'templates');
79
80 #if ( $wgUseDatabaseMessages ) { // uncomment this to fall back to GetText
81 $tpl->setTranslator(new MediaWiki_I18N());
82 #}
83
84 $tpl->setRef( "title", &$this->titletxt ); // ?
85 $tpl->set( "pagetitle", wfMsg( "pagetitle", $this->titletxt ) );
86 $tpl->setRef( "thispage", &$this->thispage );
87 $tpl->set( "subtitle", $out->getSubtitle() );
88 $tpl->set( 'catlinks', getCategories());
89 $tpl->setRef( 'mimetype', &$wgMimeType );
90 $tpl->setRef( 'charset', &$wgOutputEncoding );
91 $tpl->set( 'headlinks', $out->getHeadLinks() );
92 $tpl->setRef( 'skinname', &$this->skinname );
93 $tpl->setRef( "loggedin", &$this->loggedin );
94 /* XXX currently unused, might get useful later
95 $tpl->set( "editable", ($wgTitle->getNamespace() != NS_SPECIAL ) );
96 $tpl->set( "exists", $wgTitle->getArticleID() != 0 );
97 $tpl->set( "watch", $wgTitle->userIsWatching() ? "unwatch" : "watch" );
98 $tpl->set( "protect", count($wgTitle->getRestrictions()) ? "unprotect" : "protect" );
99 $tpl->set( "helppage", wfMsg('helppage'));
100 $tpl->set( "sysop", $wgUser->isSysop() );
101 */
102 $tpl->setRef( "searchaction", &$wgScript );
103 $tpl->setRef( "stylepath", &$wgStyleSheetPath );
104 $tpl->setRef( "logopath", &$wgLogo );
105 $tpl->setRef( "lang", &$wgLanguageCode );
106 $tpl->set( "langname", $wgLang->getLanguageName( $wgLanguageCode ) );
107 $tpl->setRef( "username", &$this->username );
108 $tpl->setRef( "userpage", &$this->userpage);
109 if( $wgUser->getNewtalk() ) {
110 $usertitle = Title::newFromText( $this->userpage );
111 $usertalktitle = $usertitle->getTalkPage();
112 if($usertalktitle->getPrefixedDbKey() != $this->thispage){
113
114 $ntl = wfMsg( "newmessages",
115 $this->makeKnownLink(
116 $wgLang->getNsText( Namespace::getTalk( Namespace::getUser() ) )
117 . ":" . $wgUser->getName(),
118 wfMsg("newmessageslink") )
119 );
120 }
121 } else {
122 $ntl = "";
123 }
124
125 $tpl->setRef( "newtalk", &$ntl );
126 $tpl->setRef( "skin", &$this);
127 $tpl->set( "logo", $this->logoText() );
128 if ( $wgOut->isArticle() and (!isset( $oldid ) or isset( $diff )) and 0 != $wgArticle->getID() ) {
129 if ( !$wgDisableCounters ) {
130 $viewcount = $wgLang->formatNum( $wgArticle->getCount() );
131 if ( $viewcount ) {
132 $tpl->set('viewcount', wfMsg( "viewcount", $viewcount ));
133 }
134 }
135 $tpl->set('lastmod', $this->lastModified());
136 $tpl->set('copyright',$this->getCopyright());
137 }
138 $tpl->set( "copyrightico", $this->getCopyrightIcon() );
139 $tpl->set( "poweredbyico", $this->getPoweredBy() );
140 $tpl->set( "disclaimer", $this->disclaimerLink() );
141 $tpl->set( "about", $this->aboutLink() );
142
143 $tpl->setRef( "debug", &$out->mDebugtext );
144 $tpl->set( "reporttime", $out->reportTime() );
145
146 $tpl->setRef( "bodytext", &$out->mBodytext );
147
148 $language_urls = array();
149 foreach( $wgOut->getLanguageLinks() as $l ) {
150 $nt = Title::newFromText( $l );
151 $language_urls[] = array('href' => $nt->getFullURL(),
152 'text' => ($wgLang->getLanguageName( $nt->getInterwiki()) != ''?$wgLang->getLanguageName( $nt->getInterwiki()) : $l),
153 'class' => $wgLang->isRTL() ? 'rtl' : 'ltr');
154 }
155 if(count($language_urls) != 0 ) {
156 $tpl->setRef( 'language_urls', &$language_urls);
157 } else {
158 $tpl->set('language_urls', false);
159 }
160 $tpl->set('personal_urls', $this->buildPersonalUrls());
161 $content_actions = $this->buildContentActionUrls();
162 $tpl->setRef('content_actions', &$content_actions);
163 // XXX: attach this from javascript, same with section editing
164 if(isset($content_actions['edit']['href']) &&
165 !(isset($content_actions['edit']['class']) && $content_actions['edit']['class'] != '') &&
166 $wgUser->getOption("editondblclick") )
167 {
168 $tpl->set('body-ondblclick', 'document.location = "' .$content_actions['edit']['href'] .'";');
169 } else {
170 $tpl->set('body-ondblclick', '');
171 }
172 $tpl->set( "nav_urls", $this->buildNavUrls() );
173
174 // execute template
175 $res = $tpl->execute();
176 // result may be an error
177 if (PEAR::isError($res)) {
178 echo $res->toString(), "\n";
179 } else {
180 echo $res;
181 }
182
183 }
184
185 # build array of urls for personal toolbar
186 function buildPersonalUrls() {
187 /* set up the default links for the personal toolbar */
188 $personal_urls = array();
189 if ($this->loggedin) {
190 $personal_urls['userpage'] = array('text' => $this->username,
191 'href' => $this->makeUrl($this->userpage),
192 'ttip' => wfMsg('tooltip-userpage'),
193 'akey' => wfMsg('accesskey-userpage'));
194 $personal_urls['mytalk'] = array('text' => wfMsg('mytalk'),
195 'href' => $this->makeTalkUrl($this->userpage),
196 'ttip' => wfMsg('tooltip-mytalk'),
197 'akey' => wfMsg('accesskey-mytalk'));
198 $personal_urls['preferences'] = array('text' => wfMsg('preferences'),
199 'href' => $this->makeSpecialUrl('Preferences'),
200 'ttip' => wfMsg('tooltip-preferences'),
201 'akey' => wfMsg('accesskey-preferences'));
202 $personal_urls['watchlist'] = array('text' => wfMsg('watchlist'),
203 'href' => $this->makeSpecialUrl('Watchlist'),
204 'ttip' => wfMsg('tooltip-watchlist'),
205 'akey' => wfMsg('accesskey-watchlist'));
206 $personal_urls['mycontris'] = array('text' => wfMsg('mycontris'),
207 'href' => $this->makeSpecialUrl('Contributions','target=' . $this->username),
208 'ttip' => wfMsg('tooltip-mycontris'),
209 'akey' => wfMsg('accesskey-mycontris'));
210 $personal_urls['logout'] = array('text' => wfMsg('userlogout'),
211 'href' => $this->makeSpecialUrl('Userlogout','returnpage=' . $this->thispage),
212 'ttip' => wfMsg('tooltip-logout'),
213 'akey' => wfMsg('accesskey-logout'));
214 } else {
215 $personal_urls['login'] = array('text' => wfMsg('userlogin'),
216 'href' => $this->makeSpecialUrl('Userlogin'),
217 'ttip' => wfMsg('tooltip-login'),
218 'akey' => wfMsg('accesskey-login'));
219 }
220 return $personal_urls;
221 }
222
223 # an array of edit links by default used for the tabs
224 function buildContentActionUrls () {
225 global $wgTitle, $wgUser, $wgRequest;
226 $action = $wgRequest->getText( 'action' );
227 $oldid = $wgRequest->getVal( 'oldid' );
228 $diff = $wgRequest->getVal( 'diff' );
229 $content_actions = array();
230
231 $iscontent = ($wgTitle->getNamespace() != Namespace::getSpecial() );
232 if( $iscontent) {
233
234 $content_actions['article'] = array('class' => (!Namespace::isTalk( $wgTitle->getNamespace())) ? 'selected' : '',
235 'text' => wfMsg('article'),
236 'href' => $this->makeArticleUrl($this->thispage),
237 'ttip' => wfMsg('tooltip-article'),
238 'akey' => wfMsg('accesskey-article'));
239
240 /* set up the classes for the talk link */
241 $talk_class = (Namespace::isTalk( $wgTitle->getNamespace()) ? 'selected' : '');
242 $talktitle = Title::newFromText( $this->titletxt );
243 $talktitle = $talktitle->getTalkPage();
244 $this->checkTitle(&$talktitle, &$this->titletxt);
245 if($talktitle->getArticleId() != 0) {
246 $content_actions['talk'] = array(
247 'class' => $talk_class,
248 'text' => wfMsg('talk'),
249 'href' => $this->makeTalkUrl($this->titletxt),
250 'ttip' => wfMsg('tooltip-talk'),
251 'akey' => wfMsg('accesskey-talk')
252 );
253 } else {
254 $content_actions['talk'] = array(
255 'class' => $talk_class.' new',
256 'text' => wfMsg('talk'),
257 'href' => $this->makeTalkUrl($this->titletxt,'action=edit'),
258 'ttip' => wfMsg('tooltip-talk'),
259 'akey' => wfMsg('accesskey-talk')
260 );
261 }
262
263 if ( $wgTitle->userCanEdit() ) {
264 $oid = ( $oldid && ! isset( $diff ) ) ? "&oldid={$oldid}" : '';
265 $content_actions['edit'] = array(
266 'class' => ($action == 'edit' or $action == 'submit') ? 'selected' : '',
267 'text' => wfMsg('edit'),
268 'href' => $this->makeUrl($this->thispage, 'action=edit'.$oid),
269 'ttip' => wfMsg('tooltip-edit'),
270 'akey' => wfMsg('accesskey-edit')
271 );
272 } else {
273 $oid = ( $oldid && ! isset( $diff ) ) ? "&oldid={$oldid}" : '';
274 $content_actions['edit'] = array('class' => ($action == 'edit') ? 'selected' : '',
275 'text' => wfMsg('viewsource'),
276 'href' => $this->makeUrl($this->thispage, 'action=edit'.$oid),
277 'ttip' => wfMsg('tooltip-viewsource'),
278 'akey' => wfMsg('accesskey-viewsource'));
279 }
280
281 if ( $wgTitle->getArticleId() ) {
282
283 $content_actions['history'] = array('class' => ($action == 'history') ? 'selected' : '',
284 'text' => wfMsg('history_short'),
285 'href' => $this->makeUrl($this->thispage, 'action=history'),
286 'ttip' => wfMsg('tooltip-history'),
287 'akey' => wfMsg('accesskey-history'));
288
289 # XXX: is there a rollback action anywhere or is it planned?
290 # Don't recall where i got this from...
291 /*if( $wgUser->getNewtalk() ) {
292 $content_actions['rollback'] = array('class' => ($action == 'rollback') ? 'selected' : '',
293 'text' => wfMsg('rollback_short'),
294 'href' => $this->makeUrl($this->thispage, 'action=rollback'),
295 'ttip' => wfMsg('tooltip-rollback'),
296 'akey' => wfMsg('accesskey-rollback'));
297 }*/
298
299 if($wgUser->isSysop()){
300 if(!$wgTitle->isProtected()){
301 $content_actions['protect'] = array(
302 'class' => ($action == 'protect') ? 'selected' : '',
303 'text' => wfMsg('protect'),
304 'href' => $this->makeUrl($this->thispage, 'action=protect'),
305 'ttip' => wfMsg('tooltip-protect'),
306 'akey' => wfMsg('accesskey-protect')
307 );
308
309 } else {
310 $content_actions['unprotect'] = array(
311 'class' => ($action == 'unprotect') ? 'selected' : '',
312 'text' => wfMsg('unprotect'),
313 'href' => $this->makeUrl($this->thispage, 'action=unprotect'),
314 'ttip' => wfMsg('tooltip-protect'),
315 'akey' => wfMsg('accesskey-protect')
316 );
317 }
318 $content_actions['delete'] = array(
319 'class' => ($action == 'delete') ? 'selected' : '',
320 'text' => wfMsg('delete'),
321 'href' => $this->makeUrl($this->thispage, 'action=delete'),
322 'ttip' => wfMsg('tooltip-delete'),
323 'akey' => wfMsg('accesskey-delete')
324 );
325 }
326 if ( $wgUser->getID() != 0 ) {
327 if ( $wgTitle->userCanEdit()) {
328 $content_actions['move'] = array('class' => ($wgTitle->getDbKey() == 'Movepage' and $wgTitle->getNamespace == Namespace::getSpecial()) ? 'selected' : '',
329 'text' => wfMsg('move'),
330 'href' => $this->makeSpecialUrl('Movepage', 'target='.$this->thispage),
331 'ttip' => wfMsg('tooltip-move'),
332 'akey' => wfMsg('accesskey-move'));
333 } else {
334 $content_actions['move'] = array('class' => 'inactive',
335 'text' => wfMsg('move'),
336 'href' => false,
337 'ttip' => wfMsg('tooltip-nomove'),
338 'akey' => false);
339
340 }
341 }
342 } else {
343 //article doesn't exist or is deleted
344 if($wgUser->isSysop()){
345 if( $n = $wgTitle->isDeleted() ) {
346 $content_actions['delete'] = array(
347 'class' => '',
348 'text' => wfMsg( "undelete_short", $n ),
349 'href' => $this->makeSpecialUrl('Undelete/'.$this->thispage),
350 'ttip' => wfMsg('tooltip-undelete', $n),
351 'akey' => wfMsg('accesskey-undelete')
352 );
353 }
354 }
355 }
356
357 if ( $wgUser->getID() != 0 and $action != 'edit' and $action != 'submit' ) {
358 if( !$wgTitle->userIsWatching()) {
359 $content_actions['watch'] = array('class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : '',
360 'text' => wfMsg('watch'),
361 'href' => $this->makeUrl($this->thispage, 'action=watch'),
362 'ttip' => wfMsg('tooltip-watch'),
363 'akey' => wfMsg('accesskey-watch'));
364 } else {
365 $content_actions['watch'] = array('class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : '',
366 'text' => wfMsg('unwatch'),
367 'href' => $this->makeUrl($this->thispage, 'action=unwatch'),
368 'ttip' => wfMsg('tooltip-unwatch'),
369 'akey' => wfMsg('accesskey-unwatch'));
370
371 }
372 }
373 } else {
374 /* show special page tab */
375
376 $content_actions['article'] = array('class' => 'selected',
377 'text' => wfMsg('specialpage'),
378 'href' => false,
379 'ttip' => wfMsg('tooltip-specialpage'),
380 'akey' => false);
381 }
382
383 return $content_actions;
384 }
385
386 # build array of common navigation links
387 function buildNavUrls () {
388 global $wgTitle, $wgUser, $wgRequest;
389 global $wgSiteSupportPage;
390
391 $action = $wgRequest->getText( 'action' );
392 $oldid = $wgRequest->getVal( 'oldid' );
393 $diff = $wgRequest->getVal( 'diff' );
394 // XXX: remove htmlspecialchars when tal:attributes works with i18n:attributes
395 $nav_urls = array();
396 $nav_urls['mainpage'] = array('href' => htmlspecialchars( $this->makeI18nUrl('mainpage')));
397 $nav_urls['randompage'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Randompage')));
398 $nav_urls['recentchanges'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Recentchanges')));
399 $nav_urls['whatlinkshere'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Whatlinkshere', 'target='.$this->thispage)));
400 $nav_urls['currentevents'] = (wfMsg('currentevents') != '') ? array('href' => htmlspecialchars( $this->makeI18nUrl('currentevents'))) : '';
401 $nav_urls['portal'] = (wfMsg('portal') != '') ? array('href' => htmlspecialchars( $this->makeI18nUrl('portal-url'))) : '';
402 $nav_urls['recentchangeslinked'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Recentchangeslinked', 'target='.$this->thispage)));
403 $nav_urls['bugreports'] = array('href' => htmlspecialchars( $this->makeI18nUrl('bugreportspage')));
404 // $nav_urls['sitesupport'] = array('href' => htmlspecialchars( $this->makeI18nUrl('sitesupportpage')));
405 $nav_urls['sitesupport'] = array('href' => htmlspecialchars( $wgSiteSupportPage));
406 $nav_urls['help'] = array('href' => htmlspecialchars( $this->makeI18nUrl('helppage')));
407 $nav_urls['upload'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Upload')));
408 $nav_urls['specialpages'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Specialpages')));
409
410
411 $id=User::idFromName($wgTitle->getText());
412 $ip=User::isIP($wgTitle->getText());
413
414 if($id || $ip) { # both anons and non-anons have contri list
415 $nav_urls['contributions'] = array(
416 'href' => htmlspecialchars( $this->makeSpecialUrl('Contributions', "target=" . $wgTitle->getPartialURL() ) )
417 );
418 }
419 if ( 0 != $wgUser->getID() ) { # show only to signed in users
420 if($id) { # can only email non-anons
421 $nav_urls['emailuser'] = array(
422 'href' => htmlspecialchars( $this->makeSpecialUrl('Emailuser', "target=" . $wgTitle->getPartialURL() ) )
423 );
424 }
425 }
426
427
428 return $nav_urls;
429 }
430
431 /*static*/ function makeSpecialUrl( $name, $urlaction='' ) {
432 $title = Title::makeTitle( NS_SPECIAL, $name );
433 $this->checkTitle(&$title, &$name);
434 return $title->getLocalURL( $urlaction );
435 }
436 /*static*/ function makeTalkUrl ( $name, $urlaction='' ) {
437 $title = Title::newFromText( $name );
438 $title = $title->getTalkPage();
439 $this->checkTitle(&$title, &$name);
440 return $title->getLocalURL( $urlaction );
441 }
442 /*static*/ function makeArticleUrl ( $name, $urlaction='' ) {
443 $title = Title::newFromText( $name );
444 $title= $title->getSubjectPage();
445 $this->checkTitle(&$title, &$name);
446 return $title->getLocalURL( $urlaction );
447 }
448 /*static*/ function makeI18nUrl ( $name, $urlaction='' ) {
449 $title = Title::newFromText( wfMsg($name) );
450 $this->checkTitle(&$title, &$name);
451 return $title->getLocalURL( $urlaction );
452 }
453 /*static*/ function makeUrl ( $name, $urlaction='' ) {
454 $title = Title::newFromText( $name );
455 $this->checkTitle(&$title, &$name);
456 return $title->getLocalURL( $urlaction );
457 }
458
459 # make sure we have some title to operate on, mind the '&'
460 /*static*/ function checkTitle ( &$title, &$name ) {
461 if(!is_object($title)) {
462 $title = Title::newFromText( $name );
463 if(!is_object($title)) {
464 $title = Title::newFromText( '<error: link target missing>' );
465 }
466 }
467 }
468
469
470 }
471
472 class SkinDaVinci extends SkinPHPTal {
473 function initPage( &$out ) {
474 SkinPHPTal::initPage( $out );
475 $this->skinname = "davinci";
476 }
477 }
478
479 class SkinMono extends SkinPHPTal {
480 function initPage( &$out ) {
481 SkinPHPTal::initPage( $out );
482 $this->skinname = "mono";
483 }
484 }
485
486 class SkinMonoBook extends SkinPHPTal {
487 function initPage( &$out ) {
488 SkinPHPTal::initPage( $out );
489 $this->skinname = "monobook";
490 }
491 }
492
493 ?>