Cleanup
[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( $wgOut->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->setRef( 'wgScript', $wgScript );
245 $tpl->setRef( 'skinname', $this->skinname );
246 $tpl->set( 'skinclass', get_class( $this ) );
247 $tpl->setRef( 'stylename', $this->stylename );
248 $tpl->set( 'printable', $wgRequest->getBool( 'printable' ) );
249 $tpl->setRef( 'loggedin', $this->loggedin );
250 $tpl->set('nsclass', 'ns-'.$this->mTitle->getNamespace());
251 $tpl->set('notspecialpage', $this->mTitle->getNamespace() != NS_SPECIAL);
252 /* XXX currently unused, might get useful later
253 $tpl->set( "editable", ($this->mTitle->getNamespace() != NS_SPECIAL ) );
254 $tpl->set( "exists", $this->mTitle->getArticleID() != 0 );
255 $tpl->set( "watch", $this->mTitle->userIsWatching() ? "unwatch" : "watch" );
256 $tpl->set( "protect", count($this->mTitle->isProtected()) ? "unprotect" : "protect" );
257 $tpl->set( "helppage", wfMsg('helppage'));
258 */
259 $tpl->set( 'searchaction', $this->escapeSearchLink() );
260 $tpl->set( 'search', trim( $wgRequest->getVal( 'search' ) ) );
261 $tpl->setRef( 'stylepath', $wgStylePath );
262 $tpl->setRef( 'articlepath', $wgArticlePath );
263 $tpl->setRef( 'scriptpath', $wgScriptPath );
264 $tpl->setRef( 'serverurl', $wgServer );
265 $tpl->setRef( 'logopath', $wgLogo );
266 $tpl->setRef( "lang", $wgContLanguageCode );
267 $tpl->set( 'dir', $wgContLang->isRTL() ? "rtl" : "ltr" );
268 $tpl->set( 'rtl', $wgContLang->isRTL() );
269 $tpl->set( 'langname', $wgContLang->getLanguageName( $wgContLanguageCode ) );
270 $tpl->set( 'showjumplinks', $wgUser->getOption( 'showjumplinks' ) );
271 $tpl->set( 'username', $wgUser->isAnon() ? NULL : $this->username );
272 $tpl->setRef( 'userpage', $this->userpage);
273 $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href']);
274 $tpl->set( 'userlang', $wgLang->getCode() );
275 $tpl->set( 'pagecss', $this->setupPageCss() );
276 $tpl->setRef( 'usercss', $this->usercss);
277 $tpl->setRef( 'userjs', $this->userjs);
278 $tpl->setRef( 'userjsprev', $this->userjsprev);
279 global $wgUseSiteJs;
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 $wgOut->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 $wgOut->setSquidMaxage(0);
320 } else {
321 $ntl = '';
322 }
323 wfProfileOut( "$fname-stuff2" );
324
325 wfProfileIn( "$fname-stuff3" );
326 $tpl->setRef( 'newtalk', $ntl );
327 $tpl->setRef( 'skin', $this);
328 $tpl->set( 'logo', $this->logoText() );
329 if ( $wgOut->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 (isset($wgMaxCredits) && $wgMaxCredits != 0) {
369 require_once("Credits.php");
370 $this->credits = getCredits($wgArticle, $wgMaxCredits, $wgShowCreditsIfMax);
371 } else {
372 $tpl->set('lastmod', $this->lastModified());
373 }
374
375 $tpl->setRef( 'credits', $this->credits );
376
377 } elseif ( isset( $oldid ) && !isset( $diff ) ) {
378 $tpl->set('copyright', $this->getCopyright());
379 $tpl->set('viewcount', false);
380 $tpl->set('lastmod', false);
381 $tpl->set('credits', false);
382 $tpl->set('numberofwatchingusers', false);
383 } else {
384 $tpl->set('copyright', false);
385 $tpl->set('viewcount', false);
386 $tpl->set('lastmod', false);
387 $tpl->set('credits', false);
388 $tpl->set('numberofwatchingusers', false);
389 }
390 wfProfileOut( "$fname-stuff3" );
391
392 wfProfileIn( "$fname-stuff4" );
393 $tpl->set( 'copyrightico', $this->getCopyrightIcon() );
394 $tpl->set( 'poweredbyico', $this->getPoweredBy() );
395 $tpl->set( 'disclaimer', $this->disclaimerLink() );
396 $tpl->set( 'privacy', $this->privacyLink() );
397 $tpl->set( 'about', $this->aboutLink() );
398
399 $tpl->setRef( 'debug', $out->mDebugtext );
400 $tpl->set( 'reporttime', $out->reportTime() );
401 $tpl->set( 'sitenotice', wfGetSiteNotice() );
402 $tpl->set( 'bottomscripts', $this->bottomScripts() );
403
404 $printfooter = "<div class=\"printfooter\">\n" . $this->printSource() . "</div>\n";
405 $out->mBodytext .= $printfooter ;
406 $tpl->setRef( 'bodytext', $out->mBodytext );
407
408 # Language links
409 $language_urls = array();
410
411 if ( !$wgHideInterlanguageLinks ) {
412 foreach( $wgOut->getLanguageLinks() as $l ) {
413 $tmp = explode( ':', $l, 2 );
414 $class = 'interwiki-' . $tmp[0];
415 unset($tmp);
416 $nt = Title::newFromText( $l );
417 $language_urls[] = array(
418 'href' => $nt->getFullURL(),
419 'text' => ($wgContLang->getLanguageName( $nt->getInterwiki()) != ''?$wgContLang->getLanguageName( $nt->getInterwiki()) : $l),
420 'class' => $class
421 );
422 }
423 }
424 if(count($language_urls)) {
425 $tpl->setRef( 'language_urls', $language_urls);
426 } else {
427 $tpl->set('language_urls', false);
428 }
429 wfProfileOut( "$fname-stuff4" );
430
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 if( $this->iseditable && $wgUser->getOption( 'editsectiononrightclick' ) ) {
445 $tpl->set( 'body_onload', 'setupRightClickEdit()' );
446 } else {
447 $tpl->set( 'body_onload', false );
448 }
449 $tpl->set( 'sidebar', $this->buildSidebar() );
450 $tpl->set( 'nav_urls', $this->buildNavUrls() );
451
452 // original version by hansm
453 if( !wfRunHooks( 'SkinTemplateOutputPageBeforeExec', array( &$this, &$tpl ) ) ) {
454 wfDebug( __METHOD__ . ': Hook SkinTemplateOutputPageBeforeExec broke outputPage execution!' );
455 }
456
457 // execute template
458 wfProfileIn( "$fname-execute" );
459 $res = $tpl->execute();
460 wfProfileOut( "$fname-execute" );
461
462 // result may be an error
463 $this->printOrError( $res );
464 wfProfileOut( $fname );
465 }
466
467 /**
468 * Output the string, or print error message if it's
469 * an error object of the appropriate type.
470 * For the base class, assume strings all around.
471 *
472 * @param mixed $str
473 * @private
474 */
475 function printOrError( $str ) {
476 echo $str;
477 }
478
479 /**
480 * build array of urls for personal toolbar
481 * @return array
482 * @private
483 */
484 function buildPersonalUrls() {
485 global $wgTitle, $wgRequest;
486
487 $fname = 'SkinTemplate::buildPersonalUrls';
488 $pageurl = $wgTitle->getLocalURL();
489 wfProfileIn( $fname );
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 if( $this->showIPinHeader() ) {
551 $href = &$this->userpageUrlDetails['href'];
552 $personal_urls['anonuserpage'] = array(
553 'text' => $this->username,
554 'href' => $href,
555 'class' => $this->userpageUrlDetails['exists']?false:'new',
556 'active' => ( $pageurl == $href )
557 );
558 $usertalkUrlDetails = $this->makeTalkUrlDetails($this->userpage);
559 $href = &$usertalkUrlDetails['href'];
560 $personal_urls['anontalk'] = array(
561 'text' => wfMsg('anontalk'),
562 'href' => $href,
563 'class' => $usertalkUrlDetails['exists']?false:'new',
564 'active' => ( $pageurl == $href )
565 );
566 $personal_urls['anonlogin'] = array(
567 'text' => wfMsg('userlogin'),
568 'href' => self::makeSpecialUrl( 'Userlogin', 'returnto=' . $this->thisurl ),
569 'active' => $wgTitle->isSpecial( 'Userlogin' )
570 );
571 } else {
572
573 $personal_urls['login'] = array(
574 'text' => wfMsg('userlogin'),
575 'href' => self::makeSpecialUrl( 'Userlogin', 'returnto=' . $this->thisurl ),
576 'active' => $wgTitle->isSpecial( 'Userlogin' )
577 );
578 }
579 }
580
581 wfRunHooks( 'PersonalUrls', array( &$personal_urls, &$wgTitle ) );
582 wfProfileOut( $fname );
583 return $personal_urls;
584 }
585
586 function tabAction( $title, $message, $selected, $query='', $checkEdit=false ) {
587 $classes = array();
588 if( $selected ) {
589 $classes[] = 'selected';
590 }
591 if( $checkEdit && !$title->isAlwaysKnown() && $title->getArticleId() == 0 ) {
592 $classes[] = 'new';
593 $query = 'action=edit';
594 }
595
596 $text = wfMsg( $message );
597 if ( wfEmptyMsg( $message, $text ) ) {
598 global $wgContLang;
599 $text = $wgContLang->getFormattedNsText( Namespace::getSubject( $title->getNamespace() ) );
600 }
601
602 $result = array();
603 if( !wfRunHooks('SkinTemplateTabAction', array(&$this,
604 $title, $message, $selected, $checkEdit,
605 &$classes, &$query, &$text, &$result)) ) {
606 return $result;
607 }
608
609 return array(
610 'class' => implode( ' ', $classes ),
611 'text' => $text,
612 'href' => $title->getLocalUrl( $query ) );
613 }
614
615 function makeTalkUrlDetails( $name, $urlaction = '' ) {
616 $title = Title::newFromText( $name );
617 if( !is_object($title) ) {
618 throw new MWException( __METHOD__." given invalid pagename $name" );
619 }
620 $title = $title->getTalkPage();
621 self::checkTitle( $title, $name );
622 return array(
623 'href' => $title->getLocalURL( $urlaction ),
624 'exists' => $title->getArticleID() != 0 ? true : false
625 );
626 }
627
628 function makeArticleUrlDetails( $name, $urlaction = '' ) {
629 $title = Title::newFromText( $name );
630 $title= $title->getSubjectPage();
631 self::checkTitle( $title, $name );
632 return array(
633 'href' => $title->getLocalURL( $urlaction ),
634 'exists' => $title->getArticleID() != 0 ? true : false
635 );
636 }
637
638 /**
639 * an array of edit links by default used for the tabs
640 * @return array
641 * @private
642 */
643 function buildContentActionUrls () {
644 global $wgContLang, $wgLang, $wgOut;
645 $fname = 'SkinTemplate::buildContentActionUrls';
646 wfProfileIn( $fname );
647
648 global $wgUser, $wgRequest;
649 $action = $wgRequest->getText( 'action' );
650 $section = $wgRequest->getText( 'section' );
651 $content_actions = array();
652
653 $prevent_active_tabs = false ;
654 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this , &$prevent_active_tabs ) ) ;
655
656 if( $this->iscontent ) {
657 $subjpage = $this->mTitle->getSubjectPage();
658 $talkpage = $this->mTitle->getTalkPage();
659
660 $nskey = $this->mTitle->getNamespaceKey();
661 $content_actions[$nskey] = $this->tabAction(
662 $subjpage,
663 $nskey,
664 !$this->mTitle->isTalkPage() && !$prevent_active_tabs,
665 '', true);
666
667 $content_actions['talk'] = $this->tabAction(
668 $talkpage,
669 'talk',
670 $this->mTitle->isTalkPage() && !$prevent_active_tabs,
671 '',
672 true);
673
674 wfProfileIn( "$fname-edit" );
675 if ( $this->mTitle->quickUserCan( 'edit' ) && ( $this->mTitle->exists() || $this->mTitle->quickUserCan( 'create' ) ) ) {
676 $istalk = $this->mTitle->isTalkPage();
677 $istalkclass = $istalk?' istalk':'';
678 $content_actions['edit'] = array(
679 'class' => ((($action == 'edit' or $action == 'submit') and $section != 'new') ? 'selected' : '').$istalkclass,
680 'text' => wfMsg('edit'),
681 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
682 );
683
684 if ( $istalk || $wgOut->showNewSectionLink() ) {
685 $content_actions['addsection'] = array(
686 'class' => $section == 'new'?'selected':false,
687 'text' => wfMsg('addsection'),
688 'href' => $this->mTitle->getLocalUrl( 'action=edit&section=new' )
689 );
690 }
691 } elseif ( $this->mTitle->exists() || $this->mTitle->isAlwaysKnown() ) {
692 $content_actions['viewsource'] = array(
693 'class' => ($action == 'edit') ? 'selected' : false,
694 'text' => wfMsg('viewsource'),
695 'href' => $this->mTitle->getLocalUrl( $this->editUrlOptions() )
696 );
697 }
698 wfProfileOut( "$fname-edit" );
699
700 wfProfileIn( "$fname-live" );
701 if ( $this->mTitle->getArticleId() ) {
702
703 $content_actions['history'] = array(
704 'class' => ($action == 'history') ? 'selected' : false,
705 'text' => wfMsg('history_short'),
706 'href' => $this->mTitle->getLocalUrl( 'action=history')
707 );
708
709 if($wgUser->isAllowed('delete')){
710 $content_actions['delete'] = array(
711 'class' => ($action == 'delete') ? 'selected' : false,
712 'text' => wfMsg('delete'),
713 'href' => $this->mTitle->getLocalUrl( 'action=delete' )
714 );
715 }
716 if ( $this->mTitle->quickUserCan( 'move' ) ) {
717 $moveTitle = SpecialPage::getTitleFor( 'Movepage', $this->thispage );
718 $content_actions['move'] = array(
719 'class' => $this->mTitle->isSpecial( 'Movepage' ) ? 'selected' : false,
720 'text' => wfMsg('move'),
721 'href' => $moveTitle->getLocalUrl()
722 );
723 }
724
725 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
726 if(!$this->mTitle->isProtected()){
727 $content_actions['protect'] = array(
728 'class' => ($action == 'protect') ? 'selected' : false,
729 'text' => wfMsg('protect'),
730 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
731 );
732
733 } else {
734 $content_actions['unprotect'] = array(
735 'class' => ($action == 'unprotect') ? 'selected' : false,
736 'text' => wfMsg('unprotect'),
737 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
738 );
739 }
740 }
741 } else {
742 //article doesn't exist or is deleted
743 if( $wgUser->isAllowed( 'deletedhistory' ) && $wgUser->isAllowed( 'undelete' ) ) {
744 if( $n = $this->mTitle->isDeleted() ) {
745 $undelTitle = SpecialPage::getTitleFor( 'Undelete' );
746 $content_actions['undelete'] = array(
747 'class' => false,
748 'text' => wfMsgExt( 'undelete_short', array( 'parsemag' ), $wgLang->formatNum($n) ),
749 'href' => $undelTitle->getLocalUrl( 'target=' . urlencode( $this->thispage ) )
750 #'href' => self::makeSpecialUrl( "Undelete/$this->thispage" )
751 );
752 }
753 }
754
755 if ( $this->mTitle->getNamespace() !== NS_MEDIAWIKI && $wgUser->isAllowed( 'protect' ) ) {
756 if( !$this->mTitle->getRestrictions( 'create' ) ) {
757 $content_actions['protect'] = array(
758 'class' => ($action == 'protect') ? 'selected' : false,
759 'text' => wfMsg('protect'),
760 'href' => $this->mTitle->getLocalUrl( 'action=protect' )
761 );
762
763 } else {
764 $content_actions['unprotect'] = array(
765 'class' => ($action == 'unprotect') ? 'selected' : false,
766 'text' => wfMsg('unprotect'),
767 'href' => $this->mTitle->getLocalUrl( 'action=unprotect' )
768 );
769 }
770 }
771 }
772
773 wfProfileOut( "$fname-live" );
774
775 if( $this->loggedin ) {
776 if( !$this->mTitle->userIsWatching()) {
777 $content_actions['watch'] = array(
778 'class' => ($action == 'watch' or $action == 'unwatch') ? 'selected' : false,
779 'text' => wfMsg('watch'),
780 'href' => $this->mTitle->getLocalUrl( 'action=watch' )
781 );
782 } else {
783 $content_actions['unwatch'] = array(
784 'class' => ($action == 'unwatch' or $action == 'watch') ? 'selected' : false,
785 'text' => wfMsg('unwatch'),
786 'href' => $this->mTitle->getLocalUrl( 'action=unwatch' )
787 );
788 }
789 }
790
791
792 wfRunHooks( 'SkinTemplateTabs', array( &$this , &$content_actions ) ) ;
793 } else {
794 /* show special page tab */
795
796 $content_actions[$this->mTitle->getNamespaceKey()] = array(
797 'class' => 'selected',
798 'text' => wfMsg('nstab-special'),
799 'href' => $wgRequest->getRequestURL(), // @bug 2457, 2510
800 );
801
802 wfRunHooks( 'SkinTemplateBuildContentActionUrlsAfterSpecialPage', array( &$this, &$content_actions ) );
803 }
804
805 /* show links to different language variants */
806 global $wgDisableLangConversion;
807 $variants = $wgContLang->getVariants();
808 if( !$wgDisableLangConversion && sizeof( $variants ) > 1 ) {
809 $preferred = $wgContLang->getPreferredVariant();
810 $vcount=0;
811 foreach( $variants as $code ) {
812 $varname = $wgContLang->getVariantname( $code );
813 if( $varname == 'disable' )
814 continue;
815 $selected = ( $code == $preferred )? 'selected' : false;
816 $content_actions['varlang-' . $vcount] = array(
817 'class' => $selected,
818 'text' => $varname,
819 'href' => $this->mTitle->getLocalURL('',$code)
820 );
821 $vcount ++;
822 }
823 }
824
825 wfRunHooks( 'SkinTemplateContentActions', array( &$content_actions ) );
826
827 wfProfileOut( $fname );
828 return $content_actions;
829 }
830
831
832
833 /**
834 * build array of common navigation links
835 * @return array
836 * @private
837 */
838 function buildNavUrls () {
839 global $wgUseTrackbacks, $wgTitle, $wgArticle;
840
841 $fname = 'SkinTemplate::buildNavUrls';
842 wfProfileIn( $fname );
843
844 global $wgUser, $wgRequest;
845 global $wgEnableUploads, $wgUploadNavigationUrl;
846
847 $action = $wgRequest->getText( 'action' );
848
849 $nav_urls = array();
850 $nav_urls['mainpage'] = array( 'href' => self::makeMainPageUrl() );
851 if( $wgEnableUploads ) {
852 if ($wgUploadNavigationUrl) {
853 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
854 } else {
855 $nav_urls['upload'] = array( 'href' => self::makeSpecialUrl( 'Upload' ) );
856 }
857 } else {
858 if ($wgUploadNavigationUrl)
859 $nav_urls['upload'] = array( 'href' => $wgUploadNavigationUrl );
860 else
861 $nav_urls['upload'] = false;
862 }
863 $nav_urls['specialpages'] = array( 'href' => self::makeSpecialUrl( 'Specialpages' ) );
864
865 // default permalink to being off, will override it as required below.
866 $nav_urls['permalink'] = false;
867
868 // A print stylesheet is attached to all pages, but nobody ever
869 // figures that out. :) Add a link...
870 if( $this->iscontent && ($action == '' || $action == 'view' || $action == 'purge' ) ) {
871 $nav_urls['print'] = array(
872 'text' => wfMsg( 'printableversion' ),
873 'href' => $wgRequest->appendQuery( 'printable=yes' )
874 );
875
876 // Also add a "permalink" while we're at it
877 if ( $this->mRevisionId ) {
878 $nav_urls['permalink'] = array(
879 'text' => wfMsg( 'permalink' ),
880 'href' => $wgTitle->getLocalURL( "oldid=$this->mRevisionId" )
881 );
882 }
883
884 // Copy in case this undocumented, shady hook tries to mess with internals
885 $revid = $this->mRevisionId;
886 wfRunHooks( 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink', array( &$this, &$nav_urls, &$revid, &$revid ) );
887 }
888
889 if( $this->mTitle->getNamespace() != NS_SPECIAL ) {
890 $wlhTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->thispage );
891 $nav_urls['whatlinkshere'] = array(
892 'href' => $wlhTitle->getLocalUrl()
893 );
894 if( $this->mTitle->getArticleId() ) {
895 $rclTitle = SpecialPage::getTitleFor( 'Recentchangeslinked', $this->thispage );
896 $nav_urls['recentchangeslinked'] = array(
897 'href' => $rclTitle->getLocalUrl()
898 );
899 } else {
900 $nav_urls['recentchangeslinked'] = false;
901 }
902 if ($wgUseTrackbacks)
903 $nav_urls['trackbacklink'] = array(
904 'href' => $wgTitle->trackbackURL()
905 );
906 }
907
908 if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) {
909 $id = User::idFromName($this->mTitle->getText());
910 $ip = User::isIP($this->mTitle->getText());
911 } else {
912 $id = 0;
913 $ip = false;
914 }
915
916 if($id || $ip) { # both anons and non-anons have contribs list
917 $nav_urls['contributions'] = array(
918 'href' => self::makeSpecialUrlSubpage( 'Contributions', $this->mTitle->getText() )
919 );
920
921 if( $id ) {
922 $logPage = SpecialPage::getTitleFor( 'Log' );
923 $nav_urls['log'] = array( 'href' => $logPage->getLocalUrl( 'user='
924 . $this->mTitle->getPartialUrl() ) );
925 } else {
926 $nav_urls['log'] = false;
927 }
928
929 if ( $wgUser->isAllowed( 'block' ) ) {
930 $nav_urls['blockip'] = array(
931 'href' => self::makeSpecialUrlSubpage( 'Blockip', $this->mTitle->getText() )
932 );
933 } else {
934 $nav_urls['blockip'] = false;
935 }
936 } else {
937 $nav_urls['contributions'] = false;
938 $nav_urls['log'] = false;
939 $nav_urls['blockip'] = false;
940 }
941 $nav_urls['emailuser'] = false;
942 if( $this->showEmailUser( $id ) ) {
943 $nav_urls['emailuser'] = array(
944 'href' => self::makeSpecialUrlSubpage( 'Emailuser', $this->mTitle->getText() )
945 );
946 }
947 wfProfileOut( $fname );
948 return $nav_urls;
949 }
950
951 /**
952 * Generate strings used for xml 'id' names
953 * @return string
954 * @private
955 */
956 function getNameSpaceKey () {
957 return $this->mTitle->getNamespaceKey();
958 }
959
960 /**
961 * @private
962 */
963 function setupUserCss() {
964 $fname = 'SkinTemplate::setupUserCss';
965 wfProfileIn( $fname );
966
967 global $wgRequest, $wgAllowUserCss, $wgUseSiteCss, $wgContLang, $wgSquidMaxage, $wgStylePath, $wgUser;
968
969 $sitecss = '';
970 $usercss = '';
971 $siteargs = '&maxage=' . $wgSquidMaxage;
972 if( $this->loggedin ) {
973 // Ensure that logged-in users' generated CSS isn't clobbered
974 // by anons' publicly cacheable generated CSS.
975 $siteargs .= '&smaxage=0';
976 }
977
978 # Add user-specific code if this is a user and we allow that kind of thing
979
980 if ( $wgAllowUserCss && $this->loggedin ) {
981 $action = $wgRequest->getText('action');
982
983 # if we're previewing the CSS page, use it
984 if( $this->mTitle->isCssSubpage() and $this->userCanPreview( $action ) ) {
985 $siteargs = "&smaxage=0&maxage=0";
986 $usercss = $wgRequest->getText('wpTextbox1');
987 } else {
988 $usercss = '@import "' .
989 self::makeUrl($this->userpage . '/'.$this->skinname.'.css',
990 'action=raw&ctype=text/css') . '";' ."\n";
991 }
992
993 $siteargs .= '&ts=' . $wgUser->mTouched;
994 }
995
996 if( $wgContLang->isRTL() ) {
997 global $wgStyleVersion;
998 $sitecss .= "@import \"$wgStylePath/$this->stylename/rtl.css?$wgStyleVersion\";\n";
999 }
1000
1001 # If we use the site's dynamic CSS, throw that in, too
1002 if ( $wgUseSiteCss ) {
1003 $query = "usemsgcache=yes&action=raw&ctype=text/css&smaxage=$wgSquidMaxage";
1004 $skinquery = '';
1005 if (($us = $wgRequest->getVal('useskin', '')) !== '')
1006 $skinquery = "&useskin=$us";
1007 $sitecss .= '@import "' . self::makeNSUrl( 'Common.css', $query, NS_MEDIAWIKI) . '";' . "\n";
1008 $sitecss .= '@import "' . self::makeNSUrl( ucfirst( $this->skinname ) . '.css', $query, NS_MEDIAWIKI ) . '";' . "\n";
1009 $sitecss .= '@import "' . self::makeUrl( '-', "action=raw&gen=css$siteargs$skinquery" ) . '";' . "\n";
1010 }
1011
1012 # If we use any dynamic CSS, make a little CDATA block out of it.
1013
1014 if ( !empty($sitecss) || !empty($usercss) ) {
1015 $this->usercss = "/*<![CDATA[*/\n" . $sitecss . $usercss . '/*]]>*/';
1016 }
1017 wfProfileOut( $fname );
1018 }
1019
1020 /**
1021 * @private
1022 */
1023 function setupUserJs( $allowUserJs ) {
1024 $fname = 'SkinTemplate::setupUserJs';
1025 wfProfileIn( $fname );
1026
1027 global $wgRequest, $wgJsMimeType;
1028 $action = $wgRequest->getText('action');
1029
1030 if( $allowUserJs && $this->loggedin ) {
1031 if( $this->mTitle->isJsSubpage() and $this->userCanPreview( $action ) ) {
1032 # XXX: additional security check/prompt?
1033 $this->userjsprev = '/*<![CDATA[*/ ' . $wgRequest->getText('wpTextbox1') . ' /*]]>*/';
1034 } else {
1035 $this->userjs = self::makeUrl($this->userpage.'/'.$this->skinname.'.js', 'action=raw&ctype='.$wgJsMimeType.'&dontcountme=s');
1036 }
1037 }
1038 wfProfileOut( $fname );
1039 }
1040
1041 /**
1042 * Code for extensions to hook into to provide per-page CSS, see
1043 * extensions/PageCSS/PageCSS.php for an implementation of this.
1044 *
1045 * @private
1046 */
1047 function setupPageCss() {
1048 $fname = 'SkinTemplate::setupPageCss';
1049 wfProfileIn( $fname );
1050 $out = false;
1051 wfRunHooks( 'SkinTemplateSetupPageCss', array( &$out ) );
1052
1053 wfProfileOut( $fname );
1054 return $out;
1055 }
1056
1057 /**
1058 * returns css with user-specific options
1059 * @public
1060 */
1061
1062 function getUserStylesheet() {
1063 $fname = 'SkinTemplate::getUserStylesheet';
1064 wfProfileIn( $fname );
1065
1066 $s = "/* generated user stylesheet */\n";
1067 $s .= $this->reallyDoGetUserStyles();
1068 wfProfileOut( $fname );
1069 return $s;
1070 }
1071
1072 /**
1073 * This returns MediaWiki:Common.js and MediaWiki:[Skinname].js concate-
1074 * nated together. For some bizarre reason, it does *not* return any
1075 * custom user JS from subpages. Huh?
1076 *
1077 * There's absolutely no reason to have separate Monobook/Common JSes.
1078 * Any JS that cares can just check the skin variable generated at the
1079 * top. For now Monobook.js will be maintained, but it should be consi-
1080 * dered deprecated.
1081 *
1082 * @return string
1083 */
1084 public function getUserJs() {
1085 $fname = 'SkinTemplate::getUserJs';
1086 wfProfileIn( $fname );
1087
1088 $s = parent::getUserJs();
1089 $s .= "\n\n/* MediaWiki:".ucfirst($this->skinname).".js (deprecated; migrate to Common.js!) */\n";
1090
1091 // avoid inclusion of non defined user JavaScript (with custom skins only)
1092 // by checking for default message content
1093 $msgKey = ucfirst($this->skinname).'.js';
1094 $userJS = wfMsgForContent($msgKey);
1095 if ( !wfEmptyMsg( $msgKey, $userJS ) ) {
1096 $s .= $userJS;
1097 }
1098
1099 wfProfileOut( $fname );
1100 return $s;
1101 }
1102 }
1103
1104 /**
1105 * Generic wrapper for template functions, with interface
1106 * compatible with what we use of PHPTAL 0.7.
1107 * @addtogroup Skins
1108 */
1109 class QuickTemplate {
1110 /**
1111 * @public
1112 */
1113 function QuickTemplate() {
1114 $this->data = array();
1115 $this->translator = new MediaWiki_I18N();
1116 }
1117
1118 /**
1119 * @public
1120 */
1121 function set( $name, $value ) {
1122 $this->data[$name] = $value;
1123 }
1124
1125 /**
1126 * @public
1127 */
1128 function setRef($name, &$value) {
1129 $this->data[$name] =& $value;
1130 }
1131
1132 /**
1133 * @public
1134 */
1135 function setTranslator( &$t ) {
1136 $this->translator = &$t;
1137 }
1138
1139 /**
1140 * @public
1141 */
1142 function execute() {
1143 echo "Override this function.";
1144 }
1145
1146
1147 /**
1148 * @private
1149 */
1150 function text( $str ) {
1151 echo htmlspecialchars( $this->data[$str] );
1152 }
1153
1154 /**
1155 * @private
1156 */
1157 function jstext( $str ) {
1158 echo Xml::escapeJsString( $this->data[$str] );
1159 }
1160
1161 /**
1162 * @private
1163 */
1164 function html( $str ) {
1165 echo $this->data[$str];
1166 }
1167
1168 /**
1169 * @private
1170 */
1171 function msg( $str ) {
1172 echo htmlspecialchars( $this->translator->translate( $str ) );
1173 }
1174
1175 /**
1176 * @private
1177 */
1178 function msgHtml( $str ) {
1179 echo $this->translator->translate( $str );
1180 }
1181
1182 /**
1183 * An ugly, ugly hack.
1184 * @private
1185 */
1186 function msgWiki( $str ) {
1187 global $wgParser, $wgTitle, $wgOut;
1188
1189 $text = $this->translator->translate( $str );
1190 $parserOutput = $wgParser->parse( $text, $wgTitle,
1191 $wgOut->parserOptions(), true );
1192 echo $parserOutput->getText();
1193 }
1194
1195 /**
1196 * @private
1197 */
1198 function haveData( $str ) {
1199 return isset( $this->data[$str] );
1200 }
1201
1202 /**
1203 * @private
1204 */
1205 function haveMsg( $str ) {
1206 $msg = $this->translator->translate( $str );
1207 return ($msg != '-') && ($msg != ''); # ????
1208 }
1209 }
1210
1211
1212
1213