* fix last commit so quickUserCan() actually skips the cascading query
[lhc/web/wiklou.git] / includes / SkinTemplate.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( 1 );
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Template-filler skin base class
22 * Formerly generic PHPTal (http://phptal.sourceforge.net/) skin
23 * Based on Brion's smarty skin
24 * Copyright (C) Gabriel Wicke -- http://www.aulinx.de/
25 *
26 * Todo: Needs some serious refactoring into functions that correspond
27 * to the computations individual esi snippets need. Most importantly no body
28 * parsing for most of those of course.
29 *
30 * @package MediaWiki
31 * @subpackage Skins
32 */
33
34 /**
35 * Wrapper object for MediaWiki's localization functions,
36 * to be passed to the template engine.
37 *
38 * @private
39 * @package MediaWiki
40 */
41 class MediaWiki_I18N {
42 var $_context = array();
43
44 function set($varName, $value) {
45 $this->_context[$varName] = $value;
46 }
47
48 function translate($value) {
49 $fname = 'SkinTemplate-translate';
50 wfProfileIn( $fname );
51
52 // Hack for i18n:attributes in PHPTAL 1.0.0 dev version as of 2004-10-23
53 $value = preg_replace( '/^string:/', '', $value );
54
55 $value = wfMsg( $value );
56 // interpolate variables
57 $m = array();
58 while (preg_match('/\$([0-9]*?)/sm', $value, $m)) {
59 list($src, $var) = $m;
60 wfSuppressWarnings();
61 $varValue = $this->_context[$var];
62 wfRestoreWarnings();
63 $value = str_replace($src, $varValue, $value);
64 }
65 wfProfileOut( $fname );
66 return $value;
67 }
68 }
69
70 /**
71 *
72 * @package MediaWiki
73 */
74 class SkinTemplate extends Skin {
75 /**#@+
76 * @private
77 */
78
79 /**
80 * Name of our skin, set in initPage()
81 * It probably need to be all lower case.
82 */
83 var $skinname;
84
85 /**
86 * Stylesheets set to use
87 * Sub directory in ./skins/ where various stylesheets are located
88 */
89 var $stylename;
90
91 /**
92 * For QuickTemplate, the name of the subclass which
93 * will actually fill the template.
94 */
95 var $template;
96
97 /**#@-*/
98
99 /**
100 * Setup the base parameters...
101 * Child classes should override this to set the name,
102 * style subdirectory, and template filler callback.
103 *
104 * @param OutputPage $out
105 */
106 function initPage( &$out ) {
107 parent::initPage( $out );
108 $this->skinname = 'monobook';
109 $this->stylename = 'monobook';
110 $this->template = 'QuickTemplate';
111 }
112
113 /**
114 * Create the template engine object; we feed it a bunch of data
115 * and eventually it spits out some HTML. Should have interface
116 * roughly equivalent to PHPTAL 0.7.
117 *
118 * @param string $callback (or file)
119 * @param string $repository subdirectory where we keep template files
120 * @param string $cache_dir
121 * @return object
122 * @private
123 */
124 function setupTemplate( $classname, $repository=false, $cache_dir=false ) {
125 return new $classname();
126 }
127
128 /**
129 * initialize various variables and generate the template
130 *
131 * @param OutputPage $out
132 * @public
133 */
134 function outputPage( &$out ) {
135 global $wgTitle, $wgArticle, $wgUser, $wgLang, $wgContLang, $wgOut;
136 global $wgScript, $wgStylePath, $wgContLanguageCode;
137 global $wgMimeType, $wgJsMimeType, $wgOutputEncoding, $wgRequest;
138 global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces;
139 global $wgDisableCounters, $wgLogo, $action, $wgFeedClasses, $wgHideInterlanguageLinks;
140 global $wgMaxCredits, $wgShowCreditsIfMax;
141 global $wgPageShowWatchingUsers;
142 global $wgUseTrackbacks;
143 global $wgArticlePath, $wgScriptPath, $wgServer, $wgLang, $wgCanonicalNamespaceNames;
144
145 $fname = 'SkinTemplate::outputPage';
146 wfProfileIn( $fname );
147
148 // Hook that allows last minute changes to the output page, e.g.
149 // adding of CSS or Javascript by extensions.
150 wfRunHooks( 'BeforePageDisplay', array( &$out ) );
151
152 $oldid = $wgRequest->getVal( 'oldid' );
153 $diff = $wgRequest->getVal( 'diff' );
154
155 wfProfileIn( "$fname-init" );
156 $this->initPage( $out );
157
158 $this->mTitle =& $wgTitle;
159 $this->mUser =& $wgUser;
160
161 $tpl = $this->setupTemplate( $this->template, 'skins' );
162
163 #if ( $wgUseDatabaseMessages ) { // uncomment this to fall back to GetText
164 $tpl->setTranslator(new MediaWiki_I18N());
165 #}
166 wfProfileOut( "$fname-init" );
167
168 wfProfileIn( "$fname-stuff" );
169 $this->thispage = $this->mTitle->getPrefixedDbKey();
170 $this->thisurl = $this->mTitle->getPrefixedURL();
171 $this->loggedin = $wgUser->isLoggedIn();
172 $this->iscontent = ($this->mTitle->getNamespace() != NS_SPECIAL );
173 $this->iseditable = ($this->iscontent and !($action == 'edit' or $action == 'submit'));
174 $this->username = $wgUser->getName();
175 $userPage = $wgUser->getUserPage();
176 $this->userpage = $userPage->getPrefixedText();
177
178 if ( $wgUser->isLoggedIn() || $this->showIPinHeader() ) {
179 $this->userpageUrlDetails = self::makeUrlDetails( $this->userpage );
180 } else {
181 # This won't be used in the standard skins, but we define it to preserve the interface
182 # To save time, we check for existence
183 $this->userpageUrlDetails = self::makeKnownUrlDetails( $this->userpage );
184 }
185
186 $this->usercss = $this->userjs = $this->userjsprev = false;
187 $this->setupUserCss();
188 $this->setupUserJs();
189 $this->titletxt = $this->mTitle->getPrefixedText();
190 wfProfileOut( "$fname-stuff" );
191
192 wfProfileIn( "$fname-stuff2" );
193 $tpl->set( 'title', $wgOut->getPageTitle() );
194 $tpl->set( 'pagetitle', $wgOut->getHTMLTitle() );
195 $tpl->set( 'displaytitle', $wgOut->mPageLinkTitle );
196 $tpl->set( 'pageclass', Sanitizer::escapeClass( 'page-'.$wgTitle->getPrefixedText() ) );
197
198 $nsname = isset( $wgCanonicalNamespaceNames[ $this->mTitle->getNamespace() ] ) ?
199 $wgCanonicalNamespaceNames[ $this->mTitle->getNamespace() ] :
200 $this->mTitle->getNsText();
201
202 $tpl->set( 'nscanonical', $nsname );
203 $tpl->set( 'nsnumber', $this->mTitle->getNamespace() );
204 $tpl->set( 'titleprefixeddbkey', $this->mTitle->getPrefixedDBKey() );
205 $tpl->set( 'titletext', $this->mTitle->getText() );
206 $tpl->set( 'articleid', $this->mTitle->getArticleId() );
207 $tpl->set( 'currevisionid', isset( $wgArticle ) ? $wgArticle->getLatest() : 0 );
208
209 $tpl->set( 'isarticle', $wgOut->isArticle() );
210
211 $tpl->setRef( "thispage", $this->thispage );
212 $subpagestr = $this->subPageSubtitle();
213 $tpl->set(
214 'subtitle', !empty($subpagestr)?
215 '<span class="subpages">'.$subpagestr.'</span>'.$out->getSubtitle():
216 $out->getSubtitle()
217 );
218 $undelete = $this->getUndeleteLink();
219 $tpl->set(
220 "undelete", !empty($undelete)?
221 '<span class="subpages">'.$undelete.'</span>':
222 ''
223 );
224
225 $tpl->set( 'catlinks', $this->getCategories());
226 if( $wgOut->isSyndicated() ) {
227 $feeds = array();
228 foreach( $wgFeedClasses as $format => $class ) {
229 $linktext = $format;
230 if ( $format == "atom" ) {
231 $linktext = wfMsg( 'feed-atom' );
232 } else if ( $format == "rss" ) {
233 $linktext = wfMsg( 'feed-rss' );
234 }
235 $feeds[$format] = array(
236 'text' => $linktext,
237 'href' => $wgRequest->appendQuery( "feed=$format" )
238 );
239 }
240 $tpl->setRef( 'feeds', $feeds );
241 } else {
242 $tpl->set( 'feeds', false );
243 }
244 if ($wgUseTrackbacks && $out->isArticleRelated()) {
245 $tpl->set( 'trackbackhtml', $wgTitle->trackbackRDF() );
246 } else {
247 $tpl->set( 'trackbackhtml', null );
248 }
249
250 $tpl->setRef( 'xhtmldefaultnamespace', $wgXhtmlDefaultNamespace );
251 $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
252 $tpl->setRef( 'mimetype', $wgMimeType );
253 $tpl->setRef( 'jsmimetype', $wgJsMimeType );
254 $tpl->setRef( 'charset', $wgOutputEncoding );
255 $tpl->set( 'headlinks', $out->getHeadLinks() );
256 $tpl->set('headscripts', $out->getScript() );
257 $tpl->setRef( 'wgScript', $wgScript );
258 $tpl->setRef( 'skinname', $this->skinname );
259 $tpl->set( 'skinclass', get_class( $this ) );
260 $tpl->setRef( 'stylename', $this->stylename );
261 $tpl->set( 'printable', $wgRequest->getBool( 'printable' ) );
262 $tpl->setRef( 'loggedin', $this->loggedin );
263 $tpl->set('nsclass', 'ns-'.$this->mTitle->getNamespace());
264 $tpl->set('notspecialpage', $this->mTitle->getNamespace() != NS_SPECIAL);
265 /* XXX currently unused, might get useful later
266 $tpl->set( "editable", ($this->mTitle->getNamespace() != NS_SPECIAL ) );
267 $tpl->set( "exists", $this->mTitle->getArticleID() != 0 );
268 $tpl->set( "watch", $this->mTitle->userIsWatching() ? "unwatch" : "watch" );
269 $tpl->set( "protect", count($this->mTitle->isProtected()) ? "unprotect" : "protect" );
270 $tpl->set( "helppage", wfMsg('helppage'));
271 */
272 $tpl->set( 'searchaction', $this->escapeSearchLink() );
273 $tpl->set( 'search', trim( $wgRequest->getVal( 'search' ) ) );
274 $tpl->setRef( 'stylepath', $wgStylePath );
275 $tpl->setRef( 'articlepath', $wgArticlePath );
276 $tpl->setRef( 'scriptpath', $wgScriptPath );
277 $tpl->setRef( 'serverurl', $wgServer );
278 $tpl->setRef( 'logopath', $wgLogo );
279 $tpl->setRef( "lang", $wgContLanguageCode );
280 $tpl->set( 'dir', $wgContLang->isRTL() ? "rtl" : "ltr" );
281 $tpl->set( 'rtl', $wgContLang->isRTL() );
282 $tpl->set( 'langname', $wgContLang->getLanguageName( $wgContLanguageCode ) );
283 $tpl->set( 'showjumplinks', $wgUser->getOption( 'showjumplinks' ) );
284 $tpl->set( 'username', $wgUser->isAnon() ? NULL : $this->username );
285 $tpl->setRef( 'userpage', $this->userpage);
286 $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href']);
287 $tpl->set( 'userlang', $wgLang->getCode() );
288 $tpl->set( 'pagecss', $this->setupPageCss() );
289 $tpl->setRef( 'usercss', $this->usercss);
290 $tpl->setRef( 'userjs', $this->userjs);
291 $tpl->setRef( 'userjsprev', $this->userjsprev);
292 global $wgUseSiteJs;
293 if ($wgUseSiteJs) {
294 if($this->loggedin) {
295 $tpl->set( 'jsvarurl', self::makeUrl('-','action=raw&smaxage=0&gen=js') );
296 } else {
297 $tpl->set( 'jsvarurl', self::makeUrl('-','action=raw&gen=js') );
298 }
299 } else {
300 $tpl->set('jsvarurl', false);
301 }
302 $newtalks = $wgUser->getNewMessageLinks();
303
304 if (count($newtalks) == 1 && $newtalks[0]["wiki"] === wfWikiID() ) {
305 $usertitle = $this->mUser->getUserPage();
306 $usertalktitle = $usertitle->getTalkPage();
307 if( !$usertalktitle->equals( $this->mTitle ) ) {
308 $ntl = wfMsg( 'youhavenewmessages',
309 $this->makeKnownLinkObj(
310 $usertalktitle,
311 wfMsgHtml( 'newmessageslink' ),
312 'redirect=no'
313 ),
314 $this->makeKnownLinkObj(
315 $usertalktitle,
316 wfMsgHtml( 'newmessagesdifflink' ),
317 'diff=cur'
318 )
319 );
320 # Disable Cache
321 $wgOut->setSquidMaxage(0);
322 }
323 } else if (count($newtalks)) {
324 $sep = str_replace("_", " ", wfMsgHtml("newtalkseperator"));
325 $msgs = array();
326 foreach ($newtalks as $newtalk) {
327 $msgs[] = wfElement("a",
328 array('href' => $newtalk["link"]), $newtalk["wiki"]);
329 }
330 $parts = implode($sep, $msgs);
331 $ntl = wfMsgHtml('youhavenewmessagesmulti', $parts);
332 $wgOut->setSquidMaxage(0);
333 } else {
334 $ntl = '';
335 }
336 wfProfileOut( "$fname-stuff2" );
337
338 wfProfileIn( "$fname-stuff3" );
339 $tpl->setRef( 'newtalk', $ntl );
340 $tpl->setRef( 'skin', $this);
341 $tpl->set( 'logo', $this->logoText() );
342 if ( $wgOut->isArticle() and (!isset( $oldid ) or isset( $diff )) and
343 $wgArticle and 0 != $wgArticle->getID() )
344 {
345 if ( !$wgDisableCounters ) {
346 $viewcount = $wgLang->formatNum( $wgArticle->getCount() );
347 if ( $viewcount ) {
348 $tpl->set('viewcount', wfMsgExt( 'viewcount', array( 'parseinline' ), $viewcount ) );
349 } else {
350 $tpl->set('viewcount', false);
351 }
352 } else {
353 $tpl->set('viewcount', false);
354 }
355
356 if ($wgPageShowWatchingUsers) {
357 $dbr =& wfGetDB( DB_SLAVE );
358 $watchlist = $dbr->tableName( 'watchlist' );
359 $sql = "SELECT COUNT(*) AS n FROM $watchlist
360 WHERE wl_title='" . $dbr->strencode($this->mTitle->getDBKey()) .
361 "' AND wl_namespace=" . $this->mTitle->getNamespace() ;
362 $res = $dbr->query( $sql, 'SkinTemplate::outputPage');
363 $x = $dbr->fetchObject( $res );
364 $numberofwatchingusers = $x->n;
365 if ($numberofwatchingusers > 0) {
366 $tpl->set('numberofwatchingusers', wfMsg('number_of_watching_users_pageview', $numberofwatchingusers));
367 } else {
368 $tpl->set('numberofwatchingusers', false);
369 }
370 } else {
371 $tpl->set('numberofwatchingusers', false);
372 }
373
374 $tpl->set('copyright',$this->getCopyright());
375
376 $this->credits = false;
377
378 if (isset($wgMaxCredits) && $wgMaxCredits != 0) {
379 require_once("Credits.php");
380 $this->credits = getCredits($wgArticle, $wgMaxCredits, $wgShowCreditsIfMax);
381 } else {
382 $tpl->set('lastmod', $this->lastModified());
383 }
384
385 $tpl->setRef( 'credits', $this->credits );
386
387 } elseif ( isset( $oldid ) && !isset( $diff ) ) {
388 $tpl->set('copyright', $this->getCopyright());
389 $tpl->set('viewcount', false);
390 $tpl->set('lastmod', false);
391 $tpl->set('credits', false);
392 $tpl->set('numberofwatchingusers', false);
393 } else {
394 $tpl->set('copyright', false);
395 $tpl->set('viewcount', false);
396 $tpl->set('lastmod', false);
397 $tpl->set('credits', false);
398 $tpl->set('numberofwatchingusers', false);
399 }
400 wfProfileOut( "$fname-stuff3" );
401
402 wfProfileIn( "$fname-stuff4" );
403 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
404 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
405 $tpl->set( 'disclaimer', $this->disclaimerLink() );
406 $tpl->set( 'privacy', $this->privacyLink() );
407 $tpl->set( 'about', $this->aboutLink() );
408
409 $tpl->setRef( 'debug', $out->mDebugtext );
410 $tpl->set( 'reporttime', $out->reportTime() );
411 $tpl->set( 'sitenotice', wfGetSiteNotice() );
412 $tpl->set( 'bottomscripts', $this->bottomScripts() );
413
414 $printfooter = "<div class=\"printfooter\">\n" . $this->printSource() . "</div>\n";
415 $out->mBodytext .= $printfooter ;
416 $tpl->setRef( 'bodytext', $out->mBodytext );
417
418 # Language links
419 $language_urls = array();
420
421 if ( !$wgHideInterlanguageLinks ) {
422 foreach( $wgOut->getLanguageLinks() as $l ) {
423 $tmp = explode( ':', $l, 2 );
424 $class = 'interwiki-' . $tmp[0];
425 unset($tmp);
426 $nt = Title::newFromText( $l );
427 $language_urls[] = array(
428 'href' => $nt->getFullURL(),
429 'text' => ($wgContLang->getLanguageName( $nt->getInterwiki()) != ''?$wgContLang->getLanguageName( $nt->getInterwiki()) : $l),
430 'class' => $class
431 );
432 }
433 }
434 if(count($language_urls)) {
435 $tpl->setRef( 'language_urls', $language_urls);
436 } else {
437 $tpl->set('language_urls', false);
438 }
439 wfProfileOut( "$fname-stuff4" );
440
441 # Personal toolbar
442 $tpl->set('personal_urls', $this->buildPersonalUrls());
443 $content_actions = $this->buildContentActionUrls();
444 $tpl->setRef('content_actions', $content_actions);
445
446 // XXX: attach this from javascript, same with section editing
447 if($this->iseditable && $wgUser->getOption("editondblclick") )
448 {
449 $tpl->set('body_ondblclick', 'document.location = "' .$content_actions['edit']['href'] .'";');
450 } else {
451 $tpl->set('body_ondblclick', false);
452 }
453 if( $this->iseditable && $wgUser->getOption( 'editsectiononrightclick' ) ) {
454 $tpl->set( 'body_onload', 'setupRightClickEdit()' );
455 } else {
456 $tpl->set( 'body_onload', false );
457 }
458 $tpl->set( 'sidebar', $this->buildSidebar() );
459 $tpl->set( 'nav_urls', $this->buildNavUrls() );
460
461 // original version by hansm
462 if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
463 wfDebug( __METHOD__ . ': Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!' );
464 }
465
466 // execute template
467 wfProfileIn( "$fname-execute" );
468 $res = $tpl->execute();
469 wfProfileOut( "$fname-execute" );
470
471 // result may be an error
472 $this->printOrError( $res );
473 wfProfileOut( $fname );
474 }
475
476 /**
477 * Output the string, or print error message if it's
478 * an error object of the appropriate type.
479 * For the base class, assume strings all around.
480 *
481 * @param mixed $str
482 * @private
483 */
484 function printOrError( $str ) {
485 echo $str;
486 }
487
488 /**
489 * build array of urls for personal toolbar
490 * @return array
491 * @private
492 */
493 function buildPersonalUrls() {
494 global $wgTitle, $wgShowIPinHeader;
495
496 $fname = 'SkinTemplate::buildPersonalUrls';
497 $pageurl = $wgTitle->getLocalURL();
498 wfProfileIn( $fname );
499
500 /* set up the default links for the personal toolbar */
501 $personal_urls = array();
502 if ($this->loggedin) {
503 $personal_urls['userpage'] = array(
504 'text' => $this->username,
505 'href' => &$this->userpageUrlDetails['href'],
506 'class' => $this->userpageUrlDetails['exists']?false:'new',
507 'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
508 );
509 $usertalkUrlDetails = $this->makeTalkUrlDetails($this->userpage);
510 $personal_urls['mytalk'] = array(
511 'text' => wfMsg('mytalk'),
512 'href' => &$usertalkUrlDetails['href'],
513 'class' => $usertalkUrlDetails['exists']?false:'new',
514 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
515 );
516 $href = self::makeSpecialUrl( 'Preferences' );
517 $personal_urls['preferences'] = array(
518 'text' => wfMsg( 'mypreferences' ),
519 'href' => self::makeSpecialUrl( 'Preferences' ),
520 'active' => ( $href == $pageurl )
521 );
522 $href = self::makeSpecialUrl( 'Watchlist' );
523 $personal_urls['watchlist'] = array(
524 'text' => wfMsg( 'watchlist' ),
525 'href' => $href,
526 'active' => ( $href == $pageurl )
527 );
528 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
529 $personal_urls['mycontris'] = array(
530 'text' => wfMsg( 'mycontris' ),
531 'href' => $href,
532 // FIXME # 'active' was disabed in r11346 with message: "disable bold link to my contributions; link was bold on all
533 // Special:Contributions, not just current user's (fix me please!)". Until resolved, explicitly setting active to false.
534 'active' => false # ( ( $href == $pageurl . '/' . $this->username )
535 );
536 $personal_urls['logout'] = array(
537 'text' => wfMsg( 'userlogout' ),
538 'href' => self::makeSpecialUrl( 'Userlogout',
539 $wgTitle->isSpecial( 'Preferences' ) ? '' : "returnto={$this->thisurl}"
540 ),
541 'active' => false
542 );
543 } else {
544 if( $wgShowIPinHeader && isset( $_COOKIE[ini_get("session.name")] ) ) {
545 $href = &$this->userpageUrlDetails['href'];
546 $personal_urls['anonuserpage'] = array(
547 'text' => $this->username,
548 'href' => $href,
549 'class' => $this->userpageUrlDetails['exists']?false:'new',
550 'active' => ( $pageurl == $href )
551 );
552 $usertalkUrlDetails = $this->makeTalkUrlDetails($this->userpage);
553 $href = &$usertalkUrlDetails['href'];
554 $personal_urls['anontalk'] = array(
555 'text' => wfMsg('anontalk'),
556 'href' => $href,
557 'class' => $usertalkUrlDetails['exists']?false:'new',
558 'active' => ( $pageurl == $href )
559 );
560 $personal_urls['anonlogin'] = array(
561 'text' => wfMsg('userlogin'),
562 'href' => self::makeSpecialUrl( 'Userlogin', 'returnto=' . $this->thisurl ),
563 'active' => $wgTitle->isSpecial( 'Userlogin' )
564 );
565 } else {
566
567 $personal_urls['login'] = array(
568 'text' => wfMsg('userlogin'),
569 'href' => self::makeSpecialUrl( 'Userlogin', 'returnto=' . $this->thisurl ),
570 'active' => $wgTitle->isSpecial( 'Userlogin' )
571 );
572 }
573 }
574
575 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$wgTitle ) );
576 wfProfileOut( $fname );
577 return $personal_urls;
578 }
579
580 /**
581 * Returns true if the IP should be shown in the header
582 */
583 function showIPinHeader() {
584 global $wgShowIPinHeader;
585 return $wgShowIPinHeader && isset( $_COOKIE[ini_get("session.name")] );
586 }
587
588 function tabAction( $title, $message, $selected, $query='', $checkEdit=false ) {
589 $classes = array();
590 if( $selected ) {
591 $classes[] = 'selected';
592 }
593 if( $checkEdit && $title->getArticleId() == 0 ) {
594 $classes[] = 'new';
595 $query = 'action=edit';
596 }
597
598 $text = wfMsg( $message );
599 if ( wfEmptyMsg( $message, $text ) ) {
600 global $wgContLang;
601 $text = $wgContLang->getFormattedNsText( Namespace::getSubject( $title->getNamespace() ) );
602 }
603
604 return array(
605 'class' => implode( ' ', $classes ),
606 'text' => $text,
607 'href' => $title->getLocalUrl( $query ) );
608 }
609
610 function makeTalkUrlDetails( $name, $urlaction = '' ) {
611 $title = Title::newFromText( $name );
612 $title = $title->getTalkPage();
613 self::checkTitle( $title, $name );
614 return array(
615 'href' => $title->getLocalURL( $urlaction ),
616 'exists' => $title->getArticleID() != 0 ? true : false
617 );
618 }
619
620 function makeArticleUrlDetails( $name, $urlaction = '' ) {
621 $title = Title::newFromText( $name );
622 $title= $title->getSubjectPage();
623 self::checkTitle( $title, $name );
624 return array(
625 'href' => $title->getLocalURL( $urlaction ),
626 'exists' => $title->getArticleID() != 0 ? true : false
627 );
628 }
629
630 /**
631 * an array of edit links by default used for the tabs
632 * @return array
633 * @private
634 */
635 function buildContentActionUrls () {
636 global $wgContLang, $wgOut;
637 $fname = 'SkinTemplate::buildContentActionUrls';
638 wfProfileIn( $fname );
639
640 global $wgUser, $wgRequest;
641 $action = $wgRequest->getText( 'action' );
642 $section = $wgRequest->getText( 'section' );
643 $content_actions = array();
644
645 $prevent_active_tabs = false ;
646 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this , &$prevent_active_tabs ) ) ;
647
648 if( $this->iscontent ) {
649 $subjpage = $this->mTitle->getSubjectPage();
650 $talkpage = $this->mTitle->getTalkPage();
651
652 $nskey = $this->mTitle->getNamespaceKey();
653 $content_actions[$nskey] = $this->tabAction(
654 $subjpage,
655 $nskey,
656 !$this->mTitle->isTalkPage() && !$prevent_active_tabs,
657 '', true);
658
659 $content_actions['talk'] = $this->tabAction(
660 $talkpage,
661 'talk',
662 $this->mTitle->isTalkPage() && !$prevent_active_tabs,
663 '',
664 true);
665
666 wfProfileIn( "$fname-edit" );
667 if ( $this->mTitle->quickUserCan( 'edit' ) && ( $this->mTitle->exists() || $this->mTitle->userCanCreate( false ) ) ) {
668 $istalk = $this->mTitle->isTalkPage();
669 $istalkclass = $istalk?' istalk':'';
670 $content_actions['edit'] = array(
671 'class' => ((($action == 'edit' or $action == 'submit') and $section != 'new') ? 'selected' : '').$istalkclass,
672 'text' => wfMsg('edit'),
673 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
674 );
675
676 if ( $istalk || $wgOut->showNewSectionLink() ) {
677 $content_actions['addsection'] = array(
678 'class' => $section == 'new'?'selected':false,
679 'text' => wfMsg('addsection'),
680 'href' => $this->mTitle->getLocalUrl( 'action=edit&section=new' )
681 );
682 }
683 } else {
684 $content_actions['viewsource'] = array(
685 'class' => ($action == 'edit') ? 'selected' : false,
686 'text' => wfMsg('viewsource'),
687 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
688 );
689 }
690 wfProfileOut( "$fname-edit" );
691
692 wfProfileIn( "$fname-live" );
693 if ( $this->mTitle->getArticleId() ) {
694
695 $content_actions['history'] = array(
696 'class' => ($action == 'history') ? 'selected' : false,
697 'text' => wfMsg('history_short'),
698 'href' => $this->mTitle->getLocalUrl( 'action=history')
699 );
700
701 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
702 if(!$this->mTitle->isProtected()){
703 $content_actions['protect'] = array(
704 'class' => ($action == 'protect') ? 'selected' : false,
705 'text' => wfMsg('protect'),
706 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
707 );
708
709 } else {
710 $content_actions['unprotect'] = array(
711 'class' => ($action == 'unprotect') ? 'selected' : false,
712 'text' => wfMsg('unprotect'),
713 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
714 );
715 }
716 }
717 if($wgUser->isAllowed('delete')){
718 $content_actions['delete'] = array(
719 'class' => ($action == 'delete') ? 'selected' : false,
720 'text' => wfMsg('delete'),
721 'href' => $this->mTitle->getLocalUrl( 'action=delete' )
722 );
723 }
724 if ( $this->mTitle->quickUserCan( 'move' ) ) {
725 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $this->thispage );
726 $content_actions['move'] = array(
727 'class' => $this->mTitle->isSpecial( 'Movepage' ) ? 'selected' : false,
728 'text' => wfMsg('move'),
729 'href' => $moveTitle->getLocalUrl()
730 );
731 }
732 } else {
733 //article doesn't exist or is deleted
734 if( $wgUser->isAllowed( 'delete' ) ) {
735 if( $n = $this->mTitle->isDeleted() ) {
736 $undelTitle = SpecialPage::getTitleFor( 'Undelete' );
737 $content_actions['undelete'] = array(
738 'class' => false,
739 'text' => wfMsgExt( 'undelete_short', array( 'parsemag' ), $n ),
740 'href' => $undelTitle->getLocalUrl( 'target=' . urlencode( $this->thispage ) )
741 #'href' => self::makeSpecialUrl( "Undelete/$this->thispage" )
742 );
743 }
744 }
745 }
746 wfProfileOut( "$fname-live" );
747
748 if( $this->loggedin ) {
749 if( !$this->mTitle->userIsWatching()) {
750 $content_actions['watch'] = array(
751 'class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : false,
752 'text' => wfMsg('watch'),
753 'href' => $this->mTitle->getLocalUrl( 'action=watch' )
754 );
755 } else {
756 $content_actions['unwatch'] = array(
757 'class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : false,
758 'text' => wfMsg('unwatch'),
759 'href' => $this->mTitle->getLocalUrl( 'action=unwatch' )
760 );
761 }
762 }
763
764 wfRunHooks( 'SkinTemplateTabs', array( &$this , &$content_actions ) ) ;
765 } else {
766 /* show special page tab */
767
768 $content_actions[$this->mTitle->getNamespaceKey()] = array(
769 'class' => 'selected',
770 'text' => wfMsg('specialpage'),
771 'href' => $wgRequest->getRequestURL(), // @bug 2457, 2510
772 );
773
774 wfRunHooks( 'SkinTemplateBuildContentActionUrlsAfterSpecialPage', array( &$this, &$content_actions ) );
775 }
776
777 /* show links to different language variants */
778 global $wgDisableLangConversion;
779 $variants = $wgContLang->getVariants();
780 if( !$wgDisableLangConversion && sizeof( $variants ) > 1 ) {
781 $preferred = $wgContLang->getPreferredVariant();
782 $vcount=0;
783 foreach( $variants as $code ) {
784 $varname = $wgContLang->getVariantname( $code );
785 if( $varname == 'disable' )
786 continue;
787 $selected = ( $code == $preferred )? 'selected' : false;
788 $content_actions['varlang-' . $vcount] = array(
789 'class' => $selected,
790 'text' => $varname,
791 'href' => $this->mTitle->getLocalURL('',$code)
792 );
793 $vcount ++;
794 }
795 }
796
797 wfRunHooks( 'SkinTemplateContentActions', array( &$content_actions ) );
798
799 wfProfileOut( $fname );
800 return $content_actions;
801 }
802
803
804
805 /**
806 * build array of common navigation links
807 * @return array
808 * @private
809 */
810 function buildNavUrls () {
811 global $wgUseTrackbacks, $wgTitle, $wgArticle;
812
813 $fname = 'SkinTemplate::buildNavUrls';
814 wfProfileIn( $fname );
815
816 global $wgUser, $wgRequest;
817 global $wgEnableUploads, $wgUploadNavigationUrl;
818
819 $action = $wgRequest->getText( 'action' );
820 $oldid = $wgRequest->getVal( 'oldid' );
821
822 $nav_urls = array();
823 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
824 if( $wgEnableUploads ) {
825 if ($wgUploadNavigationUrl) {
826 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
827 } else {
828 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
829 }
830 } else {
831 if ($wgUploadNavigationUrl)
832 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
833 else
834 $nav_urls['upload'] = false;
835 }
836 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
837
838 // default permalink to being off, will override it as required below.
839 $nav_urls['permalink'] = false;
840
841 // A print stylesheet is attached to all pages, but nobody ever
842 // figures that out. :) Add a link...
843 if( $this->iscontent && ($action == '' || $action == 'view' || $action == 'purge' ) ) {
844 $revid = $wgArticle ? $wgArticle->getLatest() : 0;
845 if ( !( $revid == 0 ) )
846 $nav_urls['print'] = array(
847 'text' => wfMsg( 'printableversion' ),
848 'href' => $wgRequest->appendQuery( 'printable=yes' )
849 );
850
851 // Also add a "permalink" while we're at it
852 if ( (int)$oldid ) {
853 $nav_urls['permalink'] = array(
854 'text' => wfMsg( 'permalink' ),
855 'href' => ''
856 );
857 } else {
858 if ( !( $revid == 0 ) )
859 $nav_urls['permalink'] = array(
860 'text' => wfMsg( 'permalink' ),
861 'href' => $wgTitle->getLocalURL( "oldid=$revid" )
862 );
863 }
864
865 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink', array( &$this, &$nav_urls, &$oldid, &$revid ) );
866 }
867
868 if( $this->mTitle->getNamespace() != NS_SPECIAL ) {
869 $wlhTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage );
870 $nav_urls['whatlinkshere'] = array(
871 'href' => $wlhTitle->getLocalUrl()
872 );
873 if( $this->mTitle->getArticleId() ) {
874 $rclTitle = SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage );
875 $nav_urls['recentchangeslinked'] = array(
876 'href' => $rclTitle->getLocalUrl()
877 );
878 } else {
879 $nav_urls['recentchangeslinked'] = false;
880 }
881 if ($wgUseTrackbacks)
882 $nav_urls['trackbacklink'] = array(
883 'href' => $wgTitle->trackbackURL()
884 );
885 }
886
887 if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) {
888 $id = User::idFromName($this->mTitle->getText());
889 $ip = User::isIP($this->mTitle->getText());
890 } else {
891 $id = 0;
892 $ip = false;
893 }
894
895 if($id || $ip) { # both anons and non-anons have contri list
896 $nav_urls['contributions'] = array(
897 'href' => self::makeSpecialUrlSubpage( 'Contributions', $this->mTitle->getText() )
898 );
899 if ( $wgUser->isAllowed( 'block' ) ) {
900 $nav_urls['blockip'] = array(
901 'href' => self::makeSpecialUrlSubpage( 'Blockip', $this->mTitle->getText() )
902 );
903 } else {
904 $nav_urls['blockip'] = false;
905 }
906 } else {
907 $nav_urls['contributions'] = false;
908 $nav_urls['blockip'] = false;
909 }
910 $nav_urls['emailuser'] = false;
911 if( $this->showEmailUser( $id ) ) {
912 $nav_urls['emailuser'] = array(
913 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $this->mTitle->getText() )
914 );
915 }
916 wfProfileOut( $fname );
917 return $nav_urls;
918 }
919
920 /**
921 * Generate strings used for xml 'id' names
922 * @return string
923 * @private
924 */
925 function getNameSpaceKey () {
926 return $this->mTitle->getNamespaceKey();
927 }
928
929 /**
930 * @private
931 */
932 function setupUserCss() {
933 $fname = 'SkinTemplate::setupUserCss';
934 wfProfileIn( $fname );
935
936 global $wgRequest, $wgAllowUserCss, $wgUseSiteCss, $wgContLang, $wgSquidMaxage, $wgStylePath, $wgUser;
937
938 $sitecss = '';
939 $usercss = '';
940 $siteargs = '&maxage=' . $wgSquidMaxage;
941 if( $this->loggedin ) {
942 // Ensure that logged-in users' generated CSS isn't clobbered
943 // by anons' publicly cacheable generated CSS.
944 $siteargs .= '&smaxage=0';
945 }
946
947 # Add user-specific code if this is a user and we allow that kind of thing
948
949 if ( $wgAllowUserCss && $this->loggedin ) {
950 $action = $wgRequest->getText('action');
951
952 # if we're previewing the CSS page, use it
953 if( $this->mTitle->isCssSubpage() and $this->userCanPreview( $action ) ) {
954 $siteargs = "&smaxage=0&maxage=0";
955 $usercss = $wgRequest->getText('wpTextbox1');
956 } else {
957 $usercss = '@import "' .
958 self::makeUrl($this->userpage . '/'.$this->skinname.'.css',
959 'action=raw&ctype=text/css') . '";' ."\n";
960 }
961
962 $siteargs .= '&ts=' . $wgUser->mTouched;
963 }
964
965 if( $wgContLang->isRTL() ) {
966 global $wgStyleVersion;
967 $sitecss .= "@import \"$wgStylePath/$this->stylename/rtl.css?$wgStyleVersion\";\n";
968 }
969
970 # If we use the site's dynamic CSS, throw that in, too
971 if ( $wgUseSiteCss ) {
972 $query = "usemsgcache=yes&action=raw&ctype=text/css&smaxage=$wgSquidMaxage";
973 $sitecss .= '@import "' . self::makeNSUrl( 'Common.css', $query, NS_MEDIAWIKI) . '";' . "\n";
974 $sitecss .= '@import "' . self::makeNSUrl( ucfirst( $this->skinname ) . '.css', $query, NS_MEDIAWIKI ) . '";' . "\n";
975 $sitecss .= '@import "' . self::makeUrl( '-', 'action=raw&gen=css' . $siteargs ) . '";' . "\n";
976 }
977
978 # If we use any dynamic CSS, make a little CDATA block out of it.
979
980 if ( !empty($sitecss) || !empty($usercss) ) {
981 $this->usercss = "/*<![CDATA[*/\n" . $sitecss . $usercss . '/*]]>*/';
982 }
983 wfProfileOut( $fname );
984 }
985
986 /**
987 * @private
988 */
989 function setupUserJs() {
990 $fname = 'SkinTemplate::setupUserJs';
991 wfProfileIn( $fname );
992
993 global $wgRequest, $wgAllowUserJs, $wgJsMimeType;
994 $action = $wgRequest->getText('action');
995
996 if( $wgAllowUserJs && $this->loggedin ) {
997 if( $this->mTitle->isJsSubpage() and $this->userCanPreview( $action ) ) {
998 # XXX: additional security check/prompt?
999 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText('wpTextbox1') . ' /*]]>*/';
1000 } else {
1001 $this->userjs = self::makeUrl($this->userpage.'/'.$this->skinname.'.js', 'action=raw&ctype='.$wgJsMimeType.'&dontcountme=s');
1002 }
1003 }
1004 wfProfileOut( $fname );
1005 }
1006
1007 /**
1008 * Code for extensions to hook into to provide per-page CSS, see
1009 * extensions/PageCSS/PageCSS.php for an implementation of this.
1010 *
1011 * @private
1012 */
1013 function setupPageCss() {
1014 $fname = 'SkinTemplate::setupPageCss';
1015 wfProfileIn( $fname );
1016 $out = false;
1017 wfRunHooks( 'SkinTemplateSetupPageCss', array( &$out ) );
1018
1019 wfProfileOut( $fname );
1020 return $out;
1021 }
1022
1023 /**
1024 * returns css with user-specific options
1025 * @public
1026 */
1027
1028 function getUserStylesheet() {
1029 $fname = 'SkinTemplate::getUserStylesheet';
1030 wfProfileIn( $fname );
1031
1032 $s = "/* generated user stylesheet */\n";
1033 $s .= $this->reallyDoGetUserStyles();
1034 wfProfileOut( $fname );
1035 return $s;
1036 }
1037
1038 /**
1039 * This returns MediaWiki:Common.js and MediaWiki:[Skinname].js concate-
1040 * nated together. For some bizarre reason, it does *not* return any
1041 * custom user JS from subpages. Huh?
1042 *
1043 * There's absolutely no reason to have separate Monobook/Common JSes.
1044 * Any JS that cares can just check the skin variable generated at the
1045 * top. For now Monobook.js will be maintained, but it should be consi-
1046 * dered deprecated.
1047 *
1048 * @return string
1049 */
1050 public function getUserJs() {
1051 $fname = 'SkinTemplate::getUserJs';
1052 wfProfileIn( $fname );
1053
1054 $s = parent::getUserJs();
1055 $s .= "\n\n/* MediaWiki:".ucfirst($this->skinname).".js (deprecated; migrate to Common.js!) */\n";
1056
1057 // avoid inclusion of non defined user JavaScript (with custom skins only)
1058 // by checking for default message content
1059 $msgKey = ucfirst($this->skinname).'.js';
1060 $userJS = wfMsgForContent($msgKey);
1061 if ( !wfEmptyMsg( $msgKey, $userJS ) ) {
1062 $s .= $userJS;
1063 }
1064
1065 wfProfileOut( $fname );
1066 return $s;
1067 }
1068 }
1069
1070 /**
1071 * Generic wrapper for template functions, with interface
1072 * compatible with what we use of PHPTAL 0.7.
1073 * @package MediaWiki
1074 * @subpackage Skins
1075 */
1076 class QuickTemplate {
1077 /**
1078 * @public
1079 */
1080 function QuickTemplate() {
1081 $this->data = array();
1082 $this->translator = new MediaWiki_I18N();
1083 }
1084
1085 /**
1086 * @public
1087 */
1088 function set( $name, $value ) {
1089 $this->data[$name] = $value;
1090 }
1091
1092 /**
1093 * @public
1094 */
1095 function setRef($name, &$value) {
1096 $this->data[$name] =& $value;
1097 }
1098
1099 /**
1100 * @public
1101 */
1102 function setTranslator( &$t ) {
1103 $this->translator = &$t;
1104 }
1105
1106 /**
1107 * @public
1108 */
1109 function execute() {
1110 echo "Override this function.";
1111 }
1112
1113
1114 /**
1115 * @private
1116 */
1117 function text( $str ) {
1118 echo htmlspecialchars( $this->data[$str] );
1119 }
1120
1121 /**
1122 * @private
1123 */
1124 function jstext( $str ) {
1125 echo Xml::escapeJsString( $this->data[$str] );
1126 }
1127
1128 /**
1129 * @private
1130 */
1131 function html( $str ) {
1132 echo $this->data[$str];
1133 }
1134
1135 /**
1136 * @private
1137 */
1138 function msg( $str ) {
1139 echo htmlspecialchars( $this->translator->translate( $str ) );
1140 }
1141
1142 /**
1143 * @private
1144 */
1145 function msgHtml( $str ) {
1146 echo $this->translator->translate( $str );
1147 }
1148
1149 /**
1150 * An ugly, ugly hack.
1151 * @private
1152 */
1153 function msgWiki( $str ) {
1154 global $wgParser, $wgTitle, $wgOut;
1155
1156 $text = $this->translator->translate( $str );
1157 $parserOutput = $wgParser->parse( $text, $wgTitle,
1158 $wgOut->parserOptions(), true );
1159 echo $parserOutput->getText();
1160 }
1161
1162 /**
1163 * @private
1164 */
1165 function haveData( $str ) {
1166 return isset( $this->data[$str] );
1167 }
1168
1169 /**
1170 * @private
1171 */
1172 function haveMsg( $str ) {
1173 $msg = $this->translator->translate( $str );
1174 return ($msg != '-') && ($msg != ''); # ????
1175 }
1176 }
1177 ?>