Changing lines like this: "extract( $dbw->tableNames( 'page', 'archive' ) );" to...
[lhc/web/wiklou.git] / includes / SiteStats.php
1 <?php
2
3 /**
4 * Static accessor class for site_stats and related things
5 * @package MediaWiki
6 */
7 class SiteStats {
8 static $row, $loaded = false;
9 static $admins;
10 static $pageCount = 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 $dbr =& wfGetDB( DB_SLAVE );
22 self::$row = $dbr->selectRow( 'site_stats', '*', false, __METHOD__ );
23
24 # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
25 if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
26 # Update schema
27 $u = new SiteStatsUpdate( 0, 0, 0 );
28 $u->doUpdate();
29 self::$row = $dbr->selectRow( 'site_stats', '*', false, $fname );
30 }
31 }
32
33 static function views() {
34 self::load();
35 return self::$row->ss_total_views;
36 }
37
38 static function edits() {
39 self::load();
40 return self::$row->ss_total_edits;
41 }
42
43 static function articles() {
44 self::load();
45 return self::$row->ss_good_articles;
46 }
47
48 static function pages() {
49 self::load();
50 return self::$row->ss_total_pages;
51 }
52
53 static function users() {
54 self::load();
55 return self::$row->ss_users;
56 }
57
58 static function images() {
59 self::load();
60 return self::$row->ss_images;
61 }
62
63 static function admins() {
64 if ( !isset( self::$admins ) ) {
65 $dbr =& wfGetDB( DB_SLAVE );
66 self::$admins = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), __METHOD__ );
67 }
68 return self::$admins;
69 }
70
71 static function pagesInNs( $ns ) {
72 wfProfileIn( __METHOD__ );
73 if( !isset( self::$pageCount[$ns] ) ) {
74 $dbr =& wfGetDB( DB_SLAVE );
75 $pageCount[$ns] = (int)$dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), __METHOD__ );
76 }
77 wfProfileOut( __METHOD__ );
78 return $pageCount[$ns];
79 }
80
81 }
82
83
84 /**
85 *
86 * @package MediaWiki
87 */
88 class SiteStatsUpdate {
89
90 var $mViews, $mEdits, $mGood, $mPages, $mUsers;
91
92 function SiteStatsUpdate( $views, $edits, $good, $pages = 0, $users = 0 ) {
93 $this->mViews = $views;
94 $this->mEdits = $edits;
95 $this->mGood = $good;
96 $this->mPages = $pages;
97 $this->mUsers = $users;
98 }
99
100 function appendUpdate( &$sql, $field, $delta ) {
101 if ( $delta ) {
102 if ( $sql ) {
103 $sql .= ',';
104 }
105 if ( $delta < 0 ) {
106 $sql .= "$field=$field-1";
107 } else {
108 $sql .= "$field=$field+1";
109 }
110 }
111 }
112
113 function doUpdate() {
114 $fname = 'SiteStatsUpdate::doUpdate';
115 $dbw =& wfGetDB( DB_MASTER );
116
117 # First retrieve the row just to find out which schema we're in
118 $row = $dbw->selectRow( 'site_stats', '*', false, $fname );
119
120 $updates = '';
121
122 $this->appendUpdate( $updates, 'ss_total_views', $this->mViews );
123 $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits );
124 $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood );
125
126 if ( isset( $row->ss_total_pages ) ) {
127 # Update schema if required
128 if ( $row->ss_total_pages == -1 && !$this->mViews ) {
129 $dbr =& wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') );
130 list( $page, $user ) = $dbr->tableNamesN( 'page', 'user' );
131
132 $sql = "SELECT COUNT(page_namespace) AS total FROM $page";
133 $res = $dbr->query( $sql, $fname );
134 $pageRow = $dbr->fetchObject( $res );
135 $pages = $pageRow->total + $this->mPages;
136
137 $sql = "SELECT COUNT(user_id) AS total FROM $user";
138 $res = $dbr->query( $sql, $fname );
139 $userRow = $dbr->fetchObject( $res );
140 $users = $userRow->total + $this->mUsers;
141
142 if ( $updates ) {
143 $updates .= ',';
144 }
145 $updates .= "ss_total_pages=$pages, ss_users=$users";
146 } else {
147 $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages );
148 $this->appendUpdate( $updates, 'ss_users', $this->mUsers );
149 }
150 }
151 if ( $updates ) {
152 $site_stats = $dbw->tableName( 'site_stats' );
153 $sql = $dbw->limitResultForUpdate("UPDATE $site_stats SET $updates", 1);
154 $dbw->begin();
155 $dbw->query( $sql, $fname );
156 $dbw->commit();
157 }
158
159 /*
160 global $wgDBname, $wgTitle;
161 if ( $this->mGood && $wgDBname == 'enwiki' ) {
162 $good = $dbw->selectField( 'site_stats', 'ss_good_articles', '', $fname );
163 error_log( $good . ' ' . $wgTitle->getPrefixedDBkey() . "\n", 3, '/home/wikipedia/logs/million.log' );
164 }
165 */
166 }
167 }
168 ?>