Various unused variables, add some braces
[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 $res = $dbr->select( 'watchlist',
362 array( 'COUNT(*) AS n' ),
363 array( 'wl_title' => $dbr->strencode( $this->mTitle->getDBkey() ), 'wl_namespace' => $this->mTitle->getNamespace() ),
364 __METHOD__
365 );
366 $x = $dbr->fetchObject( $res );
367 $numberofwatchingusers = $x->n;
368 if( $numberofwatchingusers > 0 ) {
369 $tpl->set( 'numberofwatchingusers',
370 wfMsgExt( 'number_of_watching_users_pageview', array( 'parseinline' ),
371 $wgLang->formatNum( $numberofwatchingusers ) )
372 );
373 } else {
374 $tpl->set( 'numberofwatchingusers', false );
375 }
376 } else {
377 $tpl->set( 'numberofwatchingusers', false );
378 }
379
380 $tpl->set( 'copyright', $this->getCopyright() );
381
382 $this->credits = false;
383
384 if( $wgMaxCredits != 0 ){
385 $this->credits = Credits::getCredits( $wgArticle, $wgMaxCredits, $wgShowCreditsIfMax );
386 } else {
387 $tpl->set( 'lastmod', $this->lastModified() );
388 }
389
390 $tpl->setRef( 'credits', $this->credits );
391
392 } elseif ( isset( $oldid ) && !isset( $diff ) ) {
393 $tpl->set( 'copyright', $this->getCopyright() );
394 $tpl->set( 'viewcount', false );
395 $tpl->set( 'lastmod', false );
396 $tpl->set( 'credits', false );
397 $tpl->set( 'numberofwatchingusers', false );
398 } else {
399 $tpl->set( 'copyright', false );
400 $tpl->set( 'viewcount', false );
401 $tpl->set( 'lastmod', false );
402 $tpl->set( 'credits', false );
403 $tpl->set( 'numberofwatchingusers', false );
404 }
405 wfProfileOut( __METHOD__ . '-stuff3' );
406
407 wfProfileIn( __METHOD__ . '-stuff4' );
408 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
409 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
410 $tpl->set( 'disclaimer', $this->disclaimerLink() );
411 $tpl->set( 'privacy', $this->privacyLink() );
412 $tpl->set( 'about', $this->aboutLink() );
413
414 if ( $wgDebugComments ) {
415 $tpl->setRef( 'debug', $out->mDebugtext );
416 } else {
417 $tpl->set( 'debug', '' );
418 }
419
420 $tpl->set( 'reporttime', wfReportTime() );
421 $tpl->set( 'sitenotice', wfGetSiteNotice() );
422 $tpl->set( 'bottomscripts', $this->bottomScripts( $out ) );
423
424 $printfooter = "<div class=\"printfooter\">\n" . $this->printSource() . "</div>\n";
425 global $wgBetterDirectionality;
426 if ( $wgBetterDirectionality ) {
427 $realBodyAttribs = array( 'lang' => $wgLanguageCode, 'dir' => $wgContLang->getDir() );
428 $out->mBodytext = Html::rawElement( 'div', $realBodyAttribs, $out->mBodytext );
429 }
430 $out->mBodytext .= $printfooter . $this->generateDebugHTML();
431 $tpl->setRef( 'bodytext', $out->mBodytext );
432
433 # Language links
434 $language_urls = array();
435
436 if ( !$wgHideInterlanguageLinks ) {
437 foreach( $out->getLanguageLinks() as $l ) {
438 $tmp = explode( ':', $l, 2 );
439 $class = 'interwiki-' . $tmp[0];
440 unset( $tmp );
441 $nt = Title::newFromText( $l );
442 if ( $nt ) {
443 $language_urls[] = array(
444 'href' => $nt->getFullURL(),
445 'text' => ( $wgContLang->getLanguageName( $nt->getInterwiki() ) != '' ?
446 $wgContLang->getLanguageName( $nt->getInterwiki() ) : $l ),
447 'title' => $nt->getText(),
448 'class' => $class
449 );
450 }
451 }
452 }
453 if( count( $language_urls ) ) {
454 $tpl->setRef( 'language_urls', $language_urls );
455 } else {
456 $tpl->set( 'language_urls', false );
457 }
458 wfProfileOut( __METHOD__ . '-stuff4' );
459
460 wfProfileIn( __METHOD__ . '-stuff5' );
461 # Personal toolbar
462 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
463 $content_actions = $this->buildContentActionUrls();
464 $tpl->setRef( 'content_actions', $content_actions );
465
466 $tpl->set( 'sidebar', $this->buildSidebar() );
467 $tpl->set( 'nav_urls', $this->buildNavUrls() );
468
469 // Set the head scripts near the end, in case the above actions resulted in added scripts
470 if ( $this->useHeadElement ) {
471 $tpl->set( 'headelement', $out->headElement( $this ) );
472 } else {
473 $tpl->set( 'headscripts', $out->getScript() );
474 }
475
476 // original version by hansm
477 if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
478 wfDebug( __METHOD__ . ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
479 }
480
481 // allow extensions adding stuff after the page content.
482 // See Skin::afterContentHook() for further documentation.
483 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
484 wfProfileOut( __METHOD__ . '-stuff5' );
485
486 // execute template
487 wfProfileIn( __METHOD__ . '-execute' );
488 $res = $tpl->execute();
489 wfProfileOut( __METHOD__ . '-execute' );
490
491 // result may be an error
492 $this->printOrError( $res );
493 wfProfileOut( __METHOD__ );
494 }
495
496 /**
497 * Output the string, or print error message if it's
498 * an error object of the appropriate type.
499 * For the base class, assume strings all around.
500 *
501 * @param $str Mixed
502 * @private
503 */
504 function printOrError( $str ) {
505 echo $str;
506 }
507
508 /**
509 * build array of urls for personal toolbar
510 * @return array
511 * @private
512 */
513 function buildPersonalUrls() {
514 global $wgOut, $wgRequest;
515
516 $title = $wgOut->getTitle();
517 $pageurl = $title->getLocalURL();
518 wfProfileIn( __METHOD__ );
519
520 /* set up the default links for the personal toolbar */
521 $personal_urls = array();
522 $page = $wgRequest->getVal( 'returnto', $this->thisurl );
523 $query = $wgRequest->getVal( 'returntoquery', $this->thisquery );
524 $returnto = "returnto=$page";
525 if( $this->thisquery != '' )
526 $returnto .= "&returntoquery=$query";
527 if( $this->loggedin ) {
528 $personal_urls['userpage'] = array(
529 'text' => $this->username,
530 'href' => &$this->userpageUrlDetails['href'],
531 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
532 'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
533 );
534 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
535 $personal_urls['mytalk'] = array(
536 'text' => wfMsg( 'mytalk' ),
537 'href' => &$usertalkUrlDetails['href'],
538 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
539 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
540 );
541 $href = self::makeSpecialUrl( 'Preferences' );
542 $personal_urls['preferences'] = array(
543 'text' => wfMsg( 'mypreferences' ),
544 'href' => $href,
545 'active' => ( $href == $pageurl )
546 );
547 $href = self::makeSpecialUrl( 'Watchlist' );
548 $personal_urls['watchlist'] = array(
549 'text' => wfMsg( 'mywatchlist' ),
550 'href' => $href,
551 'active' => ( $href == $pageurl )
552 );
553
554 # We need to do an explicit check for Special:Contributions, as we
555 # have to match both the title, and the target (which could come
556 # from request values or be specified in "sub page" form. The plot
557 # thickens, because $wgTitle is altered for special pages, so doesn't
558 # contain the original alias-with-subpage.
559 $origTitle = Title::newFromText( $wgRequest->getText( 'title' ) );
560 if( $origTitle instanceof Title && $origTitle->getNamespace() == NS_SPECIAL ) {
561 list( $spName, $spPar ) =
562 SpecialPage::resolveAliasWithSubpage( $origTitle->getText() );
563 $active = $spName == 'Contributions'
564 && ( ( $spPar && $spPar == $this->username )
565 || $wgRequest->getText( 'target' ) == $this->username );
566 } else {
567 $active = false;
568 }
569
570 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
571 $personal_urls['mycontris'] = array(
572 'text' => wfMsg( 'mycontris' ),
573 'href' => $href,
574 'active' => $active
575 );
576 $personal_urls['logout'] = array(
577 'text' => wfMsg( 'userlogout' ),
578 'href' => self::makeSpecialUrl( 'Userlogout',
579 $title->isSpecial( 'Preferences' ) ? '' : $returnto
580 ),
581 'active' => false
582 );
583 } else {
584 global $wgUser;
585 $loginlink = $wgUser->isAllowed( 'createaccount' )
586 ? 'nav-login-createaccount'
587 : 'login';
588
589 # anonlogin & login are the same
590 $login_url = array(
591 'text' => wfMsg( $loginlink ),
592 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
593 'active' => $title->isSpecial( 'Userlogin' )
594 );
595 global $wgProto, $wgSecureLogin;
596 if( $wgProto === 'http' && $wgSecureLogin ) {
597 $title = SpecialPage::getTitleFor( 'Userlogin' );
598 $https_url = preg_replace( '/^http:/', 'https:', $title->getFullURL() );
599 $login_url['href'] = $https_url;
600 $login_url['class'] = 'link-https'; # FIXME class depends on skin
601 }
602
603 if( $this->showIPinHeader() ) {
604 $href = &$this->userpageUrlDetails['href'];
605 $personal_urls['anonuserpage'] = array(
606 'text' => $this->username,
607 'href' => $href,
608 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
609 'active' => ( $pageurl == $href )
610 );
611 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
612 $href = &$usertalkUrlDetails['href'];
613 $personal_urls['anontalk'] = array(
614 'text' => wfMsg( 'anontalk' ),
615 'href' => $href,
616 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
617 'active' => ( $pageurl == $href )
618 );
619 $personal_urls['anonlogin'] = $login_url;
620 } else {
621 $personal_urls['login'] = $login_url;
622 }
623 }
624
625 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$title ) );
626 wfProfileOut( __METHOD__ );
627 return $personal_urls;
628 }
629
630 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
631 $classes = array();
632 if( $selected ) {
633 $classes[] = 'selected';
634 }
635 if( $checkEdit && !$title->isKnown() ) {
636 $classes[] = 'new';
637 $query = 'action=edit&redlink=1';
638 }
639
640 $text = wfMsg( $message );
641 if ( wfEmptyMsg( $message, $text ) ) {
642 global $wgContLang;
643 $text = $wgContLang->getFormattedNsText( MWNamespace::getSubject( $title->getNamespace() ) );
644 }
645
646 $result = array();
647 if( !wfRunHooks( 'SkinTemplateTabAction', array( &$this,
648 $title, $message, $selected, $checkEdit,
649 &$classes, &$query, &$text, &$result ) ) ) {
650 return $result;
651 }
652
653 return array(
654 'class' => implode( ' ', $classes ),
655 'text' => $text,
656 'href' => $title->getLocalUrl( $query ) );
657 }
658
659 function makeTalkUrlDetails( $name, $urlaction = '' ) {
660 $title = Title::newFromText( $name );
661 if( !is_object( $title ) ) {
662 throw new MWException( __METHOD__ . " given invalid pagename $name" );
663 }
664 $title = $title->getTalkPage();
665 self::checkTitle( $title, $name );
666 return array(
667 'href' => $title->getLocalURL( $urlaction ),
668 'exists' => $title->getArticleID() != 0,
669 );
670 }
671
672 function makeArticleUrlDetails( $name, $urlaction = '' ) {
673 $title = Title::newFromText( $name );
674 $title= $title->getSubjectPage();
675 self::checkTitle( $title, $name );
676 return array(
677 'href' => $title->getLocalURL( $urlaction ),
678 'exists' => $title->getArticleID() != 0,
679 );
680 }
681
682 /**
683 * an array of edit links by default used for the tabs
684 * @return array
685 * @private
686 */
687 function buildContentActionUrls() {
688 global $wgContLang, $wgLang, $wgOut, $wgUser, $wgRequest, $wgArticle;
689
690 wfProfileIn( __METHOD__ );
691
692 $action = $wgRequest->getVal( 'action', 'view' );
693 $section = $wgRequest->getVal( 'section' );
694 $content_actions = array();
695
696 $prevent_active_tabs = false;
697 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this, &$prevent_active_tabs ) );
698
699 if( $this->iscontent ) {
700 $subjpage = $this->mTitle->getSubjectPage();
701 $talkpage = $this->mTitle->getTalkPage();
702
703 $nskey = $this->mTitle->getNamespaceKey();
704 $content_actions[$nskey] = $this->tabAction(
705 $subjpage,
706 $nskey,
707 !$this->mTitle->isTalkPage() && !$prevent_active_tabs,
708 '', true
709 );
710
711 $content_actions['talk'] = $this->tabAction(
712 $talkpage,
713 'talk',
714 $this->mTitle->isTalkPage() && !$prevent_active_tabs,
715 '',
716 true
717 );
718
719 wfProfileIn( __METHOD__ . '-edit' );
720 if ( $this->mTitle->quickUserCan( 'edit' ) && ( $this->mTitle->exists() || $this->mTitle->quickUserCan( 'create' ) ) ) {
721 $istalk = $this->mTitle->isTalkPage();
722 $istalkclass = $istalk?' istalk':'';
723 $content_actions['edit'] = array(
724 'class' => ( ( ( $action == 'edit' or $action == 'submit' ) and $section != 'new' ) ? 'selected' : '' ) . $istalkclass,
725 'text' => ( $this->mTitle->exists() || ( $this->mTitle->getNamespace() == NS_MEDIAWIKI && !wfEmptyMsg( $this->mTitle->getText() ) ) )
726 ? wfMsg( 'edit' )
727 : wfMsg( 'create' ),
728 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
729 );
730
731 // adds new section link if page is a current revision of a talk page or
732 if ( ( $wgArticle && $wgArticle->isCurrent() && $istalk ) || $wgOut->showNewSectionLink() ) {
733 if ( !$wgOut->forceHideNewSectionLink() ) {
734 $content_actions['addsection'] = array(
735 'class' => $section == 'new' ? 'selected' : false,
736 'text' => wfMsg( 'addsection' ),
737 'href' => $this->mTitle->getLocalUrl( 'action=edit&section=new' )
738 );
739 }
740 }
741 } elseif ( $this->mTitle->hasSourceText() ) {
742 $content_actions['viewsource'] = array(
743 'class' => ($action == 'edit') ? 'selected' : false,
744 'text' => wfMsg( 'viewsource' ),
745 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
746 );
747 }
748 wfProfileOut( __METHOD__ . '-edit' );
749
750 wfProfileIn( __METHOD__ . '-live' );
751 if ( $this->mTitle->exists() ) {
752
753 $content_actions['history'] = array(
754 'class' => ($action == 'history') ? 'selected' : false,
755 'text' => wfMsg( 'history_short' ),
756 'href' => $this->mTitle->getLocalUrl( 'action=history' ),
757 'rel' => 'archives',
758 );
759
760 if( $wgUser->isAllowed( 'delete' ) ) {
761 $content_actions['delete'] = array(
762 'class' => ($action == 'delete') ? 'selected' : false,
763 'text' => wfMsg( 'delete' ),
764 'href' => $this->mTitle->getLocalUrl( 'action=delete' )
765 );
766 }
767 if ( $this->mTitle->quickUserCan( 'move' ) ) {
768 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $this->thispage );
769 $content_actions['move'] = array(
770 'class' => $this->mTitle->isSpecial( 'Movepage' ) ? 'selected' : false,
771 'text' => wfMsg( 'move' ),
772 'href' => $moveTitle->getLocalUrl()
773 );
774 }
775
776 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
777 if( !$this->mTitle->isProtected() ){
778 $content_actions['protect'] = array(
779 'class' => ($action == 'protect') ? 'selected' : false,
780 'text' => wfMsg( 'protect' ),
781 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
782 );
783
784 } else {
785 $content_actions['unprotect'] = array(
786 'class' => ($action == 'unprotect') ? 'selected' : false,
787 'text' => wfMsg( 'unprotect' ),
788 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
789 );
790 }
791 }
792 } else {
793 //article doesn't exist or is deleted
794 if( $wgUser->isAllowed( 'deletedhistory' ) && $wgUser->isAllowed( 'deletedtext' ) ) {
795 $n = $this->mTitle->isDeleted();
796 if( $n ) {
797 $undelTitle = SpecialPage::getTitleFor( 'Undelete' );
798 $content_actions['undelete'] = array(
799 'class' => false,
800 'text' => wfMsgExt( 'undelete_short', array( 'parsemag' ), $wgLang->formatNum( $n ) ),
801 'href' => $undelTitle->getLocalUrl( 'target=' . urlencode( $this->thispage ) )
802 #'href' => self::makeSpecialUrl( "Undelete/$this->thispage" )
803 );
804 }
805 }
806
807 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
808 if( !$this->mTitle->getRestrictions( 'create' ) ) {
809 $content_actions['protect'] = array(
810 'class' => ($action == 'protect') ? 'selected' : false,
811 'text' => wfMsg( 'protect' ),
812 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
813 );
814
815 } else {
816 $content_actions['unprotect'] = array(
817 'class' => ($action == 'unprotect') ? 'selected' : false,
818 'text' => wfMsg( 'unprotect' ),
819 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
820 );
821 }
822 }
823 }
824
825 wfProfileOut( __METHOD__ . '-live' );
826
827 if( $this->loggedin ) {
828 if( !$this->mTitle->userIsWatching()) {
829 $content_actions['watch'] = array(
830 'class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : false,
831 'text' => wfMsg( 'watch' ),
832 'href' => $this->mTitle->getLocalUrl( 'action=watch' )
833 );
834 } else {
835 $content_actions['unwatch'] = array(
836 'class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : false,
837 'text' => wfMsg( 'unwatch' ),
838 'href' => $this->mTitle->getLocalUrl( 'action=unwatch' )
839 );
840 }
841 }
842
843
844 wfRunHooks( 'SkinTemplateTabs', array( $this, &$content_actions ) );
845 } else {
846 /* show special page tab */
847
848 $content_actions[$this->mTitle->getNamespaceKey()] = array(
849 'class' => 'selected',
850 'text' => wfMsg('nstab-special'),
851 'href' => $wgRequest->getRequestURL(), // @bug 2457, 2510
852 );
853
854 wfRunHooks( 'SkinTemplateBuildContentActionUrlsAfterSpecialPage', array( &$this, &$content_actions ) );
855 }
856
857 /* show links to different language variants */
858 global $wgDisableLangConversion;
859 $variants = $wgContLang->getVariants();
860 if( !$wgDisableLangConversion && sizeof( $variants ) > 1 ) {
861 $preferred = $wgContLang->getPreferredVariant();
862 $vcount=0;
863 foreach( $variants as $code ) {
864 $varname = $wgContLang->getVariantname( $code );
865 if( $varname == 'disable' )
866 continue;
867 $selected = ( $code == $preferred )? 'selected' : false;
868 $content_actions['varlang-' . $vcount] = array(
869 'class' => $selected,
870 'text' => $varname,
871 'href' => $this->mTitle->getLocalURL( '', $code )
872 );
873 $vcount ++;
874 }
875 }
876
877 wfRunHooks( 'SkinTemplateContentActions', array( &$content_actions ) );
878
879 wfProfileOut( __METHOD__ );
880 return $content_actions;
881 }
882
883 /**
884 * build array of common navigation links
885 * @return array
886 * @private
887 */
888 function buildNavUrls() {
889 global $wgUseTrackbacks, $wgOut, $wgUser, $wgRequest;
890 global $wgUploadNavigationUrl;
891
892 wfProfileIn( __METHOD__ );
893
894 $action = $wgRequest->getVal( 'action', 'view' );
895
896 $nav_urls = array();
897 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
898 if( $wgUploadNavigationUrl ) {
899 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
900 } elseif( UploadBase::isEnabled() && UploadBase::isAllowed( $wgUser ) === true ) {
901 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
902 } else {
903 $nav_urls['upload'] = false;
904 }
905 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
906
907 // default permalink to being off, will override it as required below.
908 $nav_urls['permalink'] = false;
909
910 // A print stylesheet is attached to all pages, but nobody ever
911 // figures that out. :) Add a link...
912 if( $this->iscontent && ( $action == 'view' || $action == 'purge' ) ) {
913 if ( !$wgOut->isPrintable() ) {
914 $nav_urls['print'] = array(
915 'text' => wfMsg( 'printableversion' ),
916 'href' => $wgRequest->appendQuery( 'printable=yes' )
917 );
918 }
919
920 // Also add a "permalink" while we're at it
921 if ( $this->mRevisionId ) {
922 $nav_urls['permalink'] = array(
923 'text' => wfMsg( 'permalink' ),
924 'href' => $wgOut->getTitle()->getLocalURL( "oldid=$this->mRevisionId" )
925 );
926 }
927
928 // Copy in case this undocumented, shady hook tries to mess with internals
929 $revid = $this->mRevisionId;
930 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink', array( &$this, &$nav_urls, &$revid, &$revid ) );
931 }
932
933 if( $this->mTitle->getNamespace() != NS_SPECIAL ) {
934 $wlhTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage );
935 $nav_urls['whatlinkshere'] = array(
936 'href' => $wlhTitle->getLocalUrl()
937 );
938 if( $this->mTitle->getArticleId() ) {
939 $rclTitle = SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage );
940 $nav_urls['recentchangeslinked'] = array(
941 'href' => $rclTitle->getLocalUrl()
942 );
943 } else {
944 $nav_urls['recentchangeslinked'] = false;
945 }
946 if( $wgUseTrackbacks )
947 $nav_urls['trackbacklink'] = array(
948 'href' => $wgOut->getTitle()->trackbackURL()
949 );
950 }
951
952 if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) {
953 $parts = explode( '/', $this->mTitle->getText() );
954 $rootUser = $parts[0];
955 $id = User::idFromName( $rootUser );
956 $ip = User::isIP( $rootUser );
957 } else {
958 $id = 0;
959 $ip = false;
960 }
961
962 if( $id || $ip ) { # both anons and non-anons have contribs list
963 $nav_urls['contributions'] = array(
964 'href' => self::makeSpecialUrlSubpage( 'Contributions', $rootUser )
965 );
966
967 if( $id ) {
968 $logPage = SpecialPage::getTitleFor( 'Log' );
969 $nav_urls['log'] = array(
970 'href' => $logPage->getLocalUrl(
971 array(
972 'user' => $rootUser
973 )
974 )
975 );
976 } else {
977 $nav_urls['log'] = false;
978 }
979
980 if ( $wgUser->isAllowed( 'block' ) ) {
981 $nav_urls['blockip'] = array(
982 'href' => self::makeSpecialUrlSubpage( 'Blockip', $rootUser )
983 );
984 } else {
985 $nav_urls['blockip'] = false;
986 }
987 } else {
988 $nav_urls['contributions'] = false;
989 $nav_urls['log'] = false;
990 $nav_urls['blockip'] = false;
991 }
992 $nav_urls['emailuser'] = false;
993 if( $this->showEmailUser( $id ) ) {
994 $nav_urls['emailuser'] = array(
995 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $rootUser )
996 );
997 }
998 wfProfileOut( __METHOD__ );
999 return $nav_urls;
1000 }
1001
1002 /**
1003 * Generate strings used for xml 'id' names
1004 * @return string
1005 * @private
1006 */
1007 function getNameSpaceKey() {
1008 return $this->mTitle->getNamespaceKey();
1009 }
1010
1011 /**
1012 * @private
1013 */
1014 function setupUserJs( $allowUserJs ) {
1015 global $wgRequest, $wgJsMimeType;
1016 wfProfileIn( __METHOD__ );
1017
1018 $action = $wgRequest->getVal( 'action', 'view' );
1019
1020 if( $allowUserJs && $this->loggedin ) {
1021 if( $this->mTitle->isJsSubpage() and $this->userCanPreview( $action ) ) {
1022 # XXX: additional security check/prompt?
1023 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText( 'wpTextbox1' ) . ' /*]]>*/';
1024 } else {
1025 $this->userjs = self::makeUrl( $this->userpage . '/' . $this->skinname . '.js', 'action=raw&ctype=' . $wgJsMimeType );
1026 }
1027 }
1028 wfProfileOut( __METHOD__ );
1029 }
1030
1031 /**
1032 * Code for extensions to hook into to provide per-page CSS, see
1033 * extensions/PageCSS/PageCSS.php for an implementation of this.
1034 *
1035 * @private
1036 */
1037 function setupPageCss() {
1038 wfProfileIn( __METHOD__ );
1039 $out = false;
1040 wfRunHooks( 'SkinTemplateSetupPageCss', array( &$out ) );
1041 wfProfileOut( __METHOD__ );
1042 return $out;
1043 }
1044
1045 public function commonPrintStylesheet() {
1046 return false;
1047 }
1048 }
1049
1050 /**
1051 * Generic wrapper for template functions, with interface
1052 * compatible with what we use of PHPTAL 0.7.
1053 * @ingroup Skins
1054 */
1055 abstract class QuickTemplate {
1056 /**
1057 * Constructor
1058 */
1059 public function QuickTemplate() {
1060 $this->data = array();
1061 $this->translator = new MediaWiki_I18N();
1062 }
1063
1064 /**
1065 * Sets the value $value to $name
1066 * @param $name
1067 * @param $value
1068 */
1069 public function set( $name, $value ) {
1070 $this->data[$name] = $value;
1071 }
1072
1073 /**
1074 * @param $name
1075 * @param $value
1076 */
1077 public function setRef( $name, &$value ) {
1078 $this->data[$name] =& $value;
1079 }
1080
1081 /**
1082 * @param $t
1083 */
1084 public function setTranslator( &$t ) {
1085 $this->translator = &$t;
1086 }
1087
1088 /**
1089 * Main function, used by classes that subclass QuickTemplate
1090 * to show the actual HTML output
1091 */
1092 abstract public function execute();
1093
1094 /**
1095 * @private
1096 */
1097 function text( $str ) {
1098 echo htmlspecialchars( $this->data[$str] );
1099 }
1100
1101 /**
1102 * @private
1103 */
1104 function jstext( $str ) {
1105 echo Xml::escapeJsString( $this->data[$str] );
1106 }
1107
1108 /**
1109 * @private
1110 */
1111 function html( $str ) {
1112 echo $this->data[$str];
1113 }
1114
1115 /**
1116 * @private
1117 */
1118 function msg( $str ) {
1119 echo htmlspecialchars( $this->translator->translate( $str ) );
1120 }
1121
1122 /**
1123 * @private
1124 */
1125 function msgHtml( $str ) {
1126 echo $this->translator->translate( $str );
1127 }
1128
1129 /**
1130 * An ugly, ugly hack.
1131 * @private
1132 */
1133 function msgWiki( $str ) {
1134 global $wgParser, $wgOut;
1135
1136 $text = $this->translator->translate( $str );
1137 $parserOutput = $wgParser->parse( $text, $wgOut->getTitle(),
1138 $wgOut->parserOptions(), true );
1139 echo $parserOutput->getText();
1140 }
1141
1142 /**
1143 * @private
1144 */
1145 function haveData( $str ) {
1146 return isset( $this->data[$str] );
1147 }
1148
1149 /**
1150 * @private
1151 */
1152 function haveMsg( $str ) {
1153 $msg = $this->translator->translate( $str );
1154 return ( $msg != '-' ) && ( $msg != '' ); # ????
1155 }
1156 }