r68622 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68621‎ | r68622 | r68623 >
Date:07:11, 27 June 2010
Author:aaron
Status:ok
Tags:
Comment:
Moved per-user stats functions to new FRUserCounters class
Modified paths:
  • /trunk/extensions/FlaggedRevs/FRUserCounters.php (added) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.class.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/maintenance/updateAutoPromote.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/FRUserCounters.php
@@ -0,0 +1,108 @@
 2+<?php
 3+/**
 4+ * Class containing utility functions for per-user stats
 5+ */
 6+class FRUserCounters {
 7+ /**
 8+ * Get params for a user
 9+ * @param int $uid
 10+ * @param string $DBName, optional wiki name
 11+ * @returns array $params
 12+ */
 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+ );
 20+ # Parse params
 21+ $p = array(); // init
 22+ if ( $row ) {
 23+ $flatPars = explode( "\n", trim( $row->frp_user_params ) );
 24+ foreach ( $flatPars as $pair ) {
 25+ $m = explode( '=', trim( $pair ), 2 );
 26+ $key = $m[0];
 27+ $value = isset( $m[1] ) ? $m[1] : null;
 28+ $p[$key] = $value;
 29+ }
 30+ }
 31+ # Initialize fields as needed...
 32+ if ( !isset( $p['uniqueContentPages'] ) ) {
 33+ $p['uniqueContentPages'] = '';
 34+ }
 35+ if ( !isset( $p['totalContentEdits'] ) ) {
 36+ $p['totalContentEdits'] = 0;
 37+ }
 38+ if ( !isset( $p['editComments'] ) ) {
 39+ $p['editComments'] = 0;
 40+ }
 41+ if ( !isset( $p['revertedEdits'] ) ) {
 42+ $p['revertedEdits'] = 0;
 43+ }
 44+ return $p;
 45+ }
 46+
 47+ /**
 48+ * Save params for a user
 49+ * @param int $uid
 50+ * @param array $params
 51+ * @param string $DBName, optional wiki name
 52+ * @returns bool success
 53+ */
 54+ public static function saveUserParams( $uid, array $params, $DBName = false ) {
 55+ $flatParams = '';
 56+ foreach ( $params as $key => $value ) {
 57+ $flatParams .= "{$key}={$value}\n";
 58+ }
 59+ $dbw = wfGetDB( DB_MASTER, array(), $DBName );
 60+ $row = $dbw->replace( 'flaggedrevs_promote',
 61+ array( 'frp_user_id' ),
 62+ array( 'frp_user_id' => $uid, 'frp_user_params' => trim( $flatParams ) ),
 63+ __METHOD__
 64+ );
 65+ return ( $dbw->affectedRows() > 0 );
 66+ }
 67+
 68+ /**
 69+ * Update users params array for a user on edit
 70+ * @param &array $params
 71+ * @param Article $article the article just edited
 72+ * @param string $summary edit summary
 73+ * @returns bool anything changed
 74+ */
 75+ public static function updateUserParams( array &$p, Article $article, $summary ) {
 76+ global $wgFlaggedRevsAutoconfirm, $wgFlaggedRevsAutopromote;
 77+ # Update any special counters for non-null revisions
 78+ $changed = false;
 79+ if ( $article->getTitle()->isContentPage() ) {
 80+ $pages = explode( ',', trim( $p['uniqueContentPages'] ) ); // page IDs
 81+ # Don't let this get bloated for no reason
 82+ $maxUniquePages = 50; // some flexibility
 83+ if ( is_array( $wgFlaggedRevsAutoconfirm ) &&
 84+ $wgFlaggedRevsAutoconfirm['uniqueContentPages'] > $maxUniquePages )
 85+ {
 86+ $maxUniquePages = $wgFlaggedRevsAutoconfirm['uniqueContentPages'];
 87+ }
 88+ if ( is_array( $wgFlaggedRevsAutopromote ) &&
 89+ $wgFlaggedRevsAutopromote['uniqueContentPages'] > $maxUniquePages )
 90+ {
 91+ $maxUniquePages = $wgFlaggedRevsAutopromote['uniqueContentPages'];
 92+ }
 93+ if ( count( $pages ) < $maxUniquePages // limit the size of this
 94+ && !in_array( $article->getId(), $pages ) )
 95+ {
 96+ $pages[] = $article->getId();
 97+ // Clear out any formatting garbage
 98+ $p['uniqueContentPages'] = preg_replace( '/^,/', '', implode( ',', $pages ) );
 99+ }
 100+ $p['totalContentEdits'] += 1;
 101+ $changed = true;
 102+ }
 103+ if ( $summary != '' ) {
 104+ $p['editComments'] += 1;
 105+ $changed = true;
 106+ }
 107+ return $changed;
 108+ }
 109+}
