r69845 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69844‎ | r69845 | r69846 >
Date:20:50, 24 July 2010
Author:aaron
Status:deferred
Tags:
Comment:
* Removed pointless db master hit in checkAutoPromote when calling getUserParams
* Set locks via fetchParamsRow when updating
* Refactored & cleaned up FRUserCounters class
* Made uniqueContentPages an array
* Removed duplication in recordDemote
Modified paths:
  • /trunk/extensions/FlaggedRevs/FRUserCounters.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/maintenance/updateAutoPromote.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/FRUserCounters.php
@@ -6,31 +6,27 @@
77 /**
88 * Get params for a user
99 * @param int $uid
10 - * @param string $DBName, optional wiki name
11 - * @returns array $params
 10+ * @param int $flags FR_MASTER, FR_FOR_UPDATE
 11+ * @param string $dBName, optional wiki name
 12+ * @returns array
1213 */
13 - public static function getUserParams( $uid, $DBName = false ) {
14 - $dbw = wfGetDB( DB_MASTER, array(), $DBName );
15 - $row = $dbw->selectRow( 'flaggedrevs_promote',
16 - 'frp_user_params',
17 - array( 'frp_user_id' => $uid ),
18 - __METHOD__
19 - // 'FOR UPDATE'
20 - );
21 - # Parse params
22 - $p = array(); // init
 14+ public static function getUserParams( $uid, $flags = 0, $dBName = false ) {
 15+ $p = array();
 16+ $row = self::fetchParamsRow( $uid, $flags, $dBName );
2317 if ( $row ) {
24 - $flatPars = explode( "\n", trim( $row->frp_user_params ) );
25 - foreach ( $flatPars as $pair ) {
26 - $m = explode( '=', trim( $pair ), 2 );
27 - $key = $m[0];
28 - $value = isset( $m[1] ) ? $m[1] : null;
29 - $p[$key] = $value;
30 - }
 18+ $p = self::expandParams( $row->frp_user_params );
3119 }
32 - # Initialize fields as needed...
 20+ self::setUnitializedFields( $p );
 21+ return $p;
 22+ }
 23+
 24+ /**
 25+ * Initializes unset param fields to their starting values
 26+ * @param &array $p
 27+ */
 28+ protected static function setUnitializedFields( array &$p ) {
3329 if ( !isset( $p['uniqueContentPages'] ) ) {
34 - $p['uniqueContentPages'] = '';
 30+ $p['uniqueContentPages'] = array();
3531 }
3632 if ( !isset( $p['totalContentEdits'] ) ) {
3733 $p['totalContentEdits'] = 0;
@@ -41,31 +37,94 @@
4238 if ( !isset( $p['revertedEdits'] ) ) {
4339 $p['revertedEdits'] = 0;
4440 }
45 - return $p;
4641 }
4742
4843 /**
 44+ * Get the params row for a user
 45+ * @param int $uid
 46+ * @param int $flags FR_MASTER, FR_FOR_UPDATE
 47+ * @param string $dBName, optional wiki name
 48+ * @returns mixed (false or Row)
 49+ */
 50+ protected static function fetchParamsRow( $uid, $flags = 0, $dBName = false ) {
 51+ $options = array();
 52+ if ( $flags & FR_MASTER || $flags & FR_FOR_UPDATE ) {
 53+ $db = wfGetDB( DB_MASTER, array(), $dBName );
 54+ if ( $flags & FR_FOR_UPDATE ) $options[] = 'FOR UPDATE';
 55+ } else {
 56+ $db = wfGetDB( DB_SLAVE, array(), $dBName );
 57+ }
 58+ return $db->selectRow( 'flaggedrevs_promote',
 59+ 'frp_user_params',
 60+ array( 'frp_user_id' => $uid ),
 61+ __METHOD__,
 62+ $options
 63+ );
 64+ }
 65+
 66+ /**
4967 * Save params for a user
5068 * @param int $uid
5169 * @param array $params
52 - * @param string $DBName, optional wiki name
 70+ * @param string $dBName, optional wiki name
5371 * @returns bool success
5472 */
55 - public static function saveUserParams( $uid, array $params, $DBName = false ) {
56 - $flatParams = '';
57 - foreach ( $params as $key => $value ) {
58 - $flatParams .= "{$key}={$value}\n";
59 - }
60 - $dbw = wfGetDB( DB_MASTER, array(), $DBName );
 73+ public static function saveUserParams( $uid, array $params, $dBName = false ) {
 74+ $dbw = wfGetDB( DB_MASTER, array(), $dBName );
6175 $row = $dbw->replace( 'flaggedrevs_promote',
6276 array( 'frp_user_id' ),
63 - array( 'frp_user_id' => $uid, 'frp_user_params' => trim( $flatParams ) ),
 77+ array( 'frp_user_id' => $uid,
 78+ 'frp_user_params' => self::flattenParams( $params ) ),
6479 __METHOD__
6580 );
6681 return ( $dbw->affectedRows() > 0 );
6782 }
6883
6984 /**
 85+ * Flatten params for a user for DB storage
 86+ * Note: param values must be integers
 87+ * @param array $params
 88+ * @returns string
 89+ */
 90+ protected static function flattenParams( array $params ) {
 91+ $flatRows = array();
 92+ foreach ( $params as $key => $value ) {
 93+ if ( strpos( $key, '=' ) !== false || strpos( $key, "\n" ) !== false ) {
 94+ throw new MWException( "flattenParams() - key cannot use '=' or newline" );
 95+ }
 96+ if ( $key === 'uniqueContentPages' ) { // list
 97+ $value = implode( ',', array_map( 'intval', $value ) );
 98+ } else {
 99+ $value = intval( $value );
 100+ }
 101+ $flatRows[] = trim( $key ) . '=' . $value;
 102+ }
 103+ return implode( "\n", $flatRows );
 104+ }
 105+
 106+ /**
 107+ * Expand params for a user from DB storage
 108+ * @param string $flatPars
 109+ * @returns array
 110+ */
 111+ protected static function expandParams( $flatPars ) {
 112+ $p = array(); // init
 113+ $flatPars = explode( "\n", trim( $flatPars ) );
 114+ foreach ( $flatPars as $pair ) {
 115+ $m = explode( '=', trim( $pair ), 2 );
 116+ $key = $m[0];
 117+ $value = isset( $m[1] ) ? $m[1] : null;
 118+ if ( $key === 'uniqueContentPages' ) { // list
 119+ $value = array_map( 'intval', explode( ',', $value ) );
 120+ } else {
 121+ $value = intval( $value );
 122+ }
 123+ $p[$key] = $value;
 124+ }
 125+ return $p;
 126+ }
 127+
 128+ /**
70129 * Update users params array for a user on edit
71130 * @param &array $params
72131 * @param Article $article the article just edited
@@ -77,7 +136,7 @@
78137 # Update any special counters for non-null revisions
79138 $changed = false;
80139 if ( $article->getTitle()->isContentPage() ) {
81 - $pages = explode( ',', trim( $p['uniqueContentPages'] ) ); // page IDs
 140+ $pages = $p['uniqueContentPages']; // page IDs
82141 # Don't let this get bloated for no reason
83142 $maxUniquePages = 50; // some flexibility
84143 if ( is_array( $wgFlaggedRevsAutoconfirm ) &&
@@ -94,12 +153,12 @@
95154 && !in_array( $article->getId(), $pages ) )
96155 {
97156 $pages[] = $article->getId();
98 - // Clear out any formatting garbage
99 - $p['uniqueContentPages'] = preg_replace( '/^,/', '', implode( ',', $pages ) );
 157+ $p['uniqueContentPages'] = $pages;
100158 }
101159 $p['totalContentEdits'] += 1;
102160 $changed = true;
103161 }
 162+ // Record non-automatic summary tally
104163 if ( !preg_match( '/^\/\*.*\*\/$/', $summary ) ) {
105164 $p['editComments'] += 1;
106165 $changed = true;
Index: trunk/extensions/FlaggedRevs/maintenance/updateAutoPromote.inc
@@ -1,6 +1,5 @@
22 <?php
33
4 -
54 function update_autopromote() {
65 global $wgContentNamespaces, $wgFlaggedRevsAutopromote;
76 $batchSize = 50;
@@ -23,7 +22,7 @@
2423 # Go through and clean up missing items, as well as correct fr_quality...
2524 foreach( $res as $row ) {
2625 $user = User::newFromRow( $row );
27 - $p = FRUserCounters::getUserParams( $user->getId() );
 26+ $p = FRUserCounters::getUserParams( $user->getId(), FR_FOR_UPDATE );
2827 $oldp = $p;
2928 # Get edit comments used
3029 $sres = $db->select( 'revision', '1',
@@ -50,12 +49,11 @@
5150 __METHOD__,
5251 array( 'LIMIT' => max($wgFlaggedRevsAutopromote['uniqueContentPages'],50) )
5352 );
54 - $uniquePages = array();
 53+ $p['uniqueContentPages'] = array();
5554 foreach( $sres as $row ) {
56 - $uniquePages[] = $row->rev_page;
 55+ $p['uniqueContentPages'][] = (int)$row->rev_page;
5756 }
58 - $p['uniqueContentPages'] = implode($uniquePages,',');
59 -
 57+ # Save the new params...
6058 if( $oldp != $p ) {
6159 FRUserCounters::saveUserParams( $user->getId(), $p );
6260 $changed++;
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php
@@ -960,7 +960,7 @@
961961 # Mark when a user reverts another user, but not self-reverts
962962 $badUserId = $badRev->getRawUser();
963963 if ( $badUserId && $user->getId() != $badUserId ) {
964 - $p = FRUserCounters::getUserParams( $badUserId );
 964+ $p = FRUserCounters::getUserParams( $badUserId, FR_FOR_UPDATE );
965965 if ( !isset( $p['revertedEdits'] ) ) {
966966 $p['revertedEdits'] = 0;
967967 }
@@ -983,7 +983,7 @@
984984 if ( $badRev && $badRev->getRawUser()
985985 && $badRev->getRawUser() != $rev->getRawUser() )
986986 {
987 - $p = FRUserCounters::getUserParams( $badRev->getRawUser() );
 987+ $p = FRUserCounters::getUserParams( $badRev->getRawUser(), FR_FOR_UPDATE );
988988 if ( !isset( $p['revertedEdits'] ) ) {
989989 $p['revertedEdits'] = 0;
990990 }
@@ -1087,7 +1087,7 @@
10881088 $totalCheckedEditsNeeded = true;
10891089 }
10901090 # Check if user edited enough unique pages
1091 - $pages = explode( ',', trim( $p['uniqueContentPages'] ) ); // page IDs
 1091+ $pages = $p['uniqueContentPages']; // page IDs
10921092 if ( $wgFlaggedRevsAutoconfirm['uniqueContentPages'] > count( $pages ) ) {
10931093 return true;
10941094 }
@@ -1178,7 +1178,7 @@
11791179 } elseif ( !$wgFlaggedRevsAutopromote && !$wgFlaggedRevsAutoconfirm ) {
11801180 return true;
11811181 }
1182 - $p = FRUserCounters::getUserParams( $user->getId() );
 1182+ $p = FRUserCounters::getUserParams( $user->getId(), FR_FOR_UPDATE );
11831183 $changed = FRUserCounters::updateUserParams( $p, $article, $summary );
11841184 # Save any updates to user params
11851185 if ( $changed ) {
@@ -1210,7 +1210,7 @@
12111211 $totalCheckedEditsNeeded = true;
12121212 }
12131213 # Check if user edited enough unique pages
1214 - $pages = explode( ',', trim( $p['uniqueContentPages'] ) ); // page IDs
 1214+ $pages = $p['uniqueContentPages']; // page IDs
12151215 if ( $wgFlaggedRevsAutopromote['uniqueContentPages'] > count( $pages ) ) {
12161216 return true;
12171217 }
@@ -1370,19 +1370,16 @@
13711371 /**
13721372 * Record demotion so that auto-promote will be disabled
13731373 */
1374 - public static function recordDemote( $u, $addgroup, $removegroup ) {
 1374+ public static function recordDemote( $user, array $addgroup, array $removegroup ) {
13751375 if ( $removegroup && in_array( 'editor', $removegroup ) ) {
1376 - // Cross-wiki rights change
1377 - if ( $u instanceof UserRightsProxy ) {
1378 - $params = FRUserCounters::getUserParams( $u->getId(), $u->getDBName() );
1379 - $params['demoted'] = 1;
1380 - FRUserCounters::saveUserParams( $u->getId(), $params, $u->getDBName() );
1381 - // On-wiki rights change
1382 - } else {
1383 - $params = FRUserCounters::getUserParams( $u->getId() );
1384 - $params['demoted'] = 1;
1385 - FRUserCounters::saveUserParams( $u->getId(), $params );
 1376+ $dbName = false; // this wiki
 1377+ // Cross-wiki rights changes...
 1378+ if ( $user instanceof UserRightsProxy ) {
 1379+ $dbName = $user->getDBName(); // use foreign DB of the user
13861380 }
 1381+ $p = FRUserCounters::getUserParams( $user->getId(), FR_FOR_UPDATE, $dbName );
 1382+ $p['demoted'] = 1;
 1383+ FRUserCounters::saveUserParams( $user->getId(), $p, $dbName );
13871384 }
13881385 return true;
13891386 }

Status & tagging log