Add a class when a usergroup contains no members to allow hiding these rows per Commo...
[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 function wfSpecialStatistics( $par = '' ) {
17 global $wgOut, $wgLang, $wgRequest, $wgUser, $wgContLang, $wgMessageCache;
18 global $wgDisableCounters, $wgMiserMode, $wgImplicitGroups, $wgGroupPermissions;
19 $wgMessageCache->loadAllMessages();
20 $sk = $wgUser->getSkin();
21 $dbr = wfGetDB( DB_SLAVE );
22
23 $views = SiteStats::views();
24 $edits = SiteStats::edits();
25 $good = SiteStats::articles();
26 $images = SiteStats::images();
27 $total = SiteStats::pages();
28 $users = SiteStats::users();
29 $activeUsers = SiteStats::activeUsers();
30 $admins = SiteStats::numberingroup('sysop');
31 $numJobs = SiteStats::jobs();
32
33 # Staticic - views
34 $viewsStats = '';
35 if( !$wgDisableCounters ) {
36 $viewsStats = Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-views' ) ) .
37 formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
38 $wgLang->formatNum( $views ),
39 ' class="mw-statistics-views-total"' ) .
40 formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
41 $wgLang->formatNum( sprintf( '%.2f', $edits ? $views / $edits : 0 ) ),
42 ' class="mw-statistics-views-peredit"' );
43 }
44 # Set active user count
45 if( !$wgMiserMode ) {
46 $dbw = wfGetDB( DB_MASTER );
47 SiteStatsUpdate::cacheUpdate( $dbw );
48 }
49
50 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
51 # Depreciated, kept for backwards compatibility
52 # http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
53 $wgOut->disable();
54 header( 'Pragma: nocache' );
55 echo "total=$total;good=$good;views=$views;edits=$edits;users=$users;";
56 echo "activeusers=$activeUsers;admins=$admins;images=$images;jobs=$numJobs\n";
57 return;
58 } else {
59 $text = Xml::openElement( 'table', array( 'class' => 'mw-statistics-table' ) ) .
60 # Statistic - pages
61 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-pages' ) ) .
62 formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
63 $wgLang->formatNum( $good ),
64 ' class="mw-statistics-articles"' ) .
65 formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
66 $wgLang->formatNum( $total ),
67 ' class="mw-statistics-pages"',
68 'statistics-pages-tooltip' ) .
69 formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
70 $wgLang->formatNum( $images ),
71 ' class="mw-statistics-files"' ) .
72
73 # Statistic - edits
74 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-edits' ) ) .
75 formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
76 $wgLang->formatNum( $edits ),
77 ' class="mw-statistics-edits"' ) .
78 formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
79 $wgLang->formatNum( sprintf( '%.2f', $total ? $edits / $total : 0 ) ),
80 ' class="mw-statistics-edits-average"' ) .
81 formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
82 $wgLang->formatNum( $numJobs ),
83 ' class="mw-statistics-jobqueue"' ) .
84
85 # Statistic - users
86 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-users' ) ) .
87 formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
88 $wgLang->formatNum( $users ),
89 ' class="mw-statistics-users"' ) .
90 formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
91 $wgLang->formatNum( $activeUsers ),
92 ' class="mw-statistics-users-active"',
93 'statistics-users-active-tooltip' );
94
95 # Statistic - usergroups
96 foreach( $wgGroupPermissions as $group => $permissions ) {
97 # Skip generic * and implicit groups
98 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
99 continue;
100 }
101 $groupname = htmlspecialchars( $group );
102 $msg = wfMsg( 'group-' . $groupname );
103 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
104 $groupnameLocalized = $groupname;
105 } else {
106 $groupnameLocalized = $msg;
107 }
108 $msg = wfMsgForContent( 'grouppage-' . $groupname );
109 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
110 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
111 } else {
112 $grouppageLocalized = $msg;
113 }
114 $grouppage = $sk->makeLink( $grouppageLocalized, $groupnameLocalized );
115 $grouplink = $sk->link( SpecialPage::getTitleFor( 'Listusers' ),
116 wfMsgHtml( 'listgrouprights-members' ),
117 array(),
118 array( 'group' => $group ),
119 'known' );
120
121 # Add a class when a usergroup contains no members to allow hiding these rows
122 $classZero = '';
123 $countUsers = SiteStats::numberingroup( $groupname );
124 if( $countUsers == 0 ) {
125 $classZero = ' statistics-group-zero';
126 }
127 $text .= formatRow( $grouppage . ' ' . $grouplink,
128 $wgLang->formatNum( $countUsers ),
129 ' class="statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero . '"' );
130 }
131 }
132 $text .= $viewsStats;
133
134 # Statistic - popular pages
135 if( !$wgDisableCounters && !$wgMiserMode ) {
136 $res = $dbr->select(
137 'page',
138 array(
139 'page_namespace',
140 'page_title',
141 'page_counter',
142 ),
143 array(
144 'page_is_redirect' => 0,
145 'page_counter > 0',
146 ),
147 __METHOD__,
148 array(
149 'ORDER BY' => 'page_counter DESC',
150 'LIMIT' => 10,
151 )
152 );
153 if( $res->numRows() > 0 ) {
154 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-mostpopular' ) );
155 while( $row = $res->fetchObject() ) {
156 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
157 if( $title instanceof Title ) {
158 $text .= formatRow( $sk->link( $title ),
159 $wgLang->formatNum( $row->page_counter ) );
160
161 }
162 }
163 $res->free();
164 }
165 }
166
167 $text .= Xml::closeElement( 'table' );
168
169 # Customizable footer
170 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
171 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
172 $text .= "\n" . $footer;
173 }
174
175 $wgOut->addHTML( $text );
176 }
177
178 /**
179 * Format a row
180 *
181 * @param string $text description of the row
182 * @param float $number a number
183 * @return string table row in HTML format
184 */
185 function formatRow( $text, $number, $trExtraParams = '', $tooltip = '' ) {
186 if( $tooltip ) {
187 $text = '<div title="' . wfMsg( $tooltip ) . '">' . $text . '<sup>*</sup></div>';
188 }
189
190 return "<tr{$trExtraParams}>
191 <td>" .
192 $text .
193 '</td>
194 <td class="mw-statistics-numbers">' .
195 $number .
196 '</td>
197 </tr>';
198 }