PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/LiquidThreads/classes/ParserFunctions.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 189 lines | 137 code | 42 blank | 10 comment | 20 complexity | 255a5a6cfdb4192a31073a6605ce3929 MD5 | raw file
  1. <?php
  2. class LqtParserFunctions {
  3. static function useLiquidThreads( &$parser, $param = '1' ) {
  4. $offParams = array( 'no', 'off', 'disable' );
  5. // Figure out if they want to turn it off or on.
  6. $param = trim( strtolower( $param ) );
  7. if ( in_array( $param, $offParams ) || !$param ) {
  8. $param = 0;
  9. } else {
  10. $param = 1;
  11. }
  12. $parser->mOutput->setProperty( 'use-liquid-threads', $param );
  13. }
  14. static function lqtPageLimit( &$parser, $param = null ) {
  15. if ( $param && $param > 0 ) {
  16. $parser->mOutput->setProperty( 'lqt-page-limit', $param );
  17. }
  18. }
  19. /** To bypass the parser cache just for the LiquidThreads part, we have a cute trick.
  20. * We leave a placeholder comment in the HTML, which we expand out in a hook. This way,
  21. * most of the page can be cached, but the LiquidThreads dynamism still works.
  22. * Thanks to Tim for the idea. */
  23. static function lqtTalkPage( $parser, $args, $parser, $frame ) {
  24. $pout = $parser->getOutput();
  25. // Prepare information.
  26. $title = null;
  27. if ( !empty($args['talkpage']) ) {
  28. $title = Title::newFromText( $args['talkpage'] );
  29. }
  30. if ( is_null($title) ) {
  31. $title = $parser->getTitle();
  32. }
  33. $talkpage = new Article( $title, 0 );
  34. $article = new Article( $parser->getTitle(), 0 );
  35. $data = array(
  36. 'type' => 'talkpage',
  37. 'args' => $args,
  38. 'article' => $article,
  39. 'title' => $article->getTitle(),
  40. 'talkpage' => $talkpage,
  41. );
  42. if ( !isset( $pout->mLqtReplacements ) ) {
  43. $pout->mLqtReplacements = array();
  44. }
  45. // Generate a token
  46. $tok = wfGenerateToken();
  47. $text = '<!--LQT-PAGE-'.$tok.'-->';
  48. $pout->mLqtReplacements[$text] = $data;
  49. return $text;
  50. }
  51. static function lqtThread( $parser, $args, $parser, $frame ) {
  52. $pout = $parser->getOutput();
  53. // Prepare information.
  54. $title = Title::newFromText( $args['thread'] );
  55. $thread = null;
  56. if ( $args['thread'] ) {
  57. if ( is_numeric( $args['thread'] ) ) {
  58. $thread = Threads::withId( $args['thread'] );
  59. } elseif ( $title ) {
  60. $article = new Article( $title, 0 );
  61. $thread = Threads::withRoot( $article );
  62. }
  63. }
  64. if ( is_null( $thread ) ) {
  65. return '';
  66. }
  67. $data = array(
  68. 'type' => 'thread',
  69. 'args' => $args,
  70. 'thread' => $thread->id(),
  71. 'title' => $thread->title(),
  72. );
  73. if ( !isset( $pout->mLqtReplacements ) ) {
  74. $pout->mLqtReplacements = array();
  75. }
  76. // Generate a token
  77. $tok = wfGenerateToken();
  78. $text = '<!--LQT-THREAD-'.$tok.'-->';
  79. $pout->mLqtReplacements[$text] = $data;
  80. return $text;
  81. }
  82. static function runLqtTalkPage( $details ) {
  83. $title = $details["title"];
  84. $article = $details["article"];
  85. $talkpage = $details["talkpage"];
  86. $args = $details["args"];
  87. global $wgUser, $wgRequest, $wgOut;
  88. $oldOut = $wgOut->getHTML();
  89. $wgOut->clearHTML();
  90. $view = new TalkpageView( $wgOut, $article, $title, $wgUser, $wgRequest );
  91. $view->setTalkpage( $talkpage );
  92. // Handle show/hide preferences. Header gone by default.
  93. $view->hideItems( 'header' );
  94. if ( array_key_exists( 'show', $args ) ) {
  95. $show = explode( ' ', $args['show'] );
  96. $view->setShownItems( $show );
  97. }
  98. $view->show();
  99. $html = $wgOut->getHTML();
  100. $wgOut->clearHTML();
  101. $wgOut->getHTML( $oldOut );
  102. return $html;
  103. }
  104. static function showLqtThread( $details ) {
  105. $title = $details["title"];
  106. $article = $details["article"];
  107. global $wgUser, $wgRequest, $wgOut;
  108. $oldOut = $wgOut->getHTML();
  109. $wgOut->clearHTML();
  110. $root = new Article( $title, 0 );
  111. $thread = Threads::withRoot( $root );
  112. $view = new LqtView( $wgOut, $article, $title, $wgUser, $wgRequest );
  113. $view->showThread( $thread );
  114. $html = $wgOut->getHTML();
  115. $wgOut->clearHTML();
  116. $wgOut->getHTML( $oldOut );
  117. return $html;
  118. }
  119. static function onAddParserOutput( &$out, $pout ) {
  120. if ( !isset($pout->mLqtReplacements ) ) {
  121. return true;
  122. }
  123. if ( !isset($out->mLqtReplacements) ) {
  124. $out->mLqtReplacements = array();
  125. }
  126. foreach( $pout->mLqtReplacements as $text => $details ) {
  127. $result = '';
  128. if ( $details['type'] == 'talkpage' ) {
  129. $result = self::runLqtTalkPage( $details );
  130. } elseif ( $details['type'] == 'thread' ) {
  131. $result = self::showLqtThread( $details );
  132. }
  133. $out->mLqtReplacements[$text] = $result;
  134. $out->addModules( 'ext.liquidThreads' );
  135. }
  136. return true;
  137. }
  138. static function onAddHTML( &$out, &$text ) {
  139. if ( !isset($out->mLqtReplacements) || !count($out->mLqtReplacements) ) {
  140. return true;
  141. }
  142. $replacements = $out->mLqtReplacements;
  143. $replacer = new ReplacementArray( $replacements );
  144. $text = $replacer->replace( $text );
  145. return true;
  146. }
  147. }