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