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