fixing r65339 which broke Upload when file dest was undefined
[lhc/web/wiklou.git] / includes / SiteStats.php
1 <?php
2
3 /**
4 * Static accessor class for site_stats and related things
5 */
6 class SiteStats {
7 static $row, $loaded = false;
8 static $admins, $jobs;
9 static $pageCount = array();
10 static $groupMemberCounts = array();
11
12 static function recache() {
13 self::load( true );
14 }
15
16 static function load( $recache = false ) {
17 if ( self::$loaded && !$recache ) {
18 return;
19 }
20
21 self::$row = self::loadAndLazyInit();
22
23 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
24 if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
25 # Update schema
26 $u = new SiteStatsUpdate( 0, 0, 0 );
27 $u->doUpdate();
28 $dbr = wfGetDB( DB_SLAVE );
29 self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
30 }
31
32 self::$loaded = true;
33 }
34
35 static function loadAndLazyInit() {
36 wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
37 $row = self::doLoad( wfGetDB( DB_SLAVE ) );
38
39 if( !self::isSane( $row ) ) {
40 // Might have just been initialized during this request? Underflow?
41 wfDebug( __METHOD__ . ": site_stats damaged or missing on slave\n" );
42 $row = self::doLoad( wfGetDB( DB_MASTER ) );
43 }
44
45 if( !self::isSane( $row ) ) {
46 // Normally the site_stats table is initialized at install time.
47 // Some manual construction scenarios may leave the table empty or
48 // broken, however, for instance when importing from a dump into a
49 // clean schema with mwdumper.
50 wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
51
52 SiteStatsInit::doAllAndCommit( false );
53
54 $row = self::doLoad( wfGetDB( DB_MASTER ) );
55 }
56
57 if( !self::isSane( $row ) ) {
58 wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
59 }
60 return $row;
61 }
62
63 static function doLoad( $db ) {
64 return $db->selectRow( 'site_stats', '*', false, __METHOD__ );
65 }
66
67 static function views() {
68 self::load();
69 return self::$row->ss_total_views;
70 }
71
72 static function edits() {
73 self::load();
74 return self::$row->ss_total_edits;
75 }
76
77 static function articles() {
78 self::load();
79 return self::$row->ss_good_articles;
80 }
81
82 static function pages() {
83 self::load();
84 return self::$row->ss_total_pages;
85 }
86
87 static function users() {
88 self::load();
89 return self::$row->ss_users;
90 }
91
92 static function activeUsers() {
93 self::load();
94 return self::$row->ss_active_users;
95 }
96
97 static function images() {
98 self::load();
99 return self::$row->ss_images;
100 }
101
102 /**
103 * @deprecated Use self::numberingroup('sysop') instead
104 */
105 static function admins() {
106 wfDeprecated(__METHOD__);
107 return self::numberingroup('sysop');
108 }
109
110 /**
111 * Find the number of users in a given user group.
112 * @param string $group Name of group
113 * @return int
114 */
115 static function numberingroup($group) {
116 if ( !isset( self::$groupMemberCounts[$group] ) ) {
117 global $wgMemc;
118 $key = wfMemcKey( 'SiteStats', 'groupcounts', $group );
119 $hit = $wgMemc->get( $key );
120 if ( !$hit ) {
121 $dbr = wfGetDB( DB_SLAVE );
122 $hit = $dbr->selectField( 'user_groups', 'COUNT(*)',
123 array( 'ug_group' => $group ), __METHOD__ );
124 $wgMemc->set( $key, $hit, 3600 );
125 }
126 self::$groupMemberCounts[$group] = $hit;
127 }
128 return self::$groupMemberCounts[$group];
129 }
130
131 static function jobs() {
132 if ( !isset( self::$jobs ) ) {
133 $dbr = wfGetDB( DB_SLAVE );
134 self::$jobs = $dbr->estimateRowCount('job');
135 /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
136 if (self::$jobs == 1) {
137 self::$jobs = 0;
138 }
139 }
140 return self::$jobs;
141 }
142
143 static function pagesInNs( $ns ) {
144 wfProfileIn( __METHOD__ );
145 if( !isset( self::$pageCount[$ns] ) ) {
146 $dbr = wfGetDB( DB_SLAVE );
147 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__ );
148 }
149 wfProfileOut( __METHOD__ );
150 return $pageCount[$ns];
151 }
152
153 /** Is the provided row of site stats sane, or should it be regenerated? */
154 private static function isSane( $row ) {
155 if(
156 $row === false
157 or $row->ss_total_pages < $row->ss_good_articles
158 or $row->ss_total_edits < $row->ss_total_pages
159 ) {
160 return false;
161 }
162 // Now check for underflow/overflow
163 foreach( array( 'total_views', 'total_edits', 'good_articles',
164 'total_pages', 'users', 'admins', 'images' ) as $member ) {
165 if(
166 $row->{"ss_$member"} > 2000000000
167 or $row->{"ss_$member"} < 0
168 ) {
169 return false;
170 }
171 }
172 return true;
173 }
174 }
175
176
177 /**
178 *
179 */
180 class SiteStatsUpdate {
181
182 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
183
184 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
185 $this->mViews = $views;
186 $this->mEdits = $edits;
187 $this->mGood = $good;
188 $this->mPages = $pages;
189 $this->mUsers = $users;
190 }
191
192 function appendUpdate( &$sql, $field, $delta ) {
193 if ( $delta ) {
194 if ( $sql ) {
195 $sql .= ',';
196 }
197 if ( $delta < 0 ) {
198 $sql .= "$field=$field-1";
199 } else {
200 $sql .= "$field=$field+1";
201 }
202 }
203 }
204
205 function doUpdate() {
206 $dbw = wfGetDB( DB_MASTER );
207
208 $updates = '';
209
210 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews );
211 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits );
212 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood );
213 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages );
214 $this->appendUpdate( $updates, 'ss_users', $this->mUsers );
215
216 if ( $updates ) {
217 $site_stats = $dbw->tableName( 'site_stats' );
218 $sql = "UPDATE $site_stats SET $updates";
219
220 # Need a separate transaction because this a global lock
221 $dbw->begin();
222 $dbw->query( $sql, __METHOD__ );
223 $dbw->commit();
224 }
225 }
226
227 public static function cacheUpdate( $dbw ) {
228 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') );
229 # Get non-bot users than did some recent action other than making accounts.
230 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
231 $activeUsers = $dbr->selectField( 'recentchanges', 'COUNT( DISTINCT rc_user_text )',
232 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers' OR rc_log_type IS NULL" ),
233 __METHOD__ );
234 $dbw->update( 'site_stats',
235 array( 'ss_active_users' => intval($activeUsers) ),
236 array( 'ss_row_id' => 1 ), __METHOD__
237 );
238 return $activeUsers;
239 }
240 }
241
242 /**
243 * Class designed for counting of stats.
244 */
245 class SiteStatsInit {
246
247 // Db connection
248 private $db;
249
250 // Various stats
251 private $mEdits, $mArticles, $mPages, $mUsers, $mViews, $mFiles = 0;
252
253 /**
254 * Constructor
255 * @param $useMaster bool Whether to use the master db
256 */
257 public function __construct( $useMaster = false ) {
258 $this->db = wfGetDB( $useMaster ? DB_MASTER : DB_SLAVE );
259 }
260
261 /**
262 * Count the total number of edits
263 * @return int
264 */
265 public function edits() {
266 $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
267 $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
268 return $this->mEdits;
269 }
270
271 /**
272 * Count pages in article space
273 * @return int
274 */
275 public function articles() {
276 global $wgContentNamespaces;
277 $this->mArticles = $this->db->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $wgContentNamespaces, 'page_is_redirect' => 0, 'page_len > 0' ), __METHOD__ );
278 return $this->mArticles;
279 }
280
281 /**
282 * Count total pages
283 * @return int
284 */
285 public function pages() {
286 $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
287 return $this->mPages;
288 }
289
290 /**
291 * Count total users
292 * @return int
293 */
294 public function users() {
295 $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
296 return $this->mUsers;
297 }
298
299 /**
300 * Count views
301 * @return int
302 */
303 public function views() {
304 $this->mViews = $this->db->selectField( 'page', 'SUM(page_counter)', '', __METHOD__ );
305 return $this->mViews;
306 }
307
308 /**
309 * Count total files
310 * @return int
311 */
312 public function files() {
313 $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
314 return $this->mFiles;
315 }
316
317 /**
318 * Do all updates and commit them. More or less a replacement
319 * for the original initStats, but without the calls to wfOut()
320 * @param $update bool Whether to update the current stats or write fresh
321 * @param $noViews bool When true, do not update the number of page views
322 * @param $activeUsers Whether to update the number of active users
323 */
324 public static function doAllAndCommit( $update, $noViews = false, $activeUsers = false ) {
325 // Grab the object and count everything
326 $counter = new SiteStatsInit( false );
327 $counter->edits();
328 $counter->articles();
329 $counter->pages();
330 $counter->users();
331 $counter->files();
332
333 // Only do views if we don't want to not count them
334 if( !$noViews )
335 $counter->views();
336
337 // Update/refresh
338 if( $update )
339 $counter->update();
340 else
341 $counter->refresh();
342
343 // Count active users if need be
344 if( $activeUsers )
345 SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
346 }
347
348 /**
349 * Update the current row with the selected values
350 */
351 public function update() {
352 list( $values, $conds ) = $this->getDbParams();
353 $dbw = wfGetDB( DB_MASTER );
354 $dbw->update( 'site_stats', $values, $conds, __METHOD__ );
355 }
356
357 /**
358 * Refresh site_stats. Erase the current record and save all
359 * the new values.
360 */
361 public function refresh() {
362 list( $values, $conds, $views ) = $this->getDbParams();
363 $dbw = wfGetDB( DB_MASTER );
364 $dbw->delete( 'site_stats', $conds, __METHOD__ );
365 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), __METHOD__ );
366 }
367
368 /**
369 * Return three arrays of params for the db queries
370 * @return array
371 */
372 private function getDbParams() {
373 $values = array( 'ss_total_edits' => $this->mEdits,
374 'ss_good_articles' => $this->mArticles,
375 'ss_total_pages' => $this->mPages,
376 'ss_users' => $this->mUsers,
377 'ss_images' => $this->mFiles );
378 $conds = array( 'ss_row_id' => 1 );
379 $views = array( 'ss_total_views' => $this->mViews );
380 return array( $values, $conds, $views );
381 }
382 }