Index: trunk/extensions/ArticleFeedback/ArticleFeedback.hooks.php |
— | — | @@ -9,6 +9,12 @@ |
10 | 10 | class ArticleFeedbackHooks { |
11 | 11 | |
12 | 12 | 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 | + ), |
13 | 19 | 'ext.articleFeedback' => array( |
14 | 20 | 'scripts' => 'ext.articleFeedback/ext.articleFeedback.js', |
15 | 21 | 'styles' => 'ext.articleFeedback/ext.articleFeedback.css', |
— | — | @@ -159,25 +165,7 @@ |
160 | 166 | * BeforePageDisplay hook |
161 | 167 | */ |
162 | 168 | 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' ); |
182 | 170 | return true; |
183 | 171 | } |
184 | 172 | |
— | — | @@ -196,24 +184,13 @@ |
197 | 185 | return true; |
198 | 186 | } |
199 | 187 | |
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 |
209 | 190 | */ |
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; |
219 | 196 | } |
220 | 197 | } |
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 |
1 | 40 | + text/plain |
Index: trunk/extensions/ArticleFeedback/ArticleFeedback.php |
— | — | @@ -26,6 +26,12 @@ |
27 | 27 | // Extension is "disabled" if this field is an empty array (as per default configuration) |
28 | 28 | $wgArticleFeedbackCategories = array(); |
29 | 29 | |
| 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 | + |
30 | 36 | // Would ordinarily call this articlefeedback but survey names are 16 chars max |
31 | 37 | $wgPrefSwitchSurveys['articlerating'] = array( |
32 | 38 | 'updatable' => false, |
— | — | @@ -84,6 +90,7 @@ |
85 | 91 | $wgHooks['ParserTestTables'][] = 'ArticleFeedbackHooks::parserTestTables'; |
86 | 92 | $wgHooks['BeforePageDisplay'][] = 'ArticleFeedbackHooks::beforePageDisplay'; |
87 | 93 | $wgHooks['ResourceLoaderRegisterModules'][] = 'ArticleFeedbackHooks::resourceLoaderRegisterModules'; |
| 94 | +$wgHooks['ResourceLoaderGetConfigVars'][] = 'ArticleFeedbackHooks::resourceLoaderGetConfigVars'; |
88 | 95 | // API Registration |
89 | 96 | $wgAPIListModules['articlefeedback'] = 'ApiQueryArticleFeedback'; |
90 | 97 | $wgAPIModules['articlefeedback'] = 'ApiArticleFeedback'; |