r66986 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66985‎ | r66986 | r66987 >
Date:01:56, 28 May 2010
Author:tomasz
Status:deferred
Tags:
Comment:
Adding support for custom date ranges, removing old functions and cruft, fixing stray table bugs and generally simplifying things
Modified paths:
  • /trunk/extensions/ContributionReporting/ContributionTrackingStatistics_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ContributionReporting/ContributionTrackingStatistics_body.php
@@ -21,8 +21,12 @@
2222 }
2323
2424 public function execute( $sub ) {
25 - global $wgOut;
 25+ global $wgOut, $wgRequest;
2626
 27+ $start = $wgRequest->getIntOrNull( 'start' );
 28+ $end = $wgRequest->getIntOrNull( 'end' );
 29+ $format = $wgRequest->getIntOrNull( 'format' );
 30+
2731 // Begin output
2832 $this->setHeaders();
2933
@@ -36,52 +40,91 @@
3741 );
3842
3943 $htmlOut .= Xml::tags( 'tr', null,
40 - Xml::element( 'td', array( 'align' => 'left' ), wfMsg( 'contribstats-imperfect-data' ) ) .
41 - Xml::element( 'td', array( 'align' => 'right' ), wfTimestamp( TS_DB ) . ' (UTC)')
 44+ Xml::element( 'td', array( 'align' => 'left' ),
 45+ wfMsg( 'contribstats-imperfect-data' ) ) .
 46+ Xml::element( 'td', array( 'align' => 'right' ),
 47+ wfTimestamp( TS_DB ) . ' (UTC)')
4248 );
4349 $htmlOut .= Xml::tags( 'tr', null,
44 - Xml::element( 'td', array( 'align' => 'left' ), wfMsg( 'contribstats-fraud-note' ) . " " . wfMsg( 'contribstats-unaudited' ) )
 50+ Xml::element( 'td', array( 'align' => 'left' ),
 51+ wfMsg( 'contribstats-fraud-note' ) . " " .
 52+ wfMsg( 'contribstats-unaudited' ) )
4553 );
4654 $htmlOut .= Xml::tags( 'tr', null,
47 - Xml::element( 'td', array( 'align' => 'left' ), 'PP = ' . wfMsg( 'contribstats-paypal-donations' ) . ', ' .
48 - 'CC = ' . wfMsg( 'contribstats-credit-card' ) )
 55+ Xml::element( 'td', array( 'align' => 'left' ),
 56+ 'PP = ' . wfMsg( 'contribstats-paypal-donations' ) . ', ' .
 57+ 'CC = ' . wfMsg( 'contribstats-credit-card' ) )
4958 );
5059 $htmlOut .= Xml::closeElement( 'table' );
5160
5261 $wgOut->addHTML( $htmlOut );
5362
5463 // Show day totals
55 - $this->showDayTotals();
56 -
57 - $this->showDayTotalsForLastDays(SpecialContributionTrackingStatistics::$number_of_days_to_show);
58 - }
59 -
60 - /* Wrapper */
61 - public function showDayTotalsForLastDays( $num_days ){
62 - //Seriously, PHP 5.3 has cleaner ways of doing this, till then strtotime to the rescue!
63 - $current_day = new DateTime( "now" );
64 - ++$num_days; //really you probably don't want today
65 -
66 - for( $i = 0 ; $i < ($num_days - 1) ; $i++){ //you don't want today
67 - $current_day->modify("-1 day");
68 - $this->showDayTotals(false, $current_day->format("YmdHis")); //MW Format
 64+ if ( $start && $end && $format ) {
 65+ $this->showTotalsForRange( array( $start, $end ), $format );
 66+ } else {
 67+ $end = time();
 68+ $format = 1;
 69+ $offset = SpecialContributionTrackingStatistics::$number_of_days_to_show * 24 * 60 * 60;
 70+ $this->showTotalsForRange( array( ( $end - $offset ), $end ), $format );
6971 }
7072 }
7173
7274 /* Display Functions */
7375
74 - // Html out for the days total
75 - public function showDayTotals( $is_now = true, $timestamp = 0 ) {
76 - global $wgOut,$wgLang;
77 - global $wgAllowedTemplates, $wgAllowedSupport, $wgAllowedPaymentMethod, $wgContributionReportingBaseURL;
 76+ // Generic Table Display for Totals
 77+ // FORMAT: 1 daily, 2 weekly, 3 Monthly, 4 Combined
 78+ public function showTotalsForRange( $range, $format ) {
 79+ global $wgOut;
 80+ global $wgAllowedTempaltes, $wgAllowedSupport,
 81+ $wgAllowedPaymentMethod, $wgContributionReportingBaseURL;
7882
79 - $totals = $this->getDayTotals($is_now, $timestamp);
 83+ list( $start, $end ) = $range;
 84+ $current = $end;
 85+
 86+ switch( $format ) {
 87+ case 1:
 88+ while( $current > $start ) {
 89+ $this->showDayTotals( $current );
 90+ $current = $current - 24 * 60 * 60;
 91+ }
 92+ break;
 93+ case 2:
 94+ break;
 95+ case 3:
 96+ break;
 97+ case 4:
 98+ $totals = $this->getTotalsInRange( $range );
 99+ $this->showCombinedTotals( $totals, $range );
 100+ break;
80101
81 - $msg = wfMsg( 'contribstats-day-totals' ) . " - " . date( 'o-m-d', wfTimestamp( TS_UNIX, $is_now?time():$timestamp ) );
 102+ }
 103+ }
 104+
 105+ // Display tracking information for one day
 106+ public function showDayTotals( $timestamp ) {
 107+ global $wgOut;
 108+ $totals = $this->getDayTotals( $timestamp );
 109+
 110+ $msg = wfMsg( 'contribstats-day-totals' ) . " - " . date( 'o-m-d', $timestamp );
82111 $htmlOut = Xml::element( 'h3', null, $msg );
83112
84 - // Day
85 - $htmlOut .= Xml::openElement( 'table',
 113+ if( isset( $totals ) ) {
 114+ $htmlOut .= $this->createTable( $totals );
 115+ } else {
 116+ $htmlOut .= wfMsg( 'contribstats-nodata' );
 117+ }
 118+
 119+ // Output HTML
 120+ $wgOut->addHTML( $htmlOut );
 121+ }
 122+
 123+ public function createTable( $totals ) {
 124+ // Table headers
 125+ global $wgOut, $wgAllowedTemplates, $wgAllowedSupport;
 126+ global $wgAllowedPaymentMethod, $wgContributionReportingBaseURL;
 127+
 128+ $htmlOut = Xml::openElement( 'table',
86129 array(
87130 'class' => 'sortable',
88131 'border' => 0,
@@ -90,129 +133,68 @@
91134 )
92135 );
93136
94 - if ( isset ( $totals ) ) {
95 - // Table headers
96 - $htmlOut .= Xml::element( 'th', array( 'align' => 'left' ), wfMsg( 'contribstats-banner' ) ) ;
97 - $htmlOut .= Xml::element( 'th', array( 'align' => 'left' ), wfMsg( 'contribstats-landingpage' ) ) ;
98 - $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-payment-type' ) ) ;
99 - $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-payment-type-hits' ) ) ;
100 - $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-donations' ) );
101 - $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-amount' ) );
102 - $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-average' ) );
103 - $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-max' ) );
 137+ $htmlOut .= Xml::element( 'th', array( 'align' => 'left' ), wfMsg( 'contribstats-banner' ) ) ;
 138+ $htmlOut .= Xml::element( 'th', array( 'align' => 'left' ), wfMsg( 'contribstats-landingpage' ) ) ;
 139+ $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-payment-type' ) ) ;
 140+ $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-payment-type-hits' ) ) ;
 141+ $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-donations' ) );
 142+ $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-amount' ) );
 143+ $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-average' ) );
 144+ $htmlOut .= Xml::element( 'th', array( 'align' => 'center' ), wfMsg( 'contribstats-max' ) );
104145
105 - foreach( $totals as $template ) {
106 - //grab info from utm_src, 'unpack' template, landing page, donation page thus far
107 - $expanded_template = explode(".", $template[0]);
108 - if(!isset($expanded_template[1])){ $expanded_template[1] = "";}
109 - if(!isset($expanded_template[2])){ $expanded_template[2] = "";}
 146+ foreach( $totals as $template ) {
 147+ //grab info from utm_src, 'unpack' template, landing page, donation page thus far
 148+ $expanded_template = explode(".", $template[0]);
 149+ if(!isset($expanded_template[1])){ $expanded_template[1] = "";}
 150+ if(!isset($expanded_template[2])){ $expanded_template[2] = "";}
110151
111 - if ( ! in_array($expanded_template[0], $wgAllowedTemplates ) )
112 - continue;
113 - if( ($expanded_template[1] != "") && (! in_array($expanded_template[1], $wgAllowedSupport)) ){
114 - continue;
115 - }
116 - if( ($expanded_template[2] != "") && (! in_array($expanded_template[2], $wgAllowedPaymentMethod)) ){
117 - continue;
118 - }
119 - // Pull together templates, clicks, donations, conversion rate
120 - $amount = ( $template[3] == 0 ) ? 0 : $template[3];
 152+ if ( ! in_array($expanded_template[0], $wgAllowedTemplates ) )
 153+ continue;
 154+ if( ($expanded_template[1] != "") && (! in_array($expanded_template[1], $wgAllowedSupport)) ){
 155+ continue;
 156+ }
 157+ if( ($expanded_template[2] != "") && (! in_array($expanded_template[2], $wgAllowedPaymentMethod)) ){
 158+ continue;
 159+ }
 160+ // Pull together templates, clicks, donations, conversion rate
 161+ $amount = ( $template[3] == 0 ) ? 0 : $template[3];
121162
122 - $link = $wgContributionReportingBaseURL.$expanded_template[0];
123 - $template_link = Xml::element('a', array('href' =>"$link"), $expanded_template[0]);
 163+ $link = $wgContributionReportingBaseURL.$expanded_template[0];
 164+ $template_link = Xml::element('a', array('href' =>"$link"), $expanded_template[0]);
124165
125 - //average donations
126 - $average = 0;
127 - if($template[2] != 0){
128 - $average = $amount / $template[2];
129 - }
130 -
131 - $htmlOut .= Xml::tags( 'tr', null,
132 - Xml::tags( 'td', array( 'align' => 'left'), $template_link ) .
133 - Xml::element( 'td', array( 'align' => 'left'), $expanded_template[1] ) .
134 - Xml::element( 'td', array( 'align' => 'center'), $expanded_template[2] ) .
135 - Xml::element( 'td', array( 'align' => 'center'), $template[1] + $template[2] ) .
136 - Xml::element( 'td', array( 'align' => 'center'), $template[2] ) .
137 - Xml::element( 'td', array( 'align' => 'center'), $amount ) .
138 - Xml::element( 'td', array( 'align' => 'center'), round($average, 2) ) .
139 - Xml::element( 'td', array( 'align' => 'center'), $template[4] )
140 - );
 166+ //average donations
 167+ $average = 0;
 168+ if($template[2] != 0){
 169+ $average = $amount / $template[2];
141170 }
142 - $htmlOut .= Xml::closeElement( 'table' );
143 - } else {
144 - $htmlOut .= wfMsg( 'contribstats-nodata' );
145 - }
146171
147 - // Output HTML
148 - $wgOut->addHTML( $htmlOut );
 172+ $htmlOut .= Xml::tags( 'tr', null,
 173+ Xml::tags( 'td', array( 'align' => 'left'), $template_link ) .
 174+ Xml::element( 'td', array( 'align' => 'left'), $expanded_template[1] ) .
 175+ Xml::element( 'td', array( 'align' => 'center'), $expanded_template[2] ) .
 176+ Xml::element( 'td', array( 'align' => 'center'), $template[1] + $template[2] ) .
 177+ Xml::element( 'td', array( 'align' => 'center'), $template[2] ) .
 178+ Xml::element( 'td', array( 'align' => 'center'), $amount ) .
 179+ Xml::element( 'td', array( 'align' => 'center'), round($average, 2) ) .
 180+ Xml::element( 'td', array( 'align' => 'center'), $template[4] )
 181+ );
149182 }
150183
151 - // Html out for the weekly totals
152 - public function showWeeklyTotals() {
153 - global $wgOut,$wgLang;
154 - global $wgContributionTrackingStatisticsViewWeeks;
 184+ $htmlOut .= Xml::closeElement( 'table' );
155185
156 - $msg = wfMsgExt( 'contribstats-weekly-totals' , array ( 'parsemag' ),
157 - $wgLang->formatNum( $wgContributionTrackingStatisticsViewWeeks ) );
158 - $htmlOut = Xml::element( 'h3', null, $msg );
159 - $wgOut->addHTML( $htmlOut );
160 -
161 - $range = $this->weekRange( wfTimestampNow( TS_UNIX ) ) ;
162 - $ts = strtotime( $range[0] );
163 - while ( $wgContributionTrackingStatisticsViewWeeks > 0 ) {
164 - $this->showWeekTotal( date('Ymd000000', $ts ) ) ;
165 - $ts -= 60 * 60 * 24 * 7;
166 - $wgContributionTrackingStatisticsViewWeeks--;
167 - }
 186+ return $htmlOut;
168187 }
169188
170 - // Html out for a single week
171 - public function showWeekTotal( $week ) {
172 - global $wgOut,$wgLang;
173 - global $wgAllowedTemplates;
 189+ //Display tracking information for combined totals
 190+ public function showCombinedTotals( $totals, $range ) {
 191+ global $wgOut;
174192
175 - $totals = $this->getWeekTotals( $week );
 193+ $msg = date( 'o-m-d', wfTimestamp( TS_UNIX, $range[0] ) ) . ' - ' .
 194+ date( 'o-m-d', wfTimestamp( TS_UNIX, $range[1] ) ) ;
 195+ $htmlOut = Xml::element( 'h3', null, $msg );
176196
177 - // Weeks
178 - if ( isset ( $totals ) ) {
179 - $htmlOut = '';
180 -
181 - $htmlOut .= Xml::element( 'h2', null, date( 'o-m-d', wfTimeStamp( TS_UNIX, $week ) ) );
182 - $htmlOut .= Xml::openElement( 'table',
183 - array(
184 - 'class' => 'sortable',
185 - 'border' => 0,
186 - 'cellpadding' => 5,
187 - 'width' => '100%'
188 - )
189 - );
190 -
191 - // Table headers
192 - $htmlOut .= Xml::element( 'th', array( 'align' => 'left' ), wfMsg( 'contribstats-template' ) ) ;
193 - $htmlOut .= Xml::element( 'th', array( 'align' => 'right' ), wfMsg( 'contribstats-clicks' ) );
194 - $htmlOut .= Xml::element( 'th', array( 'align' => 'right' ), wfMsg( 'contribstats-donations' ) );
195 - $htmlOut .= Xml::element( 'th', array( 'align' => 'right' ), wfMsg( 'contribstats-amount' ) );
196 - $htmlOut .= Xml::element( 'th', array( 'align' => 'right' ), wfMsg( 'contribstats-max' ) );
197 - $htmlOut .= Xml::element( 'th', array( 'align' => 'right' ), wfMsg( 'contribstats-conversion' ) );
198 -
199 - foreach( $totals as $template ) {
200 - if ( ! in_array($template[0], $wgAllowedTemplates ) )
201 - continue;
202 - // Pull together templates, clicks, donations, conversion rate
203 - $conversion_rate = ( $template[1] == 0 ) ? 0 : $template[2] / $template[1] * 100;
204 - $amount = ( $template[3] == 0 ) ? 0 : $template[3];
205 -
206 - $htmlOut .= Xml::tags( 'tr', null,
207 - Xml::element( 'td', array( 'align' => 'left'), $template[0] ) .
208 - Xml::element( 'td', array( 'align' => 'right'), $template[1] ) .
209 - Xml::element( 'td', array( 'align' => 'right'), $template[2] ) .
210 - Xml::element( 'td', array( 'align' => 'right'), $amount ) .
211 - Xml::element( 'td', array( 'align' => 'right'), $template[4] ) .
212 - Xml::element( 'td', array( 'align' => 'right'), $wgLang->formatNum( number_format( $conversion_rate, 2 ) ) )
213 - );
214 - }
215 -
216 - $htmlOut .= Xml::closeElement( 'table' );
 197+ if( isset( $totals ) ) {
 198+ $htmlOut .= $this->createTable( $totals );
217199 } else {
218200 $htmlOut .= wfMsg( 'contribstats-nodata' );
219201 }
@@ -224,32 +206,23 @@
225207 /* Query Functions */
226208
227209 // Totals for today
228 - public function getDayTotals($is_now = true, $timestamp = 0) {
 210+ public function getDayTotals( $timestamp = 0 ) {
229211 $range = array();
230212 $end_format = 'Ymd235959';
231 - if($is_now){
232 - $timestamp = time();
233 - $end_format = 'YmdHis';
234 - }
235213
236 - $range[0] = date( 'Ymd000000' , wfTimestamp(TS_UNIX, $timestamp) );
237 - $range[1] = date( $end_format , wfTimestamp(TS_UNIX, $timestamp) );
 214+ $range[0] = strtotime( date( 'Ymd000000' , wfTimestamp(TS_UNIX, $timestamp) ) );
 215+ $range[1] = strtotime( date( $end_format , wfTimestamp(TS_UNIX, $timestamp) ) );
238216
239217 return $this->getTotalsInRange($range);
240218 }
241219
242 - // Database lookup for week totals
243 - public function getWeekTotals( $week ) {
244 - $range = $this->weekRange( $week );
245 - return $this->getTotalsInRange($range);
246 - }
247 -
248 - //generalized lookup
249 - public function getTotalsInRange($range){
 220+ //Generalized lookup
 221+ //$range @array( star, end ) UNIXTIME
 222+ public function getTotalsInRange( $range ){
250223 $dbr = efContributionTrackingConnection();
251224
252 - $conds[] = "ts >=" . $dbr->addQuotes( $range[0] );
253 - $conds[] = "ts <=" . $dbr->addQuotes( $range[1] );
 225+ $conds[] = "ts >=" . $dbr->addQuotes( date( 'YmdHis', $range[0] ) );
 226+ $conds[] = "ts <=" . $dbr->addQuotes( date( 'YmdHis', $range[1] ) );
254227
255228 $res = $dbr->select(
256229 array( 'contribution_tracking',
@@ -286,7 +259,6 @@
287260 $row[4]
288261 );
289262 }
290 -
291263 return $result;
292264 }
293265

Follow-up revisions

RevisionCommit summaryAuthorDate
r67674Merging in r66986tomasz23:03, 8 June 2010

Status & tagging log