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