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