| Index: trunk/extensions/UserDailyContribs/api/ApiUserDailyContribs.php |
| — | — | @@ -7,7 +7,8 @@ |
| 8 | 8 | $result = $this->getResult(); |
| 9 | 9 | |
| 10 | 10 | $userName = $params['user']; |
| 11 | | - $days = $params['daysago']; |
| | 11 | + $daysago = $params['daysago']; |
| | 12 | + $basetimestamp = $params['basetimestamp']; |
| 12 | 13 | $user = User::newFromName( $userName ); |
| 13 | 14 | |
| 14 | 15 | if ( !$user ) { |
| — | — | @@ -19,18 +20,26 @@ |
| 20 | 21 | if ( $wgUserDailyContributionsApiCheckAuthPlugin && !$wgAuth->userExists( $userName ) ) { |
| 21 | 22 | $this->dieUsage( 'Specified user does not exist', 'bad_user' ); |
| 22 | 23 | } |
| 23 | | - $now = time(); |
| | 24 | + |
| | 25 | + // Defaults to 'now' if not given |
| | 26 | + $totime = wfTimestamp( TS_UNIX, $basetimestamp ); |
| | 27 | + |
| | 28 | + $fromtime = $totime - ($daysago * 60 *60 *24); |
| | 29 | + |
| 24 | 30 | $result->addValue( $this->getModuleName() , |
| 25 | 31 | 'id', $user->getId() ); |
| 26 | | - // returns date of registration in YYYYMMDDHHMMSS format |
| 27 | | - $result->addValue( $this->getModuleName() , |
| | 32 | + |
| | 33 | + // Returns date of registration in YYYYMMDDHHMMSS format |
| | 34 | + $result->addValue( $this->getModuleName(), |
| 28 | 35 | 'registration', !$user->getRegistration() ? '0' : $user->getRegistration() ); |
| 29 | | - // returns number of edits since daysago param |
| | 36 | + |
| | 37 | + // Returns number of edits between daysago date and basetimestamp (or today) |
| | 38 | + $result->addValue( $this->getModuleName(), |
| | 39 | + 'timeFrameEdits', getUserEditCountSince( $fromtime, $user, $totime ) ); |
| | 40 | + |
| | 41 | + // Returns total number of edits |
| 30 | 42 | $result->addValue( $this->getModuleName() , |
| 31 | | - 'timeFrameEdits', getUserEditCountSince( $now - ($days * 60 *60 *24), $user ) ); |
| 32 | | - // returns total number of edits |
| 33 | | - $result->addValue( $this->getModuleName() , |
| 34 | | - 'totalEdits', ($user->getEditCount() == NULL)?0:$user->getEditCount() ); |
| | 43 | + 'totalEdits', $user->getEditCount() == NULL ? 0 : $user->getEditCount() ); |
| 35 | 44 | } |
| 36 | 45 | |
| 37 | 46 | public function getAllowedParams() { |
| — | — | @@ -42,6 +51,9 @@ |
| 43 | 52 | ApiBase::PARAM_TYPE => 'integer', |
| 44 | 53 | ApiBase::PARAM_MIN => 0, |
| 45 | 54 | ), |
| | 55 | + 'basetimestamp' => array( |
| | 56 | + ApiBase::PARAM_TYPE => 'timestamp', |
| | 57 | + ), |
| 46 | 58 | ); |
| 47 | 59 | } |
| 48 | 60 | |
| — | — | @@ -49,6 +61,10 @@ |
| 50 | 62 | return array( |
| 51 | 63 | 'user' => 'Username to query', |
| 52 | 64 | 'daysago' => 'Number of edits since this many days ago', |
| | 65 | + 'basetimestamp' => array( 'Date from which daysago will be calculated (instead of "today").', |
| | 66 | + 'Count returned in timeFrameEdits will be editcount between this date and the date', |
| | 67 | + '"daysago" from it.' |
| | 68 | + ), |
| 53 | 69 | ); |
| 54 | 70 | } |
| 55 | 71 | |
| Index: trunk/extensions/UserDailyContribs/UserDailyContribs.php |
| — | — | @@ -44,25 +44,34 @@ |
| 45 | 45 | /** |
| 46 | 46 | * Get the number of revisions a user has made since a given time |
| 47 | 47 | * |
| 48 | | - * @param $time beginning timestamp |
| 49 | | - * @param $user User |
| | 48 | + * @param $fromtime: beginning timestamp |
| | 49 | + * @param $user User: (optional) User object to get edit count for |
| | 50 | + * @param $totime: (optional) ending timestamp |
| 50 | 51 | * @return number of revsions this user has made |
| 51 | 52 | */ |
| 52 | | -function getUserEditCountSince( $time = null, User $user = null ) { |
| | 53 | +function getUserEditCountSince( $fromtime = null, User $user = null, $totime = null ) { |
| 53 | 54 | global $wgUser; |
| 54 | 55 | |
| 55 | 56 | // Fallback on current user |
| 56 | 57 | if ( is_null( $user ) ) { |
| 57 | 58 | $user = $wgUser; |
| 58 | 59 | } |
| 59 | | - // Round time down to a whole day |
| 60 | | - $time = gmdate( 'Y-m-d', wfTimestamp( TS_UNIX, $time ) ); |
| | 60 | + |
| | 61 | + // Round times down to a whole day, possibly letting a null value |
| | 62 | + // pass to wfTimestamp which will give us today. |
| | 63 | + $fromtime = gmdate( 'Y-m-d', wfTimestamp( TS_UNIX, $fromtime ) ); |
| | 64 | + $totime = gmdate( 'Y-m-d', wfTimestamp( TS_UNIX, $totime ) ); |
| | 65 | + |
| 61 | 66 | // Query the user contribs table |
| 62 | 67 | $dbr = wfGetDB( DB_SLAVE ); |
| 63 | 68 | $edits = $dbr->selectField( |
| 64 | 69 | 'user_daily_contribs', |
| 65 | 70 | 'SUM(contribs)', |
| 66 | | - array( 'user_id' => $user->getId(), 'day >= ' . $dbr->addQuotes( $time ) ), |
| | 71 | + array( |
| | 72 | + 'user_id' => $user->getId(), |
| | 73 | + 'day >= ' . $dbr->addQuotes( $fromtime ), |
| | 74 | + 'day <= ' . $dbr->addQuotes( $totime ) |
| | 75 | + ), |
| 67 | 76 | __METHOD__ |
| 68 | 77 | ); |
| 69 | 78 | // Return edit count as an integer |