quick hack to protect css/js subpages of userpages. Should be replaced by some more...
[mediawiki.git] / includes / SkinPHPTal.php
blob400222e4169e90f27f49e82aacc7dbafe2ddccc8
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/
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.
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;
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.
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.
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
29 require_once "PHPTAL.php";
31 class MediaWiki_I18N extends PHPTAL_I18N
33 var $_context = array();
35 function set($varName, $value) {
36 $this->_context[$varName] = $value;
39 function translate($value) {
40 $value = wfMsg( $value );
41 // interpolate variables
42 while (preg_match('/\$([0-9]*?)/sm', $value, $m)) {
43 list($src, $var) = $m;
44 $varValue = @$this->_context[$var];
45 $value = str_replace($src, $varValue, $value);
47 return $value;
51 class SkinPHPTal extends Skin {
52 var $template;
54 function initPage( &$out ) {
55 parent::initPage( $out );
56 $this->skinname = "davinci";
57 $this->template = "xhtml_slim";
60 function outputPage( &$out ) {
61 global $wgTitle, $wgArticle, $wgUser, $wgLang, $wgOut;
62 global $wgScript, $wgStylePath, $wgLanguageCode, $wgUseNewInterlanguage;
63 global $wgMimeType, $wgOutputEncoding, $wgUseDatabaseMessages, $wgRequest;
64 global $wgDisableCounters, $wgLogo, $action, $wgFeedClasses;
66 extract( $wgRequest->getValues( 'oldid', 'diff' ) );
68 $this->thispage = $wgTitle->getPrefixedDbKey();
69 $this->thisurl = $wgTitle->getPrefixedURL();
70 $this->thisurle = urlencode($this->thisurl);
71 $this->loggedin = $wgUser->getID() != 0;
72 $this->username = $wgUser->getName();
73 $this->userpage = $wgLang->getNsText( Namespace::getUser() ) . ":" . $wgUser->getName();
74 $this->titletxt = $wgTitle->getPrefixedText();
76 $this->initPage( $out );
77 $tpl = new PHPTAL($this->template . '.pt', 'templates');
79 #if ( $wgUseDatabaseMessages ) { // uncomment this to fall back to GetText
80 $tpl->setTranslator(new MediaWiki_I18N());
83 $tpl->set( "title", $wgOut->getPageTitle() );
84 $tpl->set( "pagetitle", $wgOut->getHTMLTitle() );
86 $tpl->setRef( "thispage", &$this->thispage );
87 $subpagestr = $this->subPageSubtitle();
88 $tpl->set(
89 "subtitle", !empty($subpagestr)?
90 '<span class="subpages">'.$subpagestr.'</span>'.$out->getSubtitle():
91 $out->getSubtitle()
93 $tpl->set( 'catlinks', $this->getCategories());
94 if( $wgOut->isSyndicated() ) {
95 $feeds = array();
96 foreach( $wgFeedClasses as $format => $class ) {
97 $feeds[$format] = array(
98 'text' => $format,
99 'href' => $wgRequest->appendQuery( "feed=$format" ),
100 'ttip' => wfMsg('tooltip-'.$format)
103 $tpl->setRef( 'feeds', &$feeds );
105 $tpl->setRef( 'mimetype', &$wgMimeType );
106 $tpl->setRef( 'charset', &$wgOutputEncoding );
107 $tpl->set( 'headlinks', $out->getHeadLinks() );
108 $tpl->setRef( 'skinname', &$this->skinname );
109 $tpl->setRef( "loggedin", &$this->loggedin );
110 /* XXX currently unused, might get useful later
111 $tpl->set( "editable", ($wgTitle->getNamespace() != NS_SPECIAL ) );
112 $tpl->set( "exists", $wgTitle->getArticleID() != 0 );
113 $tpl->set( "watch", $wgTitle->userIsWatching() ? "unwatch" : "watch" );
114 $tpl->set( "protect", count($wgTitle->getRestrictions()) ? "unprotect" : "protect" );
115 $tpl->set( "helppage", wfMsg('helppage'));
116 $tpl->set( "sysop", $wgUser->isSysop() );
118 $tpl->set( "searchaction", $this->escapeSearchLink() );
119 $tpl->setRef( "stylepath", &$wgStylePath );
120 $tpl->setRef( "logopath", &$wgLogo );
121 $tpl->setRef( "lang", &$wgLanguageCode );
122 $tpl->set( "dir", $wgLang->isRTL() ? "rtl" : "ltr" );
123 $tpl->set( "rtl", $wgLang->isRTL() );
124 $tpl->set( "langname", $wgLang->getLanguageName( $wgLanguageCode ) );
125 $tpl->setRef( "username", &$this->username );
126 $tpl->setRef( "userpage", &$this->userpage);
127 if( $wgUser->getNewtalk() ) {
128 $usertitle = Title::newFromText( $this->userpage );
129 $usertalktitle = $usertitle->getTalkPage();
130 if($usertalktitle->getPrefixedDbKey() != $this->thispage){
132 $ntl = wfMsg( "newmessages",
133 $this->makeKnownLink(
134 $wgLang->getNsText( Namespace::getTalk( Namespace::getUser() ) )
135 . ":" . $this->username,
136 wfMsg("newmessageslink") )
139 } else {
140 $ntl = "";
143 $tpl->setRef( "newtalk", &$ntl );
144 $tpl->setRef( "skin", &$this);
145 $tpl->set( "logo", $this->logoText() );
146 if ( $wgOut->isArticle() and (!isset( $oldid ) or isset( $diff )) and 0 != $wgArticle->getID() ) {
147 if ( !$wgDisableCounters ) {
148 $viewcount = $wgLang->formatNum( $wgArticle->getCount() );
149 if ( $viewcount ) {
150 $tpl->set('viewcount', wfMsg( "viewcount", $viewcount ));
153 $tpl->set('lastmod', $this->lastModified());
154 $tpl->set('copyright',$this->getCopyright());
156 $tpl->set( "copyrightico", $this->getCopyrightIcon() );
157 $tpl->set( "poweredbyico", $this->getPoweredBy() );
158 $tpl->set( "disclaimer", $this->disclaimerLink() );
159 $tpl->set( "about", $this->aboutLink() );
161 $tpl->setRef( "debug", &$out->mDebugtext );
162 $tpl->set( "reporttime", $out->reportTime() );
164 $tpl->setRef( "bodytext", &$out->mBodytext );
166 $language_urls = array();
167 foreach( $wgOut->getLanguageLinks() as $l ) {
168 $nt = Title::newFromText( $l );
169 $language_urls[] = array('href' => $nt->getFullURL(),
170 'text' => ($wgLang->getLanguageName( $nt->getInterwiki()) != ''?$wgLang->getLanguageName( $nt->getInterwiki()) : $l),
171 'class' => $wgLang->isRTL() ? 'rtl' : 'ltr');
173 if(count($language_urls)) {
174 $tpl->setRef( 'language_urls', &$language_urls);
175 } else {
176 $tpl->set('language_urls', false);
178 $tpl->set('personal_urls', $this->buildPersonalUrls());
179 $content_actions = $this->buildContentActionUrls();
180 $tpl->setRef('content_actions', &$content_actions);
181 // XXX: attach this from javascript, same with section editing
182 if(isset($content_actions['edit']['href']) &&
183 !(isset($content_actions['edit']['class']) && $content_actions['edit']['class'] != '') &&
184 $wgUser->getOption("editondblclick") )
186 $tpl->set('body-ondblclick', 'document.location = "' .$content_actions['edit']['href'] .'";');
187 } else {
188 $tpl->set('body-ondblclick', '');
190 $tpl->set( "nav_urls", $this->buildNavUrls() );
192 // execute template
193 $res = $tpl->execute();
194 // result may be an error
195 if (PEAR::isError($res)) {
196 echo $res->toString(), "\n";
197 } else {
198 echo $res;
203 # build array of urls for personal toolbar
204 function buildPersonalUrls() {
205 /* set up the default links for the personal toolbar */
206 global $wgShowIPinHeader;
207 $personal_urls = array();
208 if ($this->loggedin) {
209 $personal_urls['userpage'] = array(
210 'text' => $this->username,
211 'href' => $this->makeUrl($this->userpage),
212 'ttip' => wfMsg('tooltip-userpage'),
213 'akey' => wfMsg('accesskey-userpage')
215 $personal_urls['mytalk'] = array(
216 'text' => wfMsg('mytalk'),
217 'href' => $this->makeTalkUrl($this->userpage),
218 'ttip' => wfMsg('tooltip-mytalk'),
219 'akey' => wfMsg('accesskey-mytalk')
221 $personal_urls['preferences'] = array(
222 'text' => wfMsg('preferences'),
223 'href' => $this->makeSpecialUrl('Preferences'),
224 'ttip' => wfMsg('tooltip-preferences'),
225 'akey' => wfMsg('accesskey-preferences')
227 $personal_urls['watchlist'] = array(
228 'text' => wfMsg('watchlist'),
229 'href' => $this->makeSpecialUrl('Watchlist'),
230 'ttip' => wfMsg('tooltip-watchlist'),
231 'akey' => wfMsg('accesskey-watchlist')
233 $personal_urls['mycontris'] = array(
234 'text' => wfMsg('mycontris'),
235 'href' => $this->makeSpecialUrl('Contributions','target=' . $this->username),
236 'ttip' => wfMsg('tooltip-mycontris'),
237 'akey' => wfMsg('accesskey-mycontris')
239 $personal_urls['logout'] = array(
240 'text' => wfMsg('userlogout'),
241 'href' => $this->makeSpecialUrl('Userlogout','returnpage=' . $this->thisurle),
242 'ttip' => wfMsg('tooltip-logout'),
243 'akey' => wfMsg('accesskey-logout')
245 } else {
246 if( $wgShowIPinHeader && isset( $_COOKIE[ini_get("session.name")] ) ) {
247 $personal_urls['anonuserpage'] = array(
248 'text' => $this->username,
249 'href' => $this->makeUrl($this->userpage),
250 'ttip' => wfMsg('tooltip-anonuserpage'),
251 'akey' => wfMsg('accesskey-anonuserpage')
253 $personal_urls['anontalk'] = array(
254 'text' => wfMsg('anontalk'),
255 'href' => $this->makeTalkUrl($this->userpage),
256 'ttip' => wfMsg('tooltip-anontalk'),
257 'akey' => wfMsg('accesskey-anontalk')
259 $personal_urls['anonlogin'] = array(
260 'text' => wfMsg('userlogin'),
261 'href' => $this->makeSpecialUrl('Userlogin', 'return='.$this->thisurle),
262 'ttip' => wfMsg('tooltip-login'),
263 'akey' => wfMsg('accesskey-login')
265 } else {
267 $personal_urls['login'] = array(
268 'text' => wfMsg('userlogin'),
269 'href' => $this->makeSpecialUrl('Userlogin', 'return='.$this->thisurle),
270 'ttip' => wfMsg('tooltip-login'),
271 'akey' => wfMsg('accesskey-login')
276 return $personal_urls;
279 # an array of edit links by default used for the tabs
280 function buildContentActionUrls () {
281 global $wgTitle, $wgUser, $wgRequest;
282 $action = $wgRequest->getText( 'action' );
283 $oldid = $wgRequest->getVal( 'oldid' );
284 $diff = $wgRequest->getVal( 'diff' );
285 $content_actions = array();
287 $iscontent = ($wgTitle->getNamespace() != Namespace::getSpecial() );
288 if( $iscontent) {
290 $content_actions['article'] = array('class' => (!Namespace::isTalk( $wgTitle->getNamespace())) ? 'selected' : '',
291 'text' => wfMsg('article'),
292 'href' => $this->makeArticleUrl($this->thispage),
293 'ttip' => wfMsg('tooltip-article'),
294 'akey' => wfMsg('accesskey-article'));
296 /* set up the classes for the talk link */
297 $talk_class = (Namespace::isTalk( $wgTitle->getNamespace()) ? 'selected' : '');
298 $talktitle = Title::newFromText( $this->titletxt );
299 $talktitle = $talktitle->getTalkPage();
300 $this->checkTitle(&$talktitle, &$this->titletxt);
301 if($talktitle->getArticleId() != 0) {
302 $content_actions['talk'] = array(
303 'class' => $talk_class,
304 'text' => wfMsg('talk'),
305 'href' => $this->makeTalkUrl($this->titletxt),
306 'ttip' => wfMsg('tooltip-talk'),
307 'akey' => wfMsg('accesskey-talk')
309 } else {
310 $content_actions['talk'] = array(
311 'class' => $talk_class?$talk_class.' new':'new',
312 'text' => wfMsg('talk'),
313 'href' => $this->makeTalkUrl($this->titletxt,'action=edit'),
314 'ttip' => wfMsg('tooltip-talk'),
315 'akey' => wfMsg('accesskey-talk')
319 if ( $wgTitle->userCanEdit() ) {
320 $oid = ( $oldid && ! isset( $diff ) ) ? "&oldid={$oldid}" : '';
321 $content_actions['edit'] = array(
322 'class' => ($action == 'edit' or $action == 'submit') ? 'selected' : '',
323 'text' => wfMsg('edit'),
324 'href' => $this->makeUrl($this->thispage, 'action=edit'.$oid),
325 'ttip' => wfMsg('tooltip-edit'),
326 'akey' => wfMsg('accesskey-edit')
328 } else {
329 $oid = ( $oldid && ! isset( $diff ) ) ? "&oldid={$oldid}" : '';
330 $content_actions['edit'] = array('class' => ($action == 'edit') ? 'selected' : '',
331 'text' => wfMsg('viewsource'),
332 'href' => $this->makeUrl($this->thispage, 'action=edit'.$oid),
333 'ttip' => wfMsg('tooltip-viewsource'),
334 'akey' => wfMsg('accesskey-viewsource'));
337 if ( $wgTitle->getArticleId() ) {
339 $content_actions['history'] = array('class' => ($action == 'history') ? 'selected' : '',
340 'text' => wfMsg('history_short'),
341 'href' => $this->makeUrl($this->thispage, 'action=history'),
342 'ttip' => wfMsg('tooltip-history'),
343 'akey' => wfMsg('accesskey-history'));
345 # XXX: is there a rollback action anywhere or is it planned?
346 # Don't recall where i got this from...
347 /*if( $wgUser->getNewtalk() ) {
348 $content_actions['rollback'] = array('class' => ($action == 'rollback') ? 'selected' : '',
349 'text' => wfMsg('rollback_short'),
350 'href' => $this->makeUrl($this->thispage, 'action=rollback'),
351 'ttip' => wfMsg('tooltip-rollback'),
352 'akey' => wfMsg('accesskey-rollback'));
355 if($wgUser->isSysop()){
356 if(!$wgTitle->isProtected()){
357 $content_actions['protect'] = array(
358 'class' => ($action == 'protect') ? 'selected' : '',
359 'text' => wfMsg('protect'),
360 'href' => $this->makeUrl($this->thispage, 'action=protect'),
361 'ttip' => wfMsg('tooltip-protect'),
362 'akey' => wfMsg('accesskey-protect')
365 } else {
366 $content_actions['unprotect'] = array(
367 'class' => ($action == 'unprotect') ? 'selected' : '',
368 'text' => wfMsg('unprotect'),
369 'href' => $this->makeUrl($this->thispage, 'action=unprotect'),
370 'ttip' => wfMsg('tooltip-protect'),
371 'akey' => wfMsg('accesskey-protect')
374 $content_actions['delete'] = array(
375 'class' => ($action == 'delete') ? 'selected' : '',
376 'text' => wfMsg('delete'),
377 'href' => $this->makeUrl($this->thispage, 'action=delete'),
378 'ttip' => wfMsg('tooltip-delete'),
379 'akey' => wfMsg('accesskey-delete')
382 if ( $wgUser->getID() != 0 ) {
383 if ( $wgTitle->userCanEdit()) {
384 $content_actions['move'] = array('class' => ($wgTitle->getDbKey() == 'Movepage' and $wgTitle->getNamespace == Namespace::getSpecial()) ? 'selected' : '',
385 'text' => wfMsg('move'),
386 'href' => $this->makeSpecialUrl('Movepage', 'target='.$this->thispage),
387 'ttip' => wfMsg('tooltip-move'),
388 'akey' => wfMsg('accesskey-move'));
389 } else {
390 $content_actions['move'] = array('class' => 'inactive',
391 'text' => wfMsg('move'),
392 'href' => false,
393 'ttip' => wfMsg('tooltip-nomove'),
394 'akey' => false);
398 } else {
399 //article doesn't exist or is deleted
400 if($wgUser->isSysop()){
401 if( $n = $wgTitle->isDeleted() ) {
402 $content_actions['delete'] = array(
403 'class' => '',
404 'text' => wfMsg( "undelete_short", $n ),
405 'href' => $this->makeSpecialUrl('Undelete/'.$this->thispage),
406 'ttip' => wfMsg('tooltip-undelete', $n),
407 'akey' => wfMsg('accesskey-undelete')
413 if ( $wgUser->getID() != 0 and $action != 'edit' and $action != 'submit' ) {
414 if( !$wgTitle->userIsWatching()) {
415 $content_actions['watch'] = array('class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : '',
416 'text' => wfMsg('watch'),
417 'href' => $this->makeUrl($this->thispage, 'action=watch'),
418 'ttip' => wfMsg('tooltip-watch'),
419 'akey' => wfMsg('accesskey-watch'));
420 } else {
421 $content_actions['watch'] = array('class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : '',
422 'text' => wfMsg('unwatch'),
423 'href' => $this->makeUrl($this->thispage, 'action=unwatch'),
424 'ttip' => wfMsg('tooltip-unwatch'),
425 'akey' => wfMsg('accesskey-unwatch'));
429 } else {
430 /* show special page tab */
432 $content_actions['article'] = array('class' => 'selected',
433 'text' => wfMsg('specialpage'),
434 'href' => false,
435 'ttip' => wfMsg('tooltip-specialpage'),
436 'akey' => false);
439 return $content_actions;
442 # build array of common navigation links
443 function buildNavUrls () {
444 global $wgTitle, $wgUser, $wgRequest;
445 global $wgSiteSupportPage;
447 $action = $wgRequest->getText( 'action' );
448 $oldid = $wgRequest->getVal( 'oldid' );
449 $diff = $wgRequest->getVal( 'diff' );
450 // XXX: remove htmlspecialchars when tal:attributes works with i18n:attributes
451 $nav_urls = array();
452 $nav_urls['mainpage'] = array('href' => htmlspecialchars( $this->makeI18nUrl('mainpage')));
453 $nav_urls['randompage'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Randompage')));
454 $nav_urls['recentchanges'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Recentchanges')));
455 $nav_urls['whatlinkshere'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Whatlinkshere', 'target='.$this->thispage)));
456 $nav_urls['currentevents'] = (wfMsg('currentevents') != '-') ? array('href' => htmlspecialchars( $this->makeI18nUrl('currentevents'))) : '';
457 $nav_urls['portal'] = (wfMsg('portal') != '-') ? array('href' => htmlspecialchars( $this->makeI18nUrl('portal-url'))) : '';
458 $nav_urls['recentchangeslinked'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Recentchangeslinked', 'target='.$this->thispage)));
459 $nav_urls['bugreports'] = array('href' => htmlspecialchars( $this->makeI18nUrl('bugreportspage')));
460 // $nav_urls['sitesupport'] = array('href' => htmlspecialchars( $this->makeI18nUrl('sitesupportpage')));
461 $nav_urls['sitesupport'] = array('href' => htmlspecialchars( $wgSiteSupportPage));
462 $nav_urls['help'] = array('href' => htmlspecialchars( $this->makeI18nUrl('helppage')));
463 $nav_urls['upload'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Upload')));
464 $nav_urls['specialpages'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Specialpages')));
467 $id=User::idFromName($wgTitle->getText());
468 $ip=User::isIP($wgTitle->getText());
470 if($id || $ip) { # both anons and non-anons have contri list
471 $nav_urls['contributions'] = array(
472 'href' => htmlspecialchars( $this->makeSpecialUrl('Contributions', "target=" . $wgTitle->getPartialURL() ) )
475 if ( 0 != $wgUser->getID() ) { # show only to signed in users
476 if($id) { # can only email non-anons
477 $nav_urls['emailuser'] = array(
478 'href' => htmlspecialchars( $this->makeSpecialUrl('Emailuser', "target=" . $wgTitle->getPartialURL() ) )
484 return $nav_urls;
487 /*static*/ function makeSpecialUrl( $name, $urlaction='' ) {
488 $title = Title::makeTitle( NS_SPECIAL, $name );
489 $this->checkTitle(&$title, &$name);
490 return $title->getLocalURL( $urlaction );
492 /*static*/ function makeTalkUrl ( $name, $urlaction='' ) {
493 $title = Title::newFromText( $name );
494 $title = $title->getTalkPage();
495 $this->checkTitle(&$title, &$name);
496 return $title->getLocalURL( $urlaction );
498 /*static*/ function makeArticleUrl ( $name, $urlaction='' ) {
499 $title = Title::newFromText( $name );
500 $title= $title->getSubjectPage();
501 $this->checkTitle(&$title, &$name);
502 return $title->getLocalURL( $urlaction );
504 /*static*/ function makeI18nUrl ( $name, $urlaction='' ) {
505 $title = Title::newFromText( wfMsg($name) );
506 $this->checkTitle(&$title, &$name);
507 return $title->getLocalURL( $urlaction );
509 /*static*/ function makeUrl ( $name, $urlaction='' ) {
510 $title = Title::newFromText( $name );
511 $this->checkTitle(&$title, &$name);
512 return $title->getLocalURL( $urlaction );
515 # make sure we have some title to operate on, mind the '&'
516 /*static*/ function checkTitle ( &$title, &$name ) {
517 if(!is_object($title)) {
518 $title = Title::newFromText( $name );
519 if(!is_object($title)) {
520 $title = Title::newFromText( '<error: link target missing>' );
528 class SkinDaVinci extends SkinPHPTal {
529 function initPage( &$out ) {
530 SkinPHPTal::initPage( $out );
531 $this->skinname = "davinci";
535 class SkinMono extends SkinPHPTal {
536 function initPage( &$out ) {
537 SkinPHPTal::initPage( $out );
538 $this->skinname = "mono";
542 class SkinMonoBook extends SkinPHPTal {
543 function initPage( &$out ) {
544 SkinPHPTal::initPage( $out );
545 $this->skinname = "monobook";