r105659 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105658‎ | r105659 | r105660 >
Date:08:04, 9 December 2011
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
make parserfunction based on fundraiser ID instead of start time, start using the new summary tables, add some comments
Modified paths:
  • /trunk/extensions/ContributionReporting/ContributionReporting.php (modified) (history)
  • /trunk/extensions/ContributionReporting/ContributionTotal_body.php (modified) (history)
  • /trunk/extensions/ContributionReporting/YearlyTotal_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ContributionReporting/ContributionReporting.php
@@ -155,7 +155,7 @@
156156
157157 /**
158158 * Define the contributiontotal magic word
159 - * Example: {{#contributiontotal:start=2011-11-16 00:00:00|fudgefactor=0}}
 159+ * Example: {{#contributiontotal:fundraiser=2011|fudgefactor=0}}
160160 * @param $magicWords array
161161 * @param $langCode string
162162 * @return bool
@@ -210,39 +210,84 @@
211211 }
212212
213213 /**
214 - * Get the total amount of money raised since the start of the fundraiser
215 - * @param $start
216 - * @param $fudgeFactor
217 - * @return string
 214+ * Get the total amount of money raised for a specific fundraiser
 215+ * @param string $fundraiser The ID of the fundraiser to return the current total for
 216+ * @param int $fudgeFactor How much to adjust the total by
 217+ * @return integer
218218 */
219 -function efContributionReportingTotal( $start, $fudgeFactor ) {
220 - $db = efContributionReportingConnection();
 219+function efContributionReportingTotal( $fundraiser, $fudgeFactor = 0 ) {
 220+ global $wgMemc, $egFundraiserStatisticsFundraisers, $egFundraiserStatisticsCacheTimeout;
221221
222 - $sql = 'SELECT ROUND( SUM(converted_amount) ) AS ttl FROM public_reporting';
223 -
224 - if ( $start ) {
225 - $sql .= ' WHERE received >= ' . $db->addQuotes( wfTimestamp( TS_UNIX, $start ) );
 222+ $dbr = efContributionReportingConnection();
 223+
 224+ // If a total is cached, use that
 225+ $key = wfMemcKey( 'contributionreportingtotal', $fundraiser, $fudgeFactor );
 226+ $cache = $wgMemc->get( $key );
 227+ if ( $cache != false && $cache != -1 ) {
 228+ return $cache;
226229 }
 230+
 231+ // Find the index number for the requested fundraiser
 232+ $myFundraiserIndex = false;
 233+ foreach ( $egFundraiserStatisticsFundraisers as $fundraiserIndex => $fundraiserArray ) {
 234+ if ( $fundraiserArray['id'] == $fundraiser ) {
 235+ $myFundraiserIndex = $fundraiserIndex;
 236+ break;
 237+ }
 238+ }
 239+ if ( !$myFundraiserIndex ) {
 240+ // If none was found, use the most recent fundraiser
 241+ $myFundraiserIndex = count( $egFundraiserStatisticsFundraisers ) - 1;
 242+ }
 243+
 244+ $myFundraiser = $egFundraiserStatisticsFundraisers[$myFundraiserIndex];
227245
228 - $res = $db->query( $sql );
 246+ // First, try to get the total from the summary table
 247+ $result = $dbr->select(
 248+ 'public_reporting_fundraisers',
 249+ 'round( prf_total ) AS total',
 250+ 'prf_id = ' . $myFundraiser['id'],
 251+ __METHOD__
 252+ );
 253+ $row = $dbr->fetchRow( $result );
 254+
 255+ if ( $row['total'] > 0 ) {
 256+ $total = $row['total'];
 257+ } else {
229258
230 - $row = $res->fetchRow();
231 -
232 - # Output
233 - $output = $row['ttl'] ? $row['ttl'] : '0';
 259+ // Try to get the total manually from public_reporting (more expensive)
 260+ $result = $dbr->select(
 261+ 'public_reporting',
 262+ 'round( sum( converted_amount ) ) AS total',
 263+ array(
 264+ 'received >= ' . $dbr->addQuotes( wfTimestamp( TS_UNIX, strtotime( $myFundraiser['start'] ) ) ),
 265+ 'received <= ' . $dbr->addQuotes( wfTimestamp( TS_UNIX, strtotime( $myFundraiser['end'] ) + 24 * 60 * 60 ) ),
 266+ ),
 267+ __METHOD__
 268+ );
 269+ $row = $dbr->fetchRow( $result );
 270+
 271+ if ( $row['total'] > 0 ) {
 272+ $total = $row['total'];
 273+ } else {
 274+ return 0;
 275+ }
 276+ }
234277
235 - // Make sure fudge factor is a number
 278+ // Make sure the fudge factor is a number
236279 if ( is_nan( $fudgeFactor ) ) {
237280 $fudgeFactor = 0;
238281 }
239282
240 - $output += $fudgeFactor;
 283+ // Add the fudge factor to the total
 284+ $total += $fudgeFactor;
241285
242 - return $output;
 286+ $wgMemc->set( $key, $total, $egFundraiserStatisticsCacheTimeout );
 287+ return $total;
243288 }
244289
245290 /**
246 - * @return string
 291+ * @return integer
247292 */
248293 function efContributionReportingTotal_Render() {
249294 $args = func_get_args();
@@ -252,18 +297,18 @@
253298 $start = false;
254299
255300 foreach( $args as $arg ) {
256 - if ( strpos($arg,'=') === false ) {
 301+ if ( strpos( $arg,'=' ) === false ) {
257302 continue;
258303 }
259304
260 - list($key,$value) = explode( '=', trim($arg), 2 );
 305+ list( $key, $value ) = explode( '=', trim( $arg ), 2 );
261306
262 - if ($key == 'fudgefactor') {
 307+ if ( $key == 'fudgefactor' ) {
263308 $fudgeFactor = $value;
264 - } elseif ($key == 'start') {
265 - $start = $value;
 309+ } elseif ( $key == 'fundraiser' ) {
 310+ $fundraiser = $value;
266311 }
267312 }
268313
269 - return efContributionReportingTotal( $start, $fudgeFactor );
 314+ return efContributionReportingTotal( $fundraiser, $fudgeFactor );
270315 }
Index: trunk/extensions/ContributionReporting/ContributionTotal_body.php
@@ -10,12 +10,12 @@
1111
1212 $this->setHeaders();
1313
14 - # Get request data from, e.g.
15 - $start = intval( wfTimestampOrNull( TS_UNIX, $wgRequest->getVal( 'start' ) ) );
 14+ # Get request data
 15+ $fundraiser = $wgRequest->getText( 'fundraiser' );
1616 $action = $wgRequest->getText( 'action' );
1717 $fudgeFactor = $wgRequest->getInt( 'fudgefactor' );
1818
19 - $output = efContributionReportingTotal( $start, $fudgeFactor );
 19+ $output = efContributionReportingTotal( $fundraiser, $fudgeFactor );
2020
2121 header( 'Cache-Control: max-age=300,s-maxage=300' );
2222 if ( $action == 'raw' ) {
Index: trunk/extensions/ContributionReporting/YearlyTotal_body.php
@@ -46,26 +46,14 @@
4747 /* Private Functions */
4848
4949 private function query( $adjustment ) {
50 - global $wgMemc, $egFundraiserStatisticsCacheTimeout, $egFundraiserStatisticsFundraisers;
 50+ global $egFundraiserStatisticsFundraisers;
5151
5252 $currenctFundraiserIndex = count( $egFundraiserStatisticsFundraisers ) - 1;
53 - $year = $egFundraiserStatisticsFundraisers[$currenctFundraiserIndex]['id'];
 53+ $fundraiser = $egFundraiserStatisticsFundraisers[$currenctFundraiserIndex]['id'];
5454
55 - $key = wfMemcKey( 'fundraiserstatistics', $year, $adjustment );
56 - $cache = $wgMemc->get( $key );
57 - if ( $cache != false && $cache != -1 ) {
58 - return $cache;
59 - }
60 -
61 - // Get the timestamp for the start of the current fundraiser
62 - // Note: This depends on the fundraisers being listed in chronological order
63 - $start = strtotime( $egFundraiserStatisticsFundraisers[$currenctFundraiserIndex]['start'] );
64 - $start = intval( wfTimestampOrNull( TS_UNIX, $start ) );
65 -
66 - $total = efContributionReportingTotal( $start, $adjustment );
 55+ $total = efContributionReportingTotal( $fundraiser, $adjustment );
6756 if ( !$total ) $total = 0;
68 -
69 - $wgMemc->set( $key, $total, $egFundraiserStatisticsCacheTimeout );
 57+
7058 return $total;
7159 }
7260

Follow-up revisions

RevisionCommit summaryAuthorDate
r106239follow-up to r105659, only connect to db when necessary, use better condition...kaldari20:08, 14 December 2011
r106696MFT r105145, r105454, r105641, r105657, r105659, r105757, r105916, r105965, r...awjrichards20:59, 19 December 2011

Comments

#Comment by Catrope (talk | contribs)   17:23, 14 December 2011
+	$dbr = efContributionReportingConnection();
+	
+	// If a total is cached, use that
+	$key = wfMemcKey( 'contributionreportingtotal', $fundraiser, $fudgeFactor );

It would be slightly more efficient to only open the DB connection after having checked memcached, so you don't open DB connections for memc hits.

+		'prf_id = ' . $myFundraiser['id'],

Please use 'prf_id' => $myFundraiser['id'] .

OK otherwise.

#Comment by Kaldari (talk | contribs)   20:10, 14 December 2011

Fixed in r106239.

Status & tagging log