bug 25517 Assignment in conditions should be avoided/ http://www.mediawiki.org/wiki...
[lhc/web/wiklou.git] / includes / SkinTemplate.php
1 <?php
2 /**
3 * Base class for template-based skins
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 * @file
21 */
22
23 if ( ! defined( 'MEDIAWIKI' ) )
24 die( 1 );
25
26 /**
27 * Wrapper object for MediaWiki's localization functions,
28 * to be passed to the template engine.
29 *
30 * @private
31 * @ingroup Skins
32 */
33 class MediaWiki_I18N {
34 var $_context = array();
35
36 function set( $varName, $value ) {
37 $this->_context[$varName] = $value;
38 }
39
40 function translate( $value ) {
41 wfProfileIn( __METHOD__ );
42
43 // Hack for i18n:attributes in PHPTAL 1.0.0 dev version as of 2004-10-23
44 $value = preg_replace( '/^string:/', '', $value );
45
46 $value = wfMsg( $value );
47 // interpolate variables
48 $m = array();
49 while( preg_match( '/\$([0-9]*?)/sm', $value, $m ) ) {
50 list( $src, $var ) = $m;
51 wfSuppressWarnings();
52 $varValue = $this->_context[$var];
53 wfRestoreWarnings();
54 $value = str_replace( $src, $varValue, $value );
55 }
56 wfProfileOut( __METHOD__ );
57 return $value;
58 }
59 }
60
61 /**
62 * Template-filler skin base class
63 * Formerly generic PHPTal (http://phptal.sourceforge.net/) skin
64 * Based on Brion's smarty skin
65 * @copyright Copyright © Gabriel Wicke -- http://www.aulinx.de/
66 *
67 * @todo Needs some serious refactoring into functions that correspond
68 * to the computations individual esi snippets need. Most importantly no body
69 * parsing for most of those of course.
70 *
71 * @ingroup Skins
72 */
73 class SkinTemplate extends Skin {
74 /**#@+
75 * @private
76 */
77
78 /**
79 * Name of our skin, it probably needs to be all lower case. Child classes
80 * should override the default.
81 */
82 var $skinname = 'monobook';
83
84 /**
85 * Stylesheets set to use. Subdirectory in skins/ where various stylesheets
86 * are located. Child classes should override the default.
87 */
88 var $stylename = 'monobook';
89
90 /**
91 * For QuickTemplate, the name of the subclass which will actually fill the
92 * template. Child classes should override the default.
93 */
94 var $template = 'QuickTemplate';
95
96 /**
97 * Whether this skin use OutputPage::headElement() to generate the <head>
98 * tag
99 */
100 var $useHeadElement = false;
101
102 /**#@-*/
103
104 /**
105 * Add specific styles for this skin
106 *
107 * @param $out OutputPage
108 */
109 function setupSkinUserCss( OutputPage $out ){
110 $out->addModuleStyles( array( 'mediawiki.legacy.shared', 'mediawiki.legacy.commonPrint' ) );
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 $classname string (or file)
119 * @param $repository string: subdirectory where we keep template files
120 * @param $cache_dir string
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 $out OutputPage
132 */
133 function outputPage( OutputPage $out ) {
134 global $wgArticle, $wgUser, $wgLang, $wgContLang;
135 global $wgScript, $wgStylePath, $wgLanguageCode;
136 global $wgMimeType, $wgJsMimeType, $wgOutputEncoding, $wgRequest;
137 global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces, $wgHtml5Version;
138 global $wgDisableCounters, $wgLogo, $wgHideInterlanguageLinks;
139 global $wgMaxCredits, $wgShowCreditsIfMax;
140 global $wgPageShowWatchingUsers;
141 global $wgUseTrackbacks, $wgUseSiteJs, $wgDebugComments;
142 global $wgArticlePath, $wgScriptPath, $wgServer, $wgProfiler;
143
144 wfProfileIn( __METHOD__ );
145 if ( is_object( $wgProfiler ) ) {
146 $wgProfiler->setTemplated( true );
147 }
148
149 $oldid = $wgRequest->getVal( 'oldid' );
150 $diff = $wgRequest->getVal( 'diff' );
151 $action = $wgRequest->getVal( 'action', 'view' );
152
153 wfProfileIn( __METHOD__ . '-init' );
154 $this->initPage( $out );
155
156 $this->setMembers();
157 $tpl = $this->setupTemplate( $this->template, 'skins' );
158
159 #if ( $wgUseDatabaseMessages ) { // uncomment this to fall back to GetText
160 $tpl->setTranslator( new MediaWiki_I18N() );
161 #}
162 wfProfileOut( __METHOD__ . '-init' );
163
164 wfProfileIn( __METHOD__ . '-stuff' );
165 $this->thispage = $this->mTitle->getPrefixedDBkey();
166 $this->thisurl = $this->mTitle->getPrefixedURL();
167 $query = array();
168 if ( !$wgRequest->wasPosted() ) {
169 $query = $wgRequest->getValues();
170 unset( $query['title'] );
171 unset( $query['returnto'] );
172 unset( $query['returntoquery'] );
173 }
174 $this->thisquery = wfUrlencode( wfArrayToCGI( $query ) );
175 $this->loggedin = $wgUser->isLoggedIn();
176 $this->iscontent = ( $this->mTitle->getNamespace() != NS_SPECIAL );
177 $this->iseditable = ( $this->iscontent and !( $action == 'edit' or $action == 'submit' ) );
178 $this->username = $wgUser->getName();
179
180 if ( $wgUser->isLoggedIn() || $this->showIPinHeader() ) {
181 $this->userpageUrlDetails = self::makeUrlDetails( $this->userpage );
182 } else {
183 # This won't be used in the standard skins, but we define it to preserve the interface
184 # To save time, we check for existence
185 $this->userpageUrlDetails = self::makeKnownUrlDetails( $this->userpage );
186 }
187
188 $this->titletxt = $this->mTitle->getPrefixedText();
189 wfProfileOut( __METHOD__ . '-stuff' );
190
191 wfProfileIn( __METHOD__ . '-stuff-head' );
192 if ( $this->useHeadElement ) {
193 $pagecss = $this->setupPageCss();
194 if( $pagecss )
195 $out->addInlineStyle( $pagecss );
196 } else {
197 $this->setupUserCss( $out );
198
199 $tpl->set( 'pagecss', $this->setupPageCss() );
200 $tpl->setRef( 'usercss', $this->usercss );
201
202 $this->userjs = $this->userjsprev = false;
203 $this->setupUserJs( $out->isUserJsAllowed() );
204 $tpl->setRef( 'userjs', $this->userjs );
205 $tpl->setRef( 'userjsprev', $this->userjsprev );
206
207 if( $wgUseSiteJs ) {
208 $jsCache = $this->loggedin ? '&smaxage=0' : '';
209 $tpl->set( 'jsvarurl',
210 self::makeUrl( '-',
211 "action=raw$jsCache&gen=js&useskin=" .
212 urlencode( $this->getSkinName() ) ) );
213 } else {
214 $tpl->set( 'jsvarurl', false );
215 }
216
217 $tpl->setRef( 'xhtmldefaultnamespace', $wgXhtmlDefaultNamespace );
218 $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
219 $tpl->set( 'html5version', $wgHtml5Version );
220 $tpl->set( 'headlinks', $out->getHeadLinks( $this->getSkinName() ) );
221 $tpl->set( 'csslinks', $out->buildCssLinks() );
222
223 if( $wgUseTrackbacks && $out->isArticleRelated() ) {
224 $tpl->set( 'trackbackhtml', $out->getTitle()->trackbackRDF() );
225 } else {
226 $tpl->set( 'trackbackhtml', null );
227 }
228 }
229 wfProfileOut( __METHOD__ . '-stuff-head' );
230
231 wfProfileIn( __METHOD__ . '-stuff2' );
232 $tpl->set( 'title', $out->getPageTitle() );
233 $tpl->set( 'pagetitle', $out->getHTMLTitle() );
234 $tpl->set( 'displaytitle', $out->mPageLinkTitle );
235 $tpl->set( 'pageclass', $this->getPageClasses( $this->mTitle ) );
236 $tpl->set( 'skinnameclass', ( 'skin-' . Sanitizer::escapeClass( $this->getSkinName() ) ) );
237
238 $nsname = MWNamespace::exists( $this->mTitle->getNamespace() ) ?
239 MWNamespace::getCanonicalName( $this->mTitle->getNamespace() ) :
240 $this->mTitle->getNsText();
241
242 $tpl->set( 'nscanonical', $nsname );
243 $tpl->set( 'nsnumber', $this->mTitle->getNamespace() );
244 $tpl->set( 'titleprefixeddbkey', $this->mTitle->getPrefixedDBKey() );
245 $tpl->set( 'titletext', $this->mTitle->getText() );
246 $tpl->set( 'articleid', $this->mTitle->getArticleId() );
247 $tpl->set( 'currevisionid', isset( $wgArticle ) ? $wgArticle->getLatest() : 0 );
248
249 $tpl->set( 'isarticle', $out->isArticle() );
250
251 $tpl->setRef( 'thispage', $this->thispage );
252 $subpagestr = $this->subPageSubtitle();
253 $tpl->set(
254 'subtitle', !empty( $subpagestr ) ?
255 '<span class="subpages">'.$subpagestr.'</span>'.$out->getSubtitle() :
256 $out->getSubtitle()
257 );
258 $undelete = $this->getUndeleteLink();
259 $tpl->set(
260 'undelete', !empty( $undelete ) ?
261 '<span class="subpages">'.$undelete.'</span>' :
262 ''
263 );
264
265 $tpl->set( 'catlinks', $this->getCategories() );
266 if( $out->isSyndicated() ) {
267 $feeds = array();
268 foreach( $out->getSyndicationLinks() as $format => $link ) {
269 $feeds[$format] = array(
270 'text' => wfMsg( "feed-$format" ),
271 'href' => $link
272 );
273 }
274 $tpl->setRef( 'feeds', $feeds );
275 } else {
276 $tpl->set( 'feeds', false );
277 }
278
279 $tpl->setRef( 'mimetype', $wgMimeType );
280 $tpl->setRef( 'jsmimetype', $wgJsMimeType );
281 $tpl->setRef( 'charset', $wgOutputEncoding );
282 $tpl->setRef( 'wgScript', $wgScript );
283 $tpl->setRef( 'skinname', $this->skinname );
284 $tpl->set( 'skinclass', get_class( $this ) );
285 $tpl->setRef( 'stylename', $this->stylename );
286 $tpl->set( 'printable', $out->isPrintable() );
287 $tpl->set( 'handheld', $wgRequest->getBool( 'handheld' ) );
288 $tpl->setRef( 'loggedin', $this->loggedin );
289 $tpl->set( 'notspecialpage', $this->mTitle->getNamespace() != NS_SPECIAL );
290 /* XXX currently unused, might get useful later
291 $tpl->set( "editable", ($this->mTitle->getNamespace() != NS_SPECIAL ) );
292 $tpl->set( "exists", $this->mTitle->getArticleID() != 0 );
293 $tpl->set( "watch", $this->mTitle->userIsWatching() ? "unwatch" : "watch" );
294 $tpl->set( "protect", count($this->mTitle->isProtected()) ? "unprotect" : "protect" );
295 $tpl->set( "helppage", wfMsg('helppage'));
296 */
297 $tpl->set( 'searchaction', $this->escapeSearchLink() );
298 $tpl->set( 'searchtitle', SpecialPage::getTitleFor( 'Search' )->getPrefixedDBKey() );
299 $tpl->set( 'search', trim( $wgRequest->getVal( 'search' ) ) );
300 $tpl->setRef( 'stylepath', $wgStylePath );
301 $tpl->setRef( 'articlepath', $wgArticlePath );
302 $tpl->setRef( 'scriptpath', $wgScriptPath );
303 $tpl->setRef( 'serverurl', $wgServer );
304 $tpl->setRef( 'logopath', $wgLogo );
305
306 $lang = wfUILang();
307 $tpl->set( 'lang', $lang->getCode() );
308 $tpl->set( 'dir', $lang->getDir() );
309 $tpl->set( 'rtl', $lang->isRTL() );
310
311 $tpl->set( 'capitalizeallnouns', $wgLang->capitalizeAllNouns() ? ' capitalize-all-nouns' : '' );
312 $tpl->set( 'showjumplinks', $wgUser->getOption( 'showjumplinks' ) );
313 $tpl->set( 'username', $wgUser->isAnon() ? null : $this->username );
314 $tpl->setRef( 'userpage', $this->userpage );
315 $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href'] );
316 $tpl->set( 'userlang', $wgLang->getCode() );
317
318 // Users can have their language set differently than the
319 // content of the wiki. For these users, tell the web browser
320 // that interface elements are in a different language.
321 $tpl->set( 'userlangattributes', '' );
322 $tpl->set( 'specialpageattributes', '' );
323
324 $lang = $wgLang->getCode();
325 $dir = $wgLang->getDir();
326 if ( $lang !== $wgContLang->getCode() || $dir !== $wgContLang->getDir() ) {
327 $attrs = " lang='$lang' dir='$dir'";
328
329 $tpl->set( 'userlangattributes', $attrs );
330
331 // The content of SpecialPages should be presented in the
332 // user's language. Content of regular pages should not be touched.
333 if( $this->mTitle->isSpecialPage() ) {
334 $tpl->set( 'specialpageattributes', $attrs );
335 }
336 }
337
338 $newtalks = $this->getNewtalks();
339
340 wfProfileOut( __METHOD__ . '-stuff2' );
341
342 wfProfileIn( __METHOD__ . '-stuff3' );
343 $tpl->setRef( 'newtalk', $newtalks );
344 $tpl->setRef( 'skin', $this );
345 $tpl->set( 'logo', $this->logoText() );
346 if ( $out->isArticle() and ( !isset( $oldid ) or isset( $diff ) ) and
347 $wgArticle and 0 != $wgArticle->getID() ){
348 if ( !$wgDisableCounters ) {
349 $viewcount = $wgLang->formatNum( $wgArticle->getCount() );
350 if ( $viewcount ) {
351 $tpl->set( 'viewcount', wfMsgExt( 'viewcount', array( 'parseinline' ), $viewcount ) );
352 } else {
353 $tpl->set( 'viewcount', false );
354 }
355 } else {
356 $tpl->set( 'viewcount', false );
357 }
358
359 if( $wgPageShowWatchingUsers ) {
360 $dbr = wfGetDB( DB_SLAVE );
361 $watchlist = $dbr->tableName( 'watchlist' );
362 $res = $dbr->select( 'watchlist',
363 array( 'COUNT(*) AS n' ),
364 array( 'wl_title' => $dbr->strencode( $this->mTitle->getDBkey() ), 'wl_namespace' => $this->mTitle->getNamespace() ),
365 __METHOD__
366 );
367 $x = $dbr->fetchObject( $res );
368 $numberofwatchingusers = $x->n;
369 if( $numberofwatchingusers > 0 ) {
370 $tpl->set( 'numberofwatchingusers',
371 wfMsgExt( 'number_of_watching_users_pageview', array( 'parseinline' ),
372 $wgLang->formatNum( $numberofwatchingusers ) )
373 );
374 } else {
375 $tpl->set( 'numberofwatchingusers', false );
376 }
377 } else {
378 $tpl->set( 'numberofwatchingusers', false );
379 }
380
381 $tpl->set( 'copyright', $this->getCopyright() );
382
383 $this->credits = false;
384
385 if( $wgMaxCredits != 0 ){
386 $this->credits = Credits::getCredits( $wgArticle, $wgMaxCredits, $wgShowCreditsIfMax );
387 } else {
388 $tpl->set( 'lastmod', $this->lastModified() );
389 }
390
391 $tpl->setRef( 'credits', $this->credits );
392
393 } elseif ( isset( $oldid ) && !isset( $diff ) ) {
394 $tpl->set( 'copyright', $this->getCopyright() );
395 $tpl->set( 'viewcount', false );
396 $tpl->set( 'lastmod', false );
397 $tpl->set( 'credits', false );
398 $tpl->set( 'numberofwatchingusers', false );
399 } else {
400 $tpl->set( 'copyright', false );
401 $tpl->set( 'viewcount', false );
402 $tpl->set( 'lastmod', false );
403 $tpl->set( 'credits', false );
404 $tpl->set( 'numberofwatchingusers', false );
405 }
406 wfProfileOut( __METHOD__ . '-stuff3' );
407
408 wfProfileIn( __METHOD__ . '-stuff4' );
409 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
410 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
411 $tpl->set( 'disclaimer', $this->disclaimerLink() );
412 $tpl->set( 'privacy', $this->privacyLink() );
413 $tpl->set( 'about', $this->aboutLink() );
414
415 if ( $wgDebugComments ) {
416 $tpl->setRef( 'debug', $out->mDebugtext );
417 } else {
418 $tpl->set( 'debug', '' );
419 }
420
421 $tpl->set( 'reporttime', wfReportTime() );
422 $tpl->set( 'sitenotice', wfGetSiteNotice() );
423 $tpl->set( 'bottomscripts', $this->bottomScripts( $out ) );
424
425 $printfooter = "<div class=\"printfooter\">\n" . $this->printSource() . "</div>\n";
426 global $wgBetterDirectionality;
427 if ( $wgBetterDirectionality ) {
428 $realBodyAttribs = array( 'lang' => $wgLanguageCode, 'dir' => $wgContLang->getDir() );
429 $out->mBodytext = Html::rawElement( 'div', $realBodyAttribs, $out->mBodytext );
430 }
431 $out->mBodytext .= $printfooter . $this->generateDebugHTML();
432 $tpl->setRef( 'bodytext', $out->mBodytext );
433
434 # Language links
435 $language_urls = array();
436
437 if ( !$wgHideInterlanguageLinks ) {
438 foreach( $out->getLanguageLinks() as $l ) {
439 $tmp = explode( ':', $l, 2 );
440 $class = 'interwiki-' . $tmp[0];
441 unset( $tmp );
442 $nt = Title::newFromText( $l );
443 if ( $nt ) {
444 $language_urls[] = array(
445 'href' => $nt->getFullURL(),
446 'text' => ( $wgContLang->getLanguageName( $nt->getInterwiki() ) != '' ?
447 $wgContLang->getLanguageName( $nt->getInterwiki() ) : $l ),
448 'title' => $nt->getText(),
449 'class' => $class
450 );
451 }
452 }
453 }
454 if( count( $language_urls ) ) {
455 $tpl->setRef( 'language_urls', $language_urls );
456 } else {
457 $tpl->set( 'language_urls', false );
458 }
459 wfProfileOut( __METHOD__ . '-stuff4' );
460
461 wfProfileIn( __METHOD__ . '-stuff5' );
462 # Personal toolbar
463 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
464 $content_actions = $this->buildContentActionUrls();
465 $tpl->setRef( 'content_actions', $content_actions );
466
467 $tpl->set( 'sidebar', $this->buildSidebar() );
468 $tpl->set( 'nav_urls', $this->buildNavUrls() );
469
470 // Set the head scripts near the end, in case the above actions resulted in added scripts
471 if ( $this->useHeadElement ) {
472 $tpl->set( 'headelement', $out->headElement( $this ) );
473 } else {
474 $tpl->set( 'headscripts', $out->getScript() );
475 }
476
477 // original version by hansm
478 if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
479 wfDebug( __METHOD__ . ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
480 }
481
482 // allow extensions adding stuff after the page content.
483 // See Skin::afterContentHook() for further documentation.
484 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
485 wfProfileOut( __METHOD__ . '-stuff5' );
486
487 // execute template
488 wfProfileIn( __METHOD__ . '-execute' );
489 $res = $tpl->execute();
490 wfProfileOut( __METHOD__ . '-execute' );
491
492 // result may be an error
493 $this->printOrError( $res );
494 wfProfileOut( __METHOD__ );
495 }
496
497 /**
498 * Output the string, or print error message if it's
499 * an error object of the appropriate type.
500 * For the base class, assume strings all around.
501 *
502 * @param $str Mixed
503 * @private
504 */
505 function printOrError( $str ) {
506 echo $str;
507 }
508
509 /**
510 * build array of urls for personal toolbar
511 * @return array
512 * @private
513 */
514 function buildPersonalUrls() {
515 global $wgOut, $wgRequest;
516
517 $title = $wgOut->getTitle();
518 $pageurl = $title->getLocalURL();
519 wfProfileIn( __METHOD__ );
520
521 /* set up the default links for the personal toolbar */
522 $personal_urls = array();
523 $page = $wgRequest->getVal( 'returnto', $this->thisurl );
524 $query = $wgRequest->getVal( 'returntoquery', $this->thisquery );
525 $returnto = "returnto=$page";
526 if( $this->thisquery != '' )
527 $returnto .= "&returntoquery=$query";
528 if( $this->loggedin ) {
529 $personal_urls['userpage'] = array(
530 'text' => $this->username,
531 'href' => &$this->userpageUrlDetails['href'],
532 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
533 'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
534 );
535 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
536 $personal_urls['mytalk'] = array(
537 'text' => wfMsg( 'mytalk' ),
538 'href' => &$usertalkUrlDetails['href'],
539 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
540 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
541 );
542 $href = self::makeSpecialUrl( 'Preferences' );
543 $personal_urls['preferences'] = array(
544 'text' => wfMsg( 'mypreferences' ),
545 'href' => $href,
546 'active' => ( $href == $pageurl )
547 );
548 $href = self::makeSpecialUrl( 'Watchlist' );
549 $personal_urls['watchlist'] = array(
550 'text' => wfMsg( 'mywatchlist' ),
551 'href' => $href,
552 'active' => ( $href == $pageurl )
553 );
554
555 # We need to do an explicit check for Special:Contributions, as we
556 # have to match both the title, and the target (which could come
557 # from request values or be specified in "sub page" form. The plot
558 # thickens, because $wgTitle is altered for special pages, so doesn't
559 # contain the original alias-with-subpage.
560 $origTitle = Title::newFromText( $wgRequest->getText( 'title' ) );
561 if( $origTitle instanceof Title && $origTitle->getNamespace() == NS_SPECIAL ) {
562 list( $spName, $spPar ) =
563 SpecialPage::resolveAliasWithSubpage( $origTitle->getText() );
564 $active = $spName == 'Contributions'
565 && ( ( $spPar && $spPar == $this->username )
566 || $wgRequest->getText( 'target' ) == $this->username );
567 } else {
568 $active = false;
569 }
570
571 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
572 $personal_urls['mycontris'] = array(
573 'text' => wfMsg( 'mycontris' ),
574 'href' => $href,
575 'active' => $active
576 );
577 $personal_urls['logout'] = array(
578 'text' => wfMsg( 'userlogout' ),
579 'href' => self::makeSpecialUrl( 'Userlogout',
580 $title->isSpecial( 'Preferences' ) ? '' : $returnto
581 ),
582 'active' => false
583 );
584 } else {
585 global $wgUser;
586 $loginlink = $wgUser->isAllowed( 'createaccount' )
587 ? 'nav-login-createaccount'
588 : 'login';
589
590 # anonlogin & login are the same
591 $login_url = array(
592 'text' => wfMsg( $loginlink ),
593 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
594 'active' => $title->isSpecial( 'Userlogin' )
595 );
596 global $wgProto, $wgSecureLogin;
597 if( $wgProto === 'http' && $wgSecureLogin ) {
598 $title = SpecialPage::getTitleFor( 'Userlogin' );
599 $https_url = preg_replace( '/^http:/', 'https:', $title->getFullURL() );
600 $login_url['href'] = $https_url;
601 $login_url['class'] = 'link-https'; # FIXME class depends on skin
602 }
603
604 if( $this->showIPinHeader() ) {
605 $href = &$this->userpageUrlDetails['href'];
606 $personal_urls['anonuserpage'] = array(
607 'text' => $this->username,
608 'href' => $href,
609 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
610 'active' => ( $pageurl == $href )
611 );
612 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
613 $href = &$usertalkUrlDetails['href'];
614 $personal_urls['anontalk'] = array(
615 'text' => wfMsg( 'anontalk' ),
616 'href' => $href,
617 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
618 'active' => ( $pageurl == $href )
619 );
620 $personal_urls['anonlogin'] = $login_url;
621 } else {
622 $personal_urls['login'] = $login_url;
623 }
624 }
625
626 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$title ) );
627 wfProfileOut( __METHOD__ );
628 return $personal_urls;
629 }
630
631 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
632 $classes = array();
633 if( $selected ) {
634 $classes[] = 'selected';
635 }
636 if( $checkEdit && !$title->isKnown() ) {
637 $classes[] = 'new';
638 $query = 'action=edit&redlink=1';
639 }
640
641 $text = wfMsg( $message );
642 if ( wfEmptyMsg( $message, $text ) ) {
643 global $wgContLang;
644 $text = $wgContLang->getFormattedNsText( MWNamespace::getSubject( $title->getNamespace() ) );
645 }
646
647 $result = array();
648 if( !wfRunHooks( 'SkinTemplateTabAction', array( &$this,
649 $title, $message, $selected, $checkEdit,
650 &$classes, &$query, &$text, &$result ) ) ) {
651 return $result;
652 }
653
654 return array(
655 'class' => implode( ' ', $classes ),
656 'text' => $text,
657 'href' => $title->getLocalUrl( $query ) );
658 }
659
660 function makeTalkUrlDetails( $name, $urlaction = '' ) {
661 $title = Title::newFromText( $name );
662 if( !is_object( $title ) ) {
663 throw new MWException( __METHOD__ . " given invalid pagename $name" );
664 }
665 $title = $title->getTalkPage();
666 self::checkTitle( $title, $name );
667 return array(
668 'href' => $title->getLocalURL( $urlaction ),
669 'exists' => $title->getArticleID() != 0,
670 );
671 }
672
673 function makeArticleUrlDetails( $name, $urlaction = '' ) {
674 $title = Title::newFromText( $name );
675 $title= $title->getSubjectPage();
676 self::checkTitle( $title, $name );
677 return array(
678 'href' => $title->getLocalURL( $urlaction ),
679 'exists' => $title->getArticleID() != 0,
680 );
681 }
682
683 /**
684 * an array of edit links by default used for the tabs
685 * @return array
686 * @private
687 */
688 function buildContentActionUrls() {
689 global $wgContLang, $wgLang, $wgOut, $wgUser, $wgRequest, $wgArticle;
690
691 wfProfileIn( __METHOD__ );
692
693 $action = $wgRequest->getVal( 'action', 'view' );
694 $section = $wgRequest->getVal( 'section' );
695 $content_actions = array();
696
697 $prevent_active_tabs = false;
698 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this, &$prevent_active_tabs ) );
699
700 if( $this->iscontent ) {
701 $subjpage = $this->mTitle->getSubjectPage();
702 $talkpage = $this->mTitle->getTalkPage();
703
704 $nskey = $this->mTitle->getNamespaceKey();
705 $content_actions[$nskey] = $this->tabAction(
706 $subjpage,
707 $nskey,
708 !$this->mTitle->isTalkPage() && !$prevent_active_tabs,
709 '', true
710 );
711
712 $content_actions['talk'] = $this->tabAction(
713 $talkpage,
714 'talk',
715 $this->mTitle->isTalkPage() && !$prevent_active_tabs,
716 '',
717 true
718 );
719
720 wfProfileIn( __METHOD__ . '-edit' );
721 if ( $this->mTitle->quickUserCan( 'edit' ) && ( $this->mTitle->exists() || $this->mTitle->quickUserCan( 'create' ) ) ) {
722 $istalk = $this->mTitle->isTalkPage();
723 $istalkclass = $istalk?' istalk':'';
724 $content_actions['edit'] = array(
725 'class' => ( ( ( $action == 'edit' or $action == 'submit' ) and $section != 'new' ) ? 'selected' : '' ) . $istalkclass,
726 'text' => ( $this->mTitle->exists() || ( $this->mTitle->getNamespace() == NS_MEDIAWIKI && !wfEmptyMsg( $this->mTitle->getText() ) ) )
727 ? wfMsg( 'edit' )
728 : wfMsg( 'create' ),
729 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
730 );
731
732 // adds new section link if page is a current revision of a talk page or
733 if ( ( $wgArticle && $wgArticle->isCurrent() && $istalk ) || $wgOut->showNewSectionLink() ) {
734 if ( !$wgOut->forceHideNewSectionLink() ) {
735 $content_actions['addsection'] = array(
736 'class' => $section == 'new' ? 'selected' : false,
737 'text' => wfMsg( 'addsection' ),
738 'href' => $this->mTitle->getLocalUrl( 'action=edit&section=new' )
739 );
740 }
741 }
742 } elseif ( $this->mTitle->hasSourceText() ) {
743 $content_actions['viewsource'] = array(
744 'class' => ($action == 'edit') ? 'selected' : false,
745 'text' => wfMsg( 'viewsource' ),
746 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
747 );
748 }
749 wfProfileOut( __METHOD__ . '-edit' );
750
751 wfProfileIn( __METHOD__ . '-live' );
752 if ( $this->mTitle->exists() ) {
753
754 $content_actions['history'] = array(
755 'class' => ($action == 'history') ? 'selected' : false,
756 'text' => wfMsg( 'history_short' ),
757 'href' => $this->mTitle->getLocalUrl( 'action=history' ),
758 'rel' => 'archives',
759 );
760
761 if( $wgUser->isAllowed( 'delete' ) ) {
762 $content_actions['delete'] = array(
763 'class' => ($action == 'delete') ? 'selected' : false,
764 'text' => wfMsg( 'delete' ),
765 'href' => $this->mTitle->getLocalUrl( 'action=delete' )
766 );
767 }
768 if ( $this->mTitle->quickUserCan( 'move' ) ) {
769 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $this->thispage );
770 $content_actions['move'] = array(
771 'class' => $this->mTitle->isSpecial( 'Movepage' ) ? 'selected' : false,
772 'text' => wfMsg( 'move' ),
773 'href' => $moveTitle->getLocalUrl()
774 );
775 }
776
777 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
778 if( !$this->mTitle->isProtected() ){
779 $content_actions['protect'] = array(
780 'class' => ($action == 'protect') ? 'selected' : false,
781 'text' => wfMsg( 'protect' ),
782 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
783 );
784
785 } else {
786 $content_actions['unprotect'] = array(
787 'class' => ($action == 'unprotect') ? 'selected' : false,
788 'text' => wfMsg( 'unprotect' ),
789 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
790 );
791 }
792 }
793 } else {
794 //article doesn't exist or is deleted
795 if( $wgUser->isAllowed( 'deletedhistory' ) && $wgUser->isAllowed( 'deletedtext' ) ) {
796 $n = $this->mTitle->isDeleted();
797 if( $n ) {
798 $undelTitle = SpecialPage::getTitleFor( 'Undelete' );
799 $content_actions['undelete'] = array(
800 'class' => false,
801 'text' => wfMsgExt( 'undelete_short', array( 'parsemag' ), $wgLang->formatNum( $n ) ),
802 'href' => $undelTitle->getLocalUrl( 'target=' . urlencode( $this->thispage ) )
803 #'href' => self::makeSpecialUrl( "Undelete/$this->thispage" )
804 );
805 }
806 }
807
808 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
809 if( !$this->mTitle->getRestrictions( 'create' ) ) {
810 $content_actions['protect'] = array(
811 'class' => ($action == 'protect') ? 'selected' : false,
812 'text' => wfMsg( 'protect' ),
813 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
814 );
815
816 } else {
817 $content_actions['unprotect'] = array(
818 'class' => ($action == 'unprotect') ? 'selected' : false,
819 'text' => wfMsg( 'unprotect' ),
820 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
821 );
822 }
823 }
824 }
825
826 wfProfileOut( __METHOD__ . '-live' );
827
828 if( $this->loggedin ) {
829 if( !$this->mTitle->userIsWatching()) {
830 $content_actions['watch'] = array(
831 'class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : false,
832 'text' => wfMsg( 'watch' ),
833 'href' => $this->mTitle->getLocalUrl( 'action=watch' )
834 );
835 } else {
836 $content_actions['unwatch'] = array(
837 'class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : false,
838 'text' => wfMsg( 'unwatch' ),
839 'href' => $this->mTitle->getLocalUrl( 'action=unwatch' )
840 );
841 }
842 }
843
844
845 wfRunHooks( 'SkinTemplateTabs', array( $this, &$content_actions ) );
846 } else {
847 /* show special page tab */
848
849 $content_actions[$this->mTitle->getNamespaceKey()] = array(
850 'class' => 'selected',
851 'text' => wfMsg('nstab-special'),
852 'href' => $wgRequest->getRequestURL(), // @bug 2457, 2510
853 );
854
855 wfRunHooks( 'SkinTemplateBuildContentActionUrlsAfterSpecialPage', array( &$this, &$content_actions ) );
856 }
857
858 /* show links to different language variants */
859 global $wgDisableLangConversion;
860 $variants = $wgContLang->getVariants();
861 if( !$wgDisableLangConversion && sizeof( $variants ) > 1 ) {
862 $preferred = $wgContLang->getPreferredVariant();
863 $vcount=0;
864 foreach( $variants as $code ) {
865 $varname = $wgContLang->getVariantname( $code );
866 if( $varname == 'disable' )
867 continue;
868 $selected = ( $code == $preferred )? 'selected' : false;
869 $content_actions['varlang-' . $vcount] = array(
870 'class' => $selected,
871 'text' => $varname,
872 'href' => $this->mTitle->getLocalURL( '', $code )
873 );
874 $vcount ++;
875 }
876 }
877
878 wfRunHooks( 'SkinTemplateContentActions', array( &$content_actions ) );
879
880 wfProfileOut( __METHOD__ );
881 return $content_actions;
882 }
883
884 /**
885 * build array of common navigation links
886 * @return array
887 * @private
888 */
889 function buildNavUrls() {
890 global $wgUseTrackbacks, $wgOut, $wgUser, $wgRequest;
891 global $wgUploadNavigationUrl;
892
893 wfProfileIn( __METHOD__ );
894
895 $action = $wgRequest->getVal( 'action', 'view' );
896
897 $nav_urls = array();
898 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
899 if( $wgUploadNavigationUrl ) {
900 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
901 } elseif( UploadBase::isEnabled() && UploadBase::isAllowed( $wgUser ) === true ) {
902 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
903 } else {
904 $nav_urls['upload'] = false;
905 }
906 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
907
908 // default permalink to being off, will override it as required below.
909 $nav_urls['permalink'] = false;
910
911 // A print stylesheet is attached to all pages, but nobody ever
912 // figures that out. :) Add a link...
913 if( $this->iscontent && ( $action == 'view' || $action == 'purge' ) ) {
914 if ( !$wgOut->isPrintable() ) {
915 $nav_urls['print'] = array(
916 'text' => wfMsg( 'printableversion' ),
917 'href' => $wgRequest->appendQuery( 'printable=yes' )
918 );
919 }
920
921 // Also add a "permalink" while we're at it
922 if ( $this->mRevisionId ) {
923 $nav_urls['permalink'] = array(
924 'text' => wfMsg( 'permalink' ),
925 'href' => $wgOut->getTitle()->getLocalURL( "oldid=$this->mRevisionId" )
926 );
927 }
928
929 // Copy in case this undocumented, shady hook tries to mess with internals
930 $revid = $this->mRevisionId;
931 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink', array( &$this, &$nav_urls, &$revid, &$revid ) );
932 }
933
934 if( $this->mTitle->getNamespace() != NS_SPECIAL ) {
935 $wlhTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage );
936 $nav_urls['whatlinkshere'] = array(
937 'href' => $wlhTitle->getLocalUrl()
938 );
939 if( $this->mTitle->getArticleId() ) {
940 $rclTitle = SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage );
941 $nav_urls['recentchangeslinked'] = array(
942 'href' => $rclTitle->getLocalUrl()
943 );
944 } else {
945 $nav_urls['recentchangeslinked'] = false;
946 }
947 if( $wgUseTrackbacks )
948 $nav_urls['trackbacklink'] = array(
949 'href' => $wgOut->getTitle()->trackbackURL()
950 );
951 }
952
953 if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) {
954 $parts = explode( '/', $this->mTitle->getText() );
955 $rootUser = $parts[0];
956 $id = User::idFromName( $rootUser );
957 $ip = User::isIP( $rootUser );
958 } else {
959 $id = 0;
960 $ip = false;
961 }
962
963 if( $id || $ip ) { # both anons and non-anons have contribs list
964 $nav_urls['contributions'] = array(
965 'href' => self::makeSpecialUrlSubpage( 'Contributions', $rootUser )
966 );
967
968 if( $id ) {
969 $logPage = SpecialPage::getTitleFor( 'Log' );
970 $nav_urls['log'] = array(
971 'href' => $logPage->getLocalUrl(
972 array(
973 'user' => $rootUser
974 )
975 )
976 );
977 } else {
978 $nav_urls['log'] = false;
979 }
980
981 if ( $wgUser->isAllowed( 'block' ) ) {
982 $nav_urls['blockip'] = array(
983 'href' => self::makeSpecialUrlSubpage( 'Blockip', $rootUser )
984 );
985 } else {
986 $nav_urls['blockip'] = false;
987 }
988 } else {
989 $nav_urls['contributions'] = false;
990 $nav_urls['log'] = false;
991 $nav_urls['blockip'] = false;
992 }
993 $nav_urls['emailuser'] = false;
994 if( $this->showEmailUser( $id ) ) {
995 $nav_urls['emailuser'] = array(
996 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $rootUser )
997 );
998 }
999 wfProfileOut( __METHOD__ );
1000 return $nav_urls;
1001 }
1002
1003 /**
1004 * Generate strings used for xml 'id' names
1005 * @return string
1006 * @private
1007 */
1008 function getNameSpaceKey() {
1009 return $this->mTitle->getNamespaceKey();
1010 }
1011
1012 /**
1013 * @private
1014 */
1015 function setupUserJs( $allowUserJs ) {
1016 global $wgRequest, $wgJsMimeType;
1017 wfProfileIn( __METHOD__ );
1018
1019 $action = $wgRequest->getVal( 'action', 'view' );
1020
1021 if( $allowUserJs && $this->loggedin ) {
1022 if( $this->mTitle->isJsSubpage() and $this->userCanPreview( $action ) ) {
1023 # XXX: additional security check/prompt?
1024 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText( 'wpTextbox1' ) . ' /*]]>*/';
1025 } else {
1026 $this->userjs = self::makeUrl( $this->userpage . '/' . $this->skinname . '.js', 'action=raw&ctype=' . $wgJsMimeType );
1027 }
1028 }
1029 wfProfileOut( __METHOD__ );
1030 }
1031
1032 /**
1033 * Code for extensions to hook into to provide per-page CSS, see
1034 * extensions/PageCSS/PageCSS.php for an implementation of this.
1035 *
1036 * @private
1037 */
1038 function setupPageCss() {
1039 wfProfileIn( __METHOD__ );
1040 $out = false;
1041 wfRunHooks( 'SkinTemplateSetupPageCss', array( &$out ) );
1042 wfProfileOut( __METHOD__ );
1043 return $out;
1044 }
1045
1046 public function commonPrintStylesheet() {
1047 return false;
1048 }
1049 }
1050
1051 /**
1052 * Generic wrapper for template functions, with interface
1053 * compatible with what we use of PHPTAL 0.7.
1054 * @ingroup Skins
1055 */
1056 abstract class QuickTemplate {
1057 /**
1058 * Constructor
1059 */
1060 public function QuickTemplate() {
1061 $this->data = array();
1062 $this->translator = new MediaWiki_I18N();
1063 }
1064
1065 /**
1066 * Sets the value $value to $name
1067 * @param $name
1068 * @param $value
1069 */
1070 public function set( $name, $value ) {
1071 $this->data[$name] = $value;
1072 }
1073
1074 /**
1075 * @param $name
1076 * @param $value
1077 */
1078 public function setRef( $name, &$value ) {
1079 $this->data[$name] =& $value;
1080 }
1081
1082 /**
1083 * @param $t
1084 */
1085 public function setTranslator( &$t ) {
1086 $this->translator = &$t;
1087 }
1088
1089 /**
1090 * Main function, used by classes that subclass QuickTemplate
1091 * to show the actual HTML output
1092 */
1093 abstract public function execute();
1094
1095 /**
1096 * @private
1097 */
1098 function text( $str ) {
1099 echo htmlspecialchars( $this->data[$str] );
1100 }
1101
1102 /**
1103 * @private
1104 */
1105 function jstext( $str ) {
1106 echo Xml::escapeJsString( $this->data[$str] );
1107 }
1108
1109 /**
1110 * @private
1111 */
1112 function html( $str ) {
1113 echo $this->data[$str];
1114 }
1115
1116 /**
1117 * @private
1118 */
1119 function msg( $str ) {
1120 echo htmlspecialchars( $this->translator->translate( $str ) );
1121 }
1122
1123 /**
1124 * @private
1125 */
1126 function msgHtml( $str ) {
1127 echo $this->translator->translate( $str );
1128 }
1129
1130 /**
1131 * An ugly, ugly hack.
1132 * @private
1133 */
1134 function msgWiki( $str ) {
1135 global $wgParser, $wgOut;
1136
1137 $text = $this->translator->translate( $str );
1138 $parserOutput = $wgParser->parse( $text, $wgOut->getTitle(),
1139 $wgOut->parserOptions(), true );
1140 echo $parserOutput->getText();
1141 }
1142
1143 /**
1144 * @private
1145 */
1146 function haveData( $str ) {
1147 return isset( $this->data[$str] );
1148 }
1149
1150 /**
1151 * @private
1152 */
1153 function haveMsg( $str ) {
1154 $msg = $this->translator->translate( $str );
1155 return ( $msg != '-' ) && ( $msg != '' ); # ????
1156 }
1157 }