(r54511) Replace global array with hook
[lhc/web/wiklou.git] / includes / specials / SpecialStatistics.php
1 <?php
2
3 /**
4 * Special page lists various statistics, including the contents of
5 * `site_stats`, plus page view details if enabled
6 *
7 * @file
8 * @ingroup SpecialPage
9 */
10
11 /**
12 * Show the special page
13 *
14 * @param mixed $par (not used)
15 */
16 class SpecialStatistics extends SpecialPage {
17
18 private $views, $edits, $good, $images, $total, $users,
19 $activeUsers, $admins, $numJobs = 0;
20
21 public function __construct() {
22 parent::__construct( 'Statistics' );
23 }
24
25 public function execute( $par ) {
26 global $wgOut, $wgRequest, $wgMessageCache;
27 global $wgDisableCounters, $wgMiserMode;
28 $wgMessageCache->loadAllMessages();
29
30 $this->setHeaders();
31
32 $this->views = SiteStats::views();
33 $this->edits = SiteStats::edits();
34 $this->good = SiteStats::articles();
35 $this->images = SiteStats::images();
36 $this->total = SiteStats::pages();
37 $this->users = SiteStats::users();
38 $this->activeUsers = SiteStats::activeUsers();
39 $this->admins = SiteStats::numberingroup('sysop');
40 $this->numJobs = SiteStats::jobs();
41 $this->hook = '';
42
43 # Staticic - views
44 $viewsStats = '';
45 if( !$wgDisableCounters ) {
46 $viewsStats = $this->getViewsStats();
47 }
48
49 # Set active user count
50 if( !$wgMiserMode ) {
51 $dbw = wfGetDB( DB_MASTER );
52 SiteStatsUpdate::cacheUpdate( $dbw );
53 }
54
55 # Do raw output
56 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
57 $this->doRawOutput();
58 }
59
60 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
61
62 # Statistic - pages
63 $text .= $this->getPageStats();
64
65 # Statistic - edits
66 $text .= $this->getEditStats();
67
68 # Statistic - users
69 $text .= $this->getUserStats();
70
71 # Statistic - usergroups
72 $text .= $this->getGroupStats();
73 $text .= $viewsStats;
74
75 # Statistic - popular pages
76 if( !$wgDisableCounters && !$wgMiserMode ) {
77 $text .= $this->getMostViewedPages();
78 }
79
80 # Statistic - other
81 $extraStats = array();
82 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
83 $text .= $this->getOtherStats( $extraStats );
84 }
85
86 $text .= Xml::closeElement( 'table' );
87
88 # Customizable footer
89 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
90 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
91 $text .= "\n" . $footer;
92 }
93
94 $wgOut->addHTML( $text );
95 }
96
97 /**
98 * Format a row
99 * @param string $text description of the row
100 * @param float $number a number
101 * @param array $trExtraParams
102 * @param string $descMsg
103 * @param string $descMsgParam
104 * @return string table row in HTML format
105 */
106 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
107 global $wgStylePath;
108 if( $descMsg ) {
109 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
110 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
111 $descriptionText = " ($descriptionText)";
112 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'),
113 $descriptionText );
114 }
115 }
116 return Xml::openElement( 'tr', $trExtraParams ) .
117 Xml::openElement( 'td' ) . $text . Xml::closeElement( 'td' ) .
118 Xml::openElement( 'td', array( 'class' => 'mw-statistics-numbers' ) ) . $number . Xml::closeElement( 'td' ) .
119 Xml::closeElement( 'tr' );
120 }
121
122 /**
123 * Each of these methods is pretty self-explanatory, get a particular
124 * row for the table of statistics
125 * @return string
126 */
127 private function getPageStats() {
128 global $wgLang;
129 return Xml::openElement( 'tr' ) .
130 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
131 Xml::closeElement( 'tr' ) .
132 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
133 $wgLang->formatNum( $this->good ),
134 array( 'class' => 'mw-statistics-articles' ) ) .
135 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
136 $wgLang->formatNum( $this->total ),
137 array( 'class' => 'mw-statistics-pages' ),
138 'statistics-pages-desc' ) .
139 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
140 $wgLang->formatNum( $this->images ),
141 array( 'class' => 'mw-statistics-files' ) );
142 }
143 private function getEditStats() {
144 global $wgLang;
145 return Xml::openElement( 'tr' ) .
146 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
147 Xml::closeElement( 'tr' ) .
148 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
149 $wgLang->formatNum( $this->edits ),
150 array( 'class' => 'mw-statistics-edits' ) ) .
151 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
152 $wgLang->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
153 array( 'class' => 'mw-statistics-edits-average' ) ) .
154 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
155 $wgLang->formatNum( $this->numJobs ),
156 array( 'class' => 'mw-statistics-jobqueue' ) );
157 }
158 private function getUserStats() {
159 global $wgLang, $wgRCMaxAge;
160 return Xml::openElement( 'tr' ) .
161 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
162 Xml::closeElement( 'tr' ) .
163 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
164 $wgLang->formatNum( $this->users ),
165 array( 'class' => 'mw-statistics-users' ) ) .
166 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
167 $wgLang->formatNum( $this->activeUsers ),
168 array( 'class' => 'mw-statistics-users-active' ),
169 'statistics-users-active-desc',
170 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
171 }
172 private function getGroupStats() {
173 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
174 $sk = $wgUser->getSkin();
175 $text = '';
176 foreach( $wgGroupPermissions as $group => $permissions ) {
177 # Skip generic * and implicit groups
178 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
179 continue;
180 }
181 $groupname = htmlspecialchars( $group );
182 $msg = wfMsg( 'group-' . $groupname );
183 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
184 $groupnameLocalized = $groupname;
185 } else {
186 $groupnameLocalized = $msg;
187 }
188 $msg = wfMsgForContent( 'grouppage-' . $groupname );
189 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
190 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
191 } else {
192 $grouppageLocalized = $msg;
193 }
194 $linkTarget = Title::newFromText( $grouppageLocalized );
195 $grouppage = $sk->link(
196 $linkTarget,
197 htmlspecialchars( $groupnameLocalized )
198 );
199 $grouplink = $sk->link(
200 SpecialPage::getTitleFor( 'Listusers' ),
201 wfMsgHtml( 'listgrouprights-members' ),
202 array(),
203 array( 'group' => $group ),
204 'known'
205 );
206 # Add a class when a usergroup contains no members to allow hiding these rows
207 $classZero = '';
208 $countUsers = SiteStats::numberingroup( $groupname );
209 if( $countUsers == 0 ) {
210 $classZero = ' statistics-group-zero';
211 }
212 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
213 $wgLang->formatNum( $countUsers ),
214 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
215 }
216 return $text;
217 }
218 private function getViewsStats() {
219 global $wgLang;
220 return Xml::openElement( 'tr' ) .
221 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
222 Xml::closeElement( 'tr' ) .
223 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
224 $wgLang->formatNum( $this->views ),
225 array ( 'class' => 'mw-statistics-views-total' ) ) .
226 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
227 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
228 $this->views / $this->edits : 0 ) ),
229 array ( 'class' => 'mw-statistics-views-peredit' ) );
230 }
231 private function getMostViewedPages() {
232 global $wgLang, $wgUser;
233 $text = '';
234 $dbr = wfGetDB( DB_SLAVE );
235 $sk = $wgUser->getSkin();
236 $res = $dbr->select(
237 'page',
238 array(
239 'page_namespace',
240 'page_title',
241 'page_counter',
242 ),
243 array(
244 'page_is_redirect' => 0,
245 'page_counter > 0',
246 ),
247 __METHOD__,
248 array(
249 'ORDER BY' => 'page_counter DESC',
250 'LIMIT' => 10,
251 )
252 );
253 if( $res->numRows() > 0 ) {
254 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
255 while( $row = $res->fetchObject() ) {
256 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
257 if( $title instanceof Title ) {
258 $text .= $this->formatRow( $sk->link( $title ),
259 $wgLang->formatNum( $row->page_counter ) );
260
261 }
262 }
263 $res->free();
264 }
265 return $text;
266 }
267
268 private function getOtherStats( $stats ) {
269 global $wgLang, $wgAllowStatsOther, $wgStatsOther;
270
271 $return = Xml::openElement( 'tr' ) .
272 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
273 Xml::closeElement( 'tr' );
274
275 foreach( $stats as $name => $number ) {
276 $name = htmlspecialchars( $name );
277 $number = htmlspecialchars( $number );
278
279 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
280 }
281
282 return $return;
283 }
284
285 /**
286 * Do the action=raw output for this page. Legacy, but we support
287 * it for backwards compatibility
288 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
289 */
290 private function doRawOutput() {
291 global $wgOut;
292 $wgOut->disable();
293 header( 'Pragma: nocache' );
294 echo "total=" . $this->total . ";good=" . $this->good . ";views=" .
295 $this->views . ";edits=" . $this->edits . ";users=" . $this->users . ";";
296 echo "activeusers=" . $this->activeUsers . ";admins=" . $this->admins .
297 ";images=" . $this->images . ";jobs=" . $this->numJobs . "\n";
298 return;
299 }
300 }