Merge "Revert "Adding release note for category changes in watchlists""
[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 private $edits, $good, $images, $total, $users,
32 $activeUsers = 0;
33
34 public function __construct() {
35 parent::__construct( 'Statistics' );
36 }
37
38 public function execute( $par ) {
39 global $wgMemc;
40
41 $miserMode = $this->getConfig()->get( 'MiserMode' );
42
43 $this->setHeaders();
44 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
45
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 # Set active user count
55 if ( !$miserMode ) {
56 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
57 // Re-calculate the count if the last tally is old...
58 if ( !$wgMemc->get( $key ) ) {
59 $dbw = wfGetDB( DB_MASTER );
60 SiteStatsUpdate::cacheUpdate( $dbw );
61 $wgMemc->set( $key, '1', 24 * 3600 ); // don't update for 1 day
62 }
63 }
64
65 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
66
67 # Statistic - pages
68 $text .= $this->getPageStats();
69
70 # Statistic - edits
71 $text .= $this->getEditStats();
72
73 # Statistic - users
74 $text .= $this->getUserStats();
75
76 # Statistic - usergroups
77 $text .= $this->getGroupStats();
78
79 # Statistic - other
80 $extraStats = array();
81 if ( Hooks::run( 'SpecialStatsAddExtra', array( &$extraStats, $this->getContext() ) ) ) {
82 $text .= $this->getOtherStats( $extraStats );
83 }
84
85 $text .= Xml::closeElement( 'table' );
86
87 # Customizable footer
88 $footer = $this->msg( 'statistics-footer' );
89 if ( !$footer->isBlank() ) {
90 $text .= "\n" . $footer->parse();
91 }
92
93 $this->getOutput()->addHTML( $text );
94 }
95
96 /**
97 * Format a row
98 * @param string $text Description of the row
99 * @param float $number A statistical number
100 * @param array $trExtraParams Params to table row, see Html::elememt
101 * @param string $descMsg Message key
102 * @param array|string $descMsgParam Message parameters
103 * @return string Table row in HTML format
104 */
105 private function formatRow( $text, $number, $trExtraParams = array(),
106 $descMsg = '', $descMsgParam = ''
107 ) {
108 if ( $descMsg ) {
109 $msg = $this->msg( $descMsg, $descMsgParam );
110 if ( !$msg->isDisabled() ) {
111 $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
112 ->escaped();
113 $text .= "<br />" . Html::rawElement( 'small', array( 'class' => 'mw-statistic-desc' ),
114 " $descriptionHtml" );
115 }
116 }
117
118 return Html::rawElement( 'tr', $trExtraParams,
119 Html::rawElement( 'td', array(), $text ) .
120 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
121 );
122 }
123
124 /**
125 * Each of these methods is pretty self-explanatory, get a particular
126 * row for the table of statistics
127 * @return string
128 */
129 private function getPageStats() {
130 $pageStatsHtml = Xml::openElement( 'tr' ) .
131 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )
132 ->parse() ) .
133 Xml::closeElement( 'tr' ) .
134 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Allpages' ),
135 $this->msg( 'statistics-articles' )->parse() ),
136 $this->getLanguage()->formatNum( $this->good ),
137 array( 'class' => 'mw-statistics-articles' ),
138 'statistics-articles-desc' ) .
139 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
140 $this->getLanguage()->formatNum( $this->total ),
141 array( 'class' => 'mw-statistics-pages' ),
142 'statistics-pages-desc' );
143
144 // Show the image row only, when there are files or upload is possible
145 if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
146 $pageStatsHtml .= $this->formatRow(
147 Linker::linkKnown( SpecialPage::getTitleFor( 'MediaStatistics' ),
148 $this->msg( 'statistics-files' )->parse() ),
149 $this->getLanguage()->formatNum( $this->images ),
150 array( 'class' => 'mw-statistics-files' ) );
151 }
152
153 return $pageStatsHtml;
154 }
155
156 private function getEditStats() {
157 return Xml::openElement( 'tr' ) .
158 Xml::tags( 'th', array( 'colspan' => '2' ),
159 $this->msg( 'statistics-header-edits' )->parse() ) .
160 Xml::closeElement( 'tr' ) .
161 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
162 $this->getLanguage()->formatNum( $this->edits ),
163 array( 'class' => 'mw-statistics-edits' )
164 ) .
165 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
166 $this->getLanguage()
167 ->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
168 array( 'class' => 'mw-statistics-edits-average' )
169 );
170 }
171
172 private function getUserStats() {
173 return Xml::openElement( 'tr' ) .
174 Xml::tags( 'th', array( 'colspan' => '2' ),
175 $this->msg( 'statistics-header-users' )->parse() ) .
176 Xml::closeElement( 'tr' ) .
177 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
178 $this->getLanguage()->formatNum( $this->users ),
179 array( 'class' => 'mw-statistics-users' )
180 ) .
181 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
182 Linker::linkKnown(
183 SpecialPage::getTitleFor( 'Activeusers' ),
184 $this->msg( 'listgrouprights-members' )->escaped()
185 ),
186 $this->getLanguage()->formatNum( $this->activeUsers ),
187 array( 'class' => 'mw-statistics-users-active' ),
188 'statistics-users-active-desc',
189 $this->getLanguage()->formatNum( $this->getConfig()->get( 'ActiveUserDays' ) )
190 );
191 }
192
193 private function getGroupStats() {
194 $text = '';
195 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
196 # Skip generic * and implicit groups
197 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) ) || $group == '*' ) {
198 continue;
199 }
200 $groupname = htmlspecialchars( $group );
201 $msg = $this->msg( 'group-' . $groupname );
202 if ( $msg->isBlank() ) {
203 $groupnameLocalized = $groupname;
204 } else {
205 $groupnameLocalized = $msg->text();
206 }
207 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
208 if ( $msg->isBlank() ) {
209 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
210 } else {
211 $grouppageLocalized = $msg->text();
212 }
213 $linkTarget = Title::newFromText( $grouppageLocalized );
214
215 if ( $linkTarget ) {
216 $grouppage = Linker::link(
217 $linkTarget,
218 htmlspecialchars( $groupnameLocalized )
219 );
220 } else {
221 $grouppage = htmlspecialchars( $groupnameLocalized );
222 }
223
224 $grouplink = Linker::linkKnown(
225 SpecialPage::getTitleFor( 'Listusers' ),
226 $this->msg( 'listgrouprights-members' )->escaped(),
227 array(),
228 array( 'group' => $group )
229 );
230 # Add a class when a usergroup contains no members to allow hiding these rows
231 $classZero = '';
232 $countUsers = SiteStats::numberingroup( $groupname );
233 if ( $countUsers == 0 ) {
234 $classZero = ' statistics-group-zero';
235 }
236 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
237 $this->getLanguage()->formatNum( $countUsers ),
238 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
239 $classZero ) );
240 }
241
242 return $text;
243 }
244
245 /**
246 * Conversion of external statistics into an internal representation
247 * Following a ([<header-message>][<item-message>] = number) pattern
248 *
249 * @param array $stats
250 * @return string
251 */
252 private function getOtherStats( array $stats ) {
253 $return = '';
254
255 foreach ( $stats as $header => $items ) {
256 // Identify the structure used
257 if ( is_array( $items ) ) {
258
259 // Ignore headers that are recursively set as legacy header
260 if ( $header !== 'statistics-header-hooks' ) {
261 $return .= $this->formatRowHeader( $header );
262 }
263
264 // Collect all items that belong to the same header
265 foreach ( $items as $key => $value ) {
266 if ( is_array( $value ) ) {
267 $name = $value['name'];
268 $number = $value['number'];
269 } else {
270 $name = $this->msg( $key )->parse();
271 $number = $value;
272 }
273
274 $return .= $this->formatRow(
275 $name,
276 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
277 array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
278 );
279 }
280 } else {
281 // Create the legacy header only once
282 if ( $return === '' ) {
283 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
284 }
285
286 // Recursively remap the legacy structure
287 $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
288 array( $header => $items ) ) );
289 }
290 }
291
292 return $return;
293 }
294
295 /**
296 * Format row header
297 *
298 * @param string $header
299 * @return string
300 */
301 private function formatRowHeader( $header ) {
302 return Xml::openElement( 'tr' ) .
303 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
304 Xml::closeElement( 'tr' );
305 }
306
307 protected function getGroupName() {
308 return 'wiki';
309 }
310 }