d6b18c88a0173566553181164bd0a02b9ca5dda3
[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(&$talktitle, &$this->titletxt);
227 if($talktitle->getArticleId() != 0) {
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 } else {
236 $content_actions['talk'] = array(
237 'class' => $talk_class.' new',
238 'text' => wfMsg('talk'),
239 'href' => $this->makeTalkUrl($this->titletxt,'action=edit'),
240 'ttip' => wfMsg('tooltip-talk'),
241 'akey' => wfMsg('accesskey-talk')
242 );
243 }
244
245 if ( $wgTitle->userCanEdit() ) {
246 $oid = ( $oldid && ! isset( $diff ) ) ? "&oldid={$oldid}" : '';
247 $content_actions['edit'] = array(
248 'class' => ($action == 'edit' or $action == 'submit') ? 'selected' : '',
249 'text' => wfMsg('edit'),
250 'href' => $this->makeUrl($this->thispage, 'action=edit'.$oid),
251 'ttip' => wfMsg('tooltip-edit'),
252 'akey' => wfMsg('accesskey-edit')
253 );
254 } else {
255 $oid = ( $oldid && ! isset( $diff ) ) ? "&oldid={$oldid}" : '';
256 $content_actions['edit'] = array('class' => ($action == 'edit') ? 'selected' : '',
257 'text' => wfMsg('viewsource'),
258 'href' => $this->makeUrl($this->thispage, 'action=edit'.$oid),
259 'ttip' => wfMsg('tooltip-viewsource'),
260 'akey' => wfMsg('accesskey-viewsource'));
261 }
262
263 if ( $wgTitle->getArticleId() ) {
264
265 $content_actions['history'] = array('class' => ($action == 'history') ? 'selected' : '',
266 'text' => wfMsg('history_short'),
267 'href' => $this->makeUrl($this->thispage, 'action=history'),
268 'ttip' => wfMsg('tooltip-history'),
269 'akey' => wfMsg('accesskey-history'));
270
271 # XXX: is there a rollback action anywhere or is it planned?
272 # Don't recall where i got this from...
273 /*if( $wgUser->getNewtalk() ) {
274 $content_actions['rollback'] = array('class' => ($action == 'rollback') ? 'selected' : '',
275 'text' => wfMsg('rollback_short'),
276 'href' => $this->makeUrl($this->thispage, 'action=rollback'),
277 'ttip' => wfMsg('tooltip-rollback'),
278 'akey' => wfMsg('accesskey-rollback'));
279 }*/
280
281 if($wgUser->isSysop()){
282 if(!$wgTitle->isProtected()){
283 $content_actions['protect'] = array('class' => ($action == 'protect') ? 'selected' : '',
284 'text' => wfMsg('protect'),
285 'href' => $this->makeUrl($this->thispage, 'action=protect'),
286 'ttip' => wfMsg('tooltip-protect'),
287 'akey' => wfMsg('accesskey-protect'));
288
289 } else {
290 $content_actions['unprotect'] = array('class' => ($action == 'unprotect') ? 'selected' : '',
291 'text' => wfMsg('unprotect'),
292 'href' => $this->makeUrl($this->thispage, 'action=unprotect'),
293 'ttip' => wfMsg('tooltip-protect'),
294 'akey' => wfMsg('accesskey-protect'));
295 }
296 $content_actions['delete'] = array('class' => ($action == 'delete') ? 'selected' : '',
297 'text' => wfMsg('delete'),
298 'href' => $this->makeUrl($this->thispage, 'action=delete'),
299 'ttip' => wfMsg('tooltip-delete'),
300 'akey' => wfMsg('accesskey-delete'));
301 }
302 if ( $wgUser->getID() != 0 ) {
303 if ( $wgTitle->userCanEdit()) {
304 $content_actions['move'] = array('class' => ($wgTitle->getDbKey() == 'Movepage' and $wgTitle->getNamespace == Namespace::getSpecial()) ? 'selected' : '',
305 'text' => wfMsg('move'),
306 'href' => $this->makeSpecialUrl('Movepage', 'target='.$this->thispage),
307 'ttip' => wfMsg('tooltip-move'),
308 'akey' => wfMsg('accesskey-move'));
309 } else {
310 $content_actions['move'] = array('class' => 'inactive',
311 'text' => wfMsg('move'),
312 'href' => false,
313 'ttip' => wfMsg('tooltip-nomove'),
314 'akey' => false);
315
316 }
317 }
318 }
319
320 if ( $wgUser->getID() != 0 and $action != 'edit' and $action != 'submit' ) {
321 if( !$wgTitle->userIsWatching()) {
322 $content_actions['watch'] = array('class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : '',
323 'text' => wfMsg('watch'),
324 'href' => $this->makeUrl($this->thispage, 'action=watch'),
325 'ttip' => wfMsg('tooltip-watch'),
326 'akey' => wfMsg('accesskey-watch'));
327 } else {
328 $content_actions['watch'] = array('class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : '',
329 'text' => wfMsg('unwatch'),
330 'href' => $this->makeUrl($this->thispage, 'action=unwatch'),
331 'ttip' => wfMsg('tooltip-unwatch'),
332 'akey' => wfMsg('accesskey-unwatch'));
333
334 }
335 }
336 } else {
337 /* show special page tab */
338
339 $content_actions['article'] = array('class' => 'selected',
340 'text' => wfMsg('specialpage'),
341 'href' => false,
342 'ttip' => wfMsg('tooltip-specialpage'),
343 'akey' => false);
344 }
345
346 return $content_actions;
347 }
348
349 # build array of common navigation links
350 function buildNavUrls () {
351 global $wgTitle, $wgUser, $wgRequest;
352 global $wgSiteSupportPage;
353
354 $action = $wgRequest->getText( 'action' );
355 $oldid = $wgRequest->getVal( 'oldid' );
356 $diff = $wgRequest->getVal( 'diff' );
357 // XXX: remove htmlspecialchars when tal:attributes works with i18n:attributes
358 $nav_urls = array();
359 $nav_urls['mainpage'] = array('href' => htmlspecialchars( $this->makeI18nUrl('mainpage')));
360 $nav_urls['randompage'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Randompage')));
361 $nav_urls['recentchanges'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Recentchanges')));
362 $nav_urls['whatlinkshere'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Whatlinkshere', 'target='.$this->thispage)));
363 $nav_urls['currentevents'] = (wfMsg('currentevents') != '') ? array('href' => htmlspecialchars( $this->makeI18nUrl('currentevents'))) : '';
364 $nav_urls['portal'] = (wfMsg('portal') != '') ? array('href' => htmlspecialchars( $this->makeI18nUrl('portal-url'))) : '';
365 $nav_urls['recentchangeslinked'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Recentchangeslinked', 'target='.$this->thispage)));
366 $nav_urls['bugreports'] = array('href' => htmlspecialchars( $this->makeI18nUrl('bugreportspage')));
367 // $nav_urls['sitesupport'] = array('href' => htmlspecialchars( $this->makeI18nUrl('sitesupportpage')));
368 $nav_urls['sitesupport'] = array('href' => htmlspecialchars( $wgSiteSupportPage));
369 $nav_urls['help'] = array('href' => htmlspecialchars( $this->makeI18nUrl('helppage')));
370 $nav_urls['upload'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Upload')));
371 $nav_urls['specialpages'] = array('href' => htmlspecialchars( $this->makeSpecialUrl('Specialpages')));
372
373 return $nav_urls;
374 }
375
376 /*static*/ function makeSpecialUrl( $name, $urlaction='' ) {
377 $title = Title::makeTitle( NS_SPECIAL, $name );
378 $this->checkTitle(&$title, &$name);
379 return $title->getLocalURL( $urlaction );
380 }
381 /*static*/ function makeTalkUrl ( $name, $urlaction='' ) {
382 $title = Title::newFromText( $name );
383 $title = $title->getTalkPage();
384 $this->checkTitle(&$title, &$name);
385 return $title->getLocalURL( $urlaction );
386 }
387 /*static*/ function makeArticleUrl ( $name, $urlaction='' ) {
388 $title = Title::newFromText( $name );
389 $title= $title->getSubjectPage();
390 $this->checkTitle(&$title, &$name);
391 return $title->getLocalURL( $urlaction );
392 }
393 /*static*/ function makeI18nUrl ( $name, $urlaction='' ) {
394 $title = Title::newFromText( wfMsg($name) );
395 $this->checkTitle(&$title, &$name);
396 return $title->getLocalURL( $urlaction );
397 }
398 /*static*/ function makeUrl ( $name, $urlaction='' ) {
399 $title = Title::newFromText( $name );
400 $this->checkTitle(&$title, &$name);
401 return $title->getLocalURL( $urlaction );
402 }
403
404 # make sure we have some title to operate on, mind the '&'
405 /*static*/ function checkTitle ( &$title, &$name ) {
406 if(!is_object($title)) {
407 $title = Title::newFromText( $name );
408 if(!is_object($title)) {
409 $title = Title::newFromText( '<error: link target missing>' );
410 }
411 }
412 }
413
414
415 }
416
417 class SkinDaVinci extends SkinPHPTal {
418 function initPage( &$out ) {
419 SkinPHPTal::initPage( $out );
420 $this->skinname = "davinci";
421 }
422 }
423
424 class SkinMono extends SkinPHPTal {
425 function initPage( &$out ) {
426 SkinPHPTal::initPage( $out );
427 $this->skinname = "mono";
428 }
429 }
430
431 class SkinMonoBook extends SkinPHPTal {
432 function initPage( &$out ) {
433 SkinPHPTal::initPage( $out );
434 $this->skinname = "monobook";
435 }
436 }
437
438 ?>