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