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