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