r105965 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105964‎ | r105965 | r105966 >
Date:01:15, 13 December 2011
Author:kaldari
Status:ok (Comments)
Tags:
Comment:
making sure nothing touches the public reporting table except the cron job, removing YearlyTotal since its redundant with ContributionTotal
Modified paths:
  • /trunk/extensions/ContributionReporting/ContributionHistory_body.php (modified) (history)
  • /trunk/extensions/ContributionReporting/ContributionReporting.i18n.php (modified) (history)
  • /trunk/extensions/ContributionReporting/ContributionReporting.php (modified) (history)
  • /trunk/extensions/ContributionReporting/ContributionStatistics_body.php (modified) (history)
  • /trunk/extensions/ContributionReporting/ContributionTotal_body.php (modified) (history)
  • /trunk/extensions/ContributionReporting/ContributionTrackingStatistics_body.php (modified) (history)
  • /trunk/extensions/ContributionReporting/DailyTotal_body.php (modified) (history)
  • /trunk/extensions/ContributionReporting/FundraiserStatistics_body.php (modified) (history)
  • /trunk/extensions/ContributionReporting/YearlyTotal_body.php (deleted) (history)

Diff [purge]

Index: trunk/extensions/ContributionReporting/YearlyTotal_body.php
@@ -1,69 +0,0 @@
2 -<?php
3 -/**
4 - * Special Page for Contribution statistics extension
5 - *
6 - * @file
7 - * @ingroup Extensions
8 - */
9 -
10 -class SpecialYearlyTotal extends IncludableSpecialPage {
11 -
12 - protected $sharedMaxAge = 600; // Cache for 10 minutes on the server side
13 - protected $maxAge = 600; // Cache for 10 minutes on the client side
14 -
15 - /* Functions */
16 -
17 - public function __construct() {
18 - parent::__construct( 'YearlyTotal' );
19 - }
20 -
21 - public function execute( $sub ) {
22 - global $wgRequest, $wgOut, $egFundraiserStatisticsFundraisers;
23 -
24 - $js = $wgRequest->getBool( 'js', false );
25 -
26 - $adjustment = $wgRequest->getVal( 'adjustment' );
27 - // Make sure it's a number
28 - if ( is_nan( $adjustment ) ) {
29 - $adjustment = 0;
30 - }
31 -
32 - /* Setup */
33 - $wgOut->disable();
34 - $this->sendHeaders();
35 -
36 - $total = $this->query( $adjustment );
37 -
38 - $content = "wgFundraisingYearlyTotal = $total;";
39 -
40 - if ( $js ) {
41 - echo $content;
42 - } else {
43 - echo $total;
44 - }
45 - }
46 -
47 - /* Private Functions */
48 -
49 - private function query( $adjustment ) {
50 - global $egFundraiserStatisticsFundraisers;
51 -
52 - $currenctFundraiserIndex = count( $egFundraiserStatisticsFundraisers ) - 1;
53 - $fundraiser = $egFundraiserStatisticsFundraisers[$currenctFundraiserIndex]['id'];
54 -
55 - $total = efContributionReportingTotal( $fundraiser, $adjustment );
56 - if ( !$total ) $total = 0;
57 -
58 - return $total;
59 - }
60 -
61 - private function sendHeaders() {
62 - global $wgJsMimeType;
63 - header( "Content-type: $wgJsMimeType; charset=utf-8" );
64 - header( "Cache-Control: public, s-maxage=$this->sharedMaxAge, max-age=$this->maxAge" );
65 - }
66 -
67 - public function isListed(){
68 - return false;
69 - }
70 -}
Index: trunk/extensions/ContributionReporting/ContributionReporting.php
@@ -41,9 +41,8 @@
4242 $wgAutoloadClasses['SpecialContributionStatistics'] = $dir . 'ContributionStatistics_body.php';
4343 $wgAutoloadClasses['SpecialFundraiserStatistics'] = $dir . 'FundraiserStatistics_body.php';
4444 $wgAutoloadClasses['SpecialContributionTrackingStatistics'] = $dir . 'ContributionTrackingStatistics_body.php';
45 -/*
4645 $wgAutoloadClasses['SpecialDailyTotal'] = $dir . 'DailyTotal_body.php';
47 -$wgAutoloadClasses['SpecialYearlyTotal'] = $dir . 'YearlyTotal_body.php';
 46+/*
4847 $wgAutoloadClasses['DisabledNotice'] = $dir . 'DisabledNotice_body.php';
4948 */
5049
@@ -52,10 +51,7 @@
5352 $wgSpecialPages['ContributionStatistics'] = 'SpecialContributionStatistics';
5453 $wgSpecialPages['FundraiserStatistics'] = 'SpecialFundraiserStatistics';
5554 $wgSpecialPages['ContributionTrackingStatistics'] = 'SpecialContributionTrackingStatistics';
56 -/*
5755 $wgSpecialPages['DailyTotal'] = 'SpecialDailyTotal';
58 -$wgSpecialPages['YearlyTotal'] = 'SpecialYearlyTotal';
59 -*/
6056
6157 $wgSpecialPageGroups['ContributionHistory'] = 'contribution';
6258 $wgSpecialPageGroups['ContributionTotal'] = 'contribution';
@@ -254,24 +250,7 @@
255251 if ( $row['total'] > 0 ) {
256252 $total = $row['total'];
257253 } else {
258 -
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 - }
 254+ $total = 0;
