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