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