Property changes on: trunk/extensions/FlaggedRevs/FRUserCounters.php
___________________________________________________________________
Name: svn:eol-style
1110 + native
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.php
@@ -301,6 +301,7 @@
302302 $wgPHPlotDir = $dir . 'phplot-5.0.5';
303303
304304 $wgAutoloadClasses['FlaggedRevs'] = $dir . 'FlaggedRevs.class.php';
 305+$wgAutoloadClasses['FRUserCounters'] = $dir . 'FRUserCounters.php';
305306 $wgAutoloadClasses['FlaggedRevsHooks'] = $dir . 'FlaggedRevs.hooks.php';
306307 $wgAutoloadClasses['FlaggedRevsLogs'] = $dir . 'FlaggedRevsLogs.php';
307308 $wgAutoloadClasses['FRCacheUpdate'] = $dir . 'FRCacheUpdate.php';
Index: trunk/extensions/FlaggedRevs/maintenance/updateAutoPromote.inc
@@ -23,7 +23,7 @@
2424 # Go through and clean up missing items, as well as correct fr_quality...
2525 foreach( $res as $row ) {
2626 $user = User::newFromRow( $row );
27 - $p = FlaggedRevs::getUserParams( $user->getId() );
 27+ $p = FRUserCounters::getUserParams( $user->getId() );
2828 $oldp = $p;
2929 # Get edit comments used
3030 $sres = $db->select( 'revision', '1',
@@ -57,7 +57,7 @@
5858 $p['uniqueContentPages'] = implode($uniquePages,',');
5959
6060 if( $oldp != $p ) {
61 - FlaggedRevs::saveUserParams( $user->getId(), $p );
 61+ FRUserCounters::saveUserParams( $user->getId(), $p );
6262 $changed++;
6363 }
6464
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.class.php
@@ -1383,106 +1383,6 @@
13841384 $dbw->delete( 'flaggedpage_pending', array( 'fpp_page_id' => $pageId ), __METHOD__ );
13851385 }
13861386
1387 - /**
1388 - * Get params for a user
1389 - * @param int $uid
1390 - * @param string $DBName, optional wiki name
1391 - * @returns array $params
1392 - */
1393 - public static function getUserParams( $uid, $DBName = false ) {
1394 - $dbw = wfGetDB( DB_MASTER, array(), $DBName );
1395 - $row = $dbw->selectRow( 'flaggedrevs_promote',
1396 - 'frp_user_params',
1397 - array( 'frp_user_id' => $uid ),
1398 - __METHOD__
1399 - );
1400 - # Parse params
1401 - $p = array(); // init
1402 - if ( $row ) {
1403 - $flatPars = explode( "\n", trim( $row->frp_user_params ) );
1404 - foreach ( $flatPars as $pair ) {
1405 - $m = explode( '=', trim( $pair ), 2 );
1406 - $key = $m[0];
1407 - $value = isset( $m[1] ) ? $m[1] : null;
1408 - $p[$key] = $value;
1409 - }
1410 - }
1411 - # Initialize fields as needed...
1412 - if ( !isset( $p['uniqueContentPages'] ) )
1413 - $p['uniqueContentPages'] = '';
1414 - if ( !isset( $p['totalContentEdits'] ) )
1415 - $p['totalContentEdits'] = 0;
1416 - if ( !isset( $p['editComments'] ) )
1417 - $p['editComments'] = 0;
1418 - if ( !isset( $p['revertedEdits'] ) )
1419 - $p['revertedEdits'] = 0;
1420 -
1421 - return $p;
1422 - }
1423 -
1424 - /**
1425 - * Save params for a user
1426 - * @param int $uid
1427 - * @param array $params
1428 - * @param string $DBName, optional wiki name
1429 - * @returns bool success
1430 - */
1431 - public static function saveUserParams( $uid, array $params, $DBName = false ) {
1432 - $flatParams = '';
1433 - foreach ( $params as $key => $value ) {
1434 - $flatParams .= "{$key}={$value}\n";
1435 - }
1436 - $dbw = wfGetDB( DB_MASTER, array(), $DBName );
1437 - $row = $dbw->replace( 'flaggedrevs_promote',
1438 - array( 'frp_user_id' ),
1439 - array( 'frp_user_id' => $uid, 'frp_user_params' => trim( $flatParams ) ),
1440 - __METHOD__
1441 - );
1442 - return ( $dbw->affectedRows() > 0 );
1443 - }
1444 -
1445 - /**
1446 - * Update users params array for a user on edit
1447 - * @param &array $params
1448 - * @param Article $article the article just edited
1449 - * @param string $summary edit summary
1450 - * @returns bool anything changed
1451 - */
1452 - public static function updateUserParams( array &$p, Article $article, $summary ) {
1453 - global $wgFlaggedRevsAutoconfirm, $wgFlaggedRevsAutopromote;
1454 - # Update any special counters for non-null revisions
1455 - $changed = false;
1456 - if ( $article->getTitle()->isContentPage() ) {
1457 - $pages = explode( ',', trim( $p['uniqueContentPages'] ) ); // page IDs
1458 - # Don't let this get bloated for no reason
1459 - $maxUniquePages = 50; // some flexibility
1460 - if ( is_array( $wgFlaggedRevsAutoconfirm ) &&
1461 - $wgFlaggedRevsAutoconfirm['uniqueContentPages'] > $maxUniquePages )
1462 - {
1463 - $maxUniquePages = $wgFlaggedRevsAutoconfirm['uniqueContentPages'];
1464 - }
1465 - if ( is_array( $wgFlaggedRevsAutopromote ) &&
1466 - $wgFlaggedRevsAutopromote['uniqueContentPages'] > $maxUniquePages )
1467 - {
1468 - $maxUniquePages = $wgFlaggedRevsAutopromote['uniqueContentPages'];
1469 - }
1470 - if ( count( $pages ) < $maxUniquePages // limit the size of this
1471 - && !in_array( $article->getId(), $pages ) )
1472 - {
1473 - $pages[] = $article->getId();
1474 - // Clear out any formatting garbage
1475 - $p['uniqueContentPages'] = preg_replace( '/^,/', '', implode( ',', $pages ) );
1476 - }
1477 - $p['totalContentEdits'] += 1;
1478 - $changed = true;
1479 - }
1480 - if ( $summary != '' ) {
1481 - $p['editComments'] += 1;
1482 - $changed = true;
1483 - }
1484 - return $changed;
1485 - }
1486 -
14871387 # ################ Auto-review function #################
14881388
14891389 /**
Index: trunk/extensions/FlaggedRevs/FlaggedRevs.hooks.php
@@ -1117,12 +1117,12 @@
11181118 # Mark when a user reverts another user, but not self-reverts
11191119 $badUserId = $badRev->getRawUser();
11201120 if ( $badUserId && $user->getId() != $badUserId ) {
1121 - $p = FlaggedRevs::getUserParams( $badUserId );
 1121+ $p = FRUserCounters::getUserParams( $badUserId );
11221122 if ( !isset( $p['revertedEdits'] ) ) {
11231123 $p['revertedEdits'] = 0;
11241124 }
11251125 $p['revertedEdits']++;
1126 - FlaggedRevs::saveUserParams( $badUserId, $p );
 1126+ FRUserCounters::saveUserParams( $badUserId, $p );
11271127 }
11281128 return true;
11291129 }
@@ -1140,12 +1140,12 @@
11411141 if ( $badRev && $badRev->getRawUser()
11421142 && $badRev->getRawUser() != $rev->getRawUser() )
11431143 {
1144 - $p = FlaggedRevs::getUserParams( $badRev->getRawUser() );
 1144+ $p = FRUserCounters::getUserParams( $badRev->getRawUser() );
11451145 if ( !isset( $p['revertedEdits'] ) ) {
11461146 $p['revertedEdits'] = 0;
11471147 }
11481148 $p['revertedEdits']++;
1149 - FlaggedRevs::saveUserParams( $badRev->getRawUser(), $p );
 1149+ FRUserCounters::saveUserParams( $badRev->getRawUser(), $p );
11501150 }
11511151 }
11521152 return true;
@@ -1234,7 +1234,7 @@
12351235 $userage = $userCreation
12361236 ? floor( ( $now - $userCreation ) / 86400 )
12371237 : null;
1238 - $p = FlaggedRevs::getUserParams( $user->getId() );
 1238+ $p = FRUserCounters::getUserParams( $user->getId() );
12391239 # Check if user edited enough content pages
12401240 $totalCheckedEditsNeeded = false;
12411241 if ( $wgFlaggedRevsAutoconfirm['totalContentEdits'] > $p['totalContentEdits'] ) {
@@ -1335,11 +1335,11 @@
13361336 } elseif ( !$wgFlaggedRevsAutopromote && !$wgFlaggedRevsAutoconfirm ) {
13371337 return true;
13381338 }
1339 - $p = FlaggedRevs::getUserParams( $user->getId() );
1340 - $changed = FlaggedRevs::updateUserParams( $p, $article, $summary );
 1339+ $p = FRUserCounters::getUserParams( $user->getId() );
 1340+ $changed = FRUserCounters::updateUserParams( $p, $article, $summary );
13411341 # Save any updates to user params
13421342 if ( $changed ) {
1343 - FlaggedRevs::saveUserParams( $user->getId(), $p );
 1343+ FRUserCounters::saveUserParams( $user->getId(), $p );
13441344 }
13451345 if ( !is_array( $wgFlaggedRevsAutopromote ) ) {
13461346 return true; // nothing to do
@@ -1531,14 +1531,14 @@
15321532 if ( $removegroup && in_array( 'editor', $removegroup ) ) {
15331533 // Cross-wiki rights change
15341534 if ( $u instanceof UserRightsProxy ) {
1535 - $params = FlaggedRevs::getUserParams( $u->getId(), $u->getDBName() );
 1535+ $params = FRUserCounters::getUserParams( $u->getId(), $u->getDBName() );
15361536 $params['demoted'] = 1;
1537 - FlaggedRevs::saveUserParams( $u->getId(), $params, $u->getDBName() );
 1537+ FRUserCounters::saveUserParams( $u->getId(), $params, $u->getDBName() );
15381538 // On-wiki rights change
15391539 } else {
1540 - $params = FlaggedRevs::getUserParams( $u->getId() );
 1540+ $params = FRUserCounters::getUserParams( $u->getId() );
15411541 $params['demoted'] = 1;
1542 - FlaggedRevs::saveUserParams( $u->getId(), $params );
 1542+ FRUserCounters::saveUserParams( $u->getId(), $params );
15431543 }
15441544 }
15451545 return true;

Status & tagging log