PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/ReaderFeedback/ReaderFeedback.hooks.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 196 lines | 152 code | 12 blank | 32 comment | 32 complexity | a00e44059419c5b62d455a30a189a130 MD5 | raw file
  1. <?php
  2. class ReaderFeedbackHooks {
  3. /**
  4. * Add ReaderFeedback css/js.
  5. */
  6. public static function injectStyleAndJS() {
  7. global $wgOut, $wgTitle;
  8. if( $wgOut->hasHeadItem( 'ReaderFeedback' ) )
  9. return true; # Don't double-load
  10. if( !isset($wgTitle) || !$wgOut->isArticleRelated() ) {
  11. return self::InjectStyleForSpecial(); // try special page CSS?
  12. }
  13. # Try to only add to relevant pages
  14. if( !ReaderFeedback::isPageRateable($wgTitle) ) {
  15. return true;
  16. }
  17. global $wgScriptPath, $wgJsMimeType, $wgFeedbackStylePath, $wgFeedbackStyleVersion;
  18. $stylePath = str_replace( '$wgScriptPath', $wgScriptPath, $wgFeedbackStylePath );
  19. $cssFile = "$stylePath/readerfeedback.css?$wgFeedbackStyleVersion";
  20. $jsFile = "$stylePath/readerfeedback.js?$wgFeedbackStyleVersion";
  21. // Add CSS
  22. $wgOut->addExtensionStyle( $cssFile );
  23. // Add JS
  24. $wgOut->addScriptFile( $jsFile );
  25. return true;
  26. }
  27. public static function injectJSVars( &$globalVars ) {
  28. $globalVars['wgFeedbackParams'] = ReaderFeedback::getJSFeedbackParams();
  29. $globalVars['wgAjaxFeedback'] = (object) array(
  30. 'sendingMsg' => wfMsgHtml('readerfeedback-submitting'),
  31. 'sentMsg' => wfMsgHtml('readerfeedback-finished')
  32. );
  33. return true;
  34. }
  35. /**
  36. * Add ReaderFeedback css for relevant special pages.
  37. */
  38. public static function InjectStyleForSpecial() {
  39. global $wgTitle, $wgOut;
  40. if( empty($wgTitle) || $wgTitle->getNamespace() !== NS_SPECIAL ) {
  41. return true;
  42. }
  43. $spPages = array( 'RatingHistory' );
  44. foreach( $spPages as $key ) {
  45. if( $wgTitle->isSpecial( $key ) ) {
  46. global $wgScriptPath, $wgFeedbackStylePath, $wgFeedbacktyleVersion;
  47. $stylePath = str_replace( '$wgScriptPath', $wgScriptPath, $wgFeedbackStylePath );
  48. $encCssFile = htmlspecialchars( "$stylePath/readerfeedback.css?$wgFeedbacktyleVersion" );
  49. $wgOut->addExtensionStyle( $encCssFile );
  50. break;
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * Is this a view page action?
  57. * @param $action string
  58. * @returns bool
  59. */
  60. protected static function isViewAction( $action ) {
  61. return ( $action == 'view' || $action == 'purge' || $action == 'render' );
  62. }
  63. public static function addFeedbackForm( &$data, $skin ) {
  64. global $wgOut;
  65. $title = $skin->getTitle();
  66. if( $wgOut->isArticleRelated() ) {
  67. global $wgRequest, $wgUser;
  68. if( !$title->exists() || !ReaderFeedback::isPageRateable($title) || !$wgOut->getRevisionId() ) {
  69. return true;
  70. }
  71. # Check action and if page is protected
  72. $action = $wgRequest->getVal( 'action', 'view' );
  73. if( !self::isViewAction($action) ) {
  74. return true;
  75. }
  76. if( $wgUser->isAllowed( 'feedback' ) ) {
  77. # Only allow votes on the latest revision!
  78. $id = $wgOut->getRevisionId();
  79. if( $id != $title->getLatestRevID() ) {
  80. return true;
  81. }
  82. # If the user already voted, then don't show the form.
  83. # Always show for IPs however, due to squid caching...
  84. if( !$wgUser->getId() || !ReaderFeedbackPage::userAlreadyVoted( $title, $id ) ) {
  85. self::addQuickFeedback( $data, false, $title );
  86. }
  87. }
  88. return true;
  89. }
  90. return true;
  91. }
  92. /**
  93. * Adds a brief feedback form to a page.
  94. * @param OutputPage $out
  95. * @param bool $top, should this form always go on top?
  96. * @param Title $title
  97. */
  98. protected static function addQuickFeedback( &$data, $top = false, $title ) {
  99. global $wgOut, $wgUser, $wgFeedbackTags;
  100. # Are there any reader input tags?
  101. if( empty($wgFeedbackTags) ) {
  102. return false;
  103. }
  104. # Revision being displayed
  105. $id = $wgOut->getRevisionId();
  106. # Load required messages
  107. $reviewTitle = SpecialPage::getTitleFor( 'ReaderFeedback' );
  108. $action = $reviewTitle->getLocalUrl( 'action=submit' );
  109. $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action,
  110. 'id' => 'mw-feedbackform' ) );
  111. $form .= Xml::openElement( 'fieldset', array('class' => 'feedback_reviewform noprint') );
  112. $form .= "<legend><strong>" . wfMsgHtml( 'readerfeedback' ) . "</strong></legend>\n";
  113. # Avoid clutter
  114. if( !$wgUser->isAllowed('review') ) {
  115. $form .= wfMsgExt( 'readerfeedback-text', array('parse') );
  116. }
  117. $form .= Xml::openElement( 'span', array('id' => 'mw-feedbackselects') );
  118. # Loop through all different flag types
  119. foreach( ReaderFeedback::getFeedbackTags() as $quality => $levels ) {
  120. $selected = ( isset($flags[$quality]) && $flags[$quality] > 0 ) ? $flags[$quality] : -1;
  121. $form .= "<b>" . Xml::label( wfMsgHtml("readerfeedback-$quality"), "wp$quality" ) . ":</b>";
  122. $attribs = array( 'name' => "wp$quality", 'id' => "wp$quality",
  123. 'onchange' => "updateFeedbackForm()" );
  124. $form .= '&#160;' . Xml::openElement( 'select', $attribs );
  125. $levels = array_reverse($levels,true);
  126. foreach( $levels as $i => $name ) {
  127. $optionClass = array( 'class' => "rfb-rating-option-$i" );
  128. $form .= Xml::option( wfMsg("readerfeedback-level-$i"), $i, ($i == $selected), $optionClass ) ."\n";
  129. }
  130. $form .= Xml::option( wfMsg("readerfeedback-level-none"), -1, (-1 == $selected) ) ."\n";
  131. $form .= Xml::closeElement( 'select' )."\n";
  132. }
  133. $form .= Xml::closeElement( 'span' );
  134. $form .= Xml::submitButton( wfMsg('readerfeedback-submit'),
  135. array('id' => 'submitfeedback','accesskey' => wfMsg('readerfeedback-ak-review'),
  136. 'title' => wfMsg('readerfeedback-tt-review').' ['.wfMsg('readerfeedback-ak-review').']' )
  137. );
  138. # Hidden params
  139. $form .= Html::hidden( 'title', $reviewTitle->getPrefixedText() ) . "\n";
  140. $form .= Html::hidden( 'target', $title->getPrefixedDBKey() ) . "\n";
  141. $form .= Html::hidden( 'oldid', $id ) . "\n";
  142. $form .= Html::hidden( 'validatedParams', ReaderFeedbackPage::validationKey( $id, $wgUser->getId() ) );
  143. $form .= Html::hidden( 'action', 'submit') . "\n";
  144. $form .= Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
  145. # Honeypot input
  146. $form .= Xml::input( 'commentary', 12, '', array('style' => 'display:none;') ) . "\n";
  147. $form .= Xml::closeElement( 'fieldset' );
  148. $form .= Xml::closeElement( 'form' );
  149. if( $top ) {
  150. $wgOut->prependHTML( $form );
  151. } else {
  152. $data .= $form;
  153. }
  154. return true;
  155. }
  156. public static function addRatingLink( &$skintemplate, &$nav_urls, &$oldid, &$revid ) {
  157. global $wgTitle;
  158. # Add rating tab
  159. if( isset($wgTitle) && ReaderFeedback::isPageRateable($wgTitle) ) {
  160. $nav_urls['ratinghist'] = array(
  161. 'text' => wfMsg( 'ratinghistory-link' ),
  162. 'href' => $skintemplate->makeSpecialUrl( 'RatingHistory',
  163. "target=" . wfUrlencode( "{$skintemplate->thispage}" ) )
  164. );
  165. }
  166. return true;
  167. }
  168. public static function ratingToolboxLink( &$skin ) {
  169. if( isset( $skin->data['nav_urls']['ratinghist'] ) ) {
  170. ?><li id="t-rating"><?php
  171. ?><a href="<?php echo htmlspecialchars( $skin->data['nav_urls']['ratinghist']['href'] ) ?>"><?php
  172. echo $skin->msg( 'ratinghistory-link' );
  173. ?></a><?php
  174. ?></li><?php
  175. }
  176. return true;
  177. }
  178. public static function onParserTestTables( &$tables ) {
  179. $tables[] = 'reader_feedback';
  180. $tables[] = 'reader_feedback_history';
  181. $tables[] = 'reader_feedback_pages';
  182. return true;
  183. }
  184. }