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