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