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