Make LTR wgLang do the right thing on RTL wgContLang wikis. See bug 6100 and dupes...
[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;
138
139 wfProfileIn( __METHOD__ );
140
141 $oldid = $wgRequest->getVal( 'oldid' );
142 $diff = $wgRequest->getVal( 'diff' );
143 $action = $wgRequest->getVal( 'action', 'view' );
144
145 wfProfileIn( __METHOD__ . '-init' );
146 $this->initPage( $out );
147
148 $this->setMembers();
149 $tpl = $this->setupTemplate( $this->template, 'skins' );
150
151 #if ( $wgUseDatabaseMessages ) { // uncomment this to fall back to GetText
152 $tpl->setTranslator( new MediaWiki_I18N() );
153 #}
154 wfProfileOut( __METHOD__ . '-init' );
155
156 wfProfileIn( __METHOD__ . '-stuff' );
157 $this->thispage = $this->mTitle->getPrefixedDBkey();
158 $this->thisurl = $this->mTitle->getPrefixedURL();
159 $query = array();
160 if ( !$wgRequest->wasPosted() ) {
161 $query = $wgRequest->getValues();
162 unset( $query['title'] );
163 unset( $query['returnto'] );
164 unset( $query['returntoquery'] );
165 }
166 $this->thisquery = wfUrlencode( wfArrayToCGI( $query ) );
167 $this->loggedin = $wgUser->isLoggedIn();
168 $this->iscontent = ( $this->mTitle->getNamespace() != NS_SPECIAL );
169 $this->iseditable = ( $this->iscontent and !( $action == 'edit' or $action == 'submit' ) );
170 $this->username = $wgUser->getName();
171
172 if ( $wgUser->isLoggedIn() || $this->showIPinHeader() ) {
173 $this->userpageUrlDetails = self::makeUrlDetails( $this->userpage );
174 } else {
175 # This won't be used in the standard skins, but we define it to preserve the interface
176 # To save time, we check for existence
177 $this->userpageUrlDetails = self::makeKnownUrlDetails( $this->userpage );
178 }
179
180 $this->titletxt = $this->mTitle->getPrefixedText();
181 wfProfileOut( __METHOD__ . '-stuff' );
182
183 wfProfileIn( __METHOD__ . '-stuff-head' );
184 if ( $this->useHeadElement ) {
185 $pagecss = $this->setupPageCss();
186 if( $pagecss )
187 $out->addInlineStyle( $pagecss );
188 } else {
189 $this->setupUserCss( $out );
190
191 $tpl->set( 'pagecss', $this->setupPageCss() );
192 $tpl->setRef( 'usercss', $this->usercss );
193
194 $this->userjs = $this->userjsprev = false;
195 $this->setupUserJs( $out->isUserJsAllowed() );
196 $tpl->setRef( 'userjs', $this->userjs );
197 $tpl->setRef( 'userjsprev', $this->userjsprev );
198
199 if( $wgUseSiteJs ) {
200 $jsCache = $this->loggedin ? '&smaxage=0' : '';
201 $tpl->set( 'jsvarurl',
202 self::makeUrl( '-',
203 "action=raw$jsCache&gen=js&useskin=" .
204 urlencode( $this->getSkinName() ) ) );
205 } else {
206 $tpl->set( 'jsvarurl', false );
207 }
208
209 $tpl->setRef( 'xhtmldefaultnamespace', $wgXhtmlDefaultNamespace );
210 $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
211 $tpl->set( 'html5version', $wgHtml5Version );
212 $tpl->set( 'headlinks', $out->getHeadLinks() );
213 $tpl->set( 'csslinks', $out->buildCssLinks() );
214
215 if( $wgUseTrackbacks && $out->isArticleRelated() ) {
216 $tpl->set( 'trackbackhtml', $out->getTitle()->trackbackRDF() );
217 } else {
218 $tpl->set( 'trackbackhtml', null );
219 }
220 }
221 wfProfileOut( __METHOD__ . '-stuff-head' );
222
223 wfProfileIn( __METHOD__ . '-stuff2' );
224 $tpl->set( 'title', $out->getPageTitle() );
225 $tpl->set( 'pagetitle', $out->getHTMLTitle() );
226 $tpl->set( 'displaytitle', $out->mPageLinkTitle );
227 $tpl->set( 'pageclass', $this->getPageClasses( $this->mTitle ) );
228 $tpl->set( 'skinnameclass', ( 'skin-' . Sanitizer::escapeClass( $this->getSkinName() ) ) );
229
230 $nsname = MWNamespace::exists( $this->mTitle->getNamespace() ) ?
231 MWNamespace::getCanonicalName( $this->mTitle->getNamespace() ) :
232 $this->mTitle->getNsText();
233
234 $tpl->set( 'nscanonical', $nsname );
235 $tpl->set( 'nsnumber', $this->mTitle->getNamespace() );
236 $tpl->set( 'titleprefixeddbkey', $this->mTitle->getPrefixedDBKey() );
237 $tpl->set( 'titletext', $this->mTitle->getText() );
238 $tpl->set( 'articleid', $this->mTitle->getArticleId() );
239 $tpl->set( 'currevisionid', isset( $wgArticle ) ? $wgArticle->getLatest() : 0 );
240
241 $tpl->set( 'isarticle', $out->isArticle() );
242
243 $tpl->setRef( 'thispage', $this->thispage );
244 $subpagestr = $this->subPageSubtitle();
245 $tpl->set(
246 'subtitle', !empty( $subpagestr ) ?
247 '<span class="subpages">'.$subpagestr.'</span>'.$out->getSubtitle() :
248 $out->getSubtitle()
249 );
250 $undelete = $this->getUndeleteLink();
251 $tpl->set(
252 'undelete', !empty( $undelete ) ?
253 '<span class="subpages">'.$undelete.'</span>' :
254 ''
255 );
256
257 $tpl->set( 'catlinks', $this->getCategories() );
258 if( $out->isSyndicated() ) {
259 $feeds = array();
260 foreach( $out->getSyndicationLinks() as $format => $link ) {
261 $feeds[$format] = array(
262 'text' => wfMsg( "feed-$format" ),
263 'href' => $link
264 );
265 }
266 $tpl->setRef( 'feeds', $feeds );
267 } else {
268 $tpl->set( 'feeds', false );
269 }
270
271 $tpl->setRef( 'mimetype', $wgMimeType );
272 $tpl->setRef( 'jsmimetype', $wgJsMimeType );
273 $tpl->setRef( 'charset', $wgOutputEncoding );
274 $tpl->setRef( 'wgScript', $wgScript );
275 $tpl->setRef( 'skinname', $this->skinname );
276 $tpl->set( 'skinclass', get_class( $this ) );
277 $tpl->setRef( 'stylename', $this->stylename );
278 $tpl->set( 'printable', $out->isPrintable() );
279 $tpl->set( 'handheld', $wgRequest->getBool( 'handheld' ) );
280 $tpl->setRef( 'loggedin', $this->loggedin );
281 $tpl->set( 'notspecialpage', $this->mTitle->getNamespace() != NS_SPECIAL );
282 /* XXX currently unused, might get useful later
283 $tpl->set( "editable", ($this->mTitle->getNamespace() != NS_SPECIAL ) );
284 $tpl->set( "exists", $this->mTitle->getArticleID() != 0 );
285 $tpl->set( "watch", $this->mTitle->userIsWatching() ? "unwatch" : "watch" );
286 $tpl->set( "protect", count($this->mTitle->isProtected()) ? "unprotect" : "protect" );
287 $tpl->set( "helppage", wfMsg('helppage'));
288 */
289 $tpl->set( 'searchaction', $this->escapeSearchLink() );
290 $tpl->set( 'searchtitle', SpecialPage::getTitleFor( 'Search' )->getPrefixedDBKey() );
291 $tpl->set( 'search', trim( $wgRequest->getVal( 'search' ) ) );
292 $tpl->setRef( 'stylepath', $wgStylePath );
293 $tpl->setRef( 'articlepath', $wgArticlePath );
294 $tpl->setRef( 'scriptpath', $wgScriptPath );
295 $tpl->setRef( 'serverurl', $wgServer );
296 $tpl->setRef( 'logopath', $wgLogo );
297 $tpl->setRef( 'lang', $wgContLanguageCode );
298 $tpl->set( 'dir', $wgContLang->getDir() );
299 $tpl->set( 'rtl', $wgContLang->isRTL() );
300 $tpl->set( 'capitalizeallnouns', $wgLang->capitalizeAllNouns() ? ' capitalize-all-nouns' : '' );
301 $tpl->set( 'langname', $wgContLang->getLanguageName( $wgContLanguageCode ) );
302 $tpl->set( 'showjumplinks', $wgUser->getOption( 'showjumplinks' ) );
303 $tpl->set( 'username', $wgUser->isAnon() ? null : $this->username );
304 $tpl->setRef( 'userpage', $this->userpage );
305 $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href'] );
306 $tpl->set( 'userlang', $wgLang->getCode() );
307 $tpl->set( 'userlangattributes', 'lang="' . $wgLang->getCode() . '" xml:lang="' . $wgLang->getCode() . '" dir="' . $wgLang->getDir() . '"');
308 if($this->mTitle->isSpecialPage()) {
309 $tpl->set( 'specialpageattributes', 'lang="' . $wgLang->getCode() . '" xml:lang="' . $wgLang->getCode() . '" dir="' . $wgLang->getDir() . '"');
310 }
311
312 $newtalks = $wgUser->getNewMessageLinks();
313
314 if( count( $newtalks ) == 1 && $newtalks[0]['wiki'] === wfWikiID() ) {
315 $usertitle = $this->mUser->getUserPage();
316 $usertalktitle = $usertitle->getTalkPage();
317
318 if( !$usertalktitle->equals( $this->mTitle ) ) {
319 $newmessageslink = $this->link(
320 $usertalktitle,
321 wfMsgHtml( 'newmessageslink' ),
322 array(),
323 array( 'redirect' => 'no' ),
324 array( 'known', 'noclasses' )
325 );
326
327 $newmessagesdifflink = $this->link(
328 $usertalktitle,
329 wfMsgHtml( 'newmessagesdifflink' ),
330 array(),
331 array( 'diff' => 'cur' ),
332 array( 'known', 'noclasses' )
333 );
334
335 $ntl = wfMsg(
336 'youhavenewmessages',
337 $newmessageslink,
338 $newmessagesdifflink
339 );
340 # Disable Cache
341 $out->setSquidMaxage( 0 );
342 }
343 } else if( count( $newtalks ) ) {
344 // _>" " for BC <= 1.16
345 $sep = str_replace( '_', ' ', wfMsgHtml( 'newtalkseparator' ) );
346 $msgs = array();
347 foreach( $newtalks as $newtalk ) {
348 $msgs[] = Xml::element('a',
349 array( 'href' => $newtalk['link'] ), $newtalk['wiki'] );
350 }
351 $parts = implode( $sep, $msgs );
352 $ntl = wfMsgHtml( 'youhavenewmessagesmulti', $parts );
353 $out->setSquidMaxage( 0 );
354 } else {
355 $ntl = '';
356 }
357 wfProfileOut( __METHOD__ . '-stuff2' );
358
359 wfProfileIn( __METHOD__ . '-stuff3' );
360 $tpl->setRef( 'newtalk', $ntl );
361 $tpl->setRef( 'skin', $this );
362 $tpl->set( 'logo', $this->logoText() );
363 if ( $out->isArticle() and ( !isset( $oldid ) or isset( $diff ) ) and
364 $wgArticle and 0 != $wgArticle->getID() ){
365 if ( !$wgDisableCounters ) {
366 $viewcount = $wgLang->formatNum( $wgArticle->getCount() );
367 if ( $viewcount ) {
368 $tpl->set( 'viewcount', wfMsgExt( 'viewcount', array( 'parseinline' ), $viewcount ) );
369 } else {
370 $tpl->set( 'viewcount', false );
371 }
372 } else {
373 $tpl->set( 'viewcount', false );
374 }
375
376 if( $wgPageShowWatchingUsers ) {
377 $dbr = wfGetDB( DB_SLAVE );
378 $watchlist = $dbr->tableName( 'watchlist' );
379 $res = $dbr->select( 'watchlist',
380 array( 'COUNT(*) AS n' ),
381 array( 'wl_title' => $dbr->strencode( $this->mTitle->getDBkey() ), 'wl_namespace' => $this->mTitle->getNamespace() ),
382 __METHOD__
383 );
384 $x = $dbr->fetchObject( $res );
385 $numberofwatchingusers = $x->n;
386 if( $numberofwatchingusers > 0 ) {
387 $tpl->set( 'numberofwatchingusers',
388 wfMsgExt( 'number_of_watching_users_pageview', array( 'parseinline' ),
389 $wgLang->formatNum( $numberofwatchingusers ) )
390 );
391 } else {
392 $tpl->set( 'numberofwatchingusers', false );
393 }
394 } else {
395 $tpl->set( 'numberofwatchingusers', false );
396 }
397
398 $tpl->set( 'copyright', $this->getCopyright() );
399
400 $this->credits = false;
401
402 if( $wgMaxCredits != 0 ){
403 $this->credits = Credits::getCredits( $wgArticle, $wgMaxCredits, $wgShowCreditsIfMax );
404 } else {
405 $tpl->set( 'lastmod', $this->lastModified() );
406 }
407
408 $tpl->setRef( 'credits', $this->credits );
409
410 } elseif ( isset( $oldid ) && !isset( $diff ) ) {
411 $tpl->set( 'copyright', $this->getCopyright() );
412 $tpl->set( 'viewcount', false );
413 $tpl->set( 'lastmod', false );
414 $tpl->set( 'credits', false );
415 $tpl->set( 'numberofwatchingusers', false );
416 } else {
417 $tpl->set( 'copyright', false );
418 $tpl->set( 'viewcount', false );
419 $tpl->set( 'lastmod', false );
420 $tpl->set( 'credits', false );
421 $tpl->set( 'numberofwatchingusers', false );
422 }
423 wfProfileOut( __METHOD__ . '-stuff3' );
424
425 wfProfileIn( __METHOD__ . '-stuff4' );
426 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
427 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
428 $tpl->set( 'disclaimer', $this->disclaimerLink() );
429 $tpl->set( 'privacy', $this->privacyLink() );
430 $tpl->set( 'about', $this->aboutLink() );
431
432 if ( $wgDebugComments ) {
433 $tpl->setRef( 'debug', $out->mDebugtext );
434 } else {
435 $tpl->set( 'debug', '' );
436 }
437
438 $tpl->set( 'reporttime', wfReportTime() );
439 $tpl->set( 'sitenotice', wfGetSiteNotice() );
440 $tpl->set( 'bottomscripts', $this->bottomScripts() );
441
442 $printfooter = "<div class=\"printfooter\">\n" . $this->printSource() . "</div>\n";
443 $out->mBodytext .= $printfooter . $this->generateDebugHTML();
444 $tpl->setRef( 'bodytext', $out->mBodytext );
445
446 # Language links
447 $language_urls = array();
448
449 if ( !$wgHideInterlanguageLinks ) {
450 foreach( $out->getLanguageLinks() as $l ) {
451 $tmp = explode( ':', $l, 2 );
452 $class = 'interwiki-' . $tmp[0];
453 unset( $tmp );
454 $nt = Title::newFromText( $l );
455 if ( $nt ) {
456 $language_urls[] = array(
457 'href' => $nt->getFullURL(),
458 'text' => ( $wgContLang->getLanguageName( $nt->getInterwiki() ) != '' ?
459 $wgContLang->getLanguageName( $nt->getInterwiki() ) : $l ),
460 'class' => $class
461 );
462 }
463 }
464 }
465 if( count( $language_urls ) ) {
466 $tpl->setRef( 'language_urls', $language_urls );
467 } else {
468 $tpl->set( 'language_urls', false );
469 }
470 wfProfileOut( __METHOD__ . '-stuff4' );
471
472 wfProfileIn( __METHOD__ . '-stuff5' );
473 # Personal toolbar
474 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
475 $content_actions = $this->buildContentActionUrls();
476 $tpl->setRef( 'content_actions', $content_actions );
477
478 // XXX: attach this from javascript, same with section editing
479 if( $this->iseditable && $wgUser->getOption( 'editondblclick' ) ){
480 $encEditUrl = Xml::escapeJsString( $this->mTitle->getLocalUrl( $this->editUrlOptions() ) );
481 $tpl->set( 'body_ondblclick', 'document.location = "' . $encEditUrl . '";' );
482 } else {
483 $tpl->set( 'body_ondblclick', false );
484 }
485 $tpl->set( 'body_onload', false );
486 $tpl->set( 'sidebar', $this->buildSidebar() );
487 $tpl->set( 'nav_urls', $this->buildNavUrls() );
488
489 // Set the head scripts near the end, in case the above actions resulted in added scripts
490 if ( $this->useHeadElement ) {
491 $tpl->set( 'headelement', $out->headElement( $this ) );
492 } else {
493 $tpl->set( 'headscripts', $out->getScript() );
494 }
495
496 // original version by hansm
497 if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
498 wfDebug( __METHOD__ . ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
499 }
500
501 // allow extensions adding stuff after the page content.
502 // See Skin::afterContentHook() for further documentation.
503 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
504 wfProfileOut( __METHOD__ . '-stuff5' );
505
506 // execute template
507 wfProfileIn( __METHOD__ . '-execute' );
508 $res = $tpl->execute();
509 wfProfileOut( __METHOD__ . '-execute' );
510
511 // result may be an error
512 $this->printOrError( $res );
513 wfProfileOut( __METHOD__ );
514 }
515
516 /**
517 * Output the string, or print error message if it's
518 * an error object of the appropriate type.
519 * For the base class, assume strings all around.
520 *
521 * @param mixed $str
522 * @private
523 */
524 function printOrError( $str ) {
525 echo $str;
526 }
527
528 /**
529 * build array of urls for personal toolbar
530 * @return array
531 * @private
532 */
533 function buildPersonalUrls() {
534 global $wgOut, $wgRequest;
535
536 $title = $wgOut->getTitle();
537 $pageurl = $title->getLocalURL();
538 wfProfileIn( __METHOD__ );
539
540 /* set up the default links for the personal toolbar */
541 $personal_urls = array();
542 $page = $wgRequest->getVal( 'returnto', $this->thisurl );
543 $query = $wgRequest->getVal( 'returntoquery', $this->thisquery );
544 $returnto = "returnto=$page";
545 if( $this->thisquery != '' )
546 $returnto .= "&returntoquery=$query";
547 if( $this->loggedin ) {
548 $personal_urls['userpage'] = array(
549 'text' => $this->username,
550 'href' => &$this->userpageUrlDetails['href'],
551 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
552 'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
553 );
554 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
555 $personal_urls['mytalk'] = array(
556 'text' => wfMsg( 'mytalk' ),
557 'href' => &$usertalkUrlDetails['href'],
558 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
559 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
560 );
561 $href = self::makeSpecialUrl( 'Preferences' );
562 $personal_urls['preferences'] = array(
563 'text' => wfMsg( 'mypreferences' ),
564 'href' => $href,
565 'active' => ( $href == $pageurl )
566 );
567 $href = self::makeSpecialUrl( 'Watchlist' );
568 $personal_urls['watchlist'] = array(
569 'text' => wfMsg( 'mywatchlist' ),
570 'href' => $href,
571 'active' => ( $href == $pageurl )
572 );
573
574 # We need to do an explicit check for Special:Contributions, as we
575 # have to match both the title, and the target (which could come
576 # from request values or be specified in "sub page" form. The plot
577 # thickens, because $wgTitle is altered for special pages, so doesn't
578 # contain the original alias-with-subpage.
579 $origTitle = Title::newFromText( $wgRequest->getText( 'title' ) );
580 if( $origTitle instanceof Title && $origTitle->getNamespace() == NS_SPECIAL ) {
581 list( $spName, $spPar ) =
582 SpecialPage::resolveAliasWithSubpage( $origTitle->getText() );
583 $active = $spName == 'Contributions'
584 && ( ( $spPar && $spPar == $this->username )
585 || $wgRequest->getText( 'target' ) == $this->username );
586 } else {
587 $active = false;
588 }
589
590 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
591 $personal_urls['mycontris'] = array(
592 'text' => wfMsg( 'mycontris' ),
593 'href' => $href,
594 'active' => $active
595 );
596 $personal_urls['logout'] = array(
597 'text' => wfMsg( 'userlogout' ),
598 'href' => self::makeSpecialUrl( 'Userlogout',
599 $title->isSpecial( 'Preferences' ) ? '' : $returnto
600 ),
601 'active' => false
602 );
603 } else {
604 global $wgUser;
605 $loginlink = $wgUser->isAllowed( 'createaccount' )
606 ? 'nav-login-createaccount'
607 : 'login';
608 if( $this->showIPinHeader() ) {
609 $href = &$this->userpageUrlDetails['href'];
610 $personal_urls['anonuserpage'] = array(
611 'text' => $this->username,
612 'href' => $href,
613 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
614 'active' => ( $pageurl == $href )
615 );
616 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
617 $href = &$usertalkUrlDetails['href'];
618 $personal_urls['anontalk'] = array(
619 'text' => wfMsg( 'anontalk' ),
620 'href' => $href,
621 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
622 'active' => ( $pageurl == $href )
623 );
624 $personal_urls['anonlogin'] = array(
625 'text' => wfMsg( $loginlink ),
626 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
627 'active' => $title->isSpecial( 'Userlogin' )
628 );
629 } else {
630 $personal_urls['login'] = array(
631 'text' => wfMsg( $loginlink ),
632 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
633 'active' => $title->isSpecial( 'Userlogin' )
634 );
635 }
636 }
637
638 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$title ) );
639 wfProfileOut( __METHOD__ );
640 return $personal_urls;
641 }
642
643 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
644 $classes = array();
645 if( $selected ) {
646 $classes[] = 'selected';
647 }
648 if( $checkEdit && !$title->isKnown() ) {
649 $classes[] = 'new';
650 $query = 'action=edit&redlink=1';
651 }
652
653 $text = wfMsg( $message );
654 if ( wfEmptyMsg( $message, $text ) ) {
655 global $wgContLang;
656 $text = $wgContLang->getFormattedNsText( MWNamespace::getSubject( $title->getNamespace() ) );
657 }
658
659 $result = array();
660 if( !wfRunHooks( 'SkinTemplateTabAction', array( &$this,
661 $title, $message, $selected, $checkEdit,
662 &$classes, &$query, &$text, &$result ) ) ) {
663 return $result;
664 }
665
666 return array(
667 'class' => implode( ' ', $classes ),
668 'text' => $text,
669 'href' => $title->getLocalUrl( $query ) );
670 }
671
672 function makeTalkUrlDetails( $name, $urlaction = '' ) {
673 $title = Title::newFromText( $name );
674 if( !is_object( $title ) ) {
675 throw new MWException( __METHOD__ . " given invalid pagename $name" );
676 }
677 $title = $title->getTalkPage();
678 self::checkTitle( $title, $name );
679 return array(
680 'href' => $title->getLocalURL( $urlaction ),
681 'exists' => $title->getArticleID() != 0 ? true : false
682 );
683 }
684
685 function makeArticleUrlDetails( $name, $urlaction = '' ) {
686 $title = Title::newFromText( $name );
687 $title= $title->getSubjectPage();
688 self::checkTitle( $title, $name );
689 return array(
690 'href' => $title->getLocalURL( $urlaction ),
691 'exists' => $title->getArticleID() != 0 ? true : false
692 );
693 }
694
695 /**
696 * an array of edit links by default used for the tabs
697 * @return array
698 * @private
699 */
700 function buildContentActionUrls() {
701 global $wgContLang, $wgLang, $wgOut, $wgUser, $wgRequest, $wgArticle;
702
703 wfProfileIn( __METHOD__ );
704
705 $action = $wgRequest->getVal( 'action', 'view' );
706 $section = $wgRequest->getVal( 'section' );
707 $content_actions = array();
708
709 $prevent_active_tabs = false;
710 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this, &$prevent_active_tabs ) );
711
712 if( $this->iscontent ) {
713 $subjpage = $this->mTitle->getSubjectPage();
714 $talkpage = $this->mTitle->getTalkPage();
715
716 $nskey = $this->mTitle->getNamespaceKey();
717 $content_actions[$nskey] = $this->tabAction(
718 $subjpage,
719 $nskey,
720 !$this->mTitle->isTalkPage() && !$prevent_active_tabs,
721 '', true
722 );
723
724 $content_actions['talk'] = $this->tabAction(
725 $talkpage,
726 'talk',
727 $this->mTitle->isTalkPage() && !$prevent_active_tabs,
728 '',
729 true
730 );
731
732 wfProfileIn( __METHOD__ . '-edit' );
733 if ( $this->mTitle->quickUserCan( 'edit' ) && ( $this->mTitle->exists() || $this->mTitle->quickUserCan( 'create' ) ) ) {
734 $istalk = $this->mTitle->isTalkPage();
735 $istalkclass = $istalk?' istalk':'';
736 $content_actions['edit'] = array(
737 'class' => ( ( ( $action == 'edit' or $action == 'submit' ) and $section != 'new' ) ? 'selected' : '' ) . $istalkclass,
738 'text' => $this->mTitle->exists()
739 ? wfMsg( 'edit' )
740 : wfMsg( 'create' ),
741 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
742 );
743
744 // adds new section link if page is a current revision of a talk page or
745 if ( ( $wgArticle && $wgArticle->isCurrent() && $istalk ) || $wgOut->showNewSectionLink() ) {
746 if ( !$wgOut->forceHideNewSectionLink() ) {
747 $urlArgs = 'action=edit&section=new';
748 $preloadMsg = wfMsgForContent( 'talk-addsection-preload' );
749 $editintroMsg = wfMsgForContent( 'talk-addsection-editintro' );
750 if( $preloadMsg != '' ) {
751 $urlArgs .= '&preload=' . urlencode( $preloadMsg );
752 }
753 if( $editintroMsg != '' ) {
754 $urlArgs .= '&editintro=' . urlencode( $editintroMsg );
755 }
756 $content_actions['addsection'] = array(
757 'class' => $section == 'new' ? 'selected' : false,
758 'text' => wfMsg( 'addsection' ),
759 'href' => $this->mTitle->getLocalUrl( $urlArgs )
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 }