r82993 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82992‎ | r82993 | r82994 >
Date:00:32, 1 March 2011
Author:tparscal
Status:ok (Comments)
Tags:
Comment:
* Created new ext.articleFeedback.startup module which is a light-weight activator and lazy-loader for ext.articleFeedback
* Moved category activation logic from the server to the client
* Added article lottery activation, which allows a psudo-random number of articles to have ArticleFeedback activated on them
Modified paths:
  • /trunk/extensions/ArticleFeedback/ArticleFeedback.hooks.php (modified) (history)
  • /trunk/extensions/ArticleFeedback/ArticleFeedback.php (modified) (history)
  • /trunk/extensions/ArticleFeedback/modules/ext.articleFeedback/ext.articleFeedback.startup.js (added) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedback/ArticleFeedback.hooks.php
@@ -9,6 +9,12 @@
1010 class ArticleFeedbackHooks {
1111
1212 protected static $modules = array(
 13+ 'ext.articleFeedback.startup' => array(
 14+ 'scripts' => 'ext.articleFeedback/ext.articleFeedback.startup.js',
 15+ 'dependencies' => array(
 16+ 'mediawiki.util',
 17+ ),
 18+ ),
1319 'ext.articleFeedback' => array(
1420 'scripts' => 'ext.articleFeedback/ext.articleFeedback.js',
1521 'styles' => 'ext.articleFeedback/ext.articleFeedback.css',
@@ -159,25 +165,7 @@
160166 * BeforePageDisplay hook
161167 */
162168 public static function beforePageDisplay( $out ) {
163 - global $wgRequest, $wgArticleFeedbackCategories;
164 -
165 - $title = $out->getTitle();
166 -
167 - // Restrict ratings to...
168 - if (
169 - // Main namespace articles
170 - $title->getNamespace() == NS_MAIN
171 - // View pages
172 - && $wgRequest->getVal( 'action', 'view' ) == 'view'
173 - // Current revision
174 - && !$wgRequest->getCheck( 'diff' )
175 - && !$wgRequest->getCheck( 'oldid' )
176 - // Articles in valid categories
177 - && count( $wgArticleFeedbackCategories )
178 - && self::isInCategories( $title->getArticleId(), $wgArticleFeedbackCategories )
179 - ) {
180 - $out->addModules( 'ext.articleFeedback' );
181 - }
 169+ $out->addModules( 'ext.articleFeedback.startup' );
182170 return true;
183171 }
184172
@@ -196,24 +184,13 @@
197185 return true;
198186 }
199187
200 - /* Protected Static Methods */
201 -
202 - /**
203 - * Returns whether an article is in the specified categories
204 - *
205 - * @param $articleId Integer: Article ID
206 - * @param $categories Array: List of category names (without Category: prefix, with underscores)
207 - *
208 - * @return Boolean: True if article is in one of the values of $categories
 188+ /*
 189+ * ResourceLoaderGetConfigVars hook
209190 */
210 - protected static function isInCategories( $articleId, $categories ) {
211 - $dbr = wfGetDB( DB_SLAVE );
212 - return (bool)$dbr->selectRow( 'categorylinks', '1',
213 - array(
214 - 'cl_from' => $articleId,
215 - 'cl_to' => $categories,
216 - ),
217 - __METHOD__
218 - );
 191+ public static function resourceLoaderGetConfigVars( &$vars ) {
 192+ global $wgArticleFeedbackCategories, $wgArticleFeedbackLotteryOdds;
 193+ $vars['wgArticleFeedbackCategories'] = $wgArticleFeedbackCategories;
 194+ $vars['wgArticleFeedbackLotteryOdds'] = $wgArticleFeedbackLotteryOdds;
 195+ return true;
219196 }
220197 }
Index: trunk/extensions/ArticleFeedback/modules/ext.articleFeedback/ext.articleFeedback.startup.js
@@ -0,0 +1,38 @@
 2+/*
 3+ * Script for Article Feedback Extension
 4+ */
 5+
 6+$(document).ready( function() {
 7+ if (
 8+ // Main namespace articles
 9+ mw.config.get( 'wgNamespaceNumber', false ) === 0
 10+ // View pages
 11+ && mw.config.get( 'wgAction', false ) === 'view'
 12+ // Current revision
 13+ && mw.util.getParamValue( 'diff' ) === null
 14+ && mw.util.getParamValue( 'oldid' ) === null
 15+ ) {
 16+ // Category activation
 17+ var articleFeedbackCategories = mw.config.get( 'wgArticleFeedbackCategories', [] );
 18+ var articleCategories = mw.config.get( 'wgCategories', [] );
 19+ var inCategory = false;
 20+ for ( var i = 0; i < articleCategories.length; i++ ) {
 21+ for ( var j = 0; j < articleFeedbackCategories.length; j++ ) {
 22+ if ( articleCategories[i] == articleFeedbackCategories[j] ) {
 23+ inCategory = true;
 24+ // Break 2 levels - could do this with a label, but eww.
 25+ i = articleCategories.length;
 26+ j = articleFeedbackCategories.length;
 27+ }
 28+ }
 29+ }
 30+ // Lottery activation
 31+ var wonLottery = ( Number( mw.config.get( 'wgArticleId', 0 ) ) % 1000 )
 32+ < Number( mw.config.get( 'wgArticleFeedbackLotteryOdds', 0 ) ) * 100;
 33+
 34+ // Lazy loading
 35+ if ( wonLottery || inCategory ) {
 36+ mw.loader.load( 'ext.articleFeedback' );
 37+ }
 38+ }
 39+} );
Property changes on: trunk/extensions/ArticleFeedback/modules/ext.articleFeedback/ext.articleFeedback.startup.js
___________________________________________________________________
Added: svn:mime-type
140 + text/plain
Index: trunk/extensions/ArticleFeedback/ArticleFeedback.php
@@ -26,6 +26,12 @@
2727 // Extension is "disabled" if this field is an empty array (as per default configuration)
2828 $wgArticleFeedbackCategories = array();
2929
 30+// Articles not categorized as on of the values in $wgArticleFeedbackCategories can still have the
 31+// tool psudo-randomly activated by applying the following odds to a lottery based on $wgArticleId.
 32+// The value can be a floating point number (percentage) in range of 0 - 100. Tenths of a percent
 33+// are the smallest increments used.
 34+$wgArticleFeedbackLotteryOdds = 0;
 35+
3036 // Would ordinarily call this articlefeedback but survey names are 16 chars max
3137 $wgPrefSwitchSurveys['articlerating'] = array(
3238 'updatable' => false,
@@ -84,6 +90,7 @@
8591 $wgHooks['ParserTestTables'][] = 'ArticleFeedbackHooks::parserTestTables';
8692 $wgHooks['BeforePageDisplay'][] = 'ArticleFeedbackHooks::beforePageDisplay';
8793 $wgHooks['ResourceLoaderRegisterModules'][] = 'ArticleFeedbackHooks::resourceLoaderRegisterModules';
 94+$wgHooks['ResourceLoaderGetConfigVars'][] = 'ArticleFeedbackHooks::resourceLoaderGetConfigVars';
8895 // API Registration
8996 $wgAPIListModules['articlefeedback'] = 'ApiQueryArticleFeedback';
9097 $wgAPIModules['articlefeedback'] = 'ApiArticleFeedback';

Follow-up revisions

RevisionCommit summaryAuthorDate
r86753* Remove local mw>mediaWiki aliasing....krinkle11:09, 23 April 2011

Comments

#Comment by Krinkle (talk | contribs)   21:57, 20 April 2011
  • mediawiki.util is loaded by default on every OutputPage. Not needed as a dependancy, right ?
  • Although I would vote in favor of changing it, right now the convention is still that $ should not be implied as global, instead alias from jQuery. While at it, may as well switch to shorthand method:
jQuery( function( $ ) {
	...
} );

jQuery passes itself as first argument to document.ready function, when only passing a function to jQuery, it assumed you want document ready. (eg. $(fn); is the same as jQuery(document).ready(fn);).

  • The fallbacks to false for wgNamespaceNumber and wgAction seem odd to me. <code<mw.config.get won't cause a "ReferenceError: Can't find var...", instead it returns null if the variable doesn't exist. null can be compared to 0 or 'view' fine.

Status & tagging log