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