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