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