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