Index: trunk/extensions/ContributionReporting/ContributionReporting.php |
— | — | @@ -155,7 +155,7 @@ |
156 | 156 | |
157 | 157 | /** |
158 | 158 | * Define the contributiontotal magic word |
159 | | - * Example: {{#contributiontotal:start=2011-11-16 00:00:00|fudgefactor=0}} |
| 159 | + * Example: {{#contributiontotal:fundraiser=2011|fudgefactor=0}} |
160 | 160 | * @param $magicWords array |
161 | 161 | * @param $langCode string |
162 | 162 | * @return bool |
— | — | @@ -210,39 +210,84 @@ |
211 | 211 | } |
212 | 212 | |
213 | 213 | /** |
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 |
218 | 218 | */ |
219 | | -function efContributionReportingTotal( $start, $fudgeFactor ) { |
220 | | - $db = efContributionReportingConnection(); |
| 219 | +function efContributionReportingTotal( $fundraiser, $fudgeFactor = 0 ) { |
| 220 | + global $wgMemc, $egFundraiserStatisticsFundraisers, $egFundraiserStatisticsCacheTimeout; |
221 | 221 | |
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; |
226 | 229 | } |
| 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]; |
227 | 245 | |
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 { |
229 | 258 | |
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 | + } |
234 | 277 | |
235 | | - // Make sure fudge factor is a number |
| 278 | + // Make sure the fudge factor is a number |
236 | 279 | if ( is_nan( $fudgeFactor ) ) { |
237 | 280 | $fudgeFactor = 0; |
238 | 281 | } |
239 | 282 | |
240 | | - $output += $fudgeFactor; |
| 283 | + // Add the fudge factor to the total |
| 284 | + $total += $fudgeFactor; |
241 | 285 | |
242 | | - return $output; |
| 286 | + $wgMemc->set( $key, $total, $egFundraiserStatisticsCacheTimeout ); |
| 287 | + return $total; |
243 | 288 | } |
244 | 289 | |
245 | 290 | /** |
246 | | - * @return string |
| 291 | + * @return integer |
247 | 292 | */ |
248 | 293 | function efContributionReportingTotal_Render() { |
249 | 294 | $args = func_get_args(); |
— | — | @@ -252,18 +297,18 @@ |
253 | 298 | $start = false; |
254 | 299 | |
255 | 300 | foreach( $args as $arg ) { |
256 | | - if ( strpos($arg,'=') === false ) { |
| 301 | + if ( strpos( $arg,'=' ) === false ) { |
257 | 302 | continue; |
258 | 303 | } |
259 | 304 | |
260 | | - list($key,$value) = explode( '=', trim($arg), 2 ); |
| 305 | + list( $key, $value ) = explode( '=', trim( $arg ), 2 ); |
261 | 306 | |
262 | | - if ($key == 'fudgefactor') { |
| 307 | + if ( $key == 'fudgefactor' ) { |
263 | 308 | $fudgeFactor = $value; |
264 | | - } elseif ($key == 'start') { |
265 | | - $start = $value; |
| 309 | + } elseif ( $key == 'fundraiser' ) { |
| 310 | + $fundraiser = $value; |
266 | 311 | } |
267 | 312 | } |
268 | 313 | |
269 | | - return efContributionReportingTotal( $start, $fudgeFactor ); |
| 314 | + return efContributionReportingTotal( $fundraiser, $fudgeFactor ); |
270 | 315 | } |
Index: trunk/extensions/ContributionReporting/ContributionTotal_body.php |
— | — | @@ -10,12 +10,12 @@ |
11 | 11 | |
12 | 12 | $this->setHeaders(); |
13 | 13 | |
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' ); |
16 | 16 | $action = $wgRequest->getText( 'action' ); |
17 | 17 | $fudgeFactor = $wgRequest->getInt( 'fudgefactor' ); |
18 | 18 | |
19 | | - $output = efContributionReportingTotal( $start, $fudgeFactor ); |
| 19 | + $output = efContributionReportingTotal( $fundraiser, $fudgeFactor ); |
20 | 20 | |
21 | 21 | header( 'Cache-Control: max-age=300,s-maxage=300' ); |
22 | 22 | if ( $action == 'raw' ) { |
Index: trunk/extensions/ContributionReporting/YearlyTotal_body.php |
— | — | @@ -46,26 +46,14 @@ |
47 | 47 | /* Private Functions */ |
48 | 48 | |
49 | 49 | private function query( $adjustment ) { |
50 | | - global $wgMemc, $egFundraiserStatisticsCacheTimeout, $egFundraiserStatisticsFundraisers; |
| 50 | + global $egFundraiserStatisticsFundraisers; |
51 | 51 | |
52 | 52 | $currenctFundraiserIndex = count( $egFundraiserStatisticsFundraisers ) - 1; |
53 | | - $year = $egFundraiserStatisticsFundraisers[$currenctFundraiserIndex]['id']; |
| 53 | + $fundraiser = $egFundraiserStatisticsFundraisers[$currenctFundraiserIndex]['id']; |
54 | 54 | |
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 ); |
67 | 56 | if ( !$total ) $total = 0; |
68 | | - |
69 | | - $wgMemc->set( $key, $total, $egFundraiserStatisticsCacheTimeout ); |
| 57 | + |
70 | 58 | return $total; |
71 | 59 | } |
72 | 60 | |