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