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