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