Remove ?>'s from files. They're pointless, and just asking for people to mess with...
[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( $row === false ) {
37 // Might have just been initialzed during this request?
38 wfDebug( __METHOD__ . ": site_stats missing on slave\n" );
39 $row = self::doLoad( wfGetDB( DB_MASTER ) );
40 }
41
42 if( $row === false ) {
43 // Normally the site_stats table is initialized at install time.
44 // Some manual construction scenarios may leave the table empty,
45 // however, for instance when importing from a dump into a clean
46 // schema with mwdumper.
47 wfDebug( __METHOD__ . ": initializing empty 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( $row === false ) {
60 wfDebug( __METHOD__ . ": init of site_stats failed 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 }
118
119
120 /**
121 *
122 */
123 class SiteStatsUpdate {
124
125 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
126
127 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
128 $this->mViews = $views;
129 $this->mEdits = $edits;
130 $this->mGood = $good;
131 $this->mPages = $pages;
132 $this->mUsers = $users;
133 }
134
135 function appendUpdate( &$sql, $field, $delta ) {
136 if ( $delta ) {
137 if ( $sql ) {
138 $sql .= ',';
139 }
140 if ( $delta < 0 ) {
141 $sql .= "$field=$field-1";
142 } else {
143 $sql .= "$field=$field+1";
144 }
145 }
146 }
147
148 function doUpdate() {
149 $fname = 'SiteStatsUpdate::doUpdate';
150 $dbw = wfGetDB( DB_MASTER );
151
152 # First retrieve the row just to find out which schema we're in
153 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
154
155 $updates = '';
156
157 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews );
158 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits );
159 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood );
160
161 if ( isset( $row->ss_total_pages ) ) {
162 # Update schema if required
163 if ( $row->ss_total_pages == -1 && !$this->mViews ) {
164 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') );
165 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
166
167 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
168 $res = $dbr->query( $sql, $fname );
169 $pageRow = $dbr->fetchObject( $res );
170 $pages = $pageRow->total + $this->mPages;
171
172 $sql = "SELECT COUNT(user_id) AS total FROM $user";
173 $res = $dbr->query( $sql, $fname );
174 $userRow = $dbr->fetchObject( $res );
175 $users = $userRow->total + $this->mUsers;
176
177 if ( $updates ) {
178 $updates .= ',';
179 }
180 $updates .= "ss_total_pages=$pages, ss_users=$users";
181 } else {
182 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages );
183 $this->appendUpdate( $updates, 'ss_users', $this->mUsers );
184 }
185 }
186 if ( $updates ) {
187 $site_stats = $dbw->tableName( 'site_stats' );
188 $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
189 $dbw->begin();
190 $dbw->query( $sql, $fname );
191 $dbw->commit();
192 }
193
194 /*
195 global $wgDBname, $wgTitle;
196 if ( $this->mGood && $wgDBname == 'enwiki' ) {
197 $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
198 error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );
199 }
200 */
201 }
202 }
203