]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/SiteStats.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.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
11         static function recache() {
12                 self::load( true );
13         }
14
15         static function load( $recache = false ) {
16                 if ( self::$loaded && !$recache ) {
17                         return;
18                 }
19
20                 self::$row = self::loadAndLazyInit();
21
22                 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
23                 if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
24                         # Update schema
25                         $u = new SiteStatsUpdate( 0, 0, 0 );
26                         $u->doUpdate();
27                         $dbr = wfGetDB( DB_SLAVE );
28                         self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
29                 }
30                 
31                 self::$loaded = true;
32         }
33         
34         static function loadAndLazyInit() {
35                 wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
36                 $row = self::doLoad( wfGetDB( DB_SLAVE ) );
37
38                 if( !self::isSane( $row ) ) {
39                         // Might have just been initialized during this request? Underflow?
40                         wfDebug( __METHOD__ . ": site_stats damaged or missing on slave\n" );
41                         $row = self::doLoad( wfGetDB( DB_MASTER ) );
42                 }
43                 
44                 if( !self::isSane( $row ) ) {
45                         // Normally the site_stats table is initialized at install time.
46                         // Some manual construction scenarios may leave the table empty or
47                         // broken, however, for instance when importing from a dump into a
48                         // clean schema with mwdumper.
49                         wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
50                         
51                         global $IP;
52                         require_once "$IP/maintenance/initStats.inc";
53                         
54                         ob_start();
55                         wfInitStats();
56                         ob_end_clean();
57                         
58                         $row = self::doLoad( wfGetDB( DB_MASTER ) );
59                 }
60                 
61                 if( !self::isSane( $row ) ) {
62                         wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
63                 }
64                 return $row;
65         }
66
67         static function doLoad( $db ) {
68                 return $db->selectRow( 'site_stats', '*', false, __METHOD__ );
69         }
70
71         static function views() {
72                 self::load();
73                 return self::$row->ss_total_views;
74         }
75
76         static function edits() {
77                 self::load();
78                 return self::$row->ss_total_edits;
79         }
80
81         static function articles() {
82                 self::load();
83                 return self::$row->ss_good_articles;
84         }
85
86         static function pages() {
87                 self::load();
88                 return self::$row->ss_total_pages;
89         }
90
91         static function users() {
92                 self::load();
93                 return self::$row->ss_users;
94         }
95         
96         static function images() {
97                 self::load();
98                 return self::$row->ss_images;
99         }
100
101         static function admins() {
102                 if ( !isset( self::$admins ) ) {
103                         $dbr = wfGetDB( DB_SLAVE );
104                         self::$admins = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), __METHOD__ );
105                 }
106                 return self::$admins;
107         }
108
109         static function jobs() {
110                 if ( !isset( self::$jobs ) ) {
111                         $dbr = wfGetDB( DB_SLAVE );
112                         self::$jobs = $dbr->estimateRowCount('job');
113                         /* Zero rows still do single row read for row that doesn't exist, but people are annoyed by that */
114                         if (self::$jobs == 1) {
115                                 self::$jobs = 0;
116                         }
117                 }
118                 return self::$jobs;
119         }
120         
121         static function pagesInNs( $ns ) {
122                 wfProfileIn( __METHOD__ );
123                 if( !isset( self::$pageCount[$ns] ) ) {
124                         $dbr = wfGetDB( DB_SLAVE );
125                         $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__ );
126                 }
127                 wfProfileOut( __METHOD__ );
128                 return $pageCount[$ns];
129         }
130
131         /** Is the provided row of site stats sane, or should it be regenerated? */
132         private static function isSane( $row ) {
133                 if(
134                         $row === false
135                         or $row->ss_total_pages < $row->ss_good_articles
136                         or $row->ss_total_edits < $row->ss_total_pages
137                         or $row->ss_users       < $row->ss_admins
138                 ) {
139                         return false;
140                 }
141                 // Now check for underflow/overflow
142                 foreach( array( 'total_views', 'total_edits', 'good_articles',
143                 'total_pages', 'users', 'admins', 'images' ) as $member ) {
144                         if(
145                                    $row->{"ss_$member"} > 2000000000
146                                 or $row->{"ss_$member"} < 0
147                         ) {
148                                 return false;
149                         }
150                 }
151                 return true;
152         }
153 }
154
155
156 /**
157  *
158  */
159 class SiteStatsUpdate {
160
161         var $mViews, $mEdits, $mGood, $mPages, $mUsers;
162
163         function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
164                 $this->mViews = $views;
165                 $this->mEdits = $edits;
166                 $this->mGood = $good;
167                 $this->mPages = $pages;
168                 $this->mUsers = $users;
169         }
170
171         function appendUpdate( &$sql, $field, $delta ) {
172                 if ( $delta ) {
173                         if ( $sql ) {
174                                 $sql .= ',';
175                         }
176                         if ( $delta < 0 ) {
177                                 $sql .= "$field=$field-1";
178                         } else {
179                                 $sql .= "$field=$field+1";
180                         }
181                 }
182         }
183
184         function doUpdate() {
185                 $fname = 'SiteStatsUpdate::doUpdate';
186                 $dbw = wfGetDB( DB_MASTER );
187
188                 # First retrieve the row just to find out which schema we're in
189                 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
190
191                 $updates = '';
192
193                 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews );
194                 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits );
195                 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood );
196
197                 if ( isset( $row->ss_total_pages ) ) {
198                         # Update schema if required
199                         if ( $row->ss_total_pages == -1 && !$this->mViews ) {
200                                 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') );
201                                 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
202
203                                 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
204                                 $res = $dbr->query( $sql, $fname );
205                                 $pageRow = $dbr->fetchObject( $res );
206                                 $pages = $pageRow->total + $this->mPages;
207
208                                 $sql = "SELECT COUNT(user_id) AS total FROM $user";
209                                 $res = $dbr->query( $sql, $fname );
210                                 $userRow = $dbr->fetchObject( $res );
211                                 $users = $userRow->total + $this->mUsers;
212
213                                 if ( $updates ) {
214                                         $updates .= ',';
215                                 }
216                                 $updates .= "ss_total_pages=$pages, ss_users=$users";
217                         } else {
218                                 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages );
219                                 $this->appendUpdate( $updates, 'ss_users', $this->mUsers );
220                         }
221                 }
222                 if ( $updates ) {
223                         $site_stats = $dbw->tableName( 'site_stats' );
224                         $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
225                         $dbw->begin();
226                         $dbw->query( $sql, $fname );
227                         $dbw->commit();
228                 }
229
230                 /*
231                 global $wgDBname, $wgTitle;
232                 if ( $this->mGood && $wgDBname == 'enwiki' ) {
233                         $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
234                         error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );
235                 }
236                 */
237         }
238 }
239