00001 <?php
00002
00016 class SpecialStatistics extends SpecialPage {
00017
00018 private $views, $edits, $good, $images, $total, $users,
00019 $activeUsers, $admins, $numJobs = 0;
00020
00021 public function __construct() {
00022 parent::__construct( 'Statistics' );
00023 }
00024
00025 public function execute( $par ) {
00026 global $wgOut, $wgRequest, $wgMessageCache, $wgMemc;
00027 global $wgDisableCounters, $wgMiserMode;
00028 $wgMessageCache->loadAllMessages();
00029
00030 $this->setHeaders();
00031
00032 $this->views = SiteStats::views();
00033 $this->edits = SiteStats::edits();
00034 $this->good = SiteStats::articles();
00035 $this->images = SiteStats::images();
00036 $this->total = SiteStats::pages();
00037 $this->users = SiteStats::users();
00038 $this->activeUsers = SiteStats::activeUsers();
00039 $this->admins = SiteStats::numberingroup('sysop');
00040 $this->numJobs = SiteStats::jobs();
00041 $this->hook = '';
00042
00043 # Staticic - views
00044 $viewsStats = '';
00045 if( !$wgDisableCounters ) {
00046 $viewsStats = $this->getViewsStats();
00047 }
00048
00049 # Set active user count
00050 if( !$wgMiserMode ) {
00051 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
00052
00053 if( !$wgMemc->get($key) ) {
00054 $dbw = wfGetDB( DB_MASTER );
00055 SiteStatsUpdate::cacheUpdate( $dbw );
00056 $wgMemc->set( $key, '1', 24*3600 );
00057 }
00058 }
00059
00060 # Do raw output
00061 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
00062 $this->doRawOutput();
00063 }
00064
00065 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
00066
00067 # Statistic - pages
00068 $text .= $this->getPageStats();
00069
00070 # Statistic - edits
00071 $text .= $this->getEditStats();
00072
00073 # Statistic - users
00074 $text .= $this->getUserStats();
00075
00076 # Statistic - usergroups
00077 $text .= $this->getGroupStats();
00078 $text .= $viewsStats;
00079
00080 # Statistic - popular pages
00081 if( !$wgDisableCounters && !$wgMiserMode ) {
00082 $text .= $this->getMostViewedPages();
00083 }
00084
00085 # Statistic - other
00086 $extraStats = array();
00087 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
00088 $text .= $this->getOtherStats( $extraStats );
00089 }
00090
00091 $text .= Xml::closeElement( 'table' );
00092
00093 # Customizable footer
00094 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
00095 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
00096 $text .= "\n" . $footer;
00097 }
00098
00099 $wgOut->addHTML( $text );
00100 }
00101
00111 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
00112 global $wgStylePath;
00113 if( $descMsg ) {
00114 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
00115 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
00116 $descriptionText = " ($descriptionText)";
00117 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'),
00118 $descriptionText );
00119 }
00120 }
00121 return Xml::openElement( 'tr', $trExtraParams ) .
00122 Xml::openElement( 'td' ) . $text . Xml::closeElement( 'td' ) .
00123 Xml::openElement( 'td', array( 'class' => 'mw-statistics-numbers' ) ) . $number . Xml::closeElement( 'td' ) .
00124 Xml::closeElement( 'tr' );
00125 }
00126
00132 private function getPageStats() {
00133 global $wgLang;
00134 return Xml::openElement( 'tr' ) .
00135 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
00136 Xml::closeElement( 'tr' ) .
00137 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
00138 $wgLang->formatNum( $this->good ),
00139 array( 'class' => 'mw-statistics-articles' ) ) .
00140 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
00141 $wgLang->formatNum( $this->total ),
00142 array( 'class' => 'mw-statistics-pages' ),
00143 'statistics-pages-desc' ) .
00144 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
00145 $wgLang->formatNum( $this->images ),
00146 array( 'class' => 'mw-statistics-files' ) );
00147 }
00148 private function getEditStats() {
00149 global $wgLang;
00150 return Xml::openElement( 'tr' ) .
00151 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
00152 Xml::closeElement( 'tr' ) .
00153 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
00154 $wgLang->formatNum( $this->edits ),
00155 array( 'class' => 'mw-statistics-edits' ) ) .
00156 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
00157 $wgLang->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
00158 array( 'class' => 'mw-statistics-edits-average' ) ) .
00159 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
00160 $wgLang->formatNum( $this->numJobs ),
00161 array( 'class' => 'mw-statistics-jobqueue' ) );
00162 }
00163 private function getUserStats() {
00164 global $wgLang, $wgUser, $wgRCMaxAge;
00165 $sk = $wgUser->getSkin();
00166 return Xml::openElement( 'tr' ) .
00167 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
00168 Xml::closeElement( 'tr' ) .
00169 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
00170 $wgLang->formatNum( $this->users ),
00171 array( 'class' => 'mw-statistics-users' ) ) .
00172 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ) . ' ' .
00173 $sk->link(
00174 SpecialPage::getTitleFor( 'Activeusers' ),
00175 wfMsgHtml( 'listgrouprights-members' ),
00176 array(),
00177 array(),
00178 'known'
00179 ),
00180 $wgLang->formatNum( $this->activeUsers ),
00181 array( 'class' => 'mw-statistics-users-active' ),
00182 'statistics-users-active-desc',
00183 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
00184 }
00185 private function getGroupStats() {
00186 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
00187 $sk = $wgUser->getSkin();
00188 $text = '';
00189 foreach( $wgGroupPermissions as $group => $permissions ) {
00190 # Skip generic * and implicit groups
00191 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
00192 continue;
00193 }
00194 $groupname = htmlspecialchars( $group );
00195 $msg = wfMsg( 'group-' . $groupname );
00196 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
00197 $groupnameLocalized = $groupname;
00198 } else {
00199 $groupnameLocalized = $msg;
00200 }
00201 $msg = wfMsgForContent( 'grouppage-' . $groupname );
00202 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
00203 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
00204 } else {
00205 $grouppageLocalized = $msg;
00206 }
00207 $linkTarget = Title::newFromText( $grouppageLocalized );
00208 $grouppage = $sk->link(
00209 $linkTarget,
00210 htmlspecialchars( $groupnameLocalized )
00211 );
00212 $grouplink = $sk->link(
00213 SpecialPage::getTitleFor( 'Listusers' ),
00214 wfMsgHtml( 'listgrouprights-members' ),
00215 array(),
00216 array( 'group' => $group ),
00217 'known'
00218 );
00219 # Add a class when a usergroup contains no members to allow hiding these rows
00220 $classZero = '';
00221 $countUsers = SiteStats::numberingroup( $groupname );
00222 if( $countUsers == 0 ) {
00223 $classZero = ' statistics-group-zero';
00224 }
00225 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
00226 $wgLang->formatNum( $countUsers ),
00227 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
00228 }
00229 return $text;
00230 }
00231 private function getViewsStats() {
00232 global $wgLang;
00233 return Xml::openElement( 'tr' ) .
00234 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
00235 Xml::closeElement( 'tr' ) .
00236 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
00237 $wgLang->formatNum( $this->views ),
00238 array ( 'class' => 'mw-statistics-views-total' ) ) .
00239 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
00240 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
00241 $this->views / $this->edits : 0 ) ),
00242 array ( 'class' => 'mw-statistics-views-peredit' ) );
00243 }
00244 private function getMostViewedPages() {
00245 global $wgLang, $wgUser;
00246 $text = '';
00247 $dbr = wfGetDB( DB_SLAVE );
00248 $sk = $wgUser->getSkin();
00249 $res = $dbr->select(
00250 'page',
00251 array(
00252 'page_namespace',
00253 'page_title',
00254 'page_counter',
00255 ),
00256 array(
00257 'page_is_redirect' => 0,
00258 'page_counter > 0',
00259 ),
00260 __METHOD__,
00261 array(
00262 'ORDER BY' => 'page_counter DESC',
00263 'LIMIT' => 10,
00264 )
00265 );
00266 if( $res->numRows() > 0 ) {
00267 $text .= Xml::openElement( 'tr' );
00268 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
00269 $text .= Xml::closeElement( 'tr' );
00270 while( $row = $res->fetchObject() ) {
00271 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
00272 if( $title instanceof Title ) {
00273 $text .= $this->formatRow( $sk->link( $title ),
00274 $wgLang->formatNum( $row->page_counter ) );
00275
00276 }
00277 }
00278 $res->free();
00279 }
00280 return $text;
00281 }
00282
00283 private function getOtherStats( $stats ) {
00284 global $wgLang;
00285
00286 if ( !count( $stats ) )
00287 return '';
00288
00289 $return = Xml::openElement( 'tr' ) .
00290 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
00291 Xml::closeElement( 'tr' );
00292
00293 foreach( $stats as $name => $number ) {
00294 $name = htmlspecialchars( $name );
00295 $number = htmlspecialchars( $number );
00296
00297 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
00298 }
00299
00300 return $return;
00301 }
00302
00308 private function doRawOutput() {
00309 global $wgOut;
00310 $wgOut->disable();
00311 header( 'Pragma: nocache' );
00312 echo "total=" . $this->total . ";good=" . $this->good . ";views=" .
00313 $this->views . ";edits=" . $this->edits . ";users=" . $this->users . ";";
00314 echo "activeusers=" . $this->activeUsers . ";admins=" . $this->admins .
00315 ";images=" . $this->images . ";jobs=" . $this->numJobs . "\n";
00316 return;
00317 }
00318 }