9bc3b2ee4c64df8e888b48a00f382177c54f8c34
[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 } else {
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 }
433 wfProfileOut( __METHOD__ . '-stuff3' );
434
435 wfProfileIn( __METHOD__ . '-stuff4' );
436 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
437 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
438 $tpl->set( 'disclaimer', $this->disclaimerLink() );
439 $tpl->set( 'privacy', $this->privacyLink() );
440 $tpl->set( 'about', $this->aboutLink() );
441
442 if ( $wgDebugComments ) {
443 $tpl->setRef( 'debug', $out->mDebugtext );
444 } else {
445 $tpl->set( 'debug', '' );
446 }
447
448 $tpl->set( 'reporttime', wfReportTime() );
449 $tpl->set( 'sitenotice', wfGetSiteNotice() );
450 $tpl->set( 'bottomscripts', $this->bottomScripts() );
451
452 $printfooter = "<div class=\"printfooter\">\n" . $this->printSource() . "</div>\n";
453 $out->mBodytext .= $printfooter . $this->generateDebugHTML();
454 $tpl->setRef( 'bodytext', $out->mBodytext );
455
456 # Language links
457 $language_urls = array();
458
459 if ( !$wgHideInterlanguageLinks ) {
460 foreach( $out->getLanguageLinks() as $l ) {
461 $tmp = explode( ':', $l, 2 );
462 $class = 'interwiki-' . $tmp[0];
463 unset( $tmp );
464 $nt = Title::newFromText( $l );
465 if ( $nt ) {
466 $language_urls[] = array(
467 'href' => $nt->getFullURL(),
468 'text' => ( $wgContLang->getLanguageName( $nt->getInterwiki() ) != '' ?
469 $wgContLang->getLanguageName( $nt->getInterwiki() ) : $l ),
470 'class' => $class
471 );
472 }
473 }
474 }
475 if( count( $language_urls ) ) {
476 $tpl->setRef( 'language_urls', $language_urls );
477 } else {
478 $tpl->set( 'language_urls', false );
479 }
480 wfProfileOut( __METHOD__ . '-stuff4' );
481
482 wfProfileIn( __METHOD__ . '-stuff5' );
483 # Personal toolbar
484 $tpl->set( 'personal_urls', $this->buildPersonalUrls() );
485 $content_actions = $this->buildContentActionUrls();
486 $tpl->setRef( 'content_actions', $content_actions );
487
488 // XXX: attach this from javascript, same with section editing
489 if( $this->iseditable && $wgUser->getOption( 'editondblclick' ) ){
490 $encEditUrl = Xml::escapeJsString( $this->mTitle->getLocalUrl( $this->editUrlOptions() ) );
491 $tpl->set( 'body_ondblclick', 'document.location = "' . $encEditUrl . '";' );
492 } else {
493 $tpl->set( 'body_ondblclick', false );
494 }
495 $tpl->set( 'body_onload', false );
496 $tpl->set( 'sidebar', $this->buildSidebar() );
497 $tpl->set( 'nav_urls', $this->buildNavUrls() );
498
499 // Set the head scripts near the end, in case the above actions resulted in added scripts
500 if ( $this->useHeadElement ) {
501 $tpl->set( 'headelement', $out->headElement( $this ) );
502 } else {
503 $tpl->set( 'headscripts', $out->getScript() );
504 }
505
506 // original version by hansm
507 if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
508 wfDebug( __METHOD__ . ": Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!\n" );
509 }
510
511 // allow extensions adding stuff after the page content.
512 // See Skin::afterContentHook() for further documentation.
513 $tpl->set( 'dataAfterContent', $this->afterContentHook() );
514 wfProfileOut( __METHOD__ . '-stuff5' );
515
516 // execute template
517 wfProfileIn( __METHOD__ . '-execute' );
518 $res = $tpl->execute();
519 wfProfileOut( __METHOD__ . '-execute' );
520
521 // result may be an error
522 $this->printOrError( $res );
523 wfProfileOut( __METHOD__ );
524 }
525
526 /**
527 * Output the string, or print error message if it's
528 * an error object of the appropriate type.
529 * For the base class, assume strings all around.
530 *
531 * @param mixed $str
532 * @private
533 */
534 function printOrError( $str ) {
535 echo $str;
536 }
537
538 /**
539 * build array of urls for personal toolbar
540 * @return array
541 * @private
542 */
543 function buildPersonalUrls() {
544 global $wgOut, $wgRequest;
545
546 $title = $wgOut->getTitle();
547 $pageurl = $title->getLocalURL();
548 wfProfileIn( __METHOD__ );
549
550 /* set up the default links for the personal toolbar */
551 $personal_urls = array();
552 $page = $wgRequest->getVal( 'returnto', $this->thisurl );
553 $query = $wgRequest->getVal( 'returntoquery', $this->thisquery );
554 $returnto = "returnto=$page";
555 if( $this->thisquery != '' )
556 $returnto .= "&returntoquery=$query";
557 if( $this->loggedin ) {
558 $personal_urls['userpage'] = array(
559 'text' => $this->username,
560 'href' => &$this->userpageUrlDetails['href'],
561 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
562 'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
563 );
564 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
565 $personal_urls['mytalk'] = array(
566 'text' => wfMsg( 'mytalk' ),
567 'href' => &$usertalkUrlDetails['href'],
568 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
569 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
570 );
571 $href = self::makeSpecialUrl( 'Preferences' );
572 $personal_urls['preferences'] = array(
573 'text' => wfMsg( 'mypreferences' ),
574 'href' => $href,
575 'active' => ( $href == $pageurl )
576 );
577 $href = self::makeSpecialUrl( 'Watchlist' );
578 $personal_urls['watchlist'] = array(
579 'text' => wfMsg( 'mywatchlist' ),
580 'href' => $href,
581 'active' => ( $href == $pageurl )
582 );
583
584 # We need to do an explicit check for Special:Contributions, as we
585 # have to match both the title, and the target (which could come
586 # from request values or be specified in "sub page" form. The plot
587 # thickens, because $wgTitle is altered for special pages, so doesn't
588 # contain the original alias-with-subpage.
589 $origTitle = Title::newFromText( $wgRequest->getText( 'title' ) );
590 if( $origTitle instanceof Title && $origTitle->getNamespace() == NS_SPECIAL ) {
591 list( $spName, $spPar ) =
592 SpecialPage::resolveAliasWithSubpage( $origTitle->getText() );
593 $active = $spName == 'Contributions'
594 && ( ( $spPar && $spPar == $this->username )
595 || $wgRequest->getText( 'target' ) == $this->username );
596 } else {
597 $active = false;
598 }
599
600 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
601 $personal_urls['mycontris'] = array(
602 'text' => wfMsg( 'mycontris' ),
603 'href' => $href,
604 'active' => $active
605 );
606 $personal_urls['logout'] = array(
607 'text' => wfMsg( 'userlogout' ),
608 'href' => self::makeSpecialUrl( 'Userlogout',
609 $title->isSpecial( 'Preferences' ) ? '' : $returnto
610 ),
611 'active' => false
612 );
613 } else {
614 global $wgUser;
615 $loginlink = $wgUser->isAllowed( 'createaccount' )
616 ? 'nav-login-createaccount'
617 : 'login';
618 if( $this->showIPinHeader() ) {
619 $href = &$this->userpageUrlDetails['href'];
620 $personal_urls['anonuserpage'] = array(
621 'text' => $this->username,
622 'href' => $href,
623 'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
624 'active' => ( $pageurl == $href )
625 );
626 $usertalkUrlDetails = $this->makeTalkUrlDetails( $this->userpage );
627 $href = &$usertalkUrlDetails['href'];
628 $personal_urls['anontalk'] = array(
629 'text' => wfMsg( 'anontalk' ),
630 'href' => $href,
631 'class' => $usertalkUrlDetails['exists'] ? false : 'new',
632 'active' => ( $pageurl == $href )
633 );
634 $personal_urls['anonlogin'] = array(
635 'text' => wfMsg( $loginlink ),
636 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
637 'active' => $title->isSpecial( 'Userlogin' )
638 );
639 } else {
640 $personal_urls['login'] = array(
641 'text' => wfMsg( $loginlink ),
642 'href' => self::makeSpecialUrl( 'Userlogin', $returnto ),
643 'active' => $title->isSpecial( 'Userlogin' )
644 );
645 }
646 }
647
648 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$title ) );
649 wfProfileOut( __METHOD__ );
650 return $personal_urls;
651 }
652
653 function tabAction( $title, $message, $selected, $query = '', $checkEdit = false ) {
654 $classes = array();
655 if( $selected ) {
656 $classes[] = 'selected';
657 }
658 if( $checkEdit && !$title->isKnown() ) {
659 $classes[] = 'new';
660 $query = 'action=edit&redlink=1';
661 }
662
663 $text = wfMsg( $message );
664 if ( wfEmptyMsg( $message, $text ) ) {
665 global $wgContLang;
666 $text = $wgContLang->getFormattedNsText( MWNamespace::getSubject( $title->getNamespace() ) );
667 }
668
669 $result = array();
670 if( !wfRunHooks( 'SkinTemplateTabAction', array( &$this,
671 $title, $message, $selected, $checkEdit,
672 &$classes, &$query, &$text, &$result ) ) ) {
673 return $result;
674 }
675
676 return array(
677 'class' => implode( ' ', $classes ),
678 'text' => $text,
679 'href' => $title->getLocalUrl( $query ) );
680 }
681
682 function makeTalkUrlDetails( $name, $urlaction = '' ) {
683 $title = Title::newFromText( $name );
684 if( !is_object( $title ) ) {
685 throw new MWException( __METHOD__ . " given invalid pagename $name" );
686 }
687 $title = $title->getTalkPage();
688 self::checkTitle( $title, $name );
689 return array(
690 'href' => $title->getLocalURL( $urlaction ),
691 'exists' => $title->getArticleID() != 0 ? true : false
692 );
693 }
694
695 function makeArticleUrlDetails( $name, $urlaction = '' ) {
696 $title = Title::newFromText( $name );
697 $title= $title->getSubjectPage();
698 self::checkTitle( $title, $name );
699 return array(
700 'href' => $title->getLocalURL( $urlaction ),
701 'exists' => $title->getArticleID() != 0 ? true : false
702 );
703 }
704
705 /**
706 * an array of edit links by default used for the tabs
707 * @return array
708 * @private
709 */
710 function buildContentActionUrls() {
711 global $wgContLang, $wgLang, $wgOut, $wgUser, $wgRequest, $wgArticle;
712
713 wfProfileIn( __METHOD__ );
714
715 $action = $wgRequest->getVal( 'action', 'view' );
716 $section = $wgRequest->getVal( 'section' );
717 $content_actions = array();
718
719 $prevent_active_tabs = false;
720 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this, &$prevent_active_tabs ) );
721
722 if( $this->iscontent ) {
723 $subjpage = $this->mTitle->getSubjectPage();
724 $talkpage = $this->mTitle->getTalkPage();
725
726 $nskey = $this->mTitle->getNamespaceKey();
727 $content_actions[$nskey] = $this->tabAction(
728 $subjpage,
729 $nskey,
730 !$this->mTitle->isTalkPage() && !$prevent_active_tabs,
731 '', true
732 );
733
734 $content_actions['talk'] = $this->tabAction(
735 $talkpage,
736 'talk',
737 $this->mTitle->isTalkPage() && !$prevent_active_tabs,
738 '',
739 true
740 );
741
742 wfProfileIn( __METHOD__ . '-edit' );
743 if ( $this->mTitle->quickUserCan( 'edit' ) && ( $this->mTitle->exists() || $this->mTitle->quickUserCan( 'create' ) ) ) {
744 $istalk = $this->mTitle->isTalkPage();
745 $istalkclass = $istalk?' istalk':'';
746 $content_actions['edit'] = array(
747 'class' => ( ( ( $action == 'edit' or $action == 'submit' ) and $section != 'new' ) ? 'selected' : '' ) . $istalkclass,
748 'text' => $this->mTitle->exists()
749 ? wfMsg( 'edit' )
750 : wfMsg( 'create' ),
751 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
752 );
753
754 // adds new section link if page is a current revision of a talk page or
755 if ( ( $wgArticle && $wgArticle->isCurrent() && $istalk ) || $wgOut->showNewSectionLink() ) {
756 if ( !$wgOut->forceHideNewSectionLink() ) {
757 $content_actions['addsection'] = array(
758 'class' => $section == 'new' ? 'selected' : false,
759 'text' => wfMsg( 'addsection' ),
760 'href' => $this->mTitle->getLocalUrl( 'action=edit&section=new' )
761 );
762 }
763 }
764 } elseif ( $this->mTitle->isKnown() ) {
765 $content_actions['viewsource'] = array(
766 'class' => ($action == 'edit') ? 'selected' : false,
767 'text' => wfMsg( 'viewsource' ),
768 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
769 );
770 }
771 wfProfileOut( __METHOD__ . '-edit' );
772
773 wfProfileIn( __METHOD__ . '-live' );
774 if ( $this->mTitle->exists() ) {
775
776 $content_actions['history'] = array(
777 'class' => ($action == 'history') ? 'selected' : false,
778 'text' => wfMsg( 'history_short' ),
779 'href' => $this->mTitle->getLocalUrl( 'action=history' ),
780 'rel' => 'archives',
781 );
782
783 if( $wgUser->isAllowed( 'delete' ) ) {
784 $content_actions['delete'] = array(
785 'class' => ($action == 'delete') ? 'selected' : false,
786 'text' => wfMsg( 'delete' ),
787 'href' => $this->mTitle->getLocalUrl( 'action=delete' )
788 );
789 }
790 if ( $this->mTitle->quickUserCan( 'move' ) ) {
791 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $this->thispage );
792 $content_actions['move'] = array(
793 'class' => $this->mTitle->isSpecial( 'Movepage' ) ? 'selected' : false,
794 'text' => wfMsg( 'move' ),
795 'href' => $moveTitle->getLocalUrl()
796 );
797 }
798
799 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
800 if( !$this->mTitle->isProtected() ){
801 $content_actions['protect'] = array(
802 'class' => ($action == 'protect') ? 'selected' : false,
803 'text' => wfMsg( 'protect' ),
804 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
805 );
806
807 } else {
808 $content_actions['unprotect'] = array(
809 'class' => ($action == 'unprotect') ? 'selected' : false,
810 'text' => wfMsg( 'unprotect' ),
811 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
812 );
813 }
814 }
815 } else {
816 //article doesn't exist or is deleted
817 if( $wgUser->isAllowed( 'deletedhistory' ) && $wgUser->isAllowed( 'deletedtext' ) ) {
818 if( $n = $this->mTitle->isDeleted() ) {
819 $undelTitle = SpecialPage::getTitleFor( 'Undelete' );
820 $content_actions['undelete'] = array(
821 'class' => false,
822 'text' => wfMsgExt( 'undelete_short', array( 'parsemag' ), $wgLang->formatNum( $n ) ),
823 'href' => $undelTitle->getLocalUrl( 'target=' . urlencode( $this->thispage ) )
824 #'href' => self::makeSpecialUrl( "Undelete/$this->thispage" )
825 );
826 }
827 }
828
829 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
830 if( !$this->mTitle->getRestrictions( 'create' ) ) {
831 $content_actions['protect'] = array(
832 'class' => ($action == 'protect') ? 'selected' : false,
833 'text' => wfMsg( 'protect' ),
834 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
835 );
836
837 } else {
838 $content_actions['unprotect'] = array(
839 'class' => ($action == 'unprotect') ? 'selected' : false,
840 'text' => wfMsg( 'unprotect' ),
841 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
842 );
843 }
844 }
845 }
846
847 wfProfileOut( __METHOD__ . '-live' );
848
849 if( $this->loggedin ) {
850 if( !$this->mTitle->userIsWatching()) {
851 $content_actions['watch'] = array(
852 'class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : false,
853 'text' => wfMsg( 'watch' ),
854 'href' => $this->mTitle->getLocalUrl( 'action=watch' )
855 );
856 } else {
857 $content_actions['unwatch'] = array(
858 'class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : false,
859 'text' => wfMsg( 'unwatch' ),
860 'href' => $this->mTitle->getLocalUrl( 'action=unwatch' )
861 );
862 }
863 }
864
865
866 wfRunHooks( 'SkinTemplateTabs', array( $this, &$content_actions ) );
867 } else {
868 /* show special page tab */
869
870 $content_actions[$this->mTitle->getNamespaceKey()] = array(
871 'class' => 'selected',
872 'text' => wfMsg('nstab-special'),
873 'href' => $wgRequest->getRequestURL(), // @bug 2457, 2510
874 );
875
876 wfRunHooks( 'SkinTemplateBuildContentActionUrlsAfterSpecialPage', array( &$this, &$content_actions ) );
877 }
878
879 /* show links to different language variants */
880 global $wgDisableLangConversion;
881 $variants = $wgContLang->getVariants();
882 if( !$wgDisableLangConversion && sizeof( $variants ) > 1 ) {
883 $preferred = $wgContLang->getPreferredVariant();
884 $vcount=0;
885 foreach( $variants as $code ) {
886 $varname = $wgContLang->getVariantname( $code );
887 if( $varname == 'disable' )
888 continue;
889 $selected = ( $code == $preferred )? 'selected' : false;
890 $content_actions['varlang-' . $vcount] = array(
891 'class' => $selected,
892 'text' => $varname,
893 'href' => $this->mTitle->getLocalURL( '', $code )
894 );
895 $vcount ++;
896 }
897 }
898
899 wfRunHooks( 'SkinTemplateContentActions', array( &$content_actions ) );
900
901 wfProfileOut( __METHOD__ );
902 return $content_actions;
903 }
904
905 /**
906 * build array of common navigation links
907 * @return array
908 * @private
909 */
910 function buildNavUrls() {
911 global $wgUseTrackbacks, $wgOut, $wgUser, $wgRequest;
912 global $wgEnableUploads, $wgUploadNavigationUrl;
913
914 wfProfileIn( __METHOD__ );
915
916 $action = $wgRequest->getVal( 'action', 'view' );
917
918 $nav_urls = array();
919 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
920 if( $wgUploadNavigationUrl ) {
921 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
922 } elseif( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
923 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
924 } else {
925 $nav_urls['upload'] = false;
926 }
927 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
928
929 // default permalink to being off, will override it as required below.
930 $nav_urls['permalink'] = false;
931
932 // A print stylesheet is attached to all pages, but nobody ever
933 // figures that out. :) Add a link...
934 if( $this->iscontent && ( $action == 'view' || $action == 'purge' ) ) {
935 if ( !$wgOut->isPrintable() ) {
936 $nav_urls['print'] = array(
937 'text' => wfMsg( 'printableversion' ),
938 'href' => $wgRequest->appendQuery( 'printable=yes' )
939 );
940 }
941
942 // Also add a "permalink" while we're at it
943 if ( $this->mRevisionId ) {
944 $nav_urls['permalink'] = array(
945 'text' => wfMsg( 'permalink' ),
946 'href' => $wgOut->getTitle()->getLocalURL( "oldid=$this->mRevisionId" )
947 );
948 }
949
950 // Copy in case this undocumented, shady hook tries to mess with internals
951 $revid = $this->mRevisionId;
952 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink', array( &$this, &$nav_urls, &$revid, &$revid ) );
953 }
954
955 if( $this->mTitle->getNamespace() != NS_SPECIAL ) {
956 $wlhTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage );
957 $nav_urls['whatlinkshere'] = array(
958 'href' => $wlhTitle->getLocalUrl()
959 );
960 if( $this->mTitle->getArticleId() ) {
961 $rclTitle = SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage );
962 $nav_urls['recentchangeslinked'] = array(
963 'href' => $rclTitle->getLocalUrl()
964 );
965 } else {
966 $nav_urls['recentchangeslinked'] = false;
967 }
968 if( $wgUseTrackbacks )
969 $nav_urls['trackbacklink'] = array(
970 'href' => $wgOut->getTitle()->trackbackURL()
971 );
972 }
973
974 if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) {
975 $id = User::idFromName( $this->mTitle->getText() );
976 $ip = User::isIP( $this->mTitle->getText() );
977 } else {
978 $id = 0;
979 $ip = false;
980 }
981
982 if( $id || $ip ) { # both anons and non-anons have contribs list
983 $nav_urls['contributions'] = array(
984 'href' => self::makeSpecialUrlSubpage( 'Contributions', $this->mTitle->getText() )
985 );
986
987 if( $id ) {
988 $logPage = SpecialPage::getTitleFor( 'Log' );
989 $nav_urls['log'] = array(
990 'href' => $logPage->getLocalUrl(
991 array(
992 'user' => $this->mTitle->getText()
993 )
994 )
995 );
996 } else {
997 $nav_urls['log'] = false;
998 }
999
1000 if ( $wgUser->isAllowed( 'block' ) ) {
1001 $nav_urls['blockip'] = array(
1002 'href' => self::makeSpecialUrlSubpage( 'Blockip', $this->mTitle->getText() )
1003 );
1004 } else {
1005 $nav_urls['blockip'] = false;
1006 }
1007 } else {
1008 $nav_urls['contributions'] = false;
1009 $nav_urls['log'] = false;
1010 $nav_urls['blockip'] = false;
1011 }
1012 $nav_urls['emailuser'] = false;
1013 if( $this->showEmailUser( $id ) ) {
1014 $nav_urls['emailuser'] = array(
1015 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $this->mTitle->getText() )
1016 );
1017 }
1018 wfProfileOut( __METHOD__ );
1019 return $nav_urls;
1020 }
1021
1022 /**
1023 * Generate strings used for xml 'id' names
1024 * @return string
1025 * @private
1026 */
1027 function getNameSpaceKey() {
1028 return $this->mTitle->getNamespaceKey();
1029 }
1030
1031 /**
1032 * @private
1033 */
1034 function setupUserJs( $allowUserJs ) {
1035 global $wgRequest, $wgJsMimeType;
1036 wfProfileIn( __METHOD__ );
1037
1038 $action = $wgRequest->getVal( 'action', 'view' );
1039
1040 if( $allowUserJs && $this->loggedin ) {
1041 if( $this->mTitle->isJsSubpage() and $this->userCanPreview( $action ) ) {
1042 # XXX: additional security check/prompt?
1043 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText( 'wpTextbox1' ) . ' /*]]>*/';
1044 } else {
1045 $this->userjs = self::makeUrl( $this->userpage . '/' . $this->skinname . '.js', 'action=raw&ctype=' . $wgJsMimeType );
1046 }
1047 }
1048 wfProfileOut( __METHOD__ );
1049 }
1050
1051 /**
1052 * Code for extensions to hook into to provide per-page CSS, see
1053 * extensions/PageCSS/PageCSS.php for an implementation of this.
1054 *
1055 * @private
1056 */
1057 function setupPageCss() {
1058 wfProfileIn( __METHOD__ );
1059 $out = false;
1060 wfRunHooks( 'SkinTemplateSetupPageCss', array( &$out ) );
1061 wfProfileOut( __METHOD__ );
1062 return $out;
1063 }
1064
1065 public function commonPrintStylesheet() {
1066 return false;
1067 }
1068 }
1069
1070 /**
1071 * Generic wrapper for template functions, with interface
1072 * compatible with what we use of PHPTAL 0.7.
1073 * @ingroup Skins
1074 */
1075 abstract class QuickTemplate {
1076 /**
1077 * Constructor
1078 */
1079 public function QuickTemplate() {
1080 $this->data = array();
1081 $this->translator = new MediaWiki_I18N();
1082 }
1083
1084 /**
1085 * Sets the value $value to $name
1086 * @param $name
1087 * @param $value
1088 */
1089 public function set( $name, $value ) {
1090 $this->data[$name] = $value;
1091 }
1092
1093 /**
1094 * @param $name
1095 * @param $value
1096 */
1097 public function setRef( $name, &$value ) {
1098 $this->data[$name] =& $value;
1099 }
1100
1101 /**
1102 * @param $t
1103 */
1104 public function setTranslator( &$t ) {
1105 $this->translator = &$t;
1106 }
1107
1108 /**
1109 * Main function, used by classes that subclass QuickTemplate
1110 * to show the actual HTML output
1111 */
1112 abstract public function execute();
1113
1114 /**
1115 * @private
1116 */
1117 function text( $str ) {
1118 echo htmlspecialchars( $this->data[$str] );
1119 }
1120
1121 /**
1122 * @private
1123 */
1124 function jstext( $str ) {
1125 echo Xml::escapeJsString( $this->data[$str] );
1126 }
1127
1128 /**
1129 * @private
1130 */
1131 function html( $str ) {
1132 echo $this->data[$str];
1133 }
1134
1135 /**
1136 * @private
1137 */
1138 function msg( $str ) {
1139 echo htmlspecialchars( $this->translator->translate( $str ) );
1140 }
1141
1142 /**
1143 * @private
1144 */
1145 function msgHtml( $str ) {
1146 echo $this->translator->translate( $str );
1147 }
1148
1149 /**
1150 * An ugly, ugly hack.
1151 * @private
1152 */
1153 function msgWiki( $str ) {
1154 global $wgParser, $wgOut;
1155
1156 $text = $this->translator->translate( $str );
1157 $parserOutput = $wgParser->parse( $text, $wgOut->getTitle(),
1158 $wgOut->parserOptions(), true );
1159 echo $parserOutput->getText();
1160 }
1161
1162 /**
1163 * @private
1164 */
1165 function haveData( $str ) {
1166 return isset( $this->data[$str] );
1167 }
1168
1169 /**
1170 * @private
1171 */
1172 function haveMsg( $str ) {
1173 $msg = $this->translator->translate( $str );
1174 return ( $msg != '-' ) && ( $msg != '' ); # ????
1175 }
1176 }