* Use local context instead of global variables
[lhc/web/wiklou.git] / includes / specials / SpecialStatistics.php
1 <?php
2 /**
3 * Implements Special:Statistics
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special page lists various statistics, including the contents of
26 * `site_stats`, plus page view details if enabled
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialStatistics extends SpecialPage {
31
32 private $views, $edits, $good, $images, $total, $users,
33 $activeUsers = 0;
34
35 public function __construct() {
36 parent::__construct( 'Statistics' );
37 }
38
39 public function execute( $par ) {
40 global $wgMemc, $wgDisableCounters, $wgMiserMode;
41
42 $this->setHeaders();
43 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
44
45 $this->views = SiteStats::views();
46 $this->edits = SiteStats::edits();
47 $this->good = SiteStats::articles();
48 $this->images = SiteStats::images();
49 $this->total = SiteStats::pages();
50 $this->users = SiteStats::users();
51 $this->activeUsers = SiteStats::activeUsers();
52 $this->hook = '';
53
54 # Staticic - views
55 $viewsStats = '';
56 if( !$wgDisableCounters ) {
57 $viewsStats = $this->getViewsStats();
58 }
59
60 # Set active user count
61 if( !$wgMiserMode ) {
62 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
63 // Re-calculate the count if the last tally is old...
64 if( !$wgMemc->get($key) ) {
65 $dbw = wfGetDB( DB_MASTER );
66 SiteStatsUpdate::cacheUpdate( $dbw );
67 $wgMemc->set( $key, '1', 24*3600 ); // don't update for 1 day
68 }
69 }
70
71 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
72
73 # Statistic - pages
74 $text .= $this->getPageStats();
75
76 # Statistic - edits
77 $text .= $this->getEditStats();
78
79 # Statistic - users
80 $text .= $this->getUserStats();
81
82 # Statistic - usergroups
83 $text .= $this->getGroupStats();
84 $text .= $viewsStats;
85
86 # Statistic - popular pages
87 if( !$wgDisableCounters && !$wgMiserMode ) {
88 $text .= $this->getMostViewedPages();
89 }
90
91 # Statistic - other
92 $extraStats = array();
93 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
94 $text .= $this->getOtherStats( $extraStats );
95 }
96
97 $text .= Xml::closeElement( 'table' );
98
99 # Customizable footer
100 $footer = wfMessage( 'statistics-footer' );
101 if ( !$footer->isBlank() ) {
102 $text .= "\n" . $footer->parse();
103 }
104
105 $this->getOutput()->addHTML( $text );
106 }
107
108 /**
109 * Format a row
110 * @param $text String: description of the row
111 * @param $number Float: a statistical number
112 * @param $trExtraParams Array: params to table row, see Html::elememt
113 * @param $descMsg String: message key
114 * @param $descMsgParam Array: message params
115 * @return string table row in HTML format
116 */
117 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
118 if( $descMsg ) {
119 $msg = wfMessage( $descMsg, $descMsgParam );
120 if ( $msg->exists() ) {
121 $descriptionText = $msg->parse();
122 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'),
123 " ($descriptionText)" );
124 }
125 }
126 return Html::rawElement( 'tr', $trExtraParams,
127 Html::rawElement( 'td', array(), $text ) .
128 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
129 );
130 }
131
132 /**
133 * Each of these methods is pretty self-explanatory, get a particular
134 * row for the table of statistics
135 * @return string
136 */
137 private function getPageStats() {
138 return Xml::openElement( 'tr' ) .
139 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
140 Xml::closeElement( 'tr' ) .
141 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
142 $this->getLang()->formatNum( $this->good ),
143 array( 'class' => 'mw-statistics-articles' ) ) .
144 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
145 $this->getLang()->formatNum( $this->total ),
146 array( 'class' => 'mw-statistics-pages' ),
147 'statistics-pages-desc' ) .
148 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
149 $this->getLang()->formatNum( $this->images ),
150 array( 'class' => 'mw-statistics-files' ) );
151 }
152 private function getEditStats() {
153 return Xml::openElement( 'tr' ) .
154 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
155 Xml::closeElement( 'tr' ) .
156 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
157 $this->getLang()->formatNum( $this->edits ),
158 array( 'class' => 'mw-statistics-edits' ) ) .
159 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
160 $this->getLang()->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
161 array( 'class' => 'mw-statistics-edits-average' ) );
162 }
163
164 private function getUserStats() {
165 global $wgActiveUserDays;
166 return Xml::openElement( 'tr' ) .
167 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
168 Xml::closeElement( 'tr' ) .
169 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
170 $this->getLang()->formatNum( $this->users ),
171 array( 'class' => 'mw-statistics-users' ) ) .
172 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ) . ' ' .
173 Linker::linkKnown(
174 SpecialPage::getTitleFor( 'Activeusers' ),
175 wfMsgHtml( 'listgrouprights-members' )
176 ),
177 $this->getLang()->formatNum( $this->activeUsers ),
178 array( 'class' => 'mw-statistics-users-active' ),
179 'statistics-users-active-desc',
180 $this->getLang()->formatNum( $wgActiveUserDays ) );
181 }
182
183 private function getGroupStats() {
184 global $wgGroupPermissions, $wgImplicitGroups;
185 $text = '';
186 foreach( $wgGroupPermissions as $group => $permissions ) {
187 # Skip generic * and implicit groups
188 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
189 continue;
190 }
191 $groupname = htmlspecialchars( $group );
192 $msg = wfMessage( 'group-' . $groupname );
193 if ( $msg->isBlank() ) {
194 $groupnameLocalized = $groupname;
195 } else {
196 $groupnameLocalized = $msg->text();
197 }
198 $msg = wfMessage( 'grouppage-' . $groupname )->inContentLanguage();
199 if ( $msg->isBlank() ) {
200 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
201 } else {
202 $grouppageLocalized = $msg->text();
203 }
204 $linkTarget = Title::newFromText( $grouppageLocalized );
205 $grouppage = Linker::link(
206 $linkTarget,
207 htmlspecialchars( $groupnameLocalized )
208 );
209 $grouplink = Linker::linkKnown(
210 SpecialPage::getTitleFor( 'Listusers' ),
211 wfMsgHtml( 'listgrouprights-members' ),
212 array(),
213 array( 'group' => $group )
214 );
215 # Add a class when a usergroup contains no members to allow hiding these rows
216 $classZero = '';
217 $countUsers = SiteStats::numberingroup( $groupname );
218 if( $countUsers == 0 ) {
219 $classZero = ' statistics-group-zero';
220 }
221 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
222 $this->getLang()->formatNum( $countUsers ),
223 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
224 }
225 return $text;
226 }
227
228 private function getViewsStats() {
229 return Xml::openElement( 'tr' ) .
230 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
231 Xml::closeElement( 'tr' ) .
232 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
233 $this->getLang()->formatNum( $this->views ),
234 array ( 'class' => 'mw-statistics-views-total' ), 'statistics-views-total-desc' ) .
235 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
236 $this->getLang()->formatNum( sprintf( '%.2f', $this->edits ?
237 $this->views / $this->edits : 0 ) ),
238 array ( 'class' => 'mw-statistics-views-peredit' ) );
239 }
240
241 private function getMostViewedPages() {
242 $text = '';
243 $dbr = wfGetDB( DB_SLAVE );
244 $res = $dbr->select(
245 'page',
246 array(
247 'page_namespace',
248 'page_title',
249 'page_counter',
250 ),
251 array(
252 'page_is_redirect' => 0,
253 'page_counter > 0',
254 ),
255 __METHOD__,
256 array(
257 'ORDER BY' => 'page_counter DESC',
258 'LIMIT' => 10,
259 )
260 );
261 if( $res->numRows() > 0 ) {
262 $text .= Xml::openElement( 'tr' );
263 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
264 $text .= Xml::closeElement( 'tr' );
265 foreach ( $res as $row ) {
266 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
267 if( $title instanceof Title ) {
268 $text .= $this->formatRow( Linker::link( $title ),
269 $this->getLang()->formatNum( $row->page_counter ) );
270
271 }
272 }
273 $res->free();
274 }
275 return $text;
276 }
277
278 private function getOtherStats( $stats ) {
279 if ( !count( $stats ) )
280 return '';
281
282 $return = Xml::openElement( 'tr' ) .
283 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
284 Xml::closeElement( 'tr' );
285
286 foreach( $stats as $name => $number ) {
287 $name = htmlspecialchars( $name );
288 $number = htmlspecialchars( $number );
289
290 $return .= $this->formatRow( $name, $this->getLang()->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
291 }
292
293 return $return;
294 }
295 }