Followup r58980: Moved logic for addsection-editintro/preload from SkinTemplate to...
[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 $content_actions['addsection'] = array(
764 'class' => $section == 'new' ? 'selected' : false,
765 'text' => wfMsg( 'addsection' ),
766 'href' => $this->mTitle->getLocalUrl( 'action=edit&section=new' )
767 );
768 }
769 }
770 } elseif ( $this->mTitle->isKnown() ) {
771 $content_actions['viewsource'] = array(
772 'class' => ($action == 'edit') ? 'selected' : false,
773 'text' => wfMsg( 'viewsource' ),
774 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
775 );
776 }
777 wfProfileOut( __METHOD__ . '-edit' );
778
779 wfProfileIn( __METHOD__ . '-live' );
780 if ( $this->mTitle->exists() ) {
781
782 $content_actions['history'] = array(
783 'class' => ($action == 'history') ? 'selected' : false,
784 'text' => wfMsg( 'history_short' ),
785 'href' => $this->mTitle->getLocalUrl( 'action=history' ),
786 'rel' => 'archives',
787 );
788
789 if( $wgUser->isAllowed( 'delete' ) ) {
790 $content_actions['delete'] = array(
791 'class' => ($action == 'delete') ? 'selected' : false,
792 'text' => wfMsg( 'delete' ),
793 'href' => $this->mTitle->getLocalUrl( 'action=delete' )
794 );
795 }
796 if ( $this->mTitle->quickUserCan( 'move' ) ) {
797 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $this->thispage );
798 $content_actions['move'] = array(
799 'class' => $this->mTitle->isSpecial( 'Movepage' ) ? 'selected' : false,
800 'text' => wfMsg( 'move' ),
801 'href' => $moveTitle->getLocalUrl()
802 );
803 }
804
805 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
806 if( !$this->mTitle->isProtected() ){
807 $content_actions['protect'] = array(
808 'class' => ($action == 'protect') ? 'selected' : false,
809 'text' => wfMsg( 'protect' ),
810 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
811 );
812
813 } else {
814 $content_actions['unprotect'] = array(
815 'class' => ($action == 'unprotect') ? 'selected' : false,
816 'text' => wfMsg( 'unprotect' ),
817 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
818 );
819 }
820 }
821 } else {
822 //article doesn't exist or is deleted
823 if( $wgUser->isAllowed( 'deletedhistory' ) && $wgUser->isAllowed( 'deletedtext' ) ) {
824 if( $n = $this->mTitle->isDeleted() ) {
825 $undelTitle = SpecialPage::getTitleFor( 'Undelete' );
826 $content_actions['undelete'] = array(
827 'class' => false,
828 'text' => wfMsgExt( 'undelete_short', array( 'parsemag' ), $wgLang->formatNum( $n ) ),
829 'href' => $undelTitle->getLocalUrl( 'target=' . urlencode( $this->thispage ) )
830 #'href' => self::makeSpecialUrl( "Undelete/$this->thispage" )
831 );
832 }
833 }
834
835 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
836 if( !$this->mTitle->getRestrictions( 'create' ) ) {
837 $content_actions['protect'] = array(
838 'class' => ($action == 'protect') ? 'selected' : false,
839 'text' => wfMsg( 'protect' ),
840 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
841 );
842
843 } else {
844 $content_actions['unprotect'] = array(
845 'class' => ($action == 'unprotect') ? 'selected' : false,
846 'text' => wfMsg( 'unprotect' ),
847 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
848 );
849 }
850 }
851 }
852
853 wfProfileOut( __METHOD__ . '-live' );
854
855 if( $this->loggedin ) {
856 if( !$this->mTitle->userIsWatching()) {
857 $content_actions['watch'] = array(
858 'class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : false,
859 'text' => wfMsg( 'watch' ),
860 'href' => $this->mTitle->getLocalUrl( 'action=watch' )
861 );
862 } else {
863 $content_actions['unwatch'] = array(
864 'class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : false,
865 'text' => wfMsg( 'unwatch' ),
866 'href' => $this->mTitle->getLocalUrl( 'action=unwatch' )
867 );
868 }
869 }
870
871
872 wfRunHooks( 'SkinTemplateTabs', array( $this, &$content_actions ) );
873 } else {
874 /* show special page tab */
875
876 $content_actions[$this->mTitle->getNamespaceKey()] = array(
877 'class' => 'selected',
878 'text' => wfMsg('nstab-special'),
879 'href' => $wgRequest->getRequestURL(), // @bug 2457, 2510
880 );
881
882 wfRunHooks( 'SkinTemplateBuildContentActionUrlsAfterSpecialPage', array( &$this, &$content_actions ) );
883 }
884
885 /* show links to different language variants */
886 global $wgDisableLangConversion;
887 $variants = $wgContLang->getVariants();
888 if( !$wgDisableLangConversion && sizeof( $variants ) > 1 ) {
889 $preferred = $wgContLang->getPreferredVariant();
890 $vcount=0;
891 foreach( $variants as $code ) {
892 $varname = $wgContLang->getVariantname( $code );
893 if( $varname == 'disable' )
894 continue;
895 $selected = ( $code == $preferred )? 'selected' : false;
896 $content_actions['varlang-' . $vcount] = array(
897 'class' => $selected,
898 'text' => $varname,
899 'href' => $this->mTitle->getLocalURL( '', $code )
900 );
901 $vcount ++;
902 }
903 }
904
905 wfRunHooks( 'SkinTemplateContentActions', array( &$content_actions ) );
906
907 wfProfileOut( __METHOD__ );
908 return $content_actions;
909 }
910
911 /**
912 * build array of common navigation links
913 * @return array
914 * @private
915 */
916 function buildNavUrls() {
917 global $wgUseTrackbacks, $wgOut, $wgUser, $wgRequest;
918 global $wgEnableUploads, $wgUploadNavigationUrl;
919
920 wfProfileIn( __METHOD__ );
921
922 $action = $wgRequest->getVal( 'action', 'view' );
923
924 $nav_urls = array();
925 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
926 if( $wgUploadNavigationUrl ) {
927 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
928 } elseif( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
929 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
930 } else {
931 $nav_urls['upload'] = false;
932 }
933 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
934
935 // default permalink to being off, will override it as required below.
936 $nav_urls['permalink'] = false;
937
938 // A print stylesheet is attached to all pages, but nobody ever
939 // figures that out. :) Add a link...
940 if( $this->iscontent && ( $action == 'view' || $action == 'purge' ) ) {
941 if ( !$wgOut->isPrintable() ) {
942 $nav_urls['print'] = array(
943 'text' => wfMsg( 'printableversion' ),
944 'href' => $wgRequest->appendQuery( 'printable=yes' )
945 );
946 }
947
948 // Also add a "permalink" while we're at it
949 if ( $this->mRevisionId ) {
950 $nav_urls['permalink'] = array(
951 'text' => wfMsg( 'permalink' ),
952 'href' => $wgOut->getTitle()->getLocalURL( "oldid=$this->mRevisionId" )
953 );
954 }
955
956 // Copy in case this undocumented, shady hook tries to mess with internals
957 $revid = $this->mRevisionId;
958 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink', array( &$this, &$nav_urls, &$revid, &$revid ) );
959 }
960
961 if( $this->mTitle->getNamespace() != NS_SPECIAL ) {
962 $wlhTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage );
963 $nav_urls['whatlinkshere'] = array(
964 'href' => $wlhTitle->getLocalUrl()
965 );
966 if( $this->mTitle->getArticleId() ) {
967 $rclTitle = SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage );
968 $nav_urls['recentchangeslinked'] = array(
969 'href' => $rclTitle->getLocalUrl()
970 );
971 } else {
972 $nav_urls['recentchangeslinked'] = false;
973 }
974 if( $wgUseTrackbacks )
975 $nav_urls['trackbacklink'] = array(
976 'href' => $wgOut->getTitle()->trackbackURL()
977 );
978 }
979
980 if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) {
981 $id = User::idFromName( $this->mTitle->getText() );
982 $ip = User::isIP( $this->mTitle->getText() );
983 } else {
984 $id = 0;
985 $ip = false;
986 }
987
988 if( $id || $ip ) { # both anons and non-anons have contribs list
989 $nav_urls['contributions'] = array(
990 'href' => self::makeSpecialUrlSubpage( 'Contributions', $this->mTitle->getText() )
991 );
992
993 if( $id ) {
994 $logPage = SpecialPage::getTitleFor( 'Log' );
995 $nav_urls['log'] = array(
996 'href' => $logPage->getLocalUrl(
997 array(
998 'user' => $this->mTitle->getText()
999 )
1000 )
1001 );
1002 } else {
1003 $nav_urls['log'] = false;
1004 }
1005
1006 if ( $wgUser->isAllowed( 'block' ) ) {
1007 $nav_urls['blockip'] = array(
1008 'href' => self::makeSpecialUrlSubpage( 'Blockip', $this->mTitle->getText() )
1009 );
1010 } else {
1011 $nav_urls['blockip'] = false;
1012 }
1013 } else {
1014 $nav_urls['contributions'] = false;
1015 $nav_urls['log'] = false;
1016 $nav_urls['blockip'] = false;
1017 }
1018 $nav_urls['emailuser'] = false;
1019 if( $this->showEmailUser( $id ) ) {
1020 $nav_urls['emailuser'] = array(
1021 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $this->mTitle->getText() )
1022 );
1023 }
1024 wfProfileOut( __METHOD__ );
1025 return $nav_urls;
1026 }
1027
1028 /**
1029 * Generate strings used for xml 'id' names
1030 * @return string
1031 * @private
1032 */
1033 function getNameSpaceKey() {
1034 return $this->mTitle->getNamespaceKey();
1035 }
1036
1037 /**
1038 * @private
1039 */
1040 function setupUserJs( $allowUserJs ) {
1041 global $wgRequest, $wgJsMimeType;
1042 wfProfileIn( __METHOD__ );
1043
1044 $action = $wgRequest->getVal( 'action', 'view' );
1045
1046 if( $allowUserJs && $this->loggedin ) {
1047 if( $this->mTitle->isJsSubpage() and $this->userCanPreview( $action ) ) {
1048 # XXX: additional security check/prompt?
1049 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText( 'wpTextbox1' ) . ' /*]]>*/';
1050 } else {
1051 $this->userjs = self::makeUrl( $this->userpage . '/' . $this->skinname . '.js', 'action=raw&ctype=' . $wgJsMimeType );
1052 }
1053 }
1054 wfProfileOut( __METHOD__ );
1055 }
1056
1057 /**
1058 * Code for extensions to hook into to provide per-page CSS, see
1059 * extensions/PageCSS/PageCSS.php for an implementation of this.
1060 *
1061 * @private
1062 */
1063 function setupPageCss() {
1064 wfProfileIn( __METHOD__ );
1065 $out = false;
1066 wfRunHooks( 'SkinTemplateSetupPageCss', array( &$out ) );
1067 wfProfileOut( __METHOD__ );
1068 return $out;
1069 }
1070
1071 public function commonPrintStylesheet() {
1072 return false;
1073 }
1074 }
1075
1076 /**
1077 * Generic wrapper for template functions, with interface
1078 * compatible with what we use of PHPTAL 0.7.
1079 * @ingroup Skins
1080 */
1081 abstract class QuickTemplate {
1082 /**
1083 * Constructor
1084 */
1085 public function QuickTemplate() {
1086 $this->data = array();
1087 $this->translator = new MediaWiki_I18N();
1088 }
1089
1090 /**
1091 * Sets the value $value to $name
1092 * @param $name
1093 * @param $value
1094 */
1095 public function set( $name, $value ) {
1096 $this->data[$name] = $value;
1097 }
1098
1099 /**
1100 * @param $name
1101 * @param $value
1102 */
1103 public function setRef( $name, &$value ) {
1104 $this->data[$name] =& $value;
1105 }
1106
1107 /**
1108 * @param $t
1109 */
1110 public function setTranslator( &$t ) {
1111 $this->translator = &$t;
1112 }
1113
1114 /**
1115 * Main function, used by classes that subclass QuickTemplate
1116 * to show the actual HTML output
1117 */
1118 abstract public function execute();
1119
1120 /**
1121 * @private
1122 */
1123 function text( $str ) {
1124 echo htmlspecialchars( $this->data[$str] );
1125 }
1126
1127 /**
1128 * @private
1129 */
1130 function jstext( $str ) {
1131 echo Xml::escapeJsString( $this->data[$str] );
1132 }
1133
1134 /**
1135 * @private
1136 */
1137 function html( $str ) {
1138 echo $this->data[$str];
1139 }
1140
1141 /**
1142 * @private
1143 */
1144 function msg( $str ) {
1145 echo htmlspecialchars( $this->translator->translate( $str ) );
1146 }
1147
1148 /**
1149 * @private
1150 */
1151 function msgHtml( $str ) {
1152 echo $this->translator->translate( $str );
1153 }
1154
1155 /**
1156 * An ugly, ugly hack.
1157 * @private
1158 */
1159 function msgWiki( $str ) {
1160 global $wgParser, $wgOut;
1161
1162 $text = $this->translator->translate( $str );
1163 $parserOutput = $wgParser->parse( $text, $wgOut->getTitle(),
1164 $wgOut->parserOptions(), true );
1165 echo $parserOutput->getText();
1166 }
1167
1168 /**
1169 * @private
1170 */
1171 function haveData( $str ) {
1172 return isset( $this->data[$str] );
1173 }
1174
1175 /**
1176 * @private
1177 */
1178 function haveMsg( $str ) {
1179 $msg = $this->translator->translate( $str );
1180 return ( $msg != '-' ) && ( $msg != '' ); # ????
1181 }
1182 }