eb651b552e45d64c79c63e5de5a6d7ef8bca9564
[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 * @addtogroup 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 $fname = 'SkinTemplate-translate';
36 wfProfileIn( $fname );
37
38 // Hack for i18n:attributes in PHPTAL 1.0.0 dev version as of 2004-10-23
39 $value = preg_replace( '/^string:/', '', $value );
40
41 $value = wfMsg( $value );
42 // interpolate variables
43 $m = array();
44 while (preg_match('/\$([0-9]*?)/sm', $value, $m)) {
45 list($src, $var) = $m;
46 wfSuppressWarnings();
47 $varValue = $this->_context[$var];
48 wfRestoreWarnings();
49 $value = str_replace($src, $varValue, $value);
50 }
51 wfProfileOut( $fname );
52 return $value;
53 }
54 }
55
56 /**
57 * Template-filler skin base class
58 * Formerly generic PHPTal (http://phptal.sourceforge.net/) skin
59 * Based on Brion's smarty skin
60 * @copyright Copyright © Gabriel Wicke -- http://www.aulinx.de/
61 *
62 * @todo Needs some serious refactoring into functions that correspond
63 * to the computations individual esi snippets need. Most importantly no body
64 * parsing for most of those of course.
65 *
66 * @addtogroup Skins
67 */
68 class SkinTemplate extends Skin {
69 /**#@+
70 * @private
71 */
72
73 /**
74 * Name of our skin, set in initPage()
75 * It probably need to be all lower case.
76 */
77 var $skinname;
78
79 /**
80 * Stylesheets set to use
81 * Sub directory in ./skins/ where various stylesheets are located
82 */
83 var $stylename;
84
85 /**
86 * For QuickTemplate, the name of the subclass which
87 * will actually fill the template.
88 */
89 var $template;
90
91 /**#@-*/
92
93 /**
94 * Setup the base parameters...
95 * Child classes should override this to set the name,
96 * style subdirectory, and template filler callback.
97 *
98 * @param OutputPage $out
99 */
100 function initPage( &$out ) {
101 parent::initPage( $out );
102 $this->skinname = 'monobook';
103 $this->stylename = 'monobook';
104 $this->template = 'QuickTemplate';
105 }
106
107 /**
108 * Create the template engine object; we feed it a bunch of data
109 * and eventually it spits out some HTML. Should have interface
110 * roughly equivalent to PHPTAL 0.7.
111 *
112 * @param string $callback (or file)
113 * @param string $repository subdirectory where we keep template files
114 * @param string $cache_dir
115 * @return object
116 * @private
117 */
118 function setupTemplate( $classname, $repository=false, $cache_dir=false ) {
119 return new $classname();
120 }
121
122 /**
123 * initialize various variables and generate the template
124 *
125 * @param OutputPage $out
126 * @public
127 */
128 function outputPage( &$out ) {
129 global $wgTitle, $wgArticle, $wgUser, $wgLang, $wgContLang, $wgOut;
130 global $wgScript, $wgStylePath, $wgContLanguageCode;
131 global $wgMimeType, $wgJsMimeType, $wgOutputEncoding, $wgRequest;
132 global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces;
133 global $wgDisableCounters, $wgLogo, $action, $wgFeedClasses, $wgHideInterlanguageLinks;
134 global $wgMaxCredits, $wgShowCreditsIfMax;
135 global $wgPageShowWatchingUsers;
136 global $wgUseTrackbacks;
137 global $wgArticlePath, $wgScriptPath, $wgServer, $wgLang, $wgCanonicalNamespaceNames;
138
139 $fname = 'SkinTemplate::outputPage';
140 wfProfileIn( $fname );
141
142 // Hook that allows last minute changes to the output page, e.g.
143 // adding of CSS or Javascript by extensions.
144 wfRunHooks( 'BeforePageDisplay', array( &$out ) );
145
146 $oldid = $wgRequest->getVal( 'oldid' );
147 $diff = $wgRequest->getVal( 'diff' );
148
149 wfProfileIn( "$fname-init" );
150 $this->initPage( $out );
151
152 $this->mTitle =& $wgTitle;
153 $this->mUser =& $wgUser;
154
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( "$fname-init" );
161
162 wfProfileIn( "$fname-stuff" );
163 $this->thispage = $this->mTitle->getPrefixedDbKey();
164 $this->thisurl = $this->mTitle->getPrefixedURL();
165 $this->loggedin = $wgUser->isLoggedIn();
166 $this->iscontent = ($this->mTitle->getNamespace() != NS_SPECIAL );
167 $this->iseditable = ($this->iscontent and !($action == 'edit' or $action == 'submit'));
168 $this->username = $wgUser->getName();
169 $userPage = $wgUser->getUserPage();
170 $this->userpage = $userPage->getPrefixedText();
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->usercss = $this->userjs = $this->userjsprev = false;
181 $this->setupUserCss();
182 $this->setupUserJs( $out->isUserJsAllowed() );
183 $this->titletxt = $this->mTitle->getPrefixedText();
184 wfProfileOut( "$fname-stuff" );
185
186 wfProfileIn( "$fname-stuff2" );
187 $tpl->set( 'title', $wgOut->getPageTitle() );
188 $tpl->set( 'pagetitle', $wgOut->getHTMLTitle() );
189 $tpl->set( 'displaytitle', $wgOut->mPageLinkTitle );
190 $tpl->set( 'pageclass', Sanitizer::escapeClass( 'page-'.$this->mTitle->getPrefixedText() ) );
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', $wgOut->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( $wgOut->isSyndicated() ) {
221 $feeds = array();
222 foreach( $wgFeedClasses as $format => $class ) {
223 $linktext = $format;
224 if ( $format == "atom" ) {
225 $linktext = wfMsg( 'feed-atom' );
226 } else if ( $format == "rss" ) {
227 $linktext = wfMsg( 'feed-rss' );
228 }
229 $feeds[$format] = array(
230 'text' => $linktext,
231 'href' => $wgRequest->appendQuery( "feed=$format" )
232 );
233 }
234 $tpl->setRef( 'feeds', $feeds );
235 } else {
236 $tpl->set( 'feeds', false );
237 }
238 if ($wgUseTrackbacks && $out->isArticleRelated()) {
239 $tpl->set( 'trackbackhtml', $wgTitle->trackbackRDF() );
240 } else {
241 $tpl->set( 'trackbackhtml', null );
242 }
243
244 $tpl->setRef( 'xhtmldefaultnamespace', $wgXhtmlDefaultNamespace );
245 $tpl->set( 'xhtmlnamespaces', $wgXhtmlNamespaces );
246 $tpl->setRef( 'mimetype', $wgMimeType );
247 $tpl->setRef( 'jsmimetype', $wgJsMimeType );
248 $tpl->setRef( 'charset', $wgOutputEncoding );
249 $tpl->set( 'headlinks', $out->getHeadLinks() );
250 $tpl->set('headscripts', $out->getScript() );
251 $tpl->setRef( 'wgScript', $wgScript );
252 $tpl->setRef( 'skinname', $this->skinname );
253 $tpl->set( 'skinclass', get_class( $this ) );
254 $tpl->setRef( 'stylename', $this->stylename );
255 $tpl->set( 'printable', $wgRequest->getBool( 'printable' ) );
256 $tpl->setRef( 'loggedin', $this->loggedin );
257 $tpl->set('nsclass', 'ns-'.$this->mTitle->getNamespace());
258 $tpl->set('notspecialpage', $this->mTitle->getNamespace() != NS_SPECIAL);
259 /* XXX currently unused, might get useful later
260 $tpl->set( "editable", ($this->mTitle->getNamespace() != NS_SPECIAL ) );
261 $tpl->set( "exists", $this->mTitle->getArticleID() != 0 );
262 $tpl->set( "watch", $this->mTitle->userIsWatching() ? "unwatch" : "watch" );
263 $tpl->set( "protect", count($this->mTitle->isProtected()) ? "unprotect" : "protect" );
264 $tpl->set( "helppage", wfMsg('helppage'));
265 */
266 $tpl->set( 'searchaction', $this->escapeSearchLink() );
267 $tpl->set( 'search', trim( $wgRequest->getVal( 'search' ) ) );
268 $tpl->setRef( 'stylepath', $wgStylePath );
269 $tpl->setRef( 'articlepath', $wgArticlePath );
270 $tpl->setRef( 'scriptpath', $wgScriptPath );
271 $tpl->setRef( 'serverurl', $wgServer );
272 $tpl->setRef( 'logopath', $wgLogo );
273 $tpl->setRef( "lang", $wgContLanguageCode );
274 $tpl->set( 'dir', $wgContLang->isRTL() ? "rtl" : "ltr" );
275 $tpl->set( 'rtl', $wgContLang->isRTL() );
276 $tpl->set( 'langname', $wgContLang->getLanguageName( $wgContLanguageCode ) );
277 $tpl->set( 'showjumplinks', $wgUser->getOption( 'showjumplinks' ) );
278 $tpl->set( 'username', $wgUser->isAnon() ? NULL : $this->username );
279 $tpl->setRef( 'userpage', $this->userpage);
280 $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href']);
281 $tpl->set( 'userlang', $wgLang->getCode() );
282 $tpl->set( 'pagecss', $this->setupPageCss() );
283 $tpl->setRef( 'usercss', $this->usercss);
284 $tpl->setRef( 'userjs', $this->userjs);
285 $tpl->setRef( 'userjsprev', $this->userjsprev);
286 global $wgUseSiteJs;
287 if ($wgUseSiteJs) {
288 $jsCache = $this->loggedin ? '&smaxage=0' : '';
289 $tpl->set( 'jsvarurl',
290 self::makeUrl('-',
291 "action=raw$jsCache&gen=js&useskin=" .
292 urlencode( $this->getSkinName() ) ) );
293 } else {
294 $tpl->set('jsvarurl', false);
295 }
296 $newtalks = $wgUser->getNewMessageLinks();
297
298 if (count($newtalks) == 1 && $newtalks[0]["wiki"] === wfWikiID() ) {
299 $usertitle = $this->mUser->getUserPage();
300 $usertalktitle = $usertitle->getTalkPage();
301 if( !$usertalktitle->equals( $this->mTitle ) ) {
302 $ntl = wfMsg( 'youhavenewmessages',
303 $this->makeKnownLinkObj(
304 $usertalktitle,
305 wfMsgHtml( 'newmessageslink' ),
306 'redirect=no'
307 ),
308 $this->makeKnownLinkObj(
309 $usertalktitle,
310 wfMsgHtml( 'newmessagesdifflink' ),
311 'diff=cur'
312 )
313 );
314 # Disable Cache
315 $wgOut->setSquidMaxage(0);
316 }
317 } else if (count($newtalks)) {
318 $sep = str_replace("_", " ", wfMsgHtml("newtalkseperator"));
319 $msgs = array();
320 foreach ($newtalks as $newtalk) {
321 $msgs[] = wfElement("a",
322 array('href' => $newtalk["link"]), $newtalk["wiki"]);
323 }
324 $parts = implode($sep, $msgs);
325 $ntl = wfMsgHtml('youhavenewmessagesmulti', $parts);
326 $wgOut->setSquidMaxage(0);
327 } else {
328 $ntl = '';
329 }
330 wfProfileOut( "$fname-stuff2" );
331
332 wfProfileIn( "$fname-stuff3" );
333 $tpl->setRef( 'newtalk', $ntl );
334 $tpl->setRef( 'skin', $this);
335 $tpl->set( 'logo', $this->logoText() );
336 if ( $wgOut->isArticle() and (!isset( $oldid ) or isset( $diff )) and
337 $wgArticle and 0 != $wgArticle->getID() )
338 {
339 if ( !$wgDisableCounters ) {
340 $viewcount = $wgLang->formatNum( $wgArticle->getCount() );
341 if ( $viewcount ) {
342 $tpl->set('viewcount', wfMsgExt( 'viewcount', array( 'parseinline' ), $viewcount ) );
343 } else {
344 $tpl->set('viewcount', false);
345 }
346 } else {
347 $tpl->set('viewcount', false);
348 }
349
350 if ($wgPageShowWatchingUsers) {
351 $dbr = wfGetDB( DB_SLAVE );
352 $watchlist = $dbr->tableName( 'watchlist' );
353 $sql = "SELECT COUNT(*) AS n FROM $watchlist
354 WHERE wl_title='" . $dbr->strencode($this->mTitle->getDBKey()) .
355 "' AND wl_namespace=" . $this->mTitle->getNamespace() ;
356 $res = $dbr->query( $sql, 'SkinTemplate::outputPage');
357 $x = $dbr->fetchObject( $res );
358 $numberofwatchingusers = $x->n;
359 if ($numberofwatchingusers > 0) {
360 $tpl->set('numberofwatchingusers',
361 wfMsgExt('number_of_watching_users_pageview', array('parseinline'),
362 $wgLang->formatNum($numberofwatchingusers))
363 );
364 } else {
365 $tpl->set('numberofwatchingusers', false);
366 }
367 } else {
368 $tpl->set('numberofwatchingusers', false);
369 }
370
371 $tpl->set('copyright',$this->getCopyright());
372
373 $this->credits = false;
374
375 if (isset($wgMaxCredits) && $wgMaxCredits != 0) {
376 require_once("Credits.php");
377 $this->credits = getCredits($wgArticle, $wgMaxCredits, $wgShowCreditsIfMax);
378 } else {
379 $tpl->set('lastmod', $this->lastModified());
380 }
381
382 $tpl->setRef( 'credits', $this->credits );
383
384 } elseif ( isset( $oldid ) && !isset( $diff ) ) {
385 $tpl->set('copyright', $this->getCopyright());
386 $tpl->set('viewcount', false);
387 $tpl->set('lastmod', false);
388 $tpl->set('credits', false);
389 $tpl->set('numberofwatchingusers', false);
390 } else {
391 $tpl->set('copyright', false);
392 $tpl->set('viewcount', false);
393 $tpl->set('lastmod', false);
394 $tpl->set('credits', false);
395 $tpl->set('numberofwatchingusers', false);
396 }
397 wfProfileOut( "$fname-stuff3" );
398
399 wfProfileIn( "$fname-stuff4" );
400 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
401 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
402 $tpl->set( 'disclaimer', $this->disclaimerLink() );
403 $tpl->set( 'privacy', $this->privacyLink() );
404 $tpl->set( 'about', $this->aboutLink() );
405
406 $tpl->setRef( 'debug', $out->mDebugtext );
407 $tpl->set( 'reporttime', $out->reportTime() );
408 $tpl->set( 'sitenotice', wfGetSiteNotice() );
409 $tpl->set( 'bottomscripts', $this->bottomScripts() );
410
411 $printfooter = "<div class=\"printfooter\">\n" . $this->printSource() . "</div>\n";
412 $out->mBodytext .= $printfooter ;
413 $tpl->setRef( 'bodytext', $out->mBodytext );
414
415 # Language links
416 $language_urls = array();
417
418 if ( !$wgHideInterlanguageLinks ) {
419 foreach( $wgOut->getLanguageLinks() as $l ) {
420 $tmp = explode( ':', $l, 2 );
421 $class = 'interwiki-' . $tmp[0];
422 unset($tmp);
423 $nt = Title::newFromText( $l );
424 $language_urls[] = array(
425 'href' => $nt->getFullURL(),
426 'text' => ($wgContLang->getLanguageName( $nt->getInterwiki()) != ''?$wgContLang->getLanguageName( $nt->getInterwiki()) : $l),
427 'class' => $class
428 );
429 }
430 }
431 if(count($language_urls)) {
432 $tpl->setRef( 'language_urls', $language_urls);
433 } else {
434 $tpl->set('language_urls', false);
435 }
436 wfProfileOut( "$fname-stuff4" );
437
438 # Personal toolbar
439 $tpl->set('personal_urls', $this->buildPersonalUrls());
440 $content_actions = $this->buildContentActionUrls();
441 $tpl->setRef('content_actions', $content_actions);
442
443 // XXX: attach this from javascript, same with section editing
444 if($this->iseditable && $wgUser->getOption("editondblclick") )
445 {
446 $encEditUrl = wfEscapeJsString( $this->mTitle->getLocalUrl( $this->editUrlOptions() ) );
447 $tpl->set('body_ondblclick', 'document.location = "' . $encEditUrl . '";');
448 } else {
449 $tpl->set('body_ondblclick', false);
450 }
451 if( $this->iseditable && $wgUser->getOption( 'editsectiononrightclick' ) ) {
452 $tpl->set( 'body_onload', 'setupRightClickEdit()' );
453 } else {
454 $tpl->set( 'body_onload', false );
455 }
456 $tpl->set( 'sidebar', $this->buildSidebar() );
457 $tpl->set( 'nav_urls', $this->buildNavUrls() );
458
459 // original version by hansm
460 if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
461 wfDebug( __METHOD__ . ': Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!' );
462 }
463
464 // execute template
465 wfProfileIn( "$fname-execute" );
466 $res = $tpl->execute();
467 wfProfileOut( "$fname-execute" );
468
469 // result may be an error
470 $this->printOrError( $res );
471 wfProfileOut( $fname );
472 }
473
474 /**
475 * Output the string, or print error message if it's
476 * an error object of the appropriate type.
477 * For the base class, assume strings all around.
478 *
479 * @param mixed $str
480 * @private
481 */
482 function printOrError( $str ) {
483 echo $str;
484 }
485
486 /**
487 * build array of urls for personal toolbar
488 * @return array
489 * @private
490 */
491 function buildPersonalUrls() {
492 global $wgTitle, $wgRequest;
493
494 $fname = 'SkinTemplate::buildPersonalUrls';
495 $pageurl = $wgTitle->getLocalURL();
496 wfProfileIn( $fname );
497
498 /* set up the default links for the personal toolbar */
499 $personal_urls = array();
500 if ($this->loggedin) {
501 $personal_urls['userpage'] = array(
502 'text' => $this->username,
503 'href' => &$this->userpageUrlDetails['href'],
504 'class' => $this->userpageUrlDetails['exists']?false:'new',
505 'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
506 );
507 $usertalkUrlDetails = $this->makeTalkUrlDetails($this->userpage);
508 $personal_urls['mytalk'] = array(
509 'text' => wfMsg('mytalk'),
510 'href' => &$usertalkUrlDetails['href'],
511 'class' => $usertalkUrlDetails['exists']?false:'new',
512 'active' => ( $usertalkUrlDetails['href'] == $pageurl )
513 );
514 $href = self::makeSpecialUrl( 'Preferences' );
515 $personal_urls['preferences'] = array(
516 'text' => wfMsg( 'mypreferences' ),
517 'href' => $href,
518 'active' => ( $href == $pageurl )
519 );
520 $href = self::makeSpecialUrl( 'Watchlist' );
521 $personal_urls['watchlist'] = array(
522 'text' => wfMsg( 'mywatchlist' ),
523 'href' => $href,
524 'active' => ( $href == $pageurl )
525 );
526
527 # We need to do an explicit check for Special:Contributions, as we
528 # have to match both the title, and the target (which could come
529 # from request values or be specified in "sub page" form. The plot
530 # thickens, because $wgTitle is altered for special pages, so doesn't
531 # contain the original alias-with-subpage.
532 $title = Title::newFromText( $wgRequest->getText( 'title' ) );
533 if( $title instanceof Title && $title->getNamespace() == NS_SPECIAL ) {
534 list( $spName, $spPar ) =
535 SpecialPage::resolveAliasWithSubpage( $title->getText() );
536 $active = $spName == 'Contributions'
537 && ( ( $spPar && $spPar == $this->username )
538 || $wgRequest->getText( 'target' ) == $this->username );
539 } else {
540 $active = false;
541 }
542
543 $href = self::makeSpecialUrlSubpage( 'Contributions', $this->username );
544 $personal_urls['mycontris'] = array(
545 'text' => wfMsg( 'mycontris' ),
546 'href' => $href,
547 'active' => $active
548 );
549 $personal_urls['logout'] = array(
550 'text' => wfMsg( 'userlogout' ),
551 'href' => self::makeSpecialUrl( 'Userlogout',
552 $wgTitle->isSpecial( 'Preferences' ) ? '' : "returnto={$this->thisurl}"
553 ),
554 'active' => false
555 );
556 } else {
557 if( $this->showIPinHeader() ) {
558 $href = &$this->userpageUrlDetails['href'];
559 $personal_urls['anonuserpage'] = array(
560 'text' => $this->username,
561 'href' => $href,
562 'class' => $this->userpageUrlDetails['exists']?false:'new',
563 'active' => ( $pageurl == $href )
564 );
565 $usertalkUrlDetails = $this->makeTalkUrlDetails($this->userpage);
566 $href = &$usertalkUrlDetails['href'];
567 $personal_urls['anontalk'] = array(
568 'text' => wfMsg('anontalk'),
569 'href' => $href,
570 'class' => $usertalkUrlDetails['exists']?false:'new',
571 'active' => ( $pageurl == $href )
572 );
573 $personal_urls['anonlogin'] = array(
574 'text' => wfMsg('userlogin'),
575 'href' => self::makeSpecialUrl( 'Userlogin', 'returnto=' . $this->thisurl ),
576 'active' => $wgTitle->isSpecial( 'Userlogin' )
577 );
578 } else {
579
580 $personal_urls['login'] = array(
581 'text' => wfMsg('userlogin'),
582 'href' => self::makeSpecialUrl( 'Userlogin', 'returnto=' . $this->thisurl ),
583 'active' => $wgTitle->isSpecial( 'Userlogin' )
584 );
585 }
586 }
587
588 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$wgTitle ) );
589 wfProfileOut( $fname );
590 return $personal_urls;
591 }
592
593 function tabAction( $title, $message, $selected, $query='', $checkEdit=false ) {
594 $classes = array();
595 if( $selected ) {
596 $classes[] = 'selected';
597 }
598 if( $checkEdit && !$title->isAlwaysKnown() && $title->getArticleId() == 0 ) {
599 $classes[] = 'new';
600 $query = 'action=edit';
601 }
602
603 $text = wfMsg( $message );
604 if ( wfEmptyMsg( $message, $text ) ) {
605 global $wgContLang;
606 $text = $wgContLang->getFormattedNsText( Namespace::getSubject( $title->getNamespace() ) );
607 }
608
609 $result = array();
610 if( !wfRunHooks('SkinTemplateTabAction', array(&$this,
611 $title, $message, $selected, $checkEdit,
612 &$classes, &$query, &$text, &$result)) ) {
613 return $result;
614 }
615
616 return array(
617 'class' => implode( ' ', $classes ),
618 'text' => $text,
619 'href' => $title->getLocalUrl( $query ) );
620 }
621
622 function makeTalkUrlDetails( $name, $urlaction = '' ) {
623 $title = Title::newFromText( $name );
624 if( !is_object($title) ) {
625 throw new MWException( __METHOD__." given invalid pagename $name" );
626 }
627 $title = $title->getTalkPage();
628 self::checkTitle( $title, $name );
629 return array(
630 'href' => $title->getLocalURL( $urlaction ),
631 'exists' => $title->getArticleID() != 0 ? true : false
632 );
633 }
634
635 function makeArticleUrlDetails( $name, $urlaction = '' ) {
636 $title = Title::newFromText( $name );
637 $title= $title->getSubjectPage();
638 self::checkTitle( $title, $name );
639 return array(
640 'href' => $title->getLocalURL( $urlaction ),
641 'exists' => $title->getArticleID() != 0 ? true : false
642 );
643 }
644
645 /**
646 * an array of edit links by default used for the tabs
647 * @return array
648 * @private
649 */
650 function buildContentActionUrls () {
651 global $wgContLang, $wgOut;
652 $fname = 'SkinTemplate::buildContentActionUrls';
653 wfProfileIn( $fname );
654
655 global $wgUser, $wgRequest;
656 $action = $wgRequest->getText( 'action' );
657 $section = $wgRequest->getText( 'section' );
658 $content_actions = array();
659
660 $prevent_active_tabs = false ;
661 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this , &$prevent_active_tabs ) ) ;
662
663 if( $this->iscontent ) {
664 $subjpage = $this->mTitle->getSubjectPage();
665 $talkpage = $this->mTitle->getTalkPage();
666
667 $nskey = $this->mTitle->getNamespaceKey();
668 $content_actions[$nskey] = $this->tabAction(
669 $subjpage,
670 $nskey,
671 !$this->mTitle->isTalkPage() && !$prevent_active_tabs,
672 '', true);
673
674 $content_actions['talk'] = $this->tabAction(
675 $talkpage,
676 'talk',
677 $this->mTitle->isTalkPage() && !$prevent_active_tabs,
678 '',
679 true);
680
681 wfProfileIn( "$fname-edit" );
682 if ( $this->mTitle->quickUserCan( 'edit' ) && ( $this->mTitle->exists() || $this->mTitle->quickUserCan( 'create' ) ) ) {
683 $istalk = $this->mTitle->isTalkPage();
684 $istalkclass = $istalk?' istalk':'';
685 $content_actions['edit'] = array(
686 'class' => ((($action == 'edit' or $action == 'submit') and $section != 'new') ? 'selected' : '').$istalkclass,
687 'text' => wfMsg('edit'),
688 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
689 );
690
691 if ( $istalk || $wgOut->showNewSectionLink() ) {
692 $content_actions['addsection'] = array(
693 'class' => $section == 'new'?'selected':false,
694 'text' => wfMsg('addsection'),
695 'href' => $this->mTitle->getLocalUrl( 'action=edit&section=new' )
696 );
697 }
698 } elseif ( $this->mTitle->exists() || $this->mTitle->isAlwaysKnown() ) {
699 $content_actions['viewsource'] = array(
700 'class' => ($action == 'edit') ? 'selected' : false,
701 'text' => wfMsg('viewsource'),
702 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
703 );
704 }
705 wfProfileOut( "$fname-edit" );
706
707 wfProfileIn( "$fname-live" );
708 if ( $this->mTitle->getArticleId() ) {
709
710 $content_actions['history'] = array(
711 'class' => ($action == 'history') ? 'selected' : false,
712 'text' => wfMsg('history_short'),
713 'href' => $this->mTitle->getLocalUrl( 'action=history')
714 );
715
716 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
717 if(!$this->mTitle->isProtected()){
718 $content_actions['protect'] = array(
719 'class' => ($action == 'protect') ? 'selected' : false,
720 'text' => wfMsg('protect'),
721 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
722 );
723
724 } else {
725 $content_actions['unprotect'] = array(
726 'class' => ($action == 'unprotect') ? 'selected' : false,
727 'text' => wfMsg('unprotect'),
728 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
729 );
730 }
731 }
732 if($wgUser->isAllowed('delete')){
733 $content_actions['delete'] = array(
734 'class' => ($action == 'delete') ? 'selected' : false,
735 'text' => wfMsg('delete'),
736 'href' => $this->mTitle->getLocalUrl( 'action=delete' )
737 );
738 }
739 if ( $this->mTitle->quickUserCan( 'move' ) ) {
740 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $this->thispage );
741 $content_actions['move'] = array(
742 'class' => $this->mTitle->isSpecial( 'Movepage' ) ? 'selected' : false,
743 'text' => wfMsg('move'),
744 'href' => $moveTitle->getLocalUrl()
745 );
746 }
747 } else {
748 //article doesn't exist or is deleted
749 if( $wgUser->isAllowed( 'delete' ) ) {
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' ), $n ),
755 'href' => $undelTitle->getLocalUrl( 'target=' . urlencode( $this->thispage ) )
756 #'href' => self::makeSpecialUrl( "Undelete/$this->thispage" )
757 );
758 }
759 }
760 }
761 wfProfileOut( "$fname-live" );
762
763 if( $this->loggedin ) {
764 if( !$this->mTitle->userIsWatching()) {
765 $content_actions['watch'] = array(
766 'class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : false,
767 'text' => wfMsg('watch'),
768 'href' => $this->mTitle->getLocalUrl( 'action=watch' )
769 );
770 } else {
771 $content_actions['unwatch'] = array(
772 'class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : false,
773 'text' => wfMsg('unwatch'),
774 'href' => $this->mTitle->getLocalUrl( 'action=unwatch' )
775 );
776 }
777 }
778
779
780 wfRunHooks( 'SkinTemplateTabs', array( &$this , &$content_actions ) ) ;
781 } else {
782 /* show special page tab */
783
784 $content_actions[$this->mTitle->getNamespaceKey()] = array(
785 'class' => 'selected',
786 'text' => wfMsg('nstab-special'),
787 'href' => $wgRequest->getRequestURL(), // @bug 2457, 2510
788 );
789
790 wfRunHooks( 'SkinTemplateBuildContentActionUrlsAfterSpecialPage', array( &$this, &$content_actions ) );
791 }
792
793 /* show links to different language variants */
794 global $wgDisableLangConversion;
795 $variants = $wgContLang->getVariants();
796 if( !$wgDisableLangConversion && sizeof( $variants ) > 1 ) {
797 $preferred = $wgContLang->getPreferredVariant();
798 $vcount=0;
799 foreach( $variants as $code ) {
800 $varname = $wgContLang->getVariantname( $code );
801 if( $varname == 'disable' )
802 continue;
803 $selected = ( $code == $preferred )? 'selected' : false;
804 $content_actions['varlang-' . $vcount] = array(
805 'class' => $selected,
806 'text' => $varname,
807 'href' => $this->mTitle->getLocalURL('',$code)
808 );
809 $vcount ++;
810 }
811 }
812
813 wfRunHooks( 'SkinTemplateContentActions', array( &$content_actions ) );
814
815 wfProfileOut( $fname );
816 return $content_actions;
817 }
818
819
820
821 /**
822 * build array of common navigation links
823 * @return array
824 * @private
825 */
826 function buildNavUrls () {
827 global $wgUseTrackbacks, $wgTitle, $wgArticle;
828
829 $fname = 'SkinTemplate::buildNavUrls';
830 wfProfileIn( $fname );
831
832 global $wgUser, $wgRequest;
833 global $wgEnableUploads, $wgUploadNavigationUrl;
834
835 $action = $wgRequest->getText( 'action' );
836
837 $nav_urls = array();
838 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
839 if( $wgEnableUploads ) {
840 if ($wgUploadNavigationUrl) {
841 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
842 } else {
843 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
844 }
845 } else {
846 if ($wgUploadNavigationUrl)
847 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
848 else
849 $nav_urls['upload'] = false;
850 }
851 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
852
853 // default permalink to being off, will override it as required below.
854 $nav_urls['permalink'] = false;
855
856 // A print stylesheet is attached to all pages, but nobody ever
857 // figures that out. :) Add a link...
858 if( $this->iscontent && ($action == '' || $action == 'view' || $action == 'purge' ) ) {
859 $nav_urls['print'] = array(
860 'text' => wfMsg( 'printableversion' ),
861 'href' => $wgRequest->appendQuery( 'printable=yes' )
862 );
863
864 // Also add a "permalink" while we're at it
865 if ( $this->mRevisionId ) {
866 $nav_urls['permalink'] = array(
867 'text' => wfMsg( 'permalink' ),
868 'href' => $wgTitle->getLocalURL( "oldid=$this->mRevisionId" )
869 );
870 }
871
872 // Copy in case this undocumented, shady hook tries to mess with internals
873 $revid = $this->mRevisionId;
874 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink', array( &$this, &$nav_urls, &$revid, &$revid ) );
875 }
876
877 if( $this->mTitle->getNamespace() != NS_SPECIAL ) {
878 $wlhTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage );
879 $nav_urls['whatlinkshere'] = array(
880 'href' => $wlhTitle->getLocalUrl()
881 );
882 if( $this->mTitle->getArticleId() ) {
883 $rclTitle = SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage );
884 $nav_urls['recentchangeslinked'] = array(
885 'href' => $rclTitle->getLocalUrl()
886 );
887 } else {
888 $nav_urls['recentchangeslinked'] = false;
889 }
890 if ($wgUseTrackbacks)
891 $nav_urls['trackbacklink'] = array(
892 'href' => $wgTitle->trackbackURL()
893 );
894 }
895
896 if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) {
897 $id = User::idFromName($this->mTitle->getText());
898 $ip = User::isIP($this->mTitle->getText());
899 } else {
900 $id = 0;
901 $ip = false;
902 }
903
904 if($id || $ip) { # both anons and non-anons have contribs list
905 $nav_urls['contributions'] = array(
906 'href' => self::makeSpecialUrlSubpage( 'Contributions', $this->mTitle->getText() )
907 );
908
909 if( $id ) {
910 $logPage = SpecialPage::getTitleFor( 'Log' );
911 $nav_urls['log'] = array( 'href' => $logPage->getLocalUrl( 'user='
912 . $this->mTitle->getPartialUrl() ) );
913 } else {
914 $nav_urls['log'] = false;
915 }
916
917 if ( $wgUser->isAllowed( 'block' ) ) {
918 $nav_urls['blockip'] = array(
919 'href' => self::makeSpecialUrlSubpage( 'Blockip', $this->mTitle->getText() )
920 );
921 } else {
922 $nav_urls['blockip'] = false;
923 }
924 } else {
925 $nav_urls['contributions'] = false;
926 $nav_urls['log'] = false;
927 $nav_urls['blockip'] = false;
928 }
929 $nav_urls['emailuser'] = false;
930 if( $this->showEmailUser( $id ) ) {
931 $nav_urls['emailuser'] = array(
932 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $this->mTitle->getText() )
933 );
934 }
935 wfProfileOut( $fname );
936 return $nav_urls;
937 }
938
939 /**
940 * Generate strings used for xml 'id' names
941 * @return string
942 * @private
943 */
944 function getNameSpaceKey () {
945 return $this->mTitle->getNamespaceKey();
946 }
947
948 /**
949 * @private
950 */
951 function setupUserCss() {
952 $fname = 'SkinTemplate::setupUserCss';
953 wfProfileIn( $fname );
954
955 global $wgRequest, $wgAllowUserCss, $wgUseSiteCss, $wgContLang, $wgSquidMaxage, $wgStylePath, $wgUser;
956
957 $sitecss = '';
958 $usercss = '';
959 $siteargs = '&maxage=' . $wgSquidMaxage;
960 if( $this->loggedin ) {
961 // Ensure that logged-in users' generated CSS isn't clobbered
962 // by anons' publicly cacheable generated CSS.
963 $siteargs .= '&smaxage=0';
964 }
965
966 # Add user-specific code if this is a user and we allow that kind of thing
967
968 if ( $wgAllowUserCss && $this->loggedin ) {
969 $action = $wgRequest->getText('action');
970
971 # if we're previewing the CSS page, use it
972 if( $this->mTitle->isCssSubpage() and $this->userCanPreview( $action ) ) {
973 $siteargs = "&smaxage=0&maxage=0";
974 $usercss = $wgRequest->getText('wpTextbox1');
975 } else {
976 $usercss = '@import "' .
977 self::makeUrl($this->userpage . '/'.$this->skinname.'.css',
978 'action=raw&ctype=text/css') . '";' ."\n";
979 }
980
981 $siteargs .= '&ts=' . $wgUser->mTouched;
982 }
983
984 if( $wgContLang->isRTL() ) {
985 global $wgStyleVersion;
986 $sitecss .= "@import \"$wgStylePath/$this->stylename/rtl.css?$wgStyleVersion\";\n";
987 }
988
989 # If we use the site's dynamic CSS, throw that in, too
990 if ( $wgUseSiteCss ) {
991 $query = "usemsgcache=yes&action=raw&ctype=text/css&smaxage=$wgSquidMaxage";
992 $skinquery = '';
993 if (($us = $wgRequest->getVal('useskin', '')) !== '')
994 $skinquery = "&useskin=$us";
995 $sitecss .= '@import "' . self::makeNSUrl( 'Common.css', $query, NS_MEDIAWIKI) . '";' . "\n";
996 $sitecss .= '@import "' . self::makeNSUrl( ucfirst( $this->skinname ) . '.css', $query, NS_MEDIAWIKI ) . '";' . "\n";
997 $sitecss .= '@import "' . self::makeUrl( '-', "action=raw&gen=css$siteargs$skinquery" ) . '";' . "\n";
998 }
999
1000 # If we use any dynamic CSS, make a little CDATA block out of it.
1001
1002 if ( !empty($sitecss) || !empty($usercss) ) {
1003 $this->usercss = "/*<![CDATA[*/\n" . $sitecss . $usercss . '/*]]>*/';
1004 }
1005 wfProfileOut( $fname );
1006 }
1007
1008 /**
1009 * @private
1010 */
1011 function setupUserJs( $allowUserJs ) {
1012 $fname = 'SkinTemplate::setupUserJs';
1013 wfProfileIn( $fname );
1014
1015 global $wgRequest, $wgJsMimeType;
1016 $action = $wgRequest->getText('action');
1017
1018 if( $allowUserJs && $this->loggedin ) {
1019 if( $this->mTitle->isJsSubpage() and $this->userCanPreview( $action ) ) {
1020 # XXX: additional security check/prompt?
1021 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText('wpTextbox1') . ' /*]]>*/';
1022 } else {
1023 $this->userjs = self::makeUrl($this->userpage.'/'.$this->skinname.'.js', 'action=raw&ctype='.$wgJsMimeType.'&dontcountme=s');
1024 }
1025 }
1026 wfProfileOut( $fname );
1027 }
1028
1029 /**
1030 * Code for extensions to hook into to provide per-page CSS, see
1031 * extensions/PageCSS/PageCSS.php for an implementation of this.
1032 *
1033 * @private
1034 */
1035 function setupPageCss() {
1036 $fname = 'SkinTemplate::setupPageCss';
1037 wfProfileIn( $fname );
1038 $out = false;
1039 wfRunHooks( 'SkinTemplateSetupPageCss', array( &$out ) );
1040
1041 wfProfileOut( $fname );
1042 return $out;
1043 }
1044
1045 /**
1046 * returns css with user-specific options
1047 * @public
1048 */
1049
1050 function getUserStylesheet() {
1051 $fname = 'SkinTemplate::getUserStylesheet';
1052 wfProfileIn( $fname );
1053
1054 $s = "/* generated user stylesheet */\n";
1055 $s .= $this->reallyDoGetUserStyles();
1056 wfProfileOut( $fname );
1057 return $s;
1058 }
1059
1060 /**
1061 * This returns MediaWiki:Common.js and MediaWiki:[Skinname].js concate-
1062 * nated together. For some bizarre reason, it does *not* return any
1063 * custom user JS from subpages. Huh?
1064 *
1065 * There's absolutely no reason to have separate Monobook/Common JSes.
1066 * Any JS that cares can just check the skin variable generated at the
1067 * top. For now Monobook.js will be maintained, but it should be consi-
1068 * dered deprecated.
1069 *
1070 * @return string
1071 */
1072 public function getUserJs() {
1073 $fname = 'SkinTemplate::getUserJs';
1074 wfProfileIn( $fname );
1075
1076 $s = parent::getUserJs();
1077 $s .= "\n\n/* MediaWiki:".ucfirst($this->skinname).".js (deprecated; migrate to Common.js!) */\n";
1078
1079 // avoid inclusion of non defined user JavaScript (with custom skins only)
1080 // by checking for default message content
1081 $msgKey = ucfirst($this->skinname).'.js';
1082 $userJS = wfMsgForContent($msgKey);
1083 if ( !wfEmptyMsg( $msgKey, $userJS ) ) {
1084 $s .= $userJS;
1085 }
1086
1087 wfProfileOut( $fname );
1088 return $s;
1089 }
1090 }
1091
1092 /**
1093 * Generic wrapper for template functions, with interface
1094 * compatible with what we use of PHPTAL 0.7.
1095 * @addtogroup Skins
1096 */
1097 class QuickTemplate {
1098 /**
1099 * @public
1100 */
1101 function QuickTemplate() {
1102 $this->data = array();
1103 $this->translator = new MediaWiki_I18N();
1104 }
1105
1106 /**
1107 * @public
1108 */
1109 function set( $name, $value ) {
1110 $this->data[$name] = $value;
1111 }
1112
1113 /**
1114 * @public
1115 */
1116 function setRef($name, &$value) {
1117 $this->data[$name] =& $value;
1118 }
1119
1120 /**
1121 * @public
1122 */
1123 function setTranslator( &$t ) {
1124 $this->translator = &$t;
1125 }
1126
1127 /**
1128 * @public
1129 */
1130 function execute() {
1131 echo "Override this function.";
1132 }
1133
1134
1135 /**
1136 * @private
1137 */
1138 function text( $str ) {
1139 echo htmlspecialchars( $this->data[$str] );
1140 }
1141
1142 /**
1143 * @private
1144 */
1145 function jstext( $str ) {
1146 echo Xml::escapeJsString( $this->data[$str] );
1147 }
1148
1149 /**
1150 * @private
1151 */
1152 function html( $str ) {
1153 echo $this->data[$str];
1154 }
1155
1156 /**
1157 * @private
1158 */
1159 function msg( $str ) {
1160 echo htmlspecialchars( $this->translator->translate( $str ) );
1161 }
1162
1163 /**
1164 * @private
1165 */
1166 function msgHtml( $str ) {
1167 echo $this->translator->translate( $str );
1168 }
1169
1170 /**
1171 * An ugly, ugly hack.
1172 * @private
1173 */
1174 function msgWiki( $str ) {
1175 global $wgParser, $wgTitle, $wgOut;
1176
1177 $text = $this->translator->translate( $str );
1178 $parserOutput = $wgParser->parse( $text, $wgTitle,
1179 $wgOut->parserOptions(), true );
1180 echo $parserOutput->getText();
1181 }
1182
1183 /**
1184 * @private
1185 */
1186 function haveData( $str ) {
1187 return isset( $this->data[$str] );
1188 }
1189
1190 /**
1191 * @private
1192 */
1193 function haveMsg( $str ) {
1194 $msg = $this->translator->translate( $str );
1195 return ($msg != '-') && ($msg != ''); # ????
1196 }
1197 }
1198