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