fixed error reporting, fixed error
[lhc/web/wiklou.git] / skins / disabled / MonoBookCBT.php
1 <?php
2
3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
5 }
6
7 require_once( 'includes/cbt/CBTProcessor.php' );
8 require_once( 'includes/cbt/CBTCompiler.php' );
9 require_once( 'includes/SkinTemplate.php' );
10
11 /**
12 * MonoBook clone using the new dependency-tracking template processor.
13 * EXPERIMENTAL - use only for testing and profiling at this stage
14 *
15 * The main thing that's missing is cache invalidation, on change of:
16 * * messages
17 * * user preferences
18 * * source template
19 * * source code and configuration files
20 *
21 * The other thing is that lots of dependencies that are declared in the callbacks
22 * are not intelligently handled. There's some room for improvement there.
23 *
24 * The class is derived from SkinTemplate, but that's only temporary. Eventually
25 * it'll be derived from Skin, and I've avoided using SkinTemplate functions as
26 * much as possible. In fact, the only SkinTemplate dependencies I know of at the
27 * moment are the functions to generate the gen=css and gen=js files.
28 *
29 */
30 class SkinMonoBookCBT extends SkinTemplate {
31 var $mOut, $mTitle;
32 var $mStyleName = 'monobook';
33 var $mCompiling = false;
34
35 /******************************************************
36 * General functions *
37 ******************************************************/
38
39 /** Execute the template and write out the result */
40 function outputPage( &$out ) {
41 echo $this->execute( $out );
42 }
43
44 function execute( &$out ) {
45 global $wgTitle, $wgStyleDirectory, $wgParserCacheType;
46 $fname = 'SkinMonoBookCBT::execute';
47 wfProfileIn( $fname );
48 wfProfileIn( "$fname-setup" );
49 Skin::initPage( $out );
50
51 $this->mOut =& $out;
52 $this->mTitle =& $wgTitle;
53
54 $sourceFile = "$wgStyleDirectory/MonoBook.tpl";
55
56 wfProfileOut( "$fname-setup" );
57
58 if ( $wgParserCacheType == CACHE_NONE ) {
59 $template = file_get_contents( $sourceFile );
60 $text = $this->executeTemplate( $template );
61 } else {
62 $compiled = $this->getCompiledTemplate( $sourceFile );
63
64 wfProfileIn( "$fname-eval" );
65 $text = eval( $compiled );
66 wfProfileOut( "$fname-eval" );
67 }
68 wfProfileOut( $fname );
69 return $text;
70 }
71
72 function getCompiledTemplate( $sourceFile ) {
73 global $wgDBname, $wgMemc, $wgRequest, $wgUser, $parserMemc;
74 $fname = 'SkinMonoBookCBT::getCompiledTemplate';
75
76 // Sandbox template execution
77 if ( $this->mCompiling ) {
78 return;
79 }
80
81 wfProfileIn( $fname );
82
83 // Is the request an ordinary page view?
84 if ( $wgRequest->wasPosted() ||
85 count( array_diff( array_keys( $_GET ), array( 'title', 'useskin', 'recompile' ) ) ) != 0 )
86 {
87 $type = 'nonview';
88 } else {
89 $type = 'view';
90 }
91
92 // Per-user compiled template
93 // Put all logged-out users on the same cache key
94 $cacheKey = "$wgDBname:monobookcbt:$type:" . $wgUser->getId();
95
96 $recompile = $wgRequest->getVal( 'recompile' );
97 if ( !$recompile ) {
98 $php = $parserMemc->get( $cacheKey );
99 }
100 if ( $recompile || !$php ) {
101 $template = file_get_contents( $sourceFile );
102
103 $ignore = array( 'lang', 'loggedin', 'user' );
104 if ( $wgUser->isLoggedIn() ) {
105 $ignore[] = '!loggedin dynamic';
106 } else {
107 $ignore[] = 'loggedin dynamic';
108 }
109 if ( $type == 'view' ) {
110 $ignore[] = 'nonview dynamic';
111 }
112 $tp = new CBTProcessor( $template, $this, $ignore );
113 $this->mCompiling = true;
114 $compiled = $tp->compile();
115 $this->mCompiling = false;
116 if ( $tp->getLastError() ) {
117 // If there was a compile error, don't save the template
118 // Instead just print the error and exit
119 echo $compiled;
120 wfErrorExit();
121 }
122
123 // Reduce whitespace
124 // This is done here instead of in CBTProcessor because we can be
125 // more sure it is safe here.
126 $compiled = preg_replace( '/^[ \t]+/m', '', $compiled );
127 $compiled = preg_replace( '/[\r\n]+/', "\n", $compiled );
128
129 // Compile to PHP
130 $compiler = new CBTCompiler( $compiled );
131 $ret = $compiler->compile();
132 if ( $ret !== true ) {
133 echo $ret;
134 wfErrorExit();
135 }
136 $php = $compiler->generatePHP( '$this' );
137
138 $parserMemc->set( $cacheKey, $php, 3600 );
139 }
140 wfProfileOut( $fname );
141 return $php;
142 }
143
144 function executeTemplate( $template ) {
145 $fname = 'SkinMonoBookCBT::executeTemplate';
146 wfProfileIn( $fname );
147 $tp = new CBTProcessor( $template, $this );
148 $this->mCompiling = true;
149 $text = $tp->execute();
150 $this->mCompiling = false;
151 wfProfileOut( $fname );
152 return $text;
153 }
154
155 /******************************************************
156 * Callbacks *
157 ******************************************************/
158
159 function lang() { return $GLOBALS['wgContLanguageCode']; }
160
161 function dir() {
162 global $wgContLang;
163 return $wgContLang->isRTL() ? 'rtl' : 'ltr';
164 }
165
166 function mimetype() { return $GLOBALS['wgMimeType']; }
167 function charset() { return $GLOBALS['wgOutputEncoding']; }
168 function headlinks() {
169 return cbt_value( $this->mOut->getHeadLinks(), 'dynamic' );
170 }
171 function headscripts() {
172 return cbt_value( $this->mOut->getScript(), 'dynamic' );
173 }
174
175 function pagetitle() {
176 return cbt_value( $this->mOut->getHTMLTitle(), array( 'title', 'lang' ) );
177 }
178
179 function stylepath() { return $GLOBALS['wgStylePath']; }
180 function stylename() { return $this->mStyleName; }
181
182 function notprintable() {
183 global $wgRequest;
184 return cbt_value( !$wgRequest->getBool( 'printable' ), 'nonview dynamic' );
185 }
186
187 function jsmimetype() { return $GLOBALS['wgJsMimeType']; }
188
189 function jsvarurl() {
190 global $wgUseSiteJs, $wgUser;
191 if ( !$wgUseSiteJs ) return '';
192
193 if ( $wgUser->isLoggedIn() ) {
194 $url = $this->makeUrl('-','action=raw&smaxage=0&gen=js');
195 } else {
196 $url = $this->makeUrl('-','action=raw&gen=js');
197 }
198 return cbt_value( $url, 'loggedin' );
199 }
200
201 function pagecss() {
202 global $wgHooks;
203
204 $out = false;
205 wfRunHooks( 'SkinTemplateSetupPageCss', array( &$out ) );
206
207 // Unknown dependencies
208 return cbt_value( $out, 'dynamic' );
209 }
210
211 function usercss() {
212 if ( $this->isCssPreview() ) {
213 global $wgRequest;
214 $usercss = $this->makeStylesheetCdata( $wgRequest->getText('wpTextbox1') );
215 } else {
216 $usercss = $this->makeStylesheetLink( $this->makeUrl($this->getUserPageText() .
217 '/'.$this->mStyleName.'.css', 'action=raw&ctype=text/css' ) );
218 }
219
220 // Dynamic when not an ordinary page view, also depends on the username
221 return cbt_value( $usercss, array( 'nonview dynamic', 'user' ) );
222 }
223
224 function sitecss() {
225 global $wgUseSiteCss;
226 if ( !$wgUseSiteCss ) {
227 return '';
228 }
229
230 global $wgSquidMaxage, $wgContLang, $wgStylePath;
231
232 $query = "action=raw&ctype=text/css&smaxage=$wgSquidMaxage";
233
234 $sitecss = '';
235 if ( $wgContLang->isRTL() ) {
236 $sitecss .= $this->makeStylesheetLink( $wgStylePath . '/' . $this->mStyleName . '/rtl.css' ) . "\n";
237 }
238
239 $sitecss .= $this->makeStylesheetLink( $this->makeNSUrl('Common.css', $query, NS_MEDIAWIKI) ) . "\n";
240 $sitecss .= $this->makeStylesheetLink( $this->makeNSUrl(
241 ucfirst($this->mStyleName) . '.css', $query, NS_MEDIAWIKI) ) . "\n";
242
243 // No deps
244 return $sitecss;
245 }
246
247 function gencss() {
248 global $wgUseSiteCss;
249 if ( !$wgUseSiteCss ) return '';
250
251 global $wgSquidMaxage, $wgUser, $wgAllowUserCss;
252 if ( $this->isCssPreview() ) {
253 $siteargs = '&smaxage=0&maxage=0';
254 } else {
255 $siteargs = '&maxage=' . $wgSquidMaxage;
256 }
257 if ( $wgAllowUserCss && $wgUser->isLoggedIn() ) {
258 $siteargs .= '&ts={user_touched}';
259 $isTemplate = true;
260 } else {
261 $isTemplate = false;
262 }
263
264 $link = $this->makeStylesheetLink( $this->makeUrl('-','action=raw&gen=css' . $siteargs) ) . "\n";
265
266 if ( $wgAllowUserCss ) {
267 $deps = 'loggedin';
268 } else {
269 $deps = array();
270 }
271 return cbt_value( $link, $deps, $isTemplate );
272 }
273
274 function user_touched() {
275 global $wgUser;
276 return cbt_value( $wgUser->mTouched, 'dynamic' );
277 }
278
279 function userjs() {
280 global $wgAllowUserJs, $wgJsMimeType;
281 if ( !$wgAllowUserJs ) return '';
282
283 if ( $this->isJsPreview() ) {
284 $url = '';
285 } else {
286 $url = $this->makeUrl($this->getUserPageText().'/'.$this->mStyleName.'.js', 'action=raw&ctype='.$wgJsMimeType.'&dontcountme=s');
287 }
288 return cbt_value( $url, array( 'nonview dynamic', 'user' ) );
289 }
290
291 function userjsprev() {
292 global $wgAllowUserJs, $wgRequest;
293 if ( !$wgAllowUserJs ) return '';
294 if ( $this->isJsPreview() ) {
295 $js = '/*<![CDATA[*/ ' . $wgRequest->getText('wpTextbox1') . ' /*]]>*/';
296 } else {
297 $js = '';
298 }
299 return cbt_value( $js, array( 'nonview dynamic' ) );
300 }
301
302 function trackbackhtml() {
303 global $wgUseTrackbacks;
304 if ( !$wgUseTrackbacks ) return '';
305
306 if ( $this->mOut->isArticleRelated() ) {
307 $tb = $this->mTitle->trackbackRDF();
308 } else {
309 $tb = '';
310 }
311 return cbt_value( $tb, 'dynamic' );
312 }
313
314 function body_ondblclick() {
315 global $wgUser;
316 if( $this->isEditable() && $wgUser->getOption("editondblclick") ) {
317 $js = 'document.location = "' . $this->getEditUrl() .'";';
318 } else {
319 $js = '';
320 }
321
322 if ( User::getDefaultOption('editondblclick') ) {
323 return cbt_value( $js, 'user', 'title' );
324 } else {
325 // Optimise away for logged-out users
326 return cbt_value( $js, 'loggedin dynamic' );
327 }
328 }
329
330 function body_onload() {
331 global $wgUser;
332 if ( $this->isEditable() && $wgUser->getOption( 'editsectiononrightclick' ) ) {
333 $js = 'setupRightClickEdit()';
334 } else {
335 $js = '';
336 }
337 return cbt_value( $js, 'loggedin dynamic' );
338 }
339
340 function nsclass() {
341 return cbt_value( 'ns-' . $this->mTitle->getNamespace(), 'title' );
342 }
343
344 function sitenotice() {
345 // Perhaps this could be given special dependencies using our knowledge of what
346 // wfGetSiteNotice() depends on.
347 return cbt_value( wfGetSiteNotice(), 'dynamic' );
348 }
349
350 function title() {
351 return cbt_value( $this->mOut->getPageTitle(), array( 'title', 'lang' ) );
352 }
353
354 function title_urlform() {
355 return cbt_value( $this->getThisTitleUrlForm(), 'title' );
356 }
357
358 function title_userurl() {
359 return cbt_value( urlencode( $this->mTitle->getDBkey() ), 'title' );
360 }
361
362 function subtitle() {
363 $subpagestr = $this->subPageSubtitle();
364 if ( !empty( $subpagestr ) ) {
365 $s = '<span class="subpages">'.$subpagestr.'</span>'.$this->mOut->getSubtitle();
366 } else {
367 $s = $this->mOut->getSubtitle();
368 }
369 return cbt_value( $s, array( 'title', 'nonview dynamic' ) );
370 }
371
372 function undelete() {
373 return cbt_value( $this->getUndeleteLink(), array( 'title', 'lang' ) );
374 }
375
376 function newtalk() {
377 global $wgUser, $wgDBname;
378 $newtalks = $wgUser->getNewMessageLinks();
379
380 if (count($newtalks) == 1 && $newtalks[0]["wiki"] === $wgDBname) {
381 $usertitle = $this->getUserPageTitle();
382 $usertalktitle = $usertitle->getTalkPage();
383 if( !$usertalktitle->equals( $this->mTitle ) ) {
384 $ntl = wfMsg( 'youhavenewmessages',
385 $this->makeKnownLinkObj(
386 $usertalktitle,
387 wfMsgHtml( 'newmessageslink' ),
388 'redirect=no'
389 ),
390 $this->makeKnownLinkObj(
391 $usertalktitle,
392 wfMsgHtml( 'newmessagesdifflink' ),
393 'diff=cur'
394 )
395 );
396 # Disable Cache
397 $this->mOut->setSquidMaxage(0);
398 }
399 } else if (count($newtalks)) {
400 $sep = str_replace("_", " ", wfMsgHtml("newtalkseperator"));
401 $msgs = array();
402 foreach ($newtalks as $newtalk) {
403 $msgs[] = wfElement("a",
404 array('href' => $newtalk["link"]), $newtalk["wiki"]);
405 }
406 $parts = implode($sep, $msgs);
407 $ntl = wfMsgHtml('youhavenewmessagesmulti', $parts);
408 $this->mOut->setSquidMaxage(0);
409 } else {
410 $ntl = '';
411 }
412 return cbt_value( $ntl, 'dynamic' );
413 }
414
415 function showjumplinks() {
416 global $wgUser;
417 return cbt_value( $wgUser->getOption( 'showjumplinks' ) ? 'true' : '', 'user' );
418 }
419
420 function bodytext() {
421 return cbt_value( $this->mOut->getHTML(), 'dynamic' );
422 }
423
424 function catlinks() {
425 return cbt_value( $this->getCategories(), 'dynamic' );
426 }
427
428 function extratabs( $itemTemplate ) {
429 global $wgContLang, $wgDisableLangConversion;
430
431 $etpl = cbt_escape( $itemTemplate );
432
433 /* show links to different language variants */
434 $variants = $wgContLang->getVariants();
435 $s = '';
436 if ( !$wgDisableLangConversion && count( $wgContLang->getVariants() ) > 1 ) {
437 $vcount=0;
438 foreach ( $variants as $code ) {
439 $name = $wgContLang->getVariantname( $code );
440 if ( $name == 'disable' ) {
441 continue;
442 }
443 $code = cbt_escape( $code );
444 $name = cbt_escape( $name );
445 $s .= "{ca_variant {{$code}} {{$name}} {{$vcount}} {{$etpl}}}\n";
446 $vcount ++;
447 }
448 }
449 return cbt_value( $s, array(), true );
450 }
451
452 function is_special() { return cbt_value( $this->mTitle->getNamespace() == NS_SPECIAL, 'title' ); }
453 function can_edit() { return cbt_value( (string)($this->mTitle->userCanEdit()), 'dynamic' ); }
454 function can_move() { return cbt_value( (string)($this->mTitle->userCanMove()), 'dynamic' ); }
455 function is_talk() { return cbt_value( (string)($this->mTitle->isTalkPage()), 'title' ); }
456 function is_protected() { return cbt_value( (string)$this->mTitle->isProtected(), 'dynamic' ); }
457 function nskey() { return cbt_value( $this->mTitle->getNamespaceKey(), 'title' ); }
458
459 function request_url() {
460 global $wgRequest;
461 return cbt_value( $wgRequest->getRequestURL(), 'dynamic' );
462 }
463
464 function subject_url() {
465 $title = $this->getSubjectPage();
466 if ( $title->exists() ) {
467 $url = $title->getLocalUrl();
468 } else {
469 $url = $title->getLocalUrl( 'action=edit' );
470 }
471 return cbt_value( $url, 'title' );
472 }
473
474 function talk_url() {
475 $title = $this->getTalkPage();
476 if ( $title->exists() ) {
477 $url = $title->getLocalUrl();
478 } else {
479 $url = $title->getLocalUrl( 'action=edit' );
480 }
481 return cbt_value( $url, 'title' );
482 }
483
484 function edit_url() {
485 return cbt_value( $this->getEditUrl(), array( 'title', 'nonview dynamic' ) );
486 }
487
488 function move_url() {
489 return cbt_value( $this->makeSpecialParamUrl( 'Movepage' ), array(), true );
490 }
491
492 function localurl( $query ) {
493 return cbt_value( $this->mTitle->getLocalURL( $query ), 'title' );
494 }
495
496 function selecttab( $tab, $extraclass = '' ) {
497 if ( !isset( $this->mSelectedTab ) ) {
498 $prevent_active_tabs = false ;
499 wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this , &$preventActiveTabs ) );
500
501 $actionTabs = array(
502 'edit' => 'edit',
503 'submit' => 'edit',
504 'history' => 'history',
505 'protect' => 'protect',
506 'unprotect' => 'protect',
507 'delete' => 'delete',
508 'watch' => 'watch',
509 'unwatch' => 'watch',
510 );
511 if ( $preventActiveTabs ) {
512 $this->mSelectedTab = false;
513 } else {
514 $action = $this->getAction();
515 $section = $this->getSection();
516
517 if ( isset( $actionTabs[$action] ) ) {
518 $this->mSelectedTab = $actionTabs[$action];
519
520 if ( $this->mSelectedTab == 'edit' && $section == 'new' ) {
521 $this->mSelectedTab = 'addsection';
522 }
523 } elseif ( $this->mTitle->isTalkPage() ) {
524 $this->mSelectedTab = 'talk';
525 } else {
526 $this->mSelectedTab = 'subject';
527 }
528 }
529 }
530 if ( $extraclass ) {
531 if ( $this->mSelectedTab == $tab ) {
532 $s = 'class="selected ' . htmlspecialchars( $extraclass ) . '"';
533 } else {
534 $s = 'class="' . htmlspecialchars( $extraclass ) . '"';
535 }
536 } else {
537 if ( $this->mSelectedTab == $tab ) {
538 $s = 'class="selected"';
539 } else {
540 $s = '';
541 }
542 }
543 return cbt_value( $s, array( 'nonview dynamic', 'title' ) );
544 }
545
546 function subject_newclass() {
547 $title = $this->getSubjectPage();
548 $class = $title->exists() ? '' : 'new';
549 return cbt_value( $class, 'dynamic' );
550 }
551
552 function talk_newclass() {
553 $title = $this->getTalkPage();
554 $class = $title->exists() ? '' : 'new';
555 return cbt_value( $class, 'dynamic' );
556 }
557
558 function ca_variant( $code, $name, $index, $template ) {
559 global $wgContLang;
560 $selected = ($code == $wgContLang->getPreferredVariant());
561 $action = $this->getAction();
562 $actstr = '';
563 if( $action )
564 $actstr = 'action=' . $action . '&';
565 $s = strtr( $template, array(
566 '$id' => htmlspecialchars( 'varlang-' . $index ),
567 '$class' => $selected ? 'class="selected"' : '',
568 '$text' => $name,
569 '$href' => htmlspecialchars( $this->mTitle->getLocalUrl( $actstr . 'variant=' . $code ) )
570 ));
571 return cbt_value( $s, 'dynamic' );
572 }
573
574 function is_watching() {
575 return cbt_value( (string)$this->mTitle->userIsWatching(), array( 'dynamic' ) );
576 }
577
578
579 function personal_urls( $itemTemplate ) {
580 global $wgShowIPinHeader, $wgContLang;
581
582 # Split this function up into many small functions, to obtain the
583 # best specificity in the dependencies of each one. The template below
584 # has no dependencies, so its generation, and any static subfunctions,
585 # can be optimised away.
586 $etpl = cbt_escape( $itemTemplate );
587 $s = "
588 {userpage {{$etpl}}}
589 {mytalk {{$etpl}}}
590 {preferences {{$etpl}}}
591 {watchlist {{$etpl}}}
592 {mycontris {{$etpl}}}
593 {logout {{$etpl}}}
594 ";
595
596 if ( $wgShowIPinHeader ) {
597 $s .= "
598 {anonuserpage {{$etpl}}}
599 {anontalk {{$etpl}}}
600 {anonlogin {{$etpl}}}
601 ";
602 } else {
603 $s .= "{login {{$etpl}}}\n";
604 }
605 // No dependencies
606 return cbt_value( $s, array(), true /*this is a template*/ );
607 }
608
609 function userpage( $itemTemplate ) {
610 global $wgUser;
611 if ( $this->isLoggedIn() ) {
612 $userPage = $this->getUserPageTitle();
613 $s = $this->makeTemplateLink( $itemTemplate, 'userpage', $userPage, $wgUser->getName() );
614 } else {
615 $s = '';
616 }
617 return cbt_value( $s, 'user' );
618 }
619
620 function mytalk( $itemTemplate ) {
621 global $wgUser;
622 if ( $this->isLoggedIn() ) {
623 $userPage = $this->getUserPageTitle();
624 $talkPage = $userPage->getTalkPage();
625 $s = $this->makeTemplateLink( $itemTemplate, 'mytalk', $talkPage, wfMsg('mytalk') );
626 } else {
627 $s = '';
628 }
629 return cbt_value( $s, 'user' );
630 }
631
632 function preferences( $itemTemplate ) {
633 if ( $this->isLoggedIn() ) {
634 $s = $this->makeSpecialTemplateLink( $itemTemplate, 'preferences',
635 'Preferences', wfMsg( 'preferences' ) );
636 } else {
637 $s = '';
638 }
639 return cbt_value( $s, array( 'loggedin', 'lang' ) );
640 }
641
642 function watchlist( $itemTemplate ) {
643 if ( $this->isLoggedIn() ) {
644 $s = $this->makeSpecialTemplateLink( $itemTemplate, 'watchlist',
645 'Watchlist', wfMsg( 'watchlist' ) );
646 } else {
647 $s = '';
648 }
649 return cbt_value( $s, array( 'loggedin', 'lang' ) );
650 }
651
652 function mycontris( $itemTemplate ) {
653 if ( $this->isLoggedIn() ) {
654 global $wgUser;
655 $s = $this->makeSpecialTemplateLink( $itemTemplate, 'mycontris',
656 "Contributions/" . $wgUser->getTitleKey(), wfMsg('mycontris') );
657 } else {
658 $s = '';
659 }
660 return cbt_value( $s, 'user' );
661 }
662
663 function logout( $itemTemplate ) {
664 if ( $this->isLoggedIn() ) {
665 $s = $this->makeSpecialTemplateLink( $itemTemplate, 'logout',
666 'Userlogout', wfMsg( 'userlogout' ),
667 $this->mTitle->getNamespace() === NS_SPECIAL && $this->mTitle->getText() === 'Preferences'
668 ? '' : "returnto=" . $this->mTitle->getPrefixedURL() );
669 } else {
670 $s = '';
671 }
672 return cbt_value( $s, 'loggedin dynamic' );
673 }
674
675 function anonuserpage( $itemTemplate ) {
676 if ( $this->isLoggedIn() ) {
677 $s = '';
678 } else {
679 global $wgUser;
680 $userPage = $this->getUserPageTitle();
681 $s = $this->makeTemplateLink( $itemTemplate, 'userpage', $userPage, $wgUser->getName() );
682 }
683 return cbt_value( $s, '!loggedin dynamic' );
684 }
685
686 function anontalk( $itemTemplate ) {
687 if ( $this->isLoggedIn() ) {
688 $s = '';
689 } else {
690 $userPage = $this->getUserPageTitle();
691 $talkPage = $userPage->getTalkPage();
692 $s = $this->makeTemplateLink( $itemTemplate, 'mytalk', $talkPage, wfMsg('anontalk') );
693 }
694 return cbt_value( $s, '!loggedin dynamic' );
695 }
696
697 function anonlogin( $itemTemplate ) {
698 if ( $this->isLoggedIn() ) {
699 $s = '';
700 } else {
701 $s = $this->makeSpecialTemplateLink( $itemTemplate, 'anonlogin', 'Userlogin',
702 wfMsg( 'userlogin' ), 'returnto=' . urlencode( $this->getThisPDBK() ) );
703 }
704 return cbt_value( $s, '!loggedin dynamic' );
705 }
706
707 function login( $itemTemplate ) {
708 if ( $this->isLoggedIn() ) {
709 $s = '';
710 } else {
711 $s = $this->makeSpecialTemplateLink( $itemTemplate, 'login', 'Userlogin',
712 wfMsg( 'userlogin' ), 'returnto=' . urlencode( $this->getThisPDBK() ) );
713 }
714 return cbt_value( $s, '!loggedin dynamic' );
715 }
716
717 function logopath() { return $GLOBALS['wgLogo']; }
718 function mainpage() { return $this->makeI18nUrl( 'mainpage' ); }
719
720 function sidebar( $startSection, $endSection, $innerTpl ) {
721 $s = '';
722 $lines = explode( "\n", wfMsgForContent( 'sidebar' ) );
723 $firstSection = true;
724 foreach ($lines as $line) {
725 if (strpos($line, '*') !== 0)
726 continue;
727 if (strpos($line, '**') !== 0) {
728 $bar = trim($line, '* ');
729 $name = wfMsg( $bar );
730 if (wfEmptyMsg($bar, $name)) {
731 $name = $bar;
732 }
733 if ( $firstSection ) {
734 $firstSection = false;
735 } else {
736 $s .= $endSection;
737 }
738 $s .= strtr( $startSection,
739 array(
740 '$bar' => htmlspecialchars( $bar ),
741 '$barname' => $name
742 ) );
743 } else {
744 if (strpos($line, '|') !== false) { // sanity check
745 $line = explode( '|' , trim($line, '* '), 2 );
746 $link = wfMsgForContent( $line[0] );
747 if ($link == '-')
748 continue;
749 if (wfEmptyMsg($line[1], $text = wfMsg($line[1])))
750 $text = $line[1];
751 if (wfEmptyMsg($line[0], $link))
752 $link = $line[0];
753 $href = $this->makeInternalOrExternalUrl( $link );
754
755 $s .= strtr( $innerTpl,
756 array(
757 '$text' => htmlspecialchars( $text ),
758 '$href' => htmlspecialchars( $href ),
759 '$id' => htmlspecialchars( 'n-' . strtr($line[1], ' ', '-') ),
760 '$classactive' => ''
761 ) );
762 } else { continue; }
763 }
764 }
765 if ( !$firstSection ) {
766 $s .= $endSection;
767 }
768
769 // Depends on user language only
770 return cbt_value( $s, 'lang' );
771 }
772
773 function searchaction() {
774 // Static link
775 return $this->getSearchLink();
776 }
777
778 function search() {
779 global $wgRequest;
780 return cbt_value( trim( $this->getSearch() ), 'special dynamic' );
781 }
782
783 function notspecialpage() {
784 return cbt_value( $this->mTitle->getNamespace() != NS_SPECIAL, 'special' );
785 }
786
787 function nav_whatlinkshere() {
788 return cbt_value( $this->makeSpecialParamUrl('Whatlinkshere' ), array(), true );
789 }
790
791 function article_exists() {
792 return cbt_value( (string)($this->mTitle->getArticleId() !== 0), 'title' );
793 }
794
795 function nav_recentchangeslinked() {
796 return cbt_value( $this->makeSpecialParamUrl('Recentchangeslinked' ), array(), true );
797 }
798
799 function feeds( $itemTemplate = '' ) {
800 if ( !$this->mOut->isSyndicated() ) {
801 $feeds = '';
802 } elseif ( $itemTemplate == '' ) {
803 // boolean only required
804 $feeds = 'true';
805 } else {
806 $feeds = '';
807 global $wgFeedClasses, $wgRequest;
808 foreach( $wgFeedClasses as $format => $class ) {
809 $feeds .= strtr( $itemTemplate,
810 array(
811 '$key' => htmlspecialchars( $format ),
812 '$text' => $format,
813 '$href' => $wgRequest->appendQuery( "feed=$format" )
814 ) );
815 }
816 }
817 return cbt_value( $feeds, 'special dynamic' );
818 }
819
820 function is_userpage() {
821 list( $id, $ip ) = $this->getUserPageIdIp();
822 return cbt_value( (string)($id || $ip), 'title' );
823 }
824
825 function is_ns_mediawiki() {
826 return cbt_value( (string)$this->mTitle->getNamespace() == NS_MEDIAWIKI, 'title' );
827 }
828
829 function is_loggedin() {
830 global $wgUser;
831 return cbt_value( (string)($wgUser->isLoggedIn()), 'loggedin' );
832 }
833
834 function nav_contributions() {
835 $url = $this->makeSpecialParamUrl( 'Contributions', '', '{title_userurl}' );
836 return cbt_value( $url, array(), true );
837 }
838
839 function is_allowed( $right ) {
840 global $wgUser;
841 return cbt_value( (string)$wgUser->isAllowed( $right ), 'user' );
842 }
843
844 function nav_blockip() {
845 $url = $this->makeSpecialParamUrl( 'Blockip', '', '{title_userurl}' );
846 return cbt_value( $url, array(), true );
847 }
848
849 function nav_emailuser() {
850 global $wgEnableEmail, $wgEnableUserEmail, $wgUser;
851 if ( !$wgEnableEmail || !$wgEnableUserEmail ) return '';
852
853 $url = $this->makeSpecialParamUrl( 'Emailuser', '', '{title_userurl}' );
854 return cbt_value( $url, array(), true );
855 }
856
857 function nav_upload() {
858 global $wgEnableUploads, $wgUploadNavigationUrl;
859 if ( !$wgEnableUploads ) {
860 return '';
861 } elseif ( $wgUploadNavigationUrl ) {
862 return $wgUploadNavigationUrl;
863 } else {
864 return $this->makeSpecialUrl('Upload');
865 }
866 }
867
868 function nav_specialpages() {
869 return $this->makeSpecialUrl('Specialpages');
870 }
871
872 function nav_print() {
873 global $wgRequest, $wgArticle;
874 $action = $this->getAction();
875 $url = '';
876 if( $this->mTitle->getNamespace() !== NS_SPECIAL
877 && ($action == '' || $action == 'view' || $action == 'purge' ) )
878 {
879 $revid = $wgArticle->getLatest();
880 if ( $revid != 0 ) {
881 $url = $wgRequest->appendQuery( 'printable=yes' );
882 }
883 }
884 return cbt_value( $url, array( 'nonview dynamic', 'title' ) );
885 }
886
887 function nav_permalink() {
888 $url = (string)$this->getPermalink();
889 return cbt_value( $url, 'dynamic' );
890 }
891
892 function nav_trackbacklink() {
893 global $wgUseTrackbacks;
894 if ( !$wgUseTrackbacks ) return '';
895
896 return cbt_value( $this->mTitle->trackbackURL(), 'title' );
897 }
898
899 function is_permalink() {
900 return cbt_value( (string)($this->getPermalink() === false), 'nonview dynamic' );
901 }
902
903 function toolboxend() {
904 // This is where the MonoBookTemplateToolboxEnd hook went in the old skin
905 return '';
906 }
907
908 function language_urls( $outer, $inner ) {
909 global $wgHideInterlanguageLinks, $wgOut, $wgContLang;
910 if ( $wgHideInterlanguageLinks ) return '';
911
912 $links = $wgOut->getLanguageLinks();
913 $s = '';
914 if ( count( $links ) ) {
915 foreach( $links as $l ) {
916 $tmp = explode( ':', $l, 2 );
917 $nt = Title::newFromText( $l );
918 $s .= strtr( $inner,
919 array(
920 '$class' => htmlspecialchars( 'interwiki-' . $tmp[0] ),
921 '$href' => htmlspecialchars( $nt->getFullURL() ),
922 '$text' => ($wgContLang->getLanguageName( $nt->getInterwiki() ) != ''?
923 $wgContLang->getLanguageName( $nt->getInterwiki() ) : $l ),
924 )
925 );
926 }
927 $s = str_replace( '$body', $s, $outer );
928 }
929 return cbt_value( $s, 'dynamic' );
930 }
931
932 function poweredbyico() { return $this->getPoweredBy(); }
933 function copyrightico() { return $this->getCopyrightIcon(); }
934
935 function lastmod() {
936 global $wgMaxCredits;
937 if ( $wgMaxCredits ) return '';
938
939 if ( $this->isCurrentArticleView() ) {
940 $s = $this->lastModified();
941 } else {
942 $s = '';
943 }
944 return cbt_value( $s, 'dynamic' );
945 }
946
947 function viewcount() {
948 global $wgDisableCounters;
949 if ( $wgDisableCounters ) return '';
950
951 global $wgLang, $wgArticle;
952 if ( is_object( $wgArticle ) ) {
953 $viewcount = $wgLang->formatNum( $wgArticle->getCount() );
954 if ( $viewcount ) {
955 $viewcount = wfMsg( "viewcount", $viewcount );
956 } else {
957 $viewcount = '';
958 }
959 } else {
960 $viewcount = '';
961 }
962 return cbt_value( $viewcount, 'dynamic' );
963 }
964
965 function numberofwatchingusers() {
966 global $wgPageShowWatchingUsers;
967 if ( !$wgPageShowWatchingUsers ) return '';
968
969 $dbr =& wfGetDB( DB_SLAVE );
970 extract( $dbr->tableNames( 'watchlist' ) );
971 $sql = "SELECT COUNT(*) AS n FROM $watchlist
972 WHERE wl_title='" . $dbr->strencode($this->mTitle->getDBKey()) .
973 "' AND wl_namespace=" . $this->mTitle->getNamespace() ;
974 $res = $dbr->query( $sql, 'SkinTemplate::outputPage');
975 $row = $dbr->fetchObject( $res );
976 $num = $row->n;
977 if ($num > 0) {
978 $s = wfMsg('number_of_watching_users_pageview', $num);
979 } else {
980 $s = '';
981 }
982 return cbt_value( $s, 'dynamic' );
983 }
984
985 function credits() {
986 global $wgMaxCredits;
987 if ( !$wgMaxCredits ) return '';
988
989 if ( $this->isCurrentArticleView() ) {
990 require_once("Credits.php");
991 global $wgArticle, $wgShowCreditsIfMax;
992 $credits = getCredits($wgArticle, $wgMaxCredits, $wgShowCreditsIfMax);
993 } else {
994 $credits = '';
995 }
996 return cbt_value( $credits, 'view dynamic' );
997 }
998
999 function normalcopyright() {
1000 return $this->getCopyright( 'normal' );
1001 }
1002
1003 function historycopyright() {
1004 return $this->getCopyright( 'history' );
1005 }
1006
1007 function is_currentview() {
1008 global $wgRequest;
1009 return cbt_value( (string)$this->isCurrentArticleView(), 'view' );
1010 }
1011
1012 function usehistorycopyright() {
1013 global $wgRequest;
1014 if ( wfMsgForContent( 'history_copyright' ) == '-' ) return '';
1015
1016 $oldid = $this->getOldId();
1017 $diff = $this->getDiff();
1018 $use = (string)(!is_null( $oldid ) && is_null( $diff ));
1019 return cbt_value( $use, 'nonview dynamic' );
1020 }
1021
1022 function privacy() {
1023 return cbt_value( $this->privacyLink(), 'lang' );
1024 }
1025 function about() {
1026 return cbt_value( $this->aboutLink(), 'lang' );
1027 }
1028 function disclaimer() {
1029 return cbt_value( $this->disclaimerLink(), 'lang' );
1030 }
1031 function tagline() {
1032 # A reference to this tag existed in the old MonoBook.php, but the
1033 # template data wasn't set anywhere
1034 return '';
1035 }
1036 function reporttime() {
1037 return cbt_value( $this->mOut->reportTime(), 'dynamic' );
1038 }
1039
1040 function msg( $name ) {
1041 return cbt_value( wfMsg( $name ), 'lang' );
1042 }
1043
1044 function fallbackmsg( $name, $fallback ) {
1045 $text = wfMsg( $name );
1046 if ( wfEmptyMsg( $name, $text ) ) {
1047 $text = $fallback;
1048 }
1049 return cbt_value( $text, 'lang' );
1050 }
1051
1052 /******************************************************
1053 * Utility functions *
1054 ******************************************************/
1055
1056 /** Return true if this request is a valid, secure CSS preview */
1057 function isCssPreview() {
1058 if ( !isset( $this->mCssPreview ) ) {
1059 global $wgRequest, $wgAllowUserCss, $wgUser;
1060 $this->mCssPreview =
1061 $wgAllowUserCss &&
1062 $wgUser->isLoggedIn() &&
1063 $this->mTitle->isCssSubpage() &&
1064 $this->userCanPreview( $this->getAction() );
1065 }
1066 return $this->mCssPreview;
1067 }
1068
1069 /** Return true if this request is a valid, secure JS preview */
1070 function isJsPreview() {
1071 if ( !isset( $this->mJsPreview ) ) {
1072 global $wgRequest, $wgAllowUserJs, $wgUser;
1073 $this->mJsPreview =
1074 $wgAllowUserJs &&
1075 $wgUser->isLoggedIn() &&
1076 $this->mTitle->isJsSubpage() &&
1077 $this->userCanPreview( $this->getAction() );
1078 }
1079 return $this->mJsPreview;
1080 }
1081
1082 /** Get the title of the $wgUser's user page */
1083 function getUserPageTitle() {
1084 if ( !isset( $this->mUserPageTitle ) ) {
1085 global $wgUser;
1086 $this->mUserPageTitle = $wgUser->getUserPage();
1087 }
1088 return $this->mUserPageTitle;
1089 }
1090
1091 /** Get the text of the user page title */
1092 function getUserPageText() {
1093 if ( !isset( $this->mUserPageText ) ) {
1094 $userPage = $this->getUserPageTitle();
1095 $this->mUserPageText = $userPage->getPrefixedText();
1096 }
1097 return $this->mUserPageText;
1098 }
1099
1100 /** Make an HTML element for a stylesheet link */
1101 function makeStylesheetLink( $url ) {
1102 return '<link rel="stylesheet" type="text/css" href="' . htmlspecialchars( $url ) . "\"/>";
1103 }
1104
1105 /** Make an XHTML element for inline CSS */
1106 function makeStylesheetCdata( $style ) {
1107 return "<style type=\"text/css\"> /*<![CDATA[*/ {$style} /*]]>*/ </style>";
1108 }
1109
1110 /** Get the edit URL for this page */
1111 function getEditUrl() {
1112 if ( !isset( $this->mEditUrl ) ) {
1113 $this->mEditUrl = $this->mTitle->getLocalUrl( $this->editUrlOptions() );
1114 }
1115 return $this->mEditUrl;
1116 }
1117
1118 /** Get the prefixed DB key for this page */
1119 function getThisPDBK() {
1120 if ( !isset( $this->mThisPDBK ) ) {
1121 $this->mThisPDBK = $this->mTitle->getPrefixedDbKey();
1122 }
1123 return $this->mThisPDBK;
1124 }
1125
1126 function getThisTitleUrlForm() {
1127 if ( !isset( $this->mThisTitleUrlForm ) ) {
1128 $this->mThisTitleUrlForm = $this->mTitle->getPrefixedURL();
1129 }
1130 return $this->mThisTitleUrlForm;
1131 }
1132
1133 /**
1134 * If the current page is a user page, get the user's ID and IP. Otherwise return array(0,false)
1135 */
1136 function getUserPageIdIp() {
1137 if ( !isset( $this->mUserPageId ) ) {
1138 if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) {
1139 $this->mUserPageId = User::idFromName($this->mTitle->getText());
1140 $this->mUserPageIp = User::isIP($this->mTitle->getText());
1141 } else {
1142 $this->mUserPageId = 0;
1143 $this->mUserPageIp = false;
1144 }
1145 }
1146 return array( $this->mUserPageId, $this->mUserPageIp );
1147 }
1148
1149 /**
1150 * Returns a permalink URL, or false if the current page is already a
1151 * permalink, or blank if a permalink shouldn't be displayed
1152 */
1153 function getPermalink() {
1154 if ( !isset( $this->mPermalink ) ) {
1155 global $wgRequest, $wgArticle;
1156 $action = $this->getAction();
1157 $oldid = $this->getOldId();
1158 $url = '';
1159 if( $this->mTitle->getNamespace() !== NS_SPECIAL
1160 && $this->mTitle->getArticleId() != 0
1161 && ($action == '' || $action == 'view' || $action == 'purge' ) )
1162 {
1163 if ( !$oldid ) {
1164 $revid = $wgArticle->getLatest();
1165 $url = $this->mTitle->getLocalURL( "oldid=$revid" );
1166 } else {
1167 $url = false;
1168 }
1169 } else {
1170 $url = '';
1171 }
1172 }
1173 return $url;
1174 }
1175
1176 /**
1177 * Returns true if the current page is an article, not a special page,
1178 * and we are viewing a revision, not a diff
1179 */
1180 function isArticleView() {
1181 global $wgOut, $wgArticle, $wgRequest;
1182 if ( !isset( $this->mIsArticleView ) ) {
1183 $oldid = $this->getOldId();
1184 $diff = $this->getDiff();
1185 $this->mIsArticleView = $wgOut->isArticle() and
1186 (!is_null( $oldid ) or is_null( $diff )) and 0 != $wgArticle->getID();
1187 }
1188 return $this->mIsArticleView;
1189 }
1190
1191 function isCurrentArticleView() {
1192 if ( !isset( $this->mIsCurrentArticleView ) ) {
1193 global $wgOut, $wgArticle, $wgRequest;
1194 $oldid = $this->getOldId();
1195 $this->mIsCurrentArticleView = $wgOut->isArticle() && is_null( $oldid ) && 0 != $wgArticle->getID();
1196 }
1197 return $this->mIsCurrentArticleView;
1198 }
1199
1200
1201 /**
1202 * Return true if the current page is editable; if edit section on right
1203 * click should be enabled.
1204 */
1205 function isEditable() {
1206 global $wgRequest;
1207 $action = $this->getAction();
1208 return ($this->mTitle->getNamespace() != NS_SPECIAL and !($action == 'edit' or $action == 'submit'));
1209 }
1210
1211 /** Return true if the user is logged in */
1212 function isLoggedIn() {
1213 global $wgUser;
1214 return $wgUser->isLoggedIn();
1215 }
1216
1217 /** Get the local URL of the current page */
1218 function getPageUrl() {
1219 if ( !isset( $this->mPageUrl ) ) {
1220 $this->mPageUrl = $this->mTitle->getLocalURL();
1221 }
1222 return $this->mPageUrl;
1223 }
1224
1225 /** Make a link to a title using a template */
1226 function makeTemplateLink( $template, $key, $title, $text ) {
1227 $url = $title->getLocalUrl();
1228 return strtr( $template,
1229 array(
1230 '$key' => $key,
1231 '$classactive' => ($url == $this->getPageUrl()) ? 'class="active"' : '',
1232 '$class' => $title->getArticleID() == 0 ? 'class="new"' : '',
1233 '$href' => htmlspecialchars( $url ),
1234 '$text' => $text
1235 ) );
1236 }
1237
1238 /** Make a link to a URL using a template */
1239 function makeTemplateLinkUrl( $template, $key, $url, $text ) {
1240 return strtr( $template,
1241 array(
1242 '$key' => $key,
1243 '$classactive' => ($url == $this->getPageUrl()) ? 'class="active"' : '',
1244 '$class' => '',
1245 '$href' => htmlspecialchars( $url ),
1246 '$text' => $text
1247 ) );
1248 }
1249
1250 /** Make a link to a special page using a template */
1251 function makeSpecialTemplateLink( $template, $key, $specialName, $text, $query = '' ) {
1252 $url = $this->makeSpecialUrl( $specialName, $query );
1253 // Ignore the query when comparing
1254 $active = ($this->mTitle->getNamespace() == NS_SPECIAL && $this->mTitle->getDBkey() == $specialName);
1255 return strtr( $template,
1256 array(
1257 '$key' => $key,
1258 '$classactive' => $active ? 'class="active"' : '',
1259 '$class' => '',
1260 '$href' => htmlspecialchars( $url ),
1261 '$text' => $text
1262 ) );
1263 }
1264
1265 function loadRequestValues() {
1266 global $wgRequest;
1267 $this->mAction = $wgRequest->getText( 'action' );
1268 $this->mOldId = $wgRequest->getVal( 'oldid' );
1269 $this->mDiff = $wgRequest->getVal( 'diff' );
1270 $this->mSection = $wgRequest->getVal( 'section' );
1271 $this->mSearch = $wgRequest->getVal( 'search' );
1272 $this->mRequestValuesLoaded = true;
1273 }
1274
1275
1276
1277 /** Get the action parameter of the request */
1278 function getAction() {
1279 if ( !isset( $this->mRequestValuesLoaded ) ) {
1280 $this->loadRequestValues();
1281 }
1282 return $this->mAction;
1283 }
1284
1285 /** Get the oldid parameter */
1286 function getOldId() {
1287 if ( !isset( $this->mRequestValuesLoaded ) ) {
1288 $this->loadRequestValues();
1289 }
1290 return $this->mOldId;
1291 }
1292
1293 /** Get the diff parameter */
1294 function getDiff() {
1295 if ( !isset( $this->mRequestValuesLoaded ) ) {
1296 $this->loadRequestValues();
1297 }
1298 return $this->mDiff;
1299 }
1300
1301 function getSection() {
1302 if ( !isset( $this->mRequestValuesLoaded ) ) {
1303 $this->loadRequestValues();
1304 }
1305 return $this->mSection;
1306 }
1307
1308 function getSearch() {
1309 if ( !isset( $this->mRequestValuesLoaded ) ) {
1310 $this->loadRequestValues();
1311 }
1312 return $this->mSearch;
1313 }
1314
1315 /** Make a special page URL of the form [[Special:Somepage/{title_urlform}]] */
1316 function makeSpecialParamUrl( $name, $query = '', $param = '{title_urlform}' ) {
1317 // Abuse makeTitle's lax validity checking to slip a control character into the URL
1318 $title = Title::makeTitle( NS_SPECIAL, "$name/\x1a" );
1319 $url = cbt_escape( $title->getLocalURL( $query ) );
1320 // Now replace it with the parameter
1321 return str_replace( '%1A', $param, $url );
1322 }
1323
1324 function getSubjectPage() {
1325 if ( !isset( $this->mSubjectPage ) ) {
1326 $this->mSubjectPage = $this->mTitle->getSubjectPage();
1327 }
1328 return $this->mSubjectPage;
1329 }
1330
1331 function getTalkPage() {
1332 if ( !isset( $this->mTalkPage ) ) {
1333 $this->mTalkPage = $this->mTitle->getTalkPage();
1334 }
1335 return $this->mTalkPage;
1336 }
1337 }
1338 ?>