r47367 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47366‎ | r47367 | r47368 >
Date:17:45, 17 February 2009
Author:tparscal
Status:deferred
Tags:
Comment:
Changed caching scheme.
Modified paths:
  • /trunk/extensions/CommunityVoice/CommunityVoice.page.php (modified) (history)
  • /trunk/extensions/CommunityVoice/CommunityVoice.php (modified) (history)
  • /trunk/extensions/CommunityVoice/CommunityVoice.sql (modified) (history)
  • /trunk/extensions/CommunityVoice/Modules/Ratings.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CommunityVoice/CommunityVoice.php
@@ -37,48 +37,26 @@
3838 'name' => 'CommunityVoice',
3939 'author' => 'Trevor Parscal',
4040 'url' => 'http://www.mediawiki.org/wiki/Extension:CommunityVoice',
41 - 'descriptionmsg' => 'communityvoice-desc',
 41+ 'description-msg' => 'communityvoice-desc',
4242 'version' => '0.1.0',
4343 );
44 -
4544 // Shortcut to this extension directory
4645 $dir = dirname( __FILE__ ) . '/';
47 -
4846 // Internationalization
4947 $wgExtensionMessagesFiles['CommunityVoice'] = $dir . 'CommunityVoice.i18n.php';
50 -$wgExtensionAliasesFiles['CommunityVoice'] = $dir . 'CommunityVoice.alias.php';
51 -
5248 // Class Autoloading
5349 $wgAutoloadClasses['CommunityVoice'] = $dir . 'CommunityVoice.php';
5450 $wgAutoloadClasses['CommunityVoicePage'] = $dir . 'CommunityVoice.page.php';
5551 $wgAutoloadClasses['CommunityVoiceRatings'] = $dir . 'Modules/Ratings.php';
56 -
57 -// Special Pages
 52+// Spacial Pages
5853 $wgSpecialPages['CommunityVoice'] = 'CommunityVoicePage';
59 -$wgSpecialPageGroups['CommunityVoice'] = 'wiki';
60 -
6154 // Setup Hooks
6255 $wgExtensionFunctions[] = 'CommunityVoice::registerModules';
6356 $wgHooks['AjaxAddScript'][] = 'CommunityVoice::addScripts';
6457 $wgHooks['BeforePageDisplay'][] = 'CommunityVoice::addStyles';
65 -$wgHooks['LoadExtensionSchemaUpdates'][] = 'efCheckSchema';
6658
67 -function efCheckSchema() {
68 - // Get a connection
69 - $db = wfGetDB( DB_MASTER );
70 - // Create table if it doesn't exist
71 - if ( !$db->tableExists( 'cv_ratings_votes' ) ) {
72 - $db->sourceFile( dirname( __FILE__ ) . '/CommunityVoice.sql' );
73 - }
74 - if ( !$db->tableExists( 'cv_ratings_usage' ) ) {
75 - $db->sourceFile( dirname( __FILE__ ) . '/CommunityVoice.sql' );
76 - }
77 - // Continue
78 - return true;
79 -}
80 -
8159 /* Classes */
82 -// FIXME: classes should be put in their own files
 60+
