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