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