8361 abstract class CommunityVoice {
8462
8563 /* Static Members */
@@ -94,7 +72,12 @@
9573 return array_keys( self::$modules );
9674 }
9775
98 - public static function callModuleAction( $module, $type, $action = '' ) {
 76+ public static function callModuleAction(
 77+ $module,
 78+ $type,
 79+ $action = '',
 80+ $path = ''
 81+ ) {
9982 // Checks for class
10083 if ( isset( self::$modules[$module] ) ) {
10184 if ( class_exists( self::$modules[$module]['class'] ) ) {
@@ -105,7 +88,7 @@
10689 // Checks callability
10790 if ( is_callable( $function ) ) {
10891 // Calls function on class
109 - return call_user_func( $function );
 92+ return call_user_func( $function, $path );
11093 } else {
11194 // Throws unfound/uncallable function exception
11295 throw new MWException(
@@ -130,13 +113,17 @@
131114 */
132115 public static function registerModules() {
133116 // Loops over each module
134 - foreach ( self::getModules() as $module ) {
 117+ foreach( self::getModules() as $module ) {
135118 self::callModuleAction( $module, 'register' );
136119 }
137120 return true;
138121 }
139122
140 - public static function getMessage( $module, $message, $parameter = null ) {
 123+ public static function getMessage(
 124+ $module,
 125+ $message,
 126+ $parameter = null
 127+ ) {
141128 // Checks if extension messages have been loaded already
142129 if ( !self::$messagesLoaded ) {
143130 // Loads extension messages
@@ -147,20 +134,14 @@
148135 return wfMsg( 'communityvoice-' . $module . '-' . $message, $parameter );
149136 }
150137
151 - public static function touchArticle( $article ) {
152 - // Gets the title of the article which included the scale
153 - $articleTitle = Title::newFromText( $article );
154 - // Invalidates the cache of the article
155 - $articleTitle->invalidateCache();
156 - }
157 -
158138 /**
159139 * Adds scripts to document
160140 */
161 - public static function addScripts( $out ) {
 141+ public static function addScripts(
 142+ $out
 143+ ) {
162144 global $wgJsMimeType;
163145 global $egCommunityVoiceResourcesPath;
164 -
165146 $out->addInlineScript(
166147 sprintf(
167148 "var egCommunityVoiceResourcesPath = '%s';\n" ,
@@ -198,4 +179,4 @@
199180 );
200181 return true;
201182 }
202 -}
 183+}
\ No newline at end of file
Index: trunk/extensions/CommunityVoice/CommunityVoice.sql
@@ -2,8 +2,6 @@
33 -- SQL for CommunityVoice Extension
44 --
55 -- Table for ratings
6 -
76 DROP TABLE IF EXISTS /*$wgDBprefix*/cv_ratings_votes;
87 CREATE TABLE /*$wgDBPrefix*/cv_ratings_votes (
98 -- Category of item being rated
@@ -18,19 +16,4 @@
1917 INDEX vot_category_title ( vot_category, vot_title ),
2018 INDEX vot_category_title_user ( vot_category, vot_title, vot_user ),
2119 INDEX vot_category_title_rating ( vot_category, vot_title, vot_rating )
22 -) /*$wgDBTableOptions*/;
23 -DROP TABLE IF EXISTS /*$wgDBprefix*/cv_ratings_usage;
24 -CREATE TABLE /*$wgDBPrefix*/cv_ratings_usage (
25 - -- Category of item being rated
26 - usg_category VARBINARY(255) NOT NULL default '',
27 - -- Title of item being rated
28 - usg_title VARBINARY(255) NOT NULL default '',
29 - -- Title of article which includes the rating
30 - usg_article VARBINARY(255) NOT NULL default '',
31 - --
32 - INDEX rat_category_title ( usg_category, usg_title ),
33 - INDEX rat_category_title_article ( usg_category, usg_title, usg_article )
34 -) /*$wgDBTableOptions*/;
 20+) /*$wgDBTableOptions*/;
\ No newline at end of file
Index: trunk/extensions/CommunityVoice/Modules/Ratings.php
@@ -4,7 +4,10 @@
55
66 /* Private Static Functions */
77
8 - private static function getScaleFraction( $rating, $star ) {
 8+ private static function getScaleFraction(
 9+ $rating,
 10+ $star
 11+ ) {
912 if ( floor( $rating ) > $star ) {
1013 return 6;
1114 } else if ( floor( $rating ) < $star ) {
@@ -14,23 +17,6 @@
1518 }
1619 }
1720
18 - private static function getArticlesUsing( $category, $title ) {
19 - $dbr = wfGetDB( DB_SLAVE );
20 - $result = $dbr->select(
21 - 'cv_ratings_usage',
22 - 'usg_article',
23 - array(
24 - 'usg_category' => $category,
25 - 'usg_title' => $title,
26 - )
27 - );
28 - $articles = array();
29 - while ( $row = $result->fetchRow() ) {
30 - $articles[] = (string)$row['usg_article'];
31 - }
32 - return $articles;
33 - }
34 -
3521 private static function getCategories() {
3622 $dbr = wfGetDB( DB_SLAVE );
3723 $result = $dbr->select(
@@ -44,7 +30,9 @@
4531 return $categories;
4632 }
4733
48 - private static function getTitles( $category ) {
 34+ private static function getTitles(
 35+ $category
 36+ ) {
4937 $dbr = wfGetDB( DB_SLAVE );
5038 $result = $dbr->select(
5139 'cv_ratings_votes',
@@ -58,7 +46,10 @@
5947 return $titles;
6048 }
6149
62 - private static function getTotalVotes( $category, $title ) {
 50+ private static function getTotalVotes(
 51+ $category,
 52+ $title
 53+ ) {
6354 $dbr = wfGetDB( DB_SLAVE );
6455 return (integer)$dbr->selectField(
6556 'cv_ratings_votes',
@@ -70,7 +61,10 @@
7162 );
7263 }
7364
74 - private static function getUserVoted( $category, $title ) {
 65+ private static function getUserVoted(
 66+ $category,
 67+ $title
 68+ ) {
7569 global $wgUser;
7670 $dbr = wfGetDB( DB_SLAVE );
7771 return (bool)$dbr->selectField(
@@ -84,7 +78,10 @@
8579 );
8680 }
8781
88 - private static function getAverageRating( $category, $title ) {
 82+ private static function getAverageRating(
 83+ $category,
 84+ $title
 85+ ) {
8986 $dbr = wfGetDB( DB_SLAVE );
9087 return (float)$dbr->selectField(
9188 'cv_ratings_votes',
@@ -96,7 +93,11 @@
9794 );
9895 }
9996
100 - private static function addVote( $category, $title, $rating ) {
 97+ private static function addVote(
 98+ $category,
 99+ $title,
 100+ $rating
 101+ ) {
101102 global $wgUser;
102103 // Checks if...
103104 if (
@@ -131,62 +132,18 @@
132133 $wgParser->setHook( 'ratings:scale', array( __CLASS__, 'renderScale' ) );
133134 // Register ajax response hook
134135 $wgAjaxExportList[] = __CLASS__ . '::handleScaleVoteCall';
135 - // Register article save hook
136 - $wgHooks['ArticleSave'][] = __CLASS__ . '::updateArticleUsage';
137136
138137 }
139138
140 - public static function updateArticleUsage( $article, $user, $text, $summary, $minor, $watch, $sectionanchor, $flags ) {
141 - $usedRatings = array();
142 - // Extract all ratings:scale tags
143 - preg_match_all(
144 - "/<ratings:scale[^>]*[\\/]*>/i", $text, $matches, PREG_PATTERN_ORDER
145 - );
146 - // Loop over each match
147 - foreach ( $matches[0] as $match ) {
148 - $rating = array();
149 - foreach ( array( 'category', 'title' ) as $attribute ) {
150 - // Extract value of attribute
151 - preg_match(
152 - "/{$attribute}=['\"]*(?<value>[^'\"]*)['\"]*/i",
153 - $match,
154 - $values
155 - );
156 - if ( isset( $values['value'] ) ) {
157 - $rating[$attribute] = $values['value'];
158 - }
159 - }
160 - if ( isset( $rating['category'], $rating['title'] ) ) {
161 - $usedRatings[] = $rating;
162 - }
163 - }
164 - // Gets name of article
165 - $articleDbKey = $article->getTitle()->getPrefixedDBkey();
166 - // Get database connection
167 - $dbw = wfGetDB( DB_MASTER );
168 - // Remove all usage for this article
169 - $dbw->delete(
170 - 'cv_ratings_usage',
171 - array( 'usg_article' => $articleDbKey )
172 - );
173 - // Loop over each rating
174 - foreach ( $usedRatings as $rating ) {
175 - // Add usage for rating for this article
176 - $dbw->insert(
177 - 'cv_ratings_usage',
178 - array(
179 - 'usg_category' => $rating['category'],
180 - 'usg_title' => $rating['title'],
181 - 'usg_article' => $articleDbKey,
182 - )
183 - );
184 - }
185 - return true;
186 - }
187 -
188 - public static function renderScale( $input, $args, $parser ) {
 139+ public static function renderScale(
 140+ $input,
 141+ $args,
 142+ $parser
 143+ ) {
189144 global $wgUser, $wgTitle;
190145 global $egCommunityVoiceResourcesPath;
 146+ // Disable caching
 147+ $parser->disableCache();
191148 // Validate and sanitize incoming arguments
192149 $errors = array();
193150 $error = false;
@@ -195,11 +152,9 @@
196153 $args[$argument] = htmlspecialchars( $args[$argument] );
197154 } else {
198155 $error = true;
199 - if ( $parser->getOptions()->getIsPreview() ) {
200 - $errors[] = CommunityVoice::getMessage(
201 - 'ratings', 'error-missing-argument', $argument
202 - );
203 - }
 156+ $errors[] = CommunityVoice::getMessage(
 157+ 'ratings', 'error-missing-argument', $argument
 158+ );
204159 }
205160 }
206161 // Checks if an error ocurred
@@ -235,8 +190,13 @@
236191 // Adds content of tag as parsed wiki-text
237192 $htmlOut .= $parser->recursiveTagParse( $input );
238193 }
239 - // Checks if the user has not voted yet and is logged in
240 - if ( !$userVoted && $wgUser->isLoggedIn() ) {
 194+ // Checks if...
 195+ if (
 196+ // User has not voted yet
 197+ !$userVoted &&
 198+ // User is logged in
 199+ $wgUser->isLoggedIn()
 200+ ) {
241201
242202 /* Ajax Interaction */
243203
@@ -311,7 +271,7 @@
312272 $htmlOut .= Html::input(
313273 array(
314274 'type' => 'image',
315 - 'name' => 'scale[rating_' . $i . ']',
 275+ 'name' => 'scale[rating_' . ( $i + 1 ) . ']',
316276 'src' => sprintf(
317277 '%s/Icons/star-%d.png',
318278 $egCommunityVoiceResourcesPath,
@@ -366,7 +326,12 @@
367327 /**
368328 * Hanlder for ratings scale vote via ajax call
369329 */
370 - public static function handleScaleVoteCall( $category, $title, $rating, $article ) {
 330+ public static function handleScaleVoteCall(
 331+ $category,
 332+ $title,
 333+ $rating,
 334+ $article
 335+ ) {
371336 global $wgUser;
372337 // Adds vote and checks for success
373338 if ( self::addVote( $category, $title, $rating ) ) {
@@ -384,13 +349,6 @@
385350 )
386351 ),
387352 );
388 - // Gets articles that use this rating
389 - $articles = self::getArticlesUsing( $category, $title );
390 - // Loops over each article
391 - foreach ( $articles as $article ) {
392 - // Invalidates the cache of the article
393 - CommunityVoice::touchArticle( $article );
394 - }
395353 // Ensure database commits take place (since this is an ajax call)
396354 $dbw = wfGetDB( DB_MASTER );
397355 $dbw->commit();
@@ -437,15 +395,6 @@
438396 $scale['category'], $scale['title'], $rating
439397 )
440398 ) {
441 - // Gets articles that use this rating
442 - $articles = self::getArticlesUsing(
443 - $scale['category'], $scale['title']
444 - );
445 - // Loops over each article
446 - foreach ( $articles as $article ) {
447 - // Invalidates the cache of the article
448 - CommunityVoice::touchArticle( $article );
449 - }
450399 // Redirects user back to article
451400 $wgOut->redirect(
452401 Title::newFromText( $scale['article'] )->getFullUrl()
@@ -466,13 +415,15 @@
467416 /**
468417 * Outputs a summary UI for the module
469418 */
470 - public static function showSummary() {
 419+ public static function showSummary(
 420+ $path
 421+ ) {
471422 global $wgOut;
472423 //
473424 $wgOut->addWikiText( '==== Categories ====' );
474425 $xmlCategories = Html::open( 'ul' );
475426 foreach ( self::getCategories() as $category ) {
476 - $xmlCategories .= Html::tag( 'li', $category );
 427+ $xmlCategories .= Html::tag( 'li', array(), $category );
477428 }
478429 $xmlCategories .= Html::close( 'ul' );
479430 $wgOut->addHtml( $xmlCategories );
@@ -481,7 +432,9 @@
482433 /**
483434 * Outputs main UI for module
484435 */
485 - public static function showMain( $path ) {
 436+ public static function showMain(
 437+ $path
 438+ ) {
486439 global $wgOut;
487440 //
488441 $wgOut->addWikiText( '==== Detailed Information ====' );
Index: trunk/extensions/CommunityVoice/CommunityVoice.page.php
@@ -61,10 +61,10 @@
6262 // Breaks sub into path steps
6363 $path = explode( '/', $sub );
6464 // Checks if a specific module was given
65 - if ( count( $path ) > 0 ) {
 65+ if ( count( $path ) >= 1 && $path[0] != '' ) {
6666 // Calls specific action on module
6767 CommunityVoice::callModuleAction(
68 - $path[0], 'show', count( $path ) >= 2 ? $path[1] : 'Main'
 68+ $path[0], 'show', count( $path ) >= 2 ? $path[1] : 'Main', $path
6969 );
7070 // Finishes page
7171 return true;
@@ -73,10 +73,10 @@
7474 foreach ( CommunityVoice::getModules() as $module ) {
7575 // Adds heading
7676 $wgOut->addWikiText(
77 - '== ' . wfMsg( 'communityvoice-' . $module ) . ' =='
 77+ '== ' . wfMsg( 'communityvoice-' . strtolower( $module ) ) . ' =='
7878 );
7979 // Calls summary action on module
80 - self::callModuleAction( $module, 'show', 'Summary' );
 80+ CommunityVoice::callModuleAction( $module, 'show', 'Summary', $path );
8181 }
8282 // Finishes page
8383 return true;

Follow-up revisions

RevisionCommit summaryAuthorDate
r47377Follow up r47367: 'descriptionmsg' is a fix keyword and may not be changedraymond20:52, 17 February 2009

Status & tagging log