Add interface for adding own statistics with $wgStatsOther:
[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 $text .= $this->getOtherStats();
82
83 $text .= Xml::closeElement( 'table' );
84
85 # Customizable footer
86 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
87 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
88 $text .= "\n" . $footer;
89 }
90
91 $wgOut->addHTML( $text );
92 }
93
94 /**
95 * Format a row
96 * @param string $text description of the row
97 * @param float $number a number
98 * @param array $trExtraParams
99 * @param string $descMsg
100 * @param string $descMsgParam
101 * @return string table row in HTML format
102 */
103 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
104 global $wgStylePath;
105 if( $descMsg ) {
106 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
107 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
108 $descriptionText = " ($descriptionText)";
109 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'),
110 $descriptionText );
111 }
112 }
113 return Xml::openElement( 'tr', $trExtraParams ) .
114 Xml::openElement( 'td' ) . $text . Xml::closeElement( 'td' ) .
115 Xml::openElement( 'td', array( 'class' => 'mw-statistics-numbers' ) ) . $number . Xml::closeElement( 'td' ) .
116 Xml::closeElement( 'tr' );
117 }
118
119 /**
120 * Each of these methods is pretty self-explanatory, get a particular
121 * row for the table of statistics
122 * @return string
123 */
124 private function getPageStats() {
125 global $wgLang;
126 return Xml::openElement( 'tr' ) .
127 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
128 Xml::closeElement( 'tr' ) .
129 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
130 $wgLang->formatNum( $this->good ),
131 array( 'class' => 'mw-statistics-articles' ) ) .
132 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
133 $wgLang->formatNum( $this->total ),
134 array( 'class' => 'mw-statistics-pages' ),
135 'statistics-pages-desc' ) .
136 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
137 $wgLang->formatNum( $this->images ),
138 array( 'class' => 'mw-statistics-files' ) );
139 }
140 private function getEditStats() {
141 global $wgLang;
142 return Xml::openElement( 'tr' ) .
143 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
144 Xml::closeElement( 'tr' ) .
145 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
146 $wgLang->formatNum( $this->edits ),
147 array( 'class' => 'mw-statistics-edits' ) ) .
148 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
149 $wgLang->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
150 array( 'class' => 'mw-statistics-edits-average' ) ) .
151 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
152 $wgLang->formatNum( $this->numJobs ),
153 array( 'class' => 'mw-statistics-jobqueue' ) );
154 }
155 private function getUserStats() {
156 global $wgLang, $wgRCMaxAge;
157 return Xml::openElement( 'tr' ) .
158 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
159 Xml::closeElement( 'tr' ) .
160 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
161 $wgLang->formatNum( $this->users ),
162 array( 'class' => 'mw-statistics-users' ) ) .
163 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
164 $wgLang->formatNum( $this->activeUsers ),
165 array( 'class' => 'mw-statistics-users-active' ),
166 'statistics-users-active-desc',
167 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
168 }
169 private function getGroupStats() {
170 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
171 $sk = $wgUser->getSkin();
172 $text = '';
173 foreach( $wgGroupPermissions as $group => $permissions ) {
174 # Skip generic * and implicit groups
175 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
176 continue;
177 }
178 $groupname = htmlspecialchars( $group );
179 $msg = wfMsg( 'group-' . $groupname );
180 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
181 $groupnameLocalized = $groupname;
182 } else {
183 $groupnameLocalized = $msg;
184 }
185 $msg = wfMsgForContent( 'grouppage-' . $groupname );
186 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
187 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
188 } else {
189 $grouppageLocalized = $msg;
190 }
191 $linkTarget = Title::newFromText( $grouppageLocalized );
192 $grouppage = $sk->link(
193 $linkTarget,
194 htmlspecialchars( $groupnameLocalized )
195 );
196 $grouplink = $sk->link(
197 SpecialPage::getTitleFor( 'Listusers' ),
198 wfMsgHtml( 'listgrouprights-members' ),
199 array(),
200 array( 'group' => $group ),
201 'known'
202 );
203 # Add a class when a usergroup contains no members to allow hiding these rows
204 $classZero = '';
205 $countUsers = SiteStats::numberingroup( $groupname );
206 if( $countUsers == 0 ) {
207 $classZero = ' statistics-group-zero';
208 }
209 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
210 $wgLang->formatNum( $countUsers ),
211 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
212 }
213 return $text;
214 }
215 private function getViewsStats() {
216 global $wgLang;
217 return Xml::openElement( 'tr' ) .
218 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
219 Xml::closeElement( 'tr' ) .
220 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
221 $wgLang->formatNum( $this->views ),
222 array ( 'class' => 'mw-statistics-views-total' ) ) .
223 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
224 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
225 $this->views / $this->edits : 0 ) ),
226 array ( 'class' => 'mw-statistics-views-peredit' ) );
227 }
228 private function getMostViewedPages() {
229 global $wgLang, $wgUser;
230 $text = '';
231 $dbr = wfGetDB( DB_SLAVE );
232 $sk = $wgUser->getSkin();
233 $res = $dbr->select(
234 'page',
235 array(
236 'page_namespace',
237 'page_title',
238 'page_counter',
239 ),
240 array(
241 'page_is_redirect' => 0,
242 'page_counter > 0',
243 ),
244 __METHOD__,
245 array(
246 'ORDER BY' => 'page_counter DESC',
247 'LIMIT' => 10,
248 )
249 );
250 if( $res->numRows() > 0 ) {
251 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
252 while( $row = $res->fetchObject() ) {
253 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
254 if( $title instanceof Title ) {
255 $text .= $this->formatRow( $sk->link( $title ),
256 $wgLang->formatNum( $row->page_counter ) );
257
258 }
259 }
260 $res->free();
261 }
262 return $text;
263 }
264
265 private function getOtherStats() {
266 global $wgLang, $wgAllowStatsOther, $wgStatsOther;
267
268 if( !$wgAllowStatsOther ) return;
269
270 if ( count( $wgStatsOther ) < 1 ) return;
271
272 $return = Xml::openElement( 'tr' ) .
273 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
274 Xml::closeElement( 'tr' );
275
276 foreach( $wgStatsOther as $name => $number ) {
277 $name = htmlspecialchars( $name );
278 $number = htmlspecialchars( $number );
279
280 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
281 }
282
283 return $return;
284 }
285
286 /**
287 * Do the action=raw output for this page. Legacy, but we support
288 * it for backwards compatibility
289 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
290 */
291 private function doRawOutput() {
292 global $wgOut;
293 $wgOut->disable();
294 header( 'Pragma: nocache' );
295 echo "total=" . $this->total . ";good=" . $this->good . ";views=" .
296 $this->views . ";edits=" . $this->edits . ";users=" . $this->users . ";";
297 echo "activeusers=" . $this->activeUsers . ";admins=" . $this->admins .
298 ";images=" . $this->images . ";jobs=" . $this->numJobs . "\n";
299 return;
300 }
301 }