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