276255 }
277256
278257 // Make sure the fudge factor is a number
Index: trunk/extensions/ContributionReporting/ContributionTrackingStatistics_body.php
@@ -19,6 +19,10 @@
2020
2121 public function execute( $sub ) {
2222 global $wgOut, $wgRequest;
 23+
 24+ # Emergecny short cut until this is back in working order
 25+ $wgOut->redirect( SpecialPage::getTitleFor( 'FundraiserStatistics' )->getFullURL() );
 26+ return;
2327
2428 $start = $wgRequest->getIntOrNull( 'start' );
2529 $end = $wgRequest->getIntOrNull( 'end' );
Index: trunk/extensions/ContributionReporting/FundraiserStatistics_body.php
@@ -328,58 +328,27 @@
329329 // Set timezone to UTC just in case
330330 date_default_timezone_set( 'UTC' );
331331
332 - // If the summary table exists, use that, otherwise compute from scratch
333 - if ( mysql_num_rows( mysql_query( "SHOW TABLES LIKE 'public_reporting_days'" ) ) ) {
 332+ $conditions = array(
 333+ 'prd_date >= ' . $dbr->addQuotes( wfTimestamp( TS_DB, strtotime( $start ) ) ),
 334+ 'prd_date <= ' . $dbr->addQuotes( wfTimestamp( TS_DB, strtotime( $end ) ) ),
 335+ );
334336
335 - $conditions = array(
336 - 'prd_date >= ' . $dbr->addQuotes( wfTimestamp( TS_DB, strtotime( $start ) ) ),
337 - 'prd_date <= ' . $dbr->addQuotes( wfTimestamp( TS_DB, strtotime( $end ) ) ),
338 - );
339 -
340 - // Get the data for a fundraiser
341 - $select = $dbr->select( 'public_reporting_days',
342 - array(
343 - 'prd_date',
344 - 'prd_total',
345 - 'prd_number',
346 - 'prd_average',
347 - 'prd_maximum',
348 - ),
349 - $conditions,
350 - __METHOD__,
351 - array(
352 - 'ORDER BY' => 'prd_date',
353 - )
354 - );
355 -
356 - } else {
357 -
358 - $conditions = array(
359 - 'received >= ' . $dbr->addQuotes( wfTimestamp( TS_UNIX, strtotime( $start ) ) ),
360 - 'received <= ' . $dbr->addQuotes( wfTimestamp( TS_UNIX, strtotime( $end ) + 24 * 60 * 60 ) ),
361 - 'converted_amount >= ' . $egFundraiserStatisticsMinimum,
362 - 'converted_amount <= ' . $egFundraiserStatisticsMaximum,
363 - );
 337+ // Get the data for a fundraiser
 338+ $select = $dbr->select( 'public_reporting_days',
 339+ array(
 340+ 'prd_date',
 341+ 'prd_total',
 342+ 'prd_number',
 343+ 'prd_average',
 344+ 'prd_maximum',
 345+ ),
 346+ $conditions,
 347+ __METHOD__,
 348+ array(
 349+ 'ORDER BY' => 'prd_date',
 350+ )
 351+ );
364352
365 - // Get the data for a fundraiser
366 - $select = $dbr->select( 'public_reporting',
367 - array(
368 - "DATE_FORMAT(FROM_UNIXTIME(received),'%Y-%m-%d')",
369 - 'sum(converted_amount)',
370 - 'count(*)',
371 - 'avg(converted_amount)',
372 - 'max(converted_amount)',
373 - ),
374 - $conditions,
375 - __METHOD__,
376 - array(
377 - 'ORDER BY' => 'received',
378 - 'GROUP BY' => "DATE_FORMAT(FROM_UNIXTIME(received),'%Y-%m-%d')"
379 - )
380 - );
381 -
382 - }
383 -
384353 $result = array();
385354 $ytd = 0;
386355 while ( $row = $dbr->fetchRow( $select ) ) {
Index: trunk/extensions/ContributionReporting/ContributionHistory_body.php
@@ -30,7 +30,7 @@
3131 // Paging controls
3232 $newer = $db->selectField( 'public_reporting', 'received',
3333 array_merge(
34 - array( 'received > ' . strtotime( 'July 1st 2008' ) ),
 34+ array( 'received > ' . strtotime( 'July 1st 2011' ) ),
3535 ( $offset !== null ? array( 'received > ' . $offset ) : array() )
3636 ),
3737 __METHOD__,
@@ -42,7 +42,7 @@
4343 );
4444 $older = $db->selectField( 'public_reporting', 'received',
4545 array_merge(
46 - array( 'received > ' . strtotime( 'July 1st 2008' ) ),
 46+ array( 'received > ' . strtotime( 'July 1st 2011' ) ),
4747 ( $offset !== null ? array( 'received <= ' . $offset ) : array() )
4848 ),
4949 __METHOD__,
@@ -85,7 +85,7 @@
8686
8787 if ( $offset == null ) {
8888 $offset = $db->selectField( 'public_reporting', 'received',
89 - array( 'received > ' . strtotime( 'July 1st 2008' ) ),
 89+ array( 'received > ' . strtotime( 'July 1st 2011' ) ),
9090 __METHOD__,
9191 array(
9292 'ORDER BY' => 'received DESC',
@@ -98,7 +98,7 @@
9999
100100 $res = $db->select( 'public_reporting', '*',
101101 array_merge(
102 - array( 'received > ' . strtotime( 'July 1st 2008' ) ),
 102+ array( 'received > ' . strtotime( 'July 1st 2011' ) ),
103103 ( $offset !== null ? array( 'received <= ' . $offset ) : array() )
104104 ),
105105 __METHOD__,
Index: trunk/extensions/ContributionReporting/ContributionTotal_body.php
@@ -1,23 +1,33 @@
22 <?php
33
4 -class ContributionTotal extends SpecialPage {
 4+class ContributionTotal extends UnlistedSpecialPage {
 5+
 6+ protected $sharedMaxAge = 300; // Cache for 5 minutes on the server side
 7+ protected $maxAge = 300; // Cache for 5 minutes on the client side
 8+
59 function __construct() {
610 parent::__construct( 'ContributionTotal' );
711 }
812
913 function execute( $par ) {
10 - global $wgRequest, $wgOut;
 14+ global $wgRequest, $wgOut, $egFundraiserStatisticsFundraisers;
1115
1216 $this->setHeaders();
1317
14 - # Get request data
15 - $fundraiser = $wgRequest->getText( 'fundraiser' );
 18+ // Get request data
 19+ $fundraiserId = $wgRequest->getText( 'fundraiser' );
1620 $action = $wgRequest->getText( 'action' );
17 - $fudgeFactor = $wgRequest->getInt( 'fudgefactor' );
 21+ $fudgeFactor = $wgRequest->getInt( 'adjustment' );
 22+
 23+ // If no fundraiser was specified, use the most recent
 24+ if ( !$fundraiserId ) {
 25+ $mostRecentFundraiser = end( $egFundraiserStatisticsFundraisers );
 26+ $fundraiserId = $mostRecentFundraiser['id'];
 27+ }
1828
19 - $output = efContributionReportingTotal( $fundraiser, $fudgeFactor );
 29+ $output = efContributionReportingTotal( $fundraiserId, $fudgeFactor );
2030
21 - header( 'Cache-Control: max-age=300,s-maxage=300' );
 31+ header( "Cache-Control: max-age=$this->maxAge,s-maxage=$this->sharedMaxAge" );
2232 if ( $action == 'raw' ) {
2333 $wgOut->disable();
2434 echo $output;
Index: trunk/extensions/ContributionReporting/DailyTotal_body.php
@@ -6,13 +6,11 @@
77 * @ingroup Extensions
88 */
99
10 -class SpecialDailyTotal extends IncludableSpecialPage {
 10+class SpecialDailyTotal extends UnlistedSpecialPage {
1111
12 - protected $sharedMaxAge = 600; // Cache for 10 minutes on the server side
13 - protected $maxAge = 600; // Cache for 10 minutes on the client side
 12+ protected $sharedMaxAge = 300; // Cache for 5 minutes on the server side
 13+ protected $maxAge = 300; // Cache for 5 minutes on the client side
1414
15 - /* Functions */
16 -
1715 public function __construct() {
1816 parent::__construct( 'DailyTotal' );
1917 }
@@ -20,13 +18,12 @@
2119 public function execute( $sub ) {
2220 global $wgRequest, $wgOut;
2321
24 - $js = $wgRequest->getBool( 'js', false );
25 -
 22+ $this->setHeaders();
 23+
 24+ // Get request data
 25+ $action = $wgRequest->getText( 'action' );
 26+ $fudgeFactor = $wgRequest->getInt( 'adjustment' );
2627 $timezone = $wgRequest->getText( 'timezone', '0' );
27 -
28 - /* Setup */
29 - $wgOut->disable();
30 - $this->sendHeaders();
3128
3229 $zoneList = array (
3330 '-12' => array( 'name' => 'Kwajalein', 'offset' => '-12:00' ),
@@ -79,64 +76,68 @@
8077 }
8178
8279 $setTimeZone = date_default_timezone_set( $timeZoneName );
83 - $start = date( 'Y-m-d' ); // Get the current date in the requested timezone
84 - $total = $this->query( $timeZoneOffset, $start );
85 -
86 - $content = "wgFundraisingDailyTotal = $total;";
87 -
88 - if ( $js ) {
89 - echo $content;
 80+ $today = date( 'Y-m-d' ); // Get the current date in the requested timezone
 81+ $output = $this->getTodaysTotal( $timeZoneOffset, $today, $fudgeFactor );
 82+
 83+ header( "Cache-Control: max-age=$this->maxAge,s-maxage=$this->sharedMaxAge" );
 84+ if ( $action == 'raw' ) {
 85+ $wgOut->disable();
 86+ echo $output;
9087 } else {
91 - echo $total;
 88+ $wgOut->setRobotpolicy( 'noindex,nofollow' );
 89+ $wgOut->addHTML( $output );
9290 }
9391 }
9492
9593 /* Private Functions */
9694
97 - private function query( $timeZoneOffset, $start ) {
 95+ /**
 96+ * Get the total amount of money raised for today
 97+ * @param string $timeZoneOffset The timezone to request the total for
 98+ * @param string $today The current date in the requested time zone, e.g. '2011-12-16'
 99+ * @param int $fudgeFactor How much to adjust the total by
 100+ * @return integer
 101+ */
 102+ private function getTodaysTotal( $timeZoneOffset, $today, $fudgeFactor = 0 ) {
98103 global $wgMemc, $egFundraiserStatisticsMinimum, $egFundraiserStatisticsMaximum, $egFundraiserStatisticsCacheTimeout;
99104
100 - $key = wfMemcKey( 'fundraiserstatistics', $timeZoneOffset, $start );
 105+ // Delete this block once there is timezone support in the populating script
 106+ $setTimeZone = date_default_timezone_set( 'UTC' );
 107+ $today = date( 'Y-m-d' ); // Get the current date in UTC
 108+ $timeZoneOffset = '+00:00';
 109+
 110+ $key = wfMemcKey( 'fundraiserdailytotal', $timeZoneOffset, $today, $fudgeFactor );
101111 $cache = $wgMemc->get( $key );
102112 if ( $cache != false && $cache != -1 ) {
103113 return $cache;
104114 }
105115
106 - // We're only interested in donations from the past 2 days at most
107 - $recentTime = time() - 60 * 60 * 48;
108 -
109116 // Use database
110117 $dbr = efContributionReportingConnection();
111 - #$dbr = wfGetDB( DB_MASTER );
112 - $conditions = array(
113 - 'received > ' . $recentTime,
114 - 'converted_amount >= ' . $egFundraiserStatisticsMinimum,
115 - 'converted_amount <= ' . $egFundraiserStatisticsMaximum,
116 - "DATE_FORMAT(CONVERT_TZ(FROM_UNIXTIME(received),'+00:00','$timeZoneOffset'),'%Y-%m-%d') = '$start'"
117 - );
118118
119 - $select = $dbr->select( 'public_reporting',
120 - array(
121 - 'sum(converted_amount)'
122 - ),
123 - $conditions,
 119+ $result = $dbr->select(
 120+ 'public_reporting_days',
 121+ 'round( prd_total ) AS total',
 122+ "prd_date = '$today'",
124123 __METHOD__
125124 );
126 - $row = $dbr->fetchRow( $select );
127 - $total = $row['sum(converted_amount)'];
128 - if ( !$total ) $total = 0;
 125+ $row = $dbr->fetchRow( $result );
129126
 127+ if ( $row['total'] > 0 ) {
 128+ $total = $row['total'];
 129+ } else {
 130+ $total = 0;
 131+ }
 132+
 133+ // Make sure the fudge factor is a number
 134+ if ( is_nan( $fudgeFactor ) ) {
 135+ $fudgeFactor = 0;
 136+ }
 137+
 138+ // Add the fudge factor to the total
 139+ $total += $fudgeFactor;
 140+
130141 $wgMemc->set( $key, $total, $egFundraiserStatisticsCacheTimeout );
131142 return $total;
132143 }
133 -
134 - private function sendHeaders() {
135 - global $wgJsMimeType;
136 - header( "Content-type: $wgJsMimeType; charset=utf-8" );
137 - header( "Cache-Control: public, s-maxage=$this->sharedMaxAge, max-age=$this->maxAge" );
138 - }
139 -
140 - public function isListed(){
141 - return false;
142 - }
143144 }
Index: trunk/extensions/ContributionReporting/ContributionReporting.i18n.php
@@ -15,6 +15,7 @@
1616 // ContributionReporting and ContributionTotal
1717 'contributionreporting-desc' => 'Live reporting on the Wikimedia fundraiser',
1818 'contributiontotal' => 'Contribution total',
 19+ 'dailytotal' => 'Daily total',
1920
2021 'contribstats-header' => '',
2122 'contribstats-footer' => '',
Index: trunk/extensions/ContributionReporting/ContributionStatistics_body.php
@@ -24,6 +24,11 @@
2525
2626 public function execute( $sub ) {
2727 global $wgOut,$egContributionStatisticsViewDays;
 28+
 29+ # Emergecny short cut until this can be changed to use cron-generated data
 30+ # These queries are too expensive to run on demand.
 31+ $wgOut->redirect( SpecialPage::getTitleFor( 'FundraiserStatistics' )->getFullURL() );
 32+ return;
2833
2934 $this->evalDateRange();
3035

Follow-up revisions

RevisionCommit summaryAuthorDate
r106246follow-up to r105965 - better condition statementkaldari21:40, 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:33, 14 December 2011
-				array( 'received > ' . strtotime( 'July 1st 2008' ) ),
+				array( 'received > ' . strtotime( 'July 1st 2011' ) ),

Maybe you should avoid hardcoding years in the code like that? ;)

+			"prd_date = '$today'",

Please use 'prd_date' => $today

Status & tagging log