Followup r95753 per CR: prevent extensions from making isMovable() return true for...
[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 /**
28 * Wrapper object for MediaWiki's localization functions,
29 * to be passed to the template engine.
30 *
31 * @private
32 * @ingroup Skins
33 */
34 class MediaWiki_I18N {
35 var $_context = array();
36
37 function set( $varName, $value ) {
38 $this->_context[$varName] = $value;
39 }
40
41 function translate( $value ) {
42 wfProfileIn( __METHOD__ );
43
44 // Hack for i18n:attributes in PHPTAL 1.0.0 dev version as of 2004-10-23
45 $value = preg_replace( '/^string:/', '', $value );
46
47 $value = wfMsg( $value );
48 // interpolate variables
49 $m = array();
50 while( preg_match( '/\$([0-9]*?)/sm', $value, $m ) ) {
51 list( $src, $var ) = $m;
52 wfSuppressWarnings();
53 $varValue = $this->_context[$var];
54 wfRestoreWarnings();
55 $value = str_replace( $src, $varValue, $value );
56 }
57 wfProfileOut( __METHOD__ );
58 return $value;
59 }
60 }
61
62 /**
63 * Template-filler skin base class
64 * Formerly generic PHPTal (http://phptal.sourceforge.net/) skin
65 * Based on Brion's smarty skin
66 * @copyright Copyright © Gabriel Wicke -- http://www.aulinx.de/
67 *
68 * @todo Needs some serious refactoring into functions that correspond
69 * to the computations individual esi snippets need. Most importantly no body
70 * parsing for most of those of course.
71 *
72 * @ingroup Skins
73 */
74 class SkinTemplate extends Skin {
75 /**#@+
76 * @private
77 */
78
79 /**
80 * Name of our skin, it probably needs to be all lower case. Child classes
81 * should override the default.
82 */
83 var $skinname = 'monobook';
84
85 /**
86 * Stylesheets set to use. Subdirectory in skins/ where various stylesheets
87 * are located. Child classes should override the default.
88 */
89 var $stylename = 'monobook';
90
91 /**
92 * For QuickTemplate, the name of the subclass which will actually fill the
93 * template. Child classes should override the default.
94 */
95 var $template = 'QuickTemplate';
96
97 /**
98 * Whether this skin use OutputPage::headElement() to generate the <head>
99 * tag
100 */
101 var $useHeadElement = false;
102
103 /**#@-*/
104
105 /**
106 * Add specific styles for this skin
107 *
108 * @param $out OutputPage
109 */
110 function setupSkinUserCss( OutputPage $out ) {
111 $out->addModuleStyles( array( 'mediawiki.legacy.shared', 'mediawiki.legacy.commonPrint' ) );
112 }
113
114 /**
115 * Create the template engine object; we feed it a bunch of data
116 * and eventually it spits out some HTML. Should have interface
117 * roughly equivalent to PHPTAL 0.7.
118 *
119 * @param $classname String
120 * @param $repository string: subdirectory where we keep template files
121 * @param $cache_dir string
122 * @return QuickTemplate
123 * @private
124 */
125 function setupTemplate( $classname, $repository = false, $cache_dir = false ) {
126 return new $classname();
127 }
128
129 /**
130 * initialize various variables and generate the template
131 *
132 * @param $out OutputPage
133 */
134 function outputPage( OutputPage $out ) {
135 global $wgUser, $wgLang, $wgContLang;
136 global $wgScript, $wgStylePath;
137 global $wgMimeType, $wgJsMimeType, $wgRequest;
138 global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces, $wgHtml5Version;
139 global $wgDisableCounters, $wgLogo, $wgHideInterlanguageLinks;
140 global $wgMaxCredits, $wgShowCreditsIfMax;
141 global $wgPageShowWatchingUsers;
142 global $wgUseTrackbacks, $wgUseSiteJs, $wgDebugComments;
143 global $wgArticlePath, $wgScriptPath, $wgServer;
144
145 wfProfileIn( __METHOD__ );
146 Profiler::instance()->setTemplated( true );
147
148 wfProfileIn( __METHOD__ . '-init' );
149 $this->initPage( $out );
150
151 $tpl = $this->setupTemplate( $this->template, 'skins' );
152 wfProfileOut( __METHOD__ . '-init' );
153
154 wfProfileIn( __METHOD__ . '-stuff' );
155 $this->thispage = $this->getTitle()->getPrefixedDBkey();
156 $this->userpage = $this->getUser()->getUserPage()->getPrefixedText();
157 $query = array();
158 if ( !$wgRequest->wasPosted() ) {
159 $query = $wgRequest->getValues();
160 unset( $query['title'] );
161 unset( $query['returnto'] );
162 unset( $query['returntoquery'] );
163 }
164 $this->thisquery = wfArrayToCGI( $query );
165 $this->loggedin = $wgUser->isLoggedIn();
166 $this->username = $wgUser->getName();
167
168 if ( $wgUser->isLoggedIn() || $this->showIPinHeader() ) {
169 $this->userpageUrlDetails = self::makeUrlDetails( $this->userpage );
170 } else {
171 # This won't be used in the standard skins, but we define it to preserve the interface
172 # To save time, we check for existence
173 $this->userpageUrlDetails = self::makeKnownUrlDetails( $this->userpage );
174 }
175
176 $this->titletxt = $this->getTitle()->getPrefixedText();
177 wfProfileOut( __METHOD__ . '-stuff' );
178
179 wfProfileIn( __METHOD__ . '-stuff-head' );
180 if ( !$this->useHeadElement ) {
181 $tpl->set( 'pagecss', false );
182 $tpl->set( 'usercss', false );
183
184 $this->userjs = $this->userjsprev = false;
185 # @todo FIXME: This is the only use of OutputPage::isUserJsAllowed() anywhere; can we
186 # get rid of it? For that matter, why is any of this here at all?
187 $this->setupUserJs( $out->isUserJsAllowed() );
188 $tpl->setRef( 'userjs', $this->userjs );
189 $tpl->setRef( 'userjsprev', $this->userjsprev );
190
191 if( $wgUseSiteJs ) {
192 $jsCache = $this->loggedin ? '&smaxage=0' : '';
193 $tpl->set( 'jsvarurl',
194 self::makeUrl( '-',
195 "action=raw$jsCache&gen=js&useskin=" .
196 urlencode( $this->getSkinName() ) ) );
197 } else {
198 $tpl->set( 'jsvarurl', false );
199 }
200
201 $tpl->setRef( 'xhtmldefaultnamespace', $wgXhtmlDefaultNamespace );
202 $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
203 $tpl->set( 'html5version', $wgHtml5Version );
204 $tpl->set( 'headlinks', $out->getHeadLinks() );
205 $tpl->set( 'csslinks', $out->buildCssLinks() );
206
207 if( $wgUseTrackbacks && $out->isArticleRelated() ) {
208 $tpl->set( 'trackbackhtml', $out->getTitle()->trackbackRDF() );
209 } else {
210 $tpl->set( 'trackbackhtml', null );
211 }
212
213 $tpl->set( 'pageclass', $this->getPageClasses( $this->getTitle() ) );
214 $tpl->set( 'skinnameclass', ( 'skin-' . Sanitizer::escapeClass( $this->getSkinName() ) ) );
215 }
216 wfProfileOut( __METHOD__ . '-stuff-head' );
217
218 wfProfileIn( __METHOD__ . '-stuff2' );
219 $tpl->set( 'title', $out->getPageTitle() );
220 $tpl->set( 'pagetitle', $out->getHTMLTitle() );
221 $tpl->set( 'displaytitle', $out->mPageLinkTitle );
222
223 $tpl->set( 'titleprefixeddbkey', $this->getTitle()->getPrefixedDBKey() );
224 $tpl->set( 'titletext', $this->getTitle()->getText() );
225 $tpl->set( 'articleid', $this->getTitle()->getArticleId() );
226
227 $tpl->set( 'isarticle', $out->isArticle() );
228
229 $tpl->setRef( 'thispage', $this->thispage );
230 $subpagestr = $this->subPageSubtitle();
231 $tpl->set(
232 'subtitle', !empty( $subpagestr ) ?
233 '<span class="subpages">' . $subpagestr . '</span>' . $out->getSubtitle() :
234 $out->getSubtitle()
235 );
236 $undelete = $this->getUndeleteLink();
237 $tpl->set(
238 'undelete', !empty( $undelete ) ?
239 '<span class="subpages">' . $undelete . '</span>' :
240 ''
241 );
242
243 $tpl->set( 'catlinks', $this->getCategories() );
244 if( $out->isSyndicated() ) {
245 $feeds = array();
246 foreach( $out->getSyndicationLinks() as $format => $link ) {
247 $feeds[$format] = array(
248 'text' => wfMsg( "feed-$format" ),
249 'href' => $link
250 );
251 }
252 $tpl->setRef( 'feeds', $feeds );
253 } else {
254 $tpl->set( 'feeds', false );
255 }
256
257 $tpl->setRef( 'mimetype', $wgMimeType );
258 $tpl->setRef( 'jsmimetype', $wgJsMimeType );
259 $tpl->set( 'charset', 'UTF-8' );
260 $tpl->setRef( 'wgScript', $wgScript );
261 $tpl->setRef( 'skinname', $this->skinname );
262 $tpl->set( 'skinclass', get_class( $this ) );
263 $tpl->setRef( 'stylename', $this->stylename );
264 $tpl->set( 'printable', $out->isPrintable() );
265 $tpl->set( 'handheld', $wgRequest->getBool( 'handheld' ) );
266 $tpl->setRef( 'loggedin', $this->loggedin );
267 $tpl->set( 'notspecialpage', $this->getTitle()->getNamespace() != NS_SPECIAL );
268 /* XXX currently unused, might get useful later
269 $tpl->set( 'editable', ( $this->getTitle()->getNamespace() != NS_SPECIAL ) );
270 $tpl->set( 'exists', $this->getTitle()->getArticleID() != 0 );
271 $tpl->set( 'watch', $this->getTitle()->userIsWatching() ? 'unwatch' : 'watch' );
272 $tpl->set( 'protect', count( $this->getTitle()->isProtected() ) ? 'unprotect' : 'protect' );
273 $tpl->set( 'helppage', wfMsg( 'helppage' ) );
274 */
275 $tpl->set( 'searchaction', $this->escapeSearchLink() );
276 $tpl->set( 'searchtitle', SpecialPage::getTitleFor( 'Search' )->getPrefixedDBKey() );
277 $tpl->set( 'search', trim( $wgRequest->getVal( 'search' ) ) );
278 $tpl->setRef( 'stylepath', $wgStylePath );
279 $tpl->setRef( 'articlepath', $wgArticlePath );
280 $tpl->setRef( 'scriptpath', $wgScriptPath );
281 $tpl->setRef( 'serverurl', $wgServer );
282 $tpl->setRef( 'logopath', $wgLogo );
283
284 $contentlang = $wgContLang->getCode();
285 $contentdir = $wgContLang->getDir();
286 $userlang = $wgLang->getCode();
287 $userdir = $wgLang->getDir();
288
289 $tpl->set( 'lang', $userlang );
290 $tpl->set( 'dir', $userdir );
291 $tpl->set( 'rtl', $wgLang->isRTL() );
292
293 $tpl->set( 'capitalizeallnouns', $wgLang->capitalizeAllNouns() ? ' capitalize-all-nouns' : '' );
294 $tpl->set( 'showjumplinks', $wgUser->getOption( 'showjumplinks' ) );
295 $tpl->set( 'username', $wgUser->isAnon() ? null : $this->username );
296 $tpl->setRef( 'userpage', $this->userpage );
297 $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href'] );
298 $tpl->set( 'userlang', $userlang );
299
300 // Users can have their language set differently than the
301 // content of the wiki. For these users, tell the web browser
302 // that interface elements are in a different language.
303 $tpl->set( 'userlangattributes', '' );
304 $tpl->set( 'specialpageattributes', '' ); # obsolete
305
306 if ( $userlang !== $contentlang || $userdir !== $contentdir ) {
307 $attrs = " lang='$userlang' dir='$userdir'";
308 $tpl->set( 'userlangattributes', $attrs );
309 }
310
311 wfProfileOut( __METHOD__ . '-stuff2' );
312
313 wfProfileIn( __METHOD__ . '-stuff3' );
314 $tpl->set( 'newtalk', $this->getNewtalks() );
315 $tpl->setRef( 'skin', $this );
316 $tpl->set( 'logo', $this->logoText() );
317
318 $tpl->set( 'copyright', false );
319 $tpl->set( 'viewcount', false );
320 $tpl->set( 'lastmod', false );
321 $tpl->set( 'credits', false );
322 $tpl->set( 'numberofwatchingusers', false );
323 if ( $out->isArticle() && $this->getTitle()->exists() ) {
324 if ( $this->isRevisionCurrent() ) {
325 $article = new Article( $this->getTitle(), 0 );
326 if ( !$wgDisableCounters ) {
327 $viewcount = $wgLang->formatNum( $article->getCount() );
328 if ( $viewcount ) {
329 $tpl->set( 'viewcount', wfMsgExt( 'viewcount', array( 'parseinline' ), $viewcount ) );
330 }
331 }
332
333 if( $wgPageShowWatchingUsers ) {
334 $dbr = wfGetDB( DB_SLAVE );
335 $num = $dbr->selectField( 'watchlist', 'COUNT(*)',
336 array( 'wl_title' => $this->getTitle()->getDBkey(), 'wl_namespace' => $this->getTitle()->getNamespace() ),
337 __METHOD__
338 );
339 if( $num > 0 ) {
340 $tpl->set( 'numberofwatchingusers',
341 wfMsgExt( 'number_of_watching_users_pageview', array( 'parseinline' ),
342 $wgLang->formatNum( $num ) )
343 );
344 }
345 }
346
347 if ( $wgMaxCredits != 0 ) {
348 $tpl->set( 'credits', Action::factory( 'credits', $article )->getCredits( $wgMaxCredits, $wgShowCreditsIfMax ) );
349 } else {
350 $tpl->set( 'lastmod', $this->lastModified( $article ) );
351 }
352 }
353 $tpl->set( 'copyright', $this->getCopyright() );
354 }
355 wfProfileOut( __METHOD__ . '-stuff3' );
356
357 wfProfileIn( __METHOD__ . '-stuff4' );
358 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
359 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
360 $tpl->set( 'disclaimer', $this->disclaimerLink() );
361 $tpl->set( 'privacy', $this->privacyLink() );
362 $tpl->set( 'about', $this->aboutLink() );
363
364 $tpl->set( 'footerlinks', array(
365 'info' => array(
366 'lastmod',
367 'viewcount',
368 'numberofwatchingusers',
369 'credits',
370 'copyright',
371 ),
372 'places' => array(
373 'privacy',
374 'about',
375 'disclaimer',
376 ),
377 ) );
378
379 global $wgFooterIcons;
380 $tpl->set( 'footericons', $wgFooterIcons );
381 foreach ( $tpl->data['footericons'] as $footerIconsKey => &$footerIconsBlock ) {
382 if ( count( $footerIconsBlock ) > 0 ) {
383 foreach ( $footerIconsBlock as &$footerIcon ) {
384 if ( isset( $footerIcon['src'] ) ) {
385 if ( !isset( $footerIcon['width'] ) ) {
386 $footerIcon['width'] = 88;
387 }
388 if ( !isset( $footerIcon['height'] ) ) {
389 $footerIcon['height'] = 31;
390 }
391 }
392 }
393 } else {
394 unset( $tpl->data['footericons'][$footerIconsKey] );
395 }
396 }
397
398 if ( $wgDebugComments ) {
399 $tpl->setRef( 'debug', $out->mDebugtext );
400 } else {
401 $tpl->set( 'debug', '' );
402 }
403
404 $tpl->set( 'reporttime', wfReportTime() );
405 $tpl->set( 'sitenotice', $this->getSiteNotice() );
406 $tpl->set( 'bottomscripts', $this->bottomScripts() );
407 $tpl->set( 'printfooter', $this->printSource() );
408
409 # Add a <div class="mw-content-ltr/rtl"> around the body text
410 # not for special pages or file pages AND only when viewing AND if the page exists
411 # (or is in MW namespace, because that has default content)
412 if( !in_array( $this->getTitle()->getNamespace(), array( NS_SPECIAL, NS_FILE ) ) &&
413 in_array( $wgRequest->getVal( 'action', 'view' ), array( 'view', 'historysubmit' ) ) &&
414 ( $this->getTitle()->exists() || $this->getTitle()->getNamespace() == NS_MEDIAWIKI ) ) {
415 $pageLang = $this->getTitle()->getPageLanguage();
416 $realBodyAttribs = array( 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
417 'class' => 'mw-content-'.$pageLang->getDir() );
418 $out->mBodytext = Html::rawElement( 'div', $realBodyAttribs, $out->mBodytext );
419 }
420
421 $tpl->setRef( 'bodytext', $out->mBodytext );
422
423 # Language links
424 $language_urls = array();
425
426 if ( !$wgHideInterlanguageLinks ) {
427 foreach( $out->getLanguageLinks() as $l ) {
428 $tmp = explode( ':', $l, 2 );
429 $class = 'interwiki-' . $tmp[0];
430 unset( $tmp );
431 $nt = Title::newFromText( $l );
432 if ( $nt ) {
433 $language_urls[] = array(
434 'href' => $nt->getFullURL(),
435 'text' => ( $wgContLang->getLanguageName( $nt->getInterwiki() ) != '' ?
436 $wgContLang->getLanguageName( $nt->getInterwiki() ) : $l ),
437 'title' => $nt->getText(),
438 'class' => $class
439 );
440 }
441 }
442 }
443 if( count( $language_urls ) ) {
444 $tpl->setRef( 'language_urls', $language_urls );
445 } else {
446 $tpl->set( 'language_urls', false );
447 }
448 wfProfileOut( __METHOD__ . '-stuff4' );
449
450 wfProfileIn( __METHOD__ . '-stuff5' );
451 # Personal toolbar
452 $tpl->set( 'personal_urls', $this->buildPersonalUrls( $out ) );
453 $content_navigation = $this->buildContentNavigationUrls( $out );
454 $content_actions = $this->buildContentActionUrls( $content_navigation );
455 $tpl->setRef( 'content_navigation', $content_navigation );
456 $tpl->setRef( 'content_actions', $content_actions );
457
458 $tpl->set( 'sidebar', $this->buildSidebar() );
459 $tpl->set( 'nav_urls', $this->buildNavUrls( $out ) );
460
461 // Set the head scripts near the end, in case the above actions resulted in added scripts
462 if ( $this->useHeadElement ) {
463 $tpl->set( 'headelement', $out->headElement( $this ) );
464 } else {
465 $tpl->set( 'headscripts', $out->getScript() );
466 }
467
468 $tpl->set( 'debughtml', $this->generateDebugHTML() );
469
470 // original version by hansm
471 if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
472 wfDebug( __METHOD__ . ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
473 }
474
475 // allow extensions adding stuff after the page content.
476 // See Skin::afterContentHook() for further documentation.
477 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
478 wfProfileOut( __METHOD__ . '-stuff5' );
479
480 // execute template
481 wfProfileIn( __METHOD__ . '-execute' );
482 $res = $tpl->execute();
483 wfProfileOut( __METHOD__ . '-execute' );
484
485 // result may be an error
486 $this->printOrError( $res );
487 wfProfileOut( __METHOD__ );
488 }
489
490 /**
491 * Output the string, or print error message if it's
492 * an error object of the appropriate type.
493 * For the base class, assume strings all around.
494 *
495 * @param $str Mixed
496 * @private
497 */
498 function printOrError( $str ) {
499 echo $str;
500 }
501
502 /**
503 * Output a boolean indiciating if buildPersonalUrls should output separate
504 * login and create account links or output a combined link
505 * By default we simply return a global config setting that affects most skins
506 * This is setup as a method so that like with $wgLogo and getLogo() a skin
507 * can override this setting and always output one or the other if it has
508 * a reason it can't output one of the two modes.
509 */
510 function useCombinedLoginLink() {
511 global $wgUseCombinedLoginLink;
512 return $wgUseCombinedLoginLink;
513 }
514
515 /**
516 * build array of urls for personal toolbar
517 * @return array
518 */
519 protected function buildPersonalUrls( OutputPage $out ) {
520 global $wgRequest;
521
522 $title = $out->getTitle();
523 $pageurl = $title->getLocalURL();
524 wfProfileIn( __METHOD__ );
525
526 /* set up the default links for the personal toolbar */
527 $personal_urls = array();
528
529 $page = $wgRequest->getVal( 'returnto', $this->thispage );
530 $query = $wgRequest->getVal( 'returntoquery', $this->thisquery );
531 $a = array( 'returnto' => $page );
532 if( $query != '' ) {
533 $a['returntoquery'] = $query;
534 }
535 $returnto = wfArrayToCGI( $a );
536 if( $this->loggedin ) {
537 $personal_urls['userpage'] = array(
538 'text' => $this->username,
539 'href' => &$this->userpageUrlDetails['href'],
540 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
541 'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
542 );
543 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
544 $personal_urls['mytalk'] = array(
545 'text' => wfMsg( 'mytalk' ),
546 'href' => &$usertalkUrlDetails['href'],
547 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
548 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
549 );
550 $href = self::makeSpecialUrl( 'Preferences' );
551 $personal_urls['preferences'] = array(
552 'text' => wfMsg( 'mypreferences' ),
553 'href' => $href,
554 'active' => ( $href == $pageurl )
555 );
556 $href = self::makeSpecialUrl( 'Watchlist' );
557 $personal_urls['watchlist'] = array(
558 'text' => wfMsg( 'mywatchlist' ),
559 'href' => $href,
560 'active' => ( $href == $pageurl )
561 );
562
563 # We need to do an explicit check for Special:Contributions, as we
564 # have to match both the title, and the target (which could come
565 # from request values or be specified in "sub page" form. The plot
566 # thickens, because $wgTitle is altered for special pages, so doesn't
567 # contain the original alias-with-subpage.
568 $origTitle = Title::newFromText( $wgRequest->getText( 'title' ) );
569 if( $origTitle instanceof Title && $origTitle->getNamespace() == NS_SPECIAL ) {
570 list( $spName, $spPar ) = SpecialPageFactory::resolveAlias( $origTitle->getText() );
571 $active = $spName == 'Contributions'
572 && ( ( $spPar && $spPar == $this->username )
573 || $wgRequest->getText( 'target' ) == $this->username );
574 } else {
575 $active = false;
576 }
577
578 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
579 $personal_urls['mycontris'] = array(
580 'text' => wfMsg( 'mycontris' ),
581 'href' => $href,
582 'active' => $active
583 );
584 $personal_urls['logout'] = array(
585 'text' => wfMsg( 'userlogout' ),
586 'href' => self::makeSpecialUrl( 'Userlogout',
587 // userlogout link must always contain an & character, otherwise we might not be able
588 // to detect a buggy precaching proxy (bug 17790)
589 $title->isSpecial( 'Preferences' ) ? 'noreturnto' : $returnto
590 ),
591 'active' => false
592 );
593 } else {
594 global $wgUser;
595 $useCombinedLoginLink = $this->useCombinedLoginLink();
596 $loginlink = $wgUser->isAllowed( 'createaccount' ) && $useCombinedLoginLink
597 ? 'nav-login-createaccount'
598 : 'login';
599 $is_signup = $wgRequest->getText('type') == "signup";
600
601 # anonlogin & login are the same
602 $login_url = array(
603 'text' => wfMsg( $loginlink ),
604 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
605 'active' => $title->isSpecial( 'Userlogin' ) && ( $loginlink == "nav-login-createaccount" || !$is_signup )
606 );
607 if ( $wgUser->isAllowed( 'createaccount' ) && !$useCombinedLoginLink ) {
608 $createaccount_url = array(
609 'text' => wfMsg( 'createaccount' ),
610 'href' => self::makeSpecialUrl( 'Userlogin', "$returnto&type=signup" ),
611 'active' => $title->isSpecial( 'Userlogin' ) && $is_signup
612 );
613 }
614 global $wgServer, $wgSecureLogin;
615 if( substr( $wgServer, 0, 5 ) === 'http:' && $wgSecureLogin ) {
616 $title = SpecialPage::getTitleFor( 'Userlogin' );
617 $https_url = preg_replace( '/^http:/', 'https:', $title->getFullURL() );
618 $login_url['href'] = $https_url;
619 # @todo FIXME: Class depends on skin
620 $login_url['class'] = 'link-https';
621 if ( isset($createaccount_url) ) {
622 $https_url = preg_replace( '/^http:/', 'https:',
623 $title->getFullURL("type=signup") );
624 $createaccount_url['href'] = $https_url;
625 # @todo FIXME: Class depends on skin
626 $createaccount_url['class'] = 'link-https';
627 }
628 }
629
630
631 if( $this->showIPinHeader() ) {
632 $href = &$this->userpageUrlDetails['href'];
633 $personal_urls['anonuserpage'] = array(
634 'text' => $this->username,
635 'href' => $href,
636 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
637 'active' => ( $pageurl == $href )
638 );
639 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
640 $href = &$usertalkUrlDetails['href'];
641 $personal_urls['anontalk'] = array(
642 'text' => wfMsg( 'anontalk' ),
643 'href' => $href,
644 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
645 'active' => ( $pageurl == $href )
646 );
647 $personal_urls['anonlogin'] = $login_url;
648 } else {
649 $personal_urls['login'] = $login_url;
650 }
651 if ( isset($createaccount_url) ) {
652 $personal_urls['createaccount'] = $createaccount_url;
653 }
654 }
655
656 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$title ) );
657 wfProfileOut( __METHOD__ );
658 return $personal_urls;
659 }
660
661 /**
662 * TODO document
663 * @param $title Title
664 * @param $message String message key
665 * @param $selected Bool
666 * @param $query String
667 * @param $checkEdit Bool
668 * @return array
669 */
670 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
671 $classes = array();
672 if( $selected ) {
673 $classes[] = 'selected';
674 }
675 if( $checkEdit && !$title->isKnown() ) {
676 $classes[] = 'new';
677 $query = 'action=edit&redlink=1';
678 }
679
680 // wfMessageFallback will nicely accept $message as an array of fallbacks
681 // or just a single key
682 $msg = wfMessageFallback( $message );
683 if ( is_array($message) ) {
684 // for hook compatibility just keep the last message name
685 $message = end($message);
686 }
687 if ( $msg->exists() ) {
688 $text = $msg->text();
689 } else {
690 global $wgContLang;
691 $text = $wgContLang->getFormattedNsText(
692 MWNamespace::getSubject( $title->getNamespace() ) );
693 }
694
695 $result = array();
696 if( !wfRunHooks( 'SkinTemplateTabAction', array( &$this,
697 $title, $message, $selected, $checkEdit,
698 &$classes, &$query, &$text, &$result ) ) ) {
699 return $result;
700 }
701
702 return array(
703 'class' => implode( ' ', $classes ),
704 'text' => $text,
705 'href' => $title->getLocalUrl( $query ),
706 'primary' => true );
707 }
708
709 function makeTalkUrlDetails( $name, $urlaction = '' ) {
710 $title = Title::newFromText( $name );
711 if( !is_object( $title ) ) {
712 throw new MWException( __METHOD__ . " given invalid pagename $name" );
713 }
714 $title = $title->getTalkPage();
715 self::checkTitle( $title, $name );
716 return array(
717 'href' => $title->getLocalURL( $urlaction ),
718 'exists' => $title->getArticleID() != 0,
719 );
720 }
721
722 function makeArticleUrlDetails( $name, $urlaction = '' ) {
723 $title = Title::newFromText( $name );
724 $title= $title->getSubjectPage();
725 self::checkTitle( $title, $name );
726 return array(
727 'href' => $title->getLocalURL( $urlaction ),
728 'exists' => $title->getArticleID() != 0,
729 );
730 }
731
732 /**
733 * a structured array of links usually used for the tabs in a skin
734 *
735 * There are 4 standard sections
736 * namespaces: Used for namespace tabs like special, page, and talk namespaces
737 * views: Used for primary page views like read, edit, history
738 * actions: Used for most extra page actions like deletion, protection, etc...
739 * variants: Used to list the language variants for the page
740 *
741 * Each section's value is a key/value array of links for that section.
742 * The links themseves have these common keys:
743 * - class: The css classes to apply to the tab
744 * - text: The text to display on the tab
745 * - href: The href for the tab to point to
746 * - rel: An optional rel= for the tab's link
747 * - redundant: If true the tab will be dropped in skins using content_actions
748 * this is useful for tabs like "Read" which only have meaning in skins that
749 * take special meaning from the grouped structure of content_navigation
750 *
751 * Views also have an extra key which can be used:
752 * - primary: If this is not true skins like vector may try to hide the tab
753 * when the user has limited space in their browser window
754 *
755 * content_navigation using code also expects these ids to be present on the
756 * links, however these are usually automatically generated by SkinTemplate
757 * itself and are not necessary when using a hook. The only things these may
758 * matter to are people modifying content_navigation after it's initial creation:
759 * - id: A "preferred" id, most skins are best off outputting this preferred id for best compatibility
760 * - tooltiponly: This is set to true for some tabs in cases where the system
761 * believes that the accesskey should not be added to the tab.
762 *
763 * @return array
764 */
765 protected function buildContentNavigationUrls( OutputPage $out ) {
766 global $wgContLang, $wgLang, $wgUser, $wgRequest;
767 global $wgDisableLangConversion;
768
769 wfProfileIn( __METHOD__ );
770
771 $title = $this->getRelevantTitle(); // Display tabs for the relevant title rather than always the title itself
772 $onPage = $title->equals($this->getTitle());
773
774 $content_navigation = array(
775 'namespaces' => array(),
776 'views' => array(),
777 'actions' => array(),
778 'variants' => array()
779 );
780
781 // parameters
782 $action = $wgRequest->getVal( 'action', 'view' );
783 $section = $wgRequest->getVal( 'section' );
784
785 $userCanRead = $title->userCanRead();
786 $skname = $this->skinname;
787
788 $preventActiveTabs = false;
789 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this, &$preventActiveTabs ) );
790
791 // Checks if page is some kind of content
792 if( $title->getNamespace() != NS_SPECIAL ) {
793 // Gets page objects for the related namespaces
794 $subjectPage = $title->getSubjectPage();
795 $talkPage = $title->getTalkPage();
796
797 // Determines if this is a talk page
798 $isTalk = $title->isTalkPage();
799
800 // Generates XML IDs from namespace names
801 $subjectId = $title->getNamespaceKey( '' );
802
803 if ( $subjectId == 'main' ) {
804 $talkId = 'talk';
805 } else {
806 $talkId = "{$subjectId}_talk";
807 }
808
809 // Adds namespace links
810 $subjectMsg = array( "nstab-$subjectId" );
811 if ( $subjectPage->isMainPage() ) {
812 array_unshift($subjectMsg, 'mainpage-nstab');
813 }
814 $content_navigation['namespaces'][$subjectId] = $this->tabAction(
815 $subjectPage, $subjectMsg, !$isTalk && !$preventActiveTabs, '', $userCanRead
816 );
817 $content_navigation['namespaces'][$subjectId]['context'] = 'subject';
818 $content_navigation['namespaces'][$talkId] = $this->tabAction(
819 $talkPage, array( "nstab-$talkId", 'talk' ), $isTalk && !$preventActiveTabs, '', $userCanRead
820 );
821 $content_navigation['namespaces'][$talkId]['context'] = 'talk';
822
823 // Adds view view link
824 if ( $title->exists() && $userCanRead ) {
825 $content_navigation['views']['view'] = $this->tabAction(
826 $isTalk ? $talkPage : $subjectPage,
827 array( "$skname-view-view", 'view' ),
828 ( $onPage && ($action == 'view' || $action == 'purge' ) ), '', true
829 );
830 $content_navigation['views']['view']['redundant'] = true; // signal to hide this from simple content_actions
831 }
832
833 wfProfileIn( __METHOD__ . '-edit' );
834
835 // Checks if user can...
836 if (
837 // read and edit the current page
838 $userCanRead && $title->quickUserCan( 'edit' ) &&
839 (
840 // if it exists
841 $title->exists() ||
842 // or they can create one here
843 $title->quickUserCan( 'create' )
844 )
845 ) {
846 // Builds CSS class for talk page links
847 $isTalkClass = $isTalk ? ' istalk' : '';
848
849 // Determines if we're in edit mode
850 $selected = (
851 $onPage &&
852 ( $action == 'edit' || $action == 'submit' ) &&
853 ( $section != 'new' )
854 );
855 $msgKey = $title->exists() || ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDefaultMessageText() !== false ) ?
856 "edit" : "create";
857 $content_navigation['views']['edit'] = array(
858 'class' => ( $selected ? 'selected' : '' ) . $isTalkClass,
859 'text' => wfMessageFallback( "$skname-view-$msgKey", $msgKey )->text(),
860 'href' => $title->getLocalURL( $this->editUrlOptions() ),
861 'primary' => true, // don't collapse this in vector
862 );
863 // Checks if this is a current rev of talk page and we should show a new
864 // section link
865 if ( ( $isTalk && $this->isRevisionCurrent() ) || ( $out->showNewSectionLink() ) ) {
866 // Checks if we should ever show a new section link
867 if ( !$out->forceHideNewSectionLink() ) {
868 // Adds new section link
869 //$content_navigation['actions']['addsection']
870 $content_navigation['views']['addsection'] = array(
871 'class' => $section == 'new' ? 'selected' : false,
872 'text' => wfMessageFallback( "$skname-action-addsection", 'addsection' )->text(),
873 'href' => $title->getLocalURL( 'action=edit&section=new' )
874 );
875 }
876 }
877 // Checks if the page has some kind of viewable content
878 } elseif ( $title->hasSourceText() && $userCanRead ) {
879 // Adds view source view link
880 $content_navigation['views']['viewsource'] = array(
881 'class' => ( $onPage && $action == 'edit' ) ? 'selected' : false,
882 'text' => wfMessageFallback( "$skname-action-viewsource", 'viewsource' )->text(),
883 'href' => $title->getLocalURL( $this->editUrlOptions() ),
884 'primary' => true, // don't collapse this in vector
885 );
886 }
887 wfProfileOut( __METHOD__ . '-edit' );
888
889 wfProfileIn( __METHOD__ . '-live' );
890
891 // Checks if the page exists
892 if ( $title->exists() && $userCanRead ) {
893 // Adds history view link
894 $content_navigation['views']['history'] = array(
895 'class' => ( $onPage && $action == 'history' ) ? 'selected' : false,
896 'text' => wfMessageFallback( "$skname-view-history", 'history_short' )->text(),
897 'href' => $title->getLocalURL( 'action=history' ),
898 'rel' => 'archives',
899 );
900
901 if( $wgUser->isAllowed( 'delete' ) ) {
902 $content_navigation['actions']['delete'] = array(
903 'class' => ( $onPage && $action == 'delete' ) ? 'selected' : false,
904 'text' => wfMessageFallback( "$skname-action-delete", 'delete' )->text(),
905 'href' => $title->getLocalURL( 'action=delete' )
906 );
907 }
908 if ( $title->quickUserCan( 'move' ) ) {
909 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $title->getPrefixedDBkey() );
910 $content_navigation['actions']['move'] = array(
911 'class' => $this->getTitle()->isSpecial( 'Movepage' ) ? 'selected' : false,
912 'text' => wfMessageFallback( "$skname-action-move", 'move' )->text(),
913 'href' => $moveTitle->getLocalURL()
914 );
915 }
916
917 if ( $title->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
918 $mode = !$title->isProtected() ? 'protect' : 'unprotect';
919 $content_navigation['actions'][$mode] = array(
920 'class' => ( $onPage && $action == $mode ) ? 'selected' : false,
921 'text' => wfMessageFallback( "$skname-action-$mode", $mode )->text(),
922 'href' => $title->getLocalURL( "action=$mode" )
923 );
924 }
925 } else {
926 // article doesn't exist or is deleted
927 if ( $wgUser->isAllowed( 'deletedhistory' ) && !$wgUser->isBlocked() ) {
928 $includeSuppressed = $wgUser->isAllowed( 'suppressrevision' );
929 $n = $title->isDeleted( $includeSuppressed );
930 if( $n ) {
931 $undelTitle = SpecialPage::getTitleFor( 'Undelete' );
932 // If the user can't undelete but can view deleted history show them a "View .. deleted" tab instead
933 $msgKey = $wgUser->isAllowed( 'undelete' ) ? 'undelete' : 'viewdeleted';
934 $content_navigation['actions']['undelete'] = array(
935 'class' => $this->getTitle()->isSpecial( 'Undelete' ) ? 'selected' : false,
936 'text' => wfMessageFallback( "$skname-action-$msgKey", "{$msgKey}_short" )
937 ->params( $wgLang->formatNum( $n ) )->text(),
938 'href' => $undelTitle->getLocalURL( array( 'target' => $title->getPrefixedDBkey() ) )
939 );
940 }
941 }
942
943 if ( $title->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
944 $mode = !$title->getRestrictions( 'create' ) ? 'protect' : 'unprotect';
945 $content_navigation['actions'][$mode] = array(
946 'class' => ( $onPage && $action == $mode ) ? 'selected' : false,
947 'text' => wfMessageFallback( "$skname-action-$mode", $mode )->text(),
948 'href' => $title->getLocalURL( "action=$mode" )
949 );
950 }
951 }
952 wfProfileOut( __METHOD__ . '-live' );
953
954 // Checks if the user is logged in
955 if ( $this->loggedin ) {
956 /**
957 * The following actions use messages which, if made particular to
958 * the any specific skins, would break the Ajax code which makes this
959 * action happen entirely inline. Skin::makeGlobalVariablesScript
960 * defines a set of messages in a javascript object - and these
961 * messages are assumed to be global for all skins. Without making
962 * a change to that procedure these messages will have to remain as
963 * the global versions.
964 */
965 $mode = $title->userIsWatching() ? 'unwatch' : 'watch';
966 $token = WatchAction::getWatchToken( $title, $wgUser, $mode );
967 $content_navigation['actions'][$mode] = array(
968 'class' => $onPage && ( $action == 'watch' || $action == 'unwatch' ) ? 'selected' : false,
969 'text' => wfMsg( $mode ), // uses 'watch' or 'unwatch' message
970 'href' => $title->getLocalURL( array( 'action' => $mode, 'token' => $token ) )
971 );
972 }
973
974 wfRunHooks( 'SkinTemplateNavigation', array( &$this, &$content_navigation ) );
975 } else {
976 // If it's not content, it's got to be a special page
977 $content_navigation['namespaces']['special'] = array(
978 'class' => 'selected',
979 'text' => wfMsg( 'nstab-special' ),
980 'href' => $wgRequest->getRequestURL(), // @bug 2457, 2510
981 'context' => 'subject'
982 );
983
984 wfRunHooks( 'SkinTemplateNavigation::SpecialPage',
985 array( &$this, &$content_navigation ) );
986 }
987
988 // Gets list of language variants
989 $variants = $wgContLang->getVariants();
990 // Checks that language conversion is enabled and variants exist
991 if( !$wgDisableLangConversion && count( $variants ) > 1 ) {
992 // Gets preferred variant
993 $preferred = $wgContLang->getPreferredVariant();
994 // Loops over each variant
995 foreach( $variants as $code ) {
996 // Gets variant name from language code
997 $varname = $wgContLang->getVariantname( $code );
998 // Checks if the variant is marked as disabled
999 if( $varname == 'disable' ) {
1000 // Skips this variant
1001 continue;
1002 }
1003 // Appends variant link
1004 $content_navigation['variants'][] = array(
1005 'class' => ( $code == $preferred ) ? 'selected' : false,
1006 'text' => $varname,
1007 'href' => $title->getLocalURL( '', $code )
1008 );
1009 }
1010 }
1011
1012 // Equiv to SkinTemplateContentActions
1013 wfRunHooks( 'SkinTemplateNavigation::Universal', array( &$this, &$content_navigation ) );
1014
1015 // Setup xml ids and tooltip info
1016 foreach ( $content_navigation as $section => &$links ) {
1017 foreach ( $links as $key => &$link ) {
1018 $xmlID = $key;
1019 if ( isset( $link['context'] ) && $link['context'] == 'subject' ) {
1020 $xmlID = 'ca-nstab-' . $xmlID;
1021 } elseif ( isset( $link['context'] ) && $link['context'] == 'talk' ) {
1022 $xmlID = 'ca-talk';
1023 } elseif ( $section == "variants" ) {
1024 $xmlID = 'ca-varlang-' . $xmlID;
1025 } else {
1026 $xmlID = 'ca-' . $xmlID;
1027 }
1028 $link['id'] = $xmlID;
1029 }
1030 }
1031
1032 # We don't want to give the watch tab an accesskey if the
1033 # page is being edited, because that conflicts with the
1034 # accesskey on the watch checkbox. We also don't want to
1035 # give the edit tab an accesskey, because that's fairly su-
1036 # perfluous and conflicts with an accesskey (Ctrl-E) often
1037 # used for editing in Safari.
1038 if( in_array( $action, array( 'edit', 'submit' ) ) ) {
1039 if ( isset($content_navigation['views']['edit']) ) {
1040 $content_navigation['views']['edit']['tooltiponly'] = true;
1041 }
1042 if ( isset($content_navigation['actions']['watch']) ) {
1043 $content_navigation['actions']['watch']['tooltiponly'] = true;
1044 }
1045 if ( isset($content_navigation['actions']['unwatch']) ) {
1046 $content_navigation['actions']['unwatch']['tooltiponly'] = true;
1047 }
1048 }
1049
1050 wfProfileOut( __METHOD__ );
1051
1052 return $content_navigation;
1053 }
1054
1055 /**
1056 * an array of edit links by default used for the tabs
1057 * @return array
1058 * @private
1059 */
1060 function buildContentActionUrls( $content_navigation ) {
1061
1062 wfProfileIn( __METHOD__ );
1063
1064 // content_actions has been replaced with content_navigation for backwards
1065 // compatibility and also for skins that just want simple tabs content_actions
1066 // is now built by flattening the content_navigation arrays into one
1067
1068 $content_actions = array();
1069
1070 foreach ( $content_navigation as $links ) {
1071
1072 foreach ( $links as $key => $value ) {
1073
1074 if ( isset($value["redundant"]) && $value["redundant"] ) {
1075 // Redundant tabs are dropped from content_actions
1076 continue;
1077 }
1078
1079 // content_actions used to have ids built using the "ca-$key" pattern
1080 // so the xmlID based id is much closer to the actual $key that we want
1081 // for that reason we'll just strip out the ca- if present and use
1082 // the latter potion of the "id" as the $key
1083 if ( isset($value["id"]) && substr($value["id"], 0, 3) == "ca-" ) {
1084 $key = substr($value["id"], 3);
1085 }
1086
1087 if ( isset($content_actions[$key]) ) {
1088 wfDebug( __METHOD__ . ": Found a duplicate key for $key while flattening content_navigation into content_actions." );
1089 continue;
1090 }
1091
1092 $content_actions[$key] = $value;
1093
1094 }
1095
1096 }
1097
1098 wfProfileOut( __METHOD__ );
1099
1100 return $content_actions;
1101 }
1102
1103 /**
1104 * build array of common navigation links
1105 * @return array
1106 * @private
1107 */
1108 protected function buildNavUrls( OutputPage $out ) {
1109 global $wgUseTrackbacks, $wgUser, $wgRequest;
1110 global $wgUploadNavigationUrl;
1111
1112 wfProfileIn( __METHOD__ );
1113
1114 $nav_urls = array();
1115 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
1116 if( $wgUploadNavigationUrl ) {
1117 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
1118 } elseif( UploadBase::isEnabled() && UploadBase::isAllowed( $wgUser ) === true ) {
1119 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
1120 } else {
1121 $nav_urls['upload'] = false;
1122 }
1123 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
1124
1125 // default permalink to being off, will override it as required below.
1126 $nav_urls['permalink'] = false;
1127
1128 // A print stylesheet is attached to all pages, but nobody ever
1129 // figures that out. :) Add a link...
1130 if( $out->isArticle() ) {
1131 if ( !$out->isPrintable() ) {
1132 $nav_urls['print'] = array(
1133 'text' => wfMsg( 'printableversion' ),
1134 'href' => $this->getTitle()->getLocalURL(
1135 $wgRequest->appendQueryValue( 'printable', 'yes', true ) )
1136 );
1137 }
1138
1139 // Also add a "permalink" while we're at it
1140 $revid = $this->getRevisionId();
1141 if ( $revid ) {
1142 $nav_urls['permalink'] = array(
1143 'text' => wfMsg( 'permalink' ),
1144 'href' => $out->getTitle()->getLocalURL( "oldid=$revid" )
1145 );
1146 }
1147
1148 // Use the copy of revision ID in case this undocumented, shady hook tries to mess with internals
1149 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink',
1150 array( &$this, &$nav_urls, &$revid, &$revid ) );
1151 }
1152
1153 if( $this->getTitle()->getNamespace() != NS_SPECIAL ) {
1154 $wlhTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage );
1155 $nav_urls['whatlinkshere'] = array(
1156 'href' => $wlhTitle->getLocalUrl()
1157 );
1158 if( $this->getTitle()->getArticleId() ) {
1159 $rclTitle = SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage );
1160 $nav_urls['recentchangeslinked'] = array(
1161 'href' => $rclTitle->getLocalUrl()
1162 );
1163 } else {
1164 $nav_urls['recentchangeslinked'] = false;
1165 }
1166 if( $wgUseTrackbacks )
1167 $nav_urls['trackbacklink'] = array(
1168 'href' => $out->getTitle()->trackbackURL()
1169 );
1170 }
1171
1172 $user = $this->getRelevantUser();
1173 if ( $user ) {
1174 $id = $user->getID();
1175 $ip = $user->isAnon();
1176 $rootUser = $user->getName();
1177 } else {
1178 $id = 0;
1179 $ip = false;
1180 $rootUser = null;
1181 }
1182
1183 if( $id || $ip ) { # both anons and non-anons have contribs list
1184 $nav_urls['contributions'] = array(
1185 'href' => self::makeSpecialUrlSubpage( 'Contributions', $rootUser )
1186 );
1187
1188 if( $id ) {
1189 $logPage = SpecialPage::getTitleFor( 'Log' );
1190 $nav_urls['log'] = array(
1191 'href' => $logPage->getLocalUrl(
1192 array(
1193 'user' => $rootUser
1194 )
1195 )
1196 );
1197 } else {
1198 $nav_urls['log'] = false;
1199 }
1200
1201 if ( $wgUser->isAllowed( 'block' ) ) {
1202 $nav_urls['blockip'] = array(
1203 'href' => self::makeSpecialUrlSubpage( 'Block', $rootUser )
1204 );
1205 } else {
1206 $nav_urls['blockip'] = false;
1207 }
1208 } else {
1209 $nav_urls['contributions'] = false;
1210 $nav_urls['log'] = false;
1211 $nav_urls['blockip'] = false;
1212 }
1213 $nav_urls['emailuser'] = false;
1214 if( $this->showEmailUser( $id ) ) {
1215 $nav_urls['emailuser'] = array(
1216 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $rootUser )
1217 );
1218 }
1219 wfProfileOut( __METHOD__ );
1220 return $nav_urls;
1221 }
1222
1223 /**
1224 * Generate strings used for xml 'id' names
1225 * @return string
1226 * @private
1227 */
1228 function getNameSpaceKey() {
1229 return $this->getTitle()->getNamespaceKey();
1230 }
1231
1232 /**
1233 * @private
1234 * @todo FIXME: Why is this duplicated in/from OutputPage::getHeadScripts()??
1235 */
1236 function setupUserJs( $allowUserJs ) {
1237 global $wgRequest, $wgJsMimeType;
1238 wfProfileIn( __METHOD__ );
1239
1240 if( $allowUserJs && $this->loggedin ) {
1241 if( $this->getTitle()->isJsSubpage() and $this->getOutput()->userCanPreview() ) {
1242 # XXX: additional security check/prompt?
1243 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText( 'wpTextbox1' ) . ' /*]]>*/';
1244 } else {
1245 $this->userjs = self::makeUrl( $this->userpage . '/' . $this->skinname . '.js', 'action=raw&ctype=' . $wgJsMimeType );
1246 }
1247 }
1248 wfProfileOut( __METHOD__ );
1249 }
1250
1251 public function commonPrintStylesheet() {
1252 return false;
1253 }
1254 }
1255
1256 /**
1257 * Generic wrapper for template functions, with interface
1258 * compatible with what we use of PHPTAL 0.7.
1259 * @ingroup Skins
1260 */
1261 abstract class QuickTemplate {
1262 /**
1263 * Constructor
1264 */
1265 public function QuickTemplate() {
1266 $this->data = array();
1267 $this->translator = new MediaWiki_I18N();
1268 }
1269
1270 /**
1271 * Sets the value $value to $name
1272 * @param $name
1273 * @param $value
1274 */
1275 public function set( $name, $value ) {
1276 $this->data[$name] = $value;
1277 }
1278
1279 /**
1280 * @param $name
1281 * @param $value
1282 */
1283 public function setRef( $name, &$value ) {
1284 $this->data[$name] =& $value;
1285 }
1286
1287 /**
1288 * @param $t
1289 */
1290 public function setTranslator( &$t ) {
1291 $this->translator = &$t;
1292 }
1293
1294 /**
1295 * Main function, used by classes that subclass QuickTemplate
1296 * to show the actual HTML output
1297 */
1298 abstract public function execute();
1299
1300 /**
1301 * @private
1302 */
1303 function text( $str ) {
1304 echo htmlspecialchars( $this->data[$str] );
1305 }
1306
1307 /**
1308 * @private
1309 */
1310 function jstext( $str ) {
1311 echo Xml::escapeJsString( $this->data[$str] );
1312 }
1313
1314 /**
1315 * @private
1316 */
1317 function html( $str ) {
1318 echo $this->data[$str];
1319 }
1320
1321 /**
1322 * @private
1323 */
1324 function msg( $str ) {
1325 echo htmlspecialchars( $this->translator->translate( $str ) );
1326 }
1327
1328 /**
1329 * @private
1330 */
1331 function msgHtml( $str ) {
1332 echo $this->translator->translate( $str );
1333 }
1334
1335 /**
1336 * An ugly, ugly hack.
1337 * @private
1338 */
1339 function msgWiki( $str ) {
1340 global $wgOut;
1341
1342 $text = $this->translator->translate( $str );
1343 echo $wgOut->parse( $text );
1344 }
1345
1346 /**
1347 * @private
1348 */
1349 function haveData( $str ) {
1350 return isset( $this->data[$str] );
1351 }
1352
1353 /**
1354 * @private
1355 *
1356 * @return bool
1357 */
1358 function haveMsg( $str ) {
1359 $msg = $this->translator->translate( $str );
1360 return ( $msg != '-' ) && ( $msg != '' ); # ????
1361 }
1362
1363 /**
1364 * Get the Skin object related to this object
1365 *
1366 * @return Skin object
1367 */
1368 public function getSkin() {
1369 return $this->data['skin'];
1370 }
1371 }
1372
1373 /**
1374 * New base template for a skin's template extended from QuickTemplate
1375 * this class features helper methods that provide common ways of interacting
1376 * with the data stored in the QuickTemplate
1377 */
1378 abstract class BaseTemplate extends QuickTemplate {
1379
1380 /**
1381 * Create an array of common toolbox items from the data in the quicktemplate
1382 * stored by SkinTemplate.
1383 * The resulting array is built acording to a format intended to be passed
1384 * through makeListItem to generate the html.
1385 */
1386 function getToolbox() {
1387 wfProfileIn( __METHOD__ );
1388
1389 $toolbox = array();
1390 if ( $this->data['notspecialpage'] ) {
1391 $toolbox['whatlinkshere'] = $this->data['nav_urls']['whatlinkshere'];
1392 $toolbox['whatlinkshere']['id'] = 't-whatlinkshere';
1393 if ( $this->data['nav_urls']['recentchangeslinked'] ) {
1394 $toolbox['recentchangeslinked'] = $this->data['nav_urls']['recentchangeslinked'];
1395 $toolbox['recentchangeslinked']['msg'] = 'recentchangeslinked-toolbox';
1396 $toolbox['recentchangeslinked']['id'] = 't-recentchangeslinked';
1397 }
1398 }
1399 if( isset( $this->data['nav_urls']['trackbacklink'] ) && $this->data['nav_urls']['trackbacklink'] ) {
1400 $toolbox['trackbacklink'] = $this->data['nav_urls']['trackbacklink'];
1401 $toolbox['trackbacklink']['id'] = 't-trackbacklink';
1402 }
1403 if ( $this->data['feeds'] ) {
1404 $toolbox['feeds']['id'] = 'feedlinks';
1405 $toolbox['feeds']['links'] = array();
1406 foreach ( $this->data['feeds'] as $key => $feed ) {
1407 $toolbox['feeds']['links'][$key] = $feed;
1408 $toolbox['feeds']['links'][$key]['id'] = "feed-$key";
1409 $toolbox['feeds']['links'][$key]['rel'] = 'alternate';
1410 $toolbox['feeds']['links'][$key]['type'] = "application/{$key}+xml";
1411 $toolbox['feeds']['links'][$key]['class'] = 'feedlink';
1412 }
1413 }
1414 foreach ( array( 'contributions', 'log', 'blockip', 'emailuser', 'upload', 'specialpages' ) as $special ) {
1415 if ( $this->data['nav_urls'][$special] ) {
1416 $toolbox[$special] = $this->data['nav_urls'][$special];
1417 $toolbox[$special]['id'] = "t-$special";
1418 }
1419 }
1420 if ( !empty( $this->data['nav_urls']['print']['href'] ) ) {
1421 $toolbox['print'] = $this->data['nav_urls']['print'];
1422 $toolbox['print']['rel'] = 'alternate';
1423 $toolbox['print']['msg'] = 'printableversion';
1424 }
1425 if( $this->data['nav_urls']['permalink'] ) {
1426 $toolbox['permalink'] = $this->data['nav_urls']['permalink'];
1427 if( $toolbox['permalink']['href'] === '' ) {
1428 unset( $toolbox['permalink']['href'] );
1429 $toolbox['ispermalink']['tooltiponly'] = true;
1430 $toolbox['ispermalink']['id'] = 't-ispermalink';
1431 $toolbox['ispermalink']['msg'] = 'permalink';
1432 } else {
1433 $toolbox['permalink']['id'] = 't-permalink';
1434 }
1435 }
1436 wfRunHooks( 'BaseTemplateToolbox', array( &$this, &$toolbox ) );
1437 wfProfileOut( __METHOD__ );
1438 return $toolbox;
1439 }
1440
1441 /**
1442 * Create an array of personal tools items from the data in the quicktemplate
1443 * stored by SkinTemplate.
1444 * The resulting array is built acording to a format intended to be passed
1445 * through makeListItem to generate the html.
1446 * This is in reality the same list as already stored in personal_urls
1447 * however it is reformatted so that you can just pass the individual items
1448 * to makeListItem instead of hardcoding the element creation boilerplate.
1449 */
1450 function getPersonalTools() {
1451 $personal_tools = array();
1452 foreach( $this->data['personal_urls'] as $key => $ptool ) {
1453 # The class on a personal_urls item is meant to go on the <a> instead
1454 # of the <li> so we have to use a single item "links" array instead
1455 # of using most of the personal_url's keys directly
1456 $personal_tools[$key] = array();
1457 $personal_tools[$key]["links"][] = array();
1458 $personal_tools[$key]["links"][0]["single-id"] = $personal_tools[$key]["id"] = "pt-$key";
1459 if ( isset($ptool["active"]) ) {
1460 $personal_tools[$key]["active"] = $ptool["active"];
1461 }
1462 foreach ( array("href", "class", "text") as $k ) {
1463 if ( isset($ptool[$k]) )
1464 $personal_tools[$key]["links"][0][$k] = $ptool[$k];
1465 }
1466 }
1467 return $personal_tools;
1468 }
1469
1470 /**
1471 * Makes a link, usually used by makeListItem to generate a link for an item
1472 * in a list used in navigation lists, portlets, portals, sidebars, etc...
1473 *
1474 * $key is a string, usually a key from the list you are generating this link from
1475 * $item is an array containing some of a specific set of keys.
1476 * The text of the link will be generated either from the contents of the "text"
1477 * key in the $item array, if a "msg" key is present a message by that name will
1478 * be used, and if neither of those are set the $key will be used as a message name.
1479 * If a "href" key is not present makeLink will just output htmlescaped text.
1480 * The href, id, class, rel, and type keys are used as attributes for the link if present.
1481 * If an "id" or "single-id" (if you don't want the actual id to be output on the link)
1482 * is present it will be used to generate a tooltip and accesskey for the link.
1483 * If you don't want an accesskey, set $item['tooltiponly'] = true;
1484 */
1485 function makeLink( $key, $item ) {
1486 if ( isset( $item['text'] ) ) {
1487 $text = $item['text'];
1488 } else {
1489 $text = $this->translator->translate( isset( $item['msg'] ) ? $item['msg'] : $key );
1490 }
1491
1492 if ( !isset( $item['href'] ) ) {
1493 return htmlspecialchars( $text );
1494 }
1495
1496 $attrs = array();
1497 foreach ( array( 'href', 'id', 'class', 'rel', 'type', 'target') as $attr ) {
1498 if ( isset( $item[$attr] ) ) {
1499 $attrs[$attr] = $item[$attr];
1500 }
1501 }
1502
1503 if ( isset( $item['id'] ) ) {
1504 $item['single-id'] = $item['id'];
1505 }
1506 if ( isset( $item['single-id'] ) ) {
1507 if ( isset( $item['tooltiponly'] ) && $item['tooltiponly'] ) {
1508 $attrs['title'] = $this->skin->titleAttrib( $item['single-id'] );
1509 if ( $attrs['title'] === false ) {
1510 unset( $attrs['title'] );
1511 }
1512 } else {
1513 $attrs = array_merge(
1514 $attrs,
1515 Linker::tooltipAndAccesskeyAttribs( $item['single-id'] )
1516 );
1517 }
1518 }
1519
1520 return Html::element( 'a', $attrs, $text );
1521 }
1522
1523 /**
1524 * Generates a list item for a navigation, portlet, portal, sidebar... etc list
1525 * $key is a string, usually a key from the list you are generating this link from
1526 * $item is an array of list item data containing some of a specific set of keys.
1527 * The "id" and "class" keys will be used as attributes for the list item,
1528 * if "active" contains a value of true a "active" class will also be appended to class.
1529 * If you want something other than a <li> you can pass a tag name such as
1530 * "tag" => "span" in the $options array to change the tag used.
1531 * link/content data for the list item may come in one of two forms
1532 * A "links" key may be used, in which case it should contain an array with
1533 * a list of links to include inside the list item, see makeLink for the format
1534 * of individual links array items.
1535 * Otherwise the relevant keys from the list item $item array will be passed
1536 * to makeLink instead. Note however that "id" and "class" are used by the
1537 * list item directly so they will not be passed to makeLink
1538 * (however the link will still support a tooltip and accesskey from it)
1539 * If you need an id or class on a single link you should include a "links"
1540 * array with just one link item inside of it.
1541 */
1542 function makeListItem( $key, $item, $options = array() ) {
1543 if ( isset( $item['links'] ) ) {
1544 $html = '';
1545 foreach ( $item['links'] as $linkKey => $link ) {
1546 $html .= $this->makeLink( $linkKey, $link );
1547 }
1548 } else {
1549 $link = array();
1550 foreach ( array( 'text', 'msg', 'href', 'rel', 'type', 'tooltiponly', 'target' ) as $k ) {
1551 if ( isset( $item[$k] ) ) {
1552 $link[$k] = $item[$k];
1553 }
1554 }
1555 if ( isset( $item['id'] ) ) {
1556 // The id goes on the <li> not on the <a> for single links
1557 // but makeSidebarLink still needs to know what id to use when
1558 // generating tooltips and accesskeys.
1559 $link['single-id'] = $item['id'];
1560 }
1561 $html = $this->makeLink( $key, $link );
1562 }
1563
1564 $attrs = array();
1565 foreach ( array( 'id', 'class' ) as $attr ) {
1566 if ( isset( $item[$attr] ) ) {
1567 $attrs[$attr] = $item[$attr];
1568 }
1569 }
1570 if ( isset( $item['active'] ) && $item['active'] ) {
1571 if ( !isset( $attrs['class'] ) ) {
1572 $attrs['class'] = '';
1573 }
1574 $attrs['class'] .= ' active';
1575 $attrs['class'] = trim( $attrs['class'] );
1576 }
1577 return Html::rawElement( isset( $options['tag'] ) ? $options['tag'] : 'li', $attrs, $html );
1578 }
1579
1580 function makeSearchInput( $attrs = array() ) {
1581 $realAttrs = array(
1582 'type' => 'search',
1583 'name' => 'search',
1584 'value' => isset( $this->data['search'] ) ? $this->data['search'] : '',
1585 );
1586 $realAttrs = array_merge( $realAttrs, Linker::tooltipAndAccesskeyAttribs( 'search' ), $attrs );
1587 return Html::element( 'input', $realAttrs );
1588 }
1589
1590 function makeSearchButton( $mode, $attrs = array() ) {
1591 switch( $mode ) {
1592 case 'go':
1593 case 'fulltext':
1594 $realAttrs = array(
1595 'type' => 'submit',
1596 'name' => $mode,
1597 'value' => $this->translator->translate(
1598 $mode == 'go' ? 'searcharticle' : 'searchbutton' ),
1599 );
1600 $realAttrs = array_merge(
1601 $realAttrs,
1602 Linker::tooltipAndAccesskeyAttribs( "search-$mode" ),
1603 $attrs
1604 );
1605 return Html::element( 'input', $realAttrs );
1606 case 'image':
1607 $buttonAttrs = array(
1608 'type' => 'submit',
1609 'name' => 'button',
1610 );
1611 $buttonAttrs = array_merge(
1612 $buttonAttrs,
1613 Linker::tooltipAndAccesskeyAttribs( 'search-fulltext' ),
1614 $attrs
1615 );
1616 unset( $buttonAttrs['src'] );
1617 unset( $buttonAttrs['alt'] );
1618 $imgAttrs = array(
1619 'src' => $attrs['src'],
1620 'alt' => isset( $attrs['alt'] )
1621 ? $attrs['alt']
1622 : $this->translator->translate( 'searchbutton' ),
1623 );
1624 return Html::rawElement( 'button', $buttonAttrs, Html::element( 'img', $imgAttrs ) );
1625 default:
1626 throw new MWException( 'Unknown mode passed to BaseTemplate::makeSearchButton' );
1627 }
1628 }
1629
1630 /**
1631 * Returns an array of footerlinks trimmed down to only those footer links that
1632 * are valid.
1633 * If you pass "flat" as an option then the returned array will be a flat array
1634 * of footer icons instead of a key/value array of footerlinks arrays broken
1635 * up into categories.
1636 */
1637 function getFooterLinks( $option = null ) {
1638 $footerlinks = $this->data['footerlinks'];
1639
1640 // Reduce footer links down to only those which are being used
1641 $validFooterLinks = array();
1642 foreach( $footerlinks as $category => $links ) {
1643 $validFooterLinks[$category] = array();
1644 foreach( $links as $link ) {
1645 if( isset( $this->data[$link] ) && $this->data[$link] ) {
1646 $validFooterLinks[$category][] = $link;
1647 }
1648 }
1649 if ( count( $validFooterLinks[$category] ) <= 0 ) {
1650 unset( $validFooterLinks[$category] );
1651 }
1652 }
1653
1654 if ( $option == 'flat' ) {
1655 // fold footerlinks into a single array using a bit of trickery
1656 $validFooterLinks = call_user_func_array(
1657 'array_merge',
1658 array_values( $validFooterLinks )
1659 );
1660 }
1661
1662 return $validFooterLinks;
1663 }
1664
1665 /**
1666 * Returns an array of footer icons filtered down by options relevant to how
1667 * the skin wishes to display them.
1668 * If you pass "icononly" as the option all footer icons which do not have an
1669 * image icon set will be filtered out.
1670 * If you pass "nocopyright" then MediaWiki's copyright icon will not be included
1671 * in the list of footer icons. This is mostly useful for skins which only
1672 * display the text from footericons instead of the images and don't want a
1673 * duplicate copyright statement because footerlinks already rendered one.
1674 */
1675 function getFooterIcons( $option = null ) {
1676 // Generate additional footer icons
1677 $footericons = $this->data['footericons'];
1678
1679 if ( $option == 'icononly' ) {
1680 // Unset any icons which don't have an image
1681 foreach ( $footericons as &$footerIconsBlock ) {
1682 foreach ( $footerIconsBlock as $footerIconKey => $footerIcon ) {
1683 if ( !is_string( $footerIcon ) && !isset( $footerIcon['src'] ) ) {
1684 unset( $footerIconsBlock[$footerIconKey] );
1685 }
1686 }
1687 }
1688 // Redo removal of any empty blocks
1689 foreach ( $footericons as $footerIconsKey => &$footerIconsBlock ) {
1690 if ( count( $footerIconsBlock ) <= 0 ) {
1691 unset( $footericons[$footerIconsKey] );
1692 }
1693 }
1694 } elseif ( $option == 'nocopyright' ) {
1695 unset( $footericons['copyright']['copyright'] );
1696 if ( count( $footericons['copyright'] ) <= 0 ) {
1697 unset( $footericons['copyright'] );
1698 }
1699 }
1700
1701 return $footericons;
1702 }
1703
1704 /**
1705 * Output the basic end-page trail including bottomscripts, reporttime, and
1706 * debug stuff. This should be called right before outputting the closing
1707 * body and html tags.
1708 */
1709 function printTrail() { ?>
1710 <?php $this->html('bottomscripts'); /* JS call to runBodyOnloadHook */ ?>
1711 <?php $this->html('reporttime') ?>
1712 <?php if ( $this->data['debug'] ): ?>
1713 <!-- Debug output:
1714 <?php $this->text( 'debug' ); ?>
1715
1716 -->
1717 <?php endif;
1718 }
1719
1720 }
1721