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