PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/Ratings/starrating/RatingsStars.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 238 lines | 113 code | 37 blank | 88 comment | 5 complexity | ed4450cf835cfb9cda339b9861873109 MD5 | raw file
  1. <?php
  2. /**
  3. * Class to render star ratings.
  4. *
  5. * @since 0.1
  6. *
  7. * @file RatingsStars.php
  8. * @ingroup Ratings
  9. *
  10. * @licence GNU GPL v3 or later
  11. * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  12. */
  13. final class RatingsStars extends ParserHook {
  14. /**
  15. * No LSB in pre-5.3 PHP *sigh*.
  16. * This is to be refactored as soon as php >=5.3 becomes acceptable.
  17. */
  18. public static function staticMagic( array &$magicWords, $langCode ) {
  19. $instance = new self;
  20. return $instance->magic( $magicWords, $langCode );
  21. }
  22. /**
  23. * No LSB in pre-5.3 PHP *sigh*.
  24. * This is to be refactored as soon as php >=5.3 becomes acceptable.
  25. */
  26. public static function staticInit( Parser &$parser ) {
  27. $instance = new self;
  28. return $instance->init( $parser );
  29. }
  30. /**
  31. * Gets the name of the parser hook.
  32. * @see ParserHook::getName
  33. *
  34. * @since 0.1
  35. *
  36. * @return string
  37. */
  38. protected function getName() {
  39. return array( 'starrating' );
  40. }
  41. /**
  42. * Returns an array containing the parameter info.
  43. * @see ParserHook::getParameterInfo
  44. *
  45. * @since 0.1
  46. *
  47. * @return array
  48. */
  49. protected function getParameterInfo( $type ) {
  50. global $egRatingsShowWhenDisabled, $egRatingsIncSummary;
  51. $params = array();
  52. $params['page'] = new Parameter( 'page' );
  53. $params['page']->setDescription( wfMsg( 'ratings-par-page' ) );
  54. $params['page']->setDefault( false, false );
  55. $params['tag'] = new Parameter( 'tag' );
  56. $params['tag']->setDescription( wfMsg( 'ratings-par-tag' ) );
  57. $params['showdisabled'] = new Parameter( 'showdisabled', Parameter::TYPE_BOOLEAN );
  58. $params['showdisabled']->setDescription( wfMsg( 'ratings-par-showdisabled' ) );
  59. $params['showdisabled']->setDefault( $egRatingsShowWhenDisabled );
  60. $params['incsummary'] = new Parameter( 'incsummary', Parameter::TYPE_BOOLEAN );
  61. $params['incsummary']->setDescription( wfMsg( 'ratings-par-incsummary' ) );
  62. $params['incsummary']->setDefault( $egRatingsIncSummary );
  63. return $params;
  64. }
  65. /**
  66. * Returns the list of default parameters.
  67. * @see ParserHook::getDefaultParameters
  68. *
  69. * @since 0.1
  70. *
  71. * @return array
  72. */
  73. protected function getDefaultParameters( $type ) {
  74. return array( 'tag', 'page' );
  75. }
  76. /**
  77. * Renders and returns the output.
  78. * @see ParserHook::render
  79. *
  80. * @since 0.1
  81. *
  82. * @param array $parameters
  83. *
  84. * @return string
  85. */
  86. public function render( array $parameters ) {
  87. $this->loadJs( $parameters );
  88. $parameters['page'] = $parameters['page'] === false ? $GLOBALS['wgTitle'] : Title::newFromText( $parameters['page'] );
  89. static $ratingStarNr = 0; $ratingStarNr++;
  90. $inputs = array();
  91. for ( $i = 0; $i < 5; $i++ ) {
  92. $inputs[] = Html::element(
  93. 'input',
  94. array(
  95. 'class' => 'starrating',
  96. 'type' => 'radio',
  97. 'name' => 'ratingstars_' . $ratingStarNr,
  98. 'value' => $i,
  99. 'page' => $parameters['page']->getFullText(),
  100. 'tag' => $parameters['tag'],
  101. )
  102. );
  103. }
  104. if ( $parameters['incsummary'] ) {
  105. array_unshift( $inputs, htmlspecialchars( Ratings::getRatingSummaryMessage( $parameters['page'], $parameters['tag'] ) ) . '<br />' );
  106. }
  107. return Html::rawElement(
  108. 'div',
  109. array( 'style' => 'display:none; position:static', 'class' => 'starrating-div' ),
  110. implode( '', $inputs )
  111. );
  112. }
  113. /**
  114. * Loads the needed JavaScript.
  115. * Takes care of non-RL compatibility.
  116. *
  117. * @since 0.1
  118. *
  119. * @param array $parameters
  120. */
  121. protected function loadJs( array $parameters ) {
  122. static $loadedJs = false;
  123. if ( $loadedJs ) {
  124. return;
  125. }
  126. $loadedJs = true;
  127. $this->addJSWikiData( $parameters );
  128. // For backward compatibility with MW < 1.17.
  129. if ( is_callable( array( $this->parser->getOutput(), 'addModules' ) ) ) {
  130. $this->parser->getOutput()->addModules( 'ext.ratings.stars' );
  131. }
  132. else {
  133. global $egRatingsScriptPath, $wgStylePath, $wgStyleVersion;
  134. $this->addJSLocalisation();
  135. $this->parser->getOutput()->addHeadItem(
  136. Html::linkedScript( "$wgStylePath/common/jquery.min.js?$wgStyleVersion" ),
  137. 'jQuery'
  138. );
  139. Ratings::loadJs( $this->parser );
  140. $this->parser->getOutput()->addHeadItem(
  141. Html::linkedScript( $egRatingsScriptPath . '/starrating/star-rating/jquery.rating.js' )
  142. . Html::linkedStyle( $egRatingsScriptPath . '/starrating/star-rating/jquery.rating.css' ),
  143. 'ext.ratings.stars.jquery'
  144. );
  145. $this->parser->getOutput()->addHeadItem(
  146. Html::linkedScript( $egRatingsScriptPath . '/starrating/ext.ratings.stars.js' ),
  147. 'ext.ratings.stars'
  148. );
  149. }
  150. }
  151. /**
  152. * Ouput the wiki data needed to display the licence links.
  153. *
  154. * @since 0.1
  155. *
  156. * @param array $parameters
  157. */
  158. protected function addJSWikiData( array $parameters ) {
  159. $this->parser->getOutput()->addHeadItem(
  160. Html::inlineScript(
  161. 'var wgRatingsShowDisabled =' . json_encode( $parameters['showdisabled'] ) . ';'
  162. )
  163. );
  164. }
  165. /**
  166. * Adds the needed JS messages to the page output.
  167. * This is for backward compatibility with pre-RL MediaWiki.
  168. *
  169. * @since 0.1
  170. */
  171. protected function addJSLocalisation() {
  172. global $egRatingsStarsJSMessages;
  173. $data = array();
  174. foreach ( $egRatingsStarsJSMessages as $msg ) {
  175. $data[$msg] = wfMsgNoTrans( $msg );
  176. }
  177. $this->parser->getOutput()->addHeadItem( Html::inlineScript( 'var wgRatingsStarsMessages = ' . json_encode( $data ) . ';' ) );
  178. }
  179. /**
  180. * Returns the parser function otpions.
  181. * @see ParserHook::getFunctionOptions
  182. *
  183. * @since 0.1
  184. *
  185. * @return array
  186. */
  187. protected function getFunctionOptions() {
  188. return array(
  189. 'noparse' => true,
  190. 'isHTML' => true
  191. );
  192. }
  193. /**
  194. * @see ParserHook::getDescription()
  195. *
  196. * @since 0.1
  197. */
  198. public function getDescription() {
  199. return wfMsg( 'ratings-starsratings-desc' );
  200. }
  201. }