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