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