54c9c5b3b26b2725c837cd43d1e9e47c45c9b435
[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
42 # Staticic - views
43 $viewsStats = '';
44 if( !$wgDisableCounters ) {
45 $viewsStats = $this->getViewsStats();
46 }
47
48 # Set active user count
49 if( !$wgMiserMode ) {
50 $dbw = wfGetDB( DB_MASTER );
51 SiteStatsUpdate::cacheUpdate( $dbw );
52 }
53
54 # Do raw output
55 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
56 $this->doRawOutput();
57 }
58
59 $text = Xml::openElement( 'table', array( 'class' => 'mw-statistics-table' ) );
60
61 # Statistic - pages
62 $text .= $this->getPageStats();
63
64 # Statistic - edits
65 $text .= $this->getEditStats();
66
67 # Statistic - users
68 $text .= $this->getUserStats();
69
70 # Statistic - usergroups
71 $text .= $this->getGroupStats();
72 $text .= $viewsStats;
73
74 # Statistic - popular pages
75 if( !$wgDisableCounters && !$wgMiserMode ) {
76 $text .= $this->getMostViewedPages();
77 }
78
79 $text .= Xml::closeElement( 'table' );
80
81 # Customizable footer
82 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
83 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
84 $text .= "\n" . $footer;
85 }
86
87 $wgOut->addHTML( $text );
88 }
89
90 /**
91 * Format a row
92 * @param string $text description of the row
93 * @param float $number a number
94 * @return string table row in HTML format
95 */
96 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '' ) {
97 global $wgStylePath;
98
99 if( $descMsg ) {
100 $descriptionText = wfMsg( $descMsg );
101 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
102 $descriptionText = " ($descriptionText)";
103 $text = $text . "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'), $descriptionText );
104 }
105 }
106
107 return Xml::openElement( 'tr', $trExtraParams ) .
108 Xml::openElement( 'td' ) . $text . Xml::closeElement( 'td' ) .
109 Xml::openElement( 'td' ) . $number . Xml::closeElement( 'td' ) .
110 Xml::closeElement( 'tr' );
111 }
112
113 /**
114 * Each of these methods is pretty self-explanatory, get a particular
115 * row for the table of statistics
116 * @return string
117 */
118 private function getPageStats() {
119 global $wgLang;
120 return Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-pages' ) ) .
121 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
122 $wgLang->formatNum( $this->good ),
123 array( 'class' => 'mw-statistics-articles' ) ) .
124 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
125 $wgLang->formatNum( $this->total ),
126 array( 'class' => 'mw-statistics-pages' ),
127 'statistics-pages-desc' ) .
128 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
129 $wgLang->formatNum( $this->images ),
130 array( 'class' => 'mw-statistics-files' ) );
131 }
132 private function getEditStats() {
133 global $wgLang;
134 return Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-edits' ) ) .
135 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
136 $wgLang->formatNum( $this->edits ),
137 array( 'class' => 'mw-statistics-edits' ) ) .
138 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
139 $wgLang->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
140 array( 'class' => 'mw-statistics-edits-average' ) ) .
141 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
142 $wgLang->formatNum( $this->numJobs ),
143 array( 'class' => 'mw-statistics-jobqueue' ) );
144 }
145 private function getUserStats() {
146 global $wgLang;
147 return Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-users' ) ) .
148 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
149 $wgLang->formatNum( $this->users ),
150 array( 'class' => 'mw-statistics-users' ) ) .
151 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
152 $wgLang->formatNum( $this->activeUsers ),
153 array( 'class' => 'mw-statistics-users-active' ),
154 'statistics-users-active-desc' );
155 }
156 private function getGroupStats() {
157 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
158 $sk = $wgUser->getSkin();
159 $text = '';
160 foreach( $wgGroupPermissions as $group => $permissions ) {
161 # Skip generic * and implicit groups
162 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
163 continue;
164 }
165 $groupname = htmlspecialchars( $group );
166 $msg = wfMsg( 'group-' . $groupname );
167 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
168 $groupnameLocalized = $groupname;
169 } else {
170 $groupnameLocalized = $msg;
171 }
172 $msg = wfMsgForContent( 'grouppage-' . $groupname );
173 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
174 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
175 } else {
176 $grouppageLocalized = $msg;
177 }
178 $grouppage = $sk->makeLink( $grouppageLocalized, $groupnameLocalized );
179 $grouplink = $sk->link( SpecialPage::getTitleFor( 'Listusers' ),
180 wfMsgHtml( 'listgrouprights-members' ),
181 array(),
182 array( 'group' => $group ),
183 'known' );
184 # Add a class when a usergroup contains no members to allow hiding these rows
185 $classZero = '';
186 $countUsers = SiteStats::numberingroup( $groupname );
187 if( $countUsers == 0 ) {
188 $classZero = ' statistics-group-zero';
189 }
190 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
191 $wgLang->formatNum( $countUsers ),
192 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
193 }
194 return $text;
195 }
196 private function getViewsStats() {
197 global $wgLang;
198 return Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-views' ) ) .
199 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
200 $wgLang->formatNum( $this->views ),
201 array ( 'class' => 'mw-statistics-views-total' ) ) .
202 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
203 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
204 $this->views / $this->edits : 0 ) ),
205 array ( 'class' => 'mw-statistics-views-peredit' ) );
206 }
207 private function getMostViewedPages() {
208 global $wgLang, $wgUser;
209 $text = '';
210 $dbr = wfGetDB( DB_SLAVE );
211 $sk = $wgUser->getSkin();
212 $res = $dbr->select(
213 'page',
214 array(
215 'page_namespace',
216 'page_title',
217 'page_counter',
218 ),
219 array(
220 'page_is_redirect' => 0,
221 'page_counter > 0',
222 ),
223 __METHOD__,
224 array(
225 'ORDER BY' => 'page_counter DESC',
226 'LIMIT' => 10,
227 )
228 );
229 if( $res->numRows() > 0 ) {
230 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-mostpopular' ) );
231 while( $row = $res->fetchObject() ) {
232 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
233 if( $title instanceof Title ) {
234 $text .= $this->formatRow( $sk->link( $title ),
235 $wgLang->formatNum( $row->page_counter ) );
236
237 }
238 }
239 $res->free();
240 }
241 return $text;
242 }
243
244 /**
245 * Do the action=raw output for this page. Legacy, but we support
246 * it for backwards compatibility
247 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
248 */
249 private function doRawOutput() {
250 global $wgOut;
251 $wgOut->disable();
252 header( 'Pragma: nocache' );
253 echo "total=" . $this->total . ";good=" . $this->good . ";views=" .
254 $this->views . ";edits=" . $this->edits . ";users=" . $this->users . ";";
255 echo "activeusers=" . $this->activeUsers . ";admins=" . $this->admins .
256 ";images=" . $this->images . ";jobs=" . $this->numJobs . "\n";
257 return;
258 }
259 }