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