MediaWiki  REL1_23
SpecialStatistics.php
Go to the documentation of this file.
00001 <?php
00030 class SpecialStatistics extends SpecialPage {
00031     private $views, $edits, $good, $images, $total, $users,
00032         $activeUsers = 0;
00033 
00034     public function __construct() {
00035         parent::__construct( 'Statistics' );
00036     }
00037 
00038     public function execute( $par ) {
00039         global $wgMemc, $wgDisableCounters, $wgMiserMode;
00040 
00041         $this->setHeaders();
00042         $this->getOutput()->addModuleStyles( 'mediawiki.special' );
00043 
00044         $this->views = SiteStats::views();
00045         $this->edits = SiteStats::edits();
00046         $this->good = SiteStats::articles();
00047         $this->images = SiteStats::images();
00048         $this->total = SiteStats::pages();
00049         $this->users = SiteStats::users();
00050         $this->activeUsers = SiteStats::activeUsers();
00051         $this->hook = '';
00052 
00053         # Staticic - views
00054         $viewsStats = '';
00055         if ( !$wgDisableCounters ) {
00056             $viewsStats = $this->getViewsStats();
00057         }
00058 
00059         # Set active user count
00060         if ( !$wgMiserMode ) {
00061             $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
00062             // Re-calculate the count if the last tally is old...
00063             if ( !$wgMemc->get( $key ) ) {
00064                 $dbw = wfGetDB( DB_MASTER );
00065                 SiteStatsUpdate::cacheUpdate( $dbw );
00066                 $wgMemc->set( $key, '1', 24 * 3600 ); // don't update for 1 day
00067             }
00068         }
00069 
00070         $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
00071 
00072         # Statistic - pages
00073         $text .= $this->getPageStats();
00074 
00075         # Statistic - edits
00076         $text .= $this->getEditStats();
00077 
00078         # Statistic - users
00079         $text .= $this->getUserStats();
00080 
00081         # Statistic - usergroups
00082         $text .= $this->getGroupStats();
00083         $text .= $viewsStats;
00084 
00085         # Statistic - popular pages
00086         if ( !$wgDisableCounters && !$wgMiserMode ) {
00087             $text .= $this->getMostViewedPages();
00088         }
00089 
00090         # Statistic - other
00091         $extraStats = array();
00092         if ( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
00093             $text .= $this->getOtherStats( $extraStats );
00094         }
00095 
00096         $text .= Xml::closeElement( 'table' );
00097 
00098         # Customizable footer
00099         $footer = $this->msg( 'statistics-footer' );
00100         if ( !$footer->isBlank() ) {
00101             $text .= "\n" . $footer->parse();
00102         }
00103 
00104         $this->getOutput()->addHTML( $text );
00105     }
00106 
00116     private function formatRow( $text, $number, $trExtraParams = array(),
00117         $descMsg = '', $descMsgParam = ''
00118     ) {
00119         if ( $descMsg ) {
00120             $msg = $this->msg( $descMsg, $descMsgParam );
00121             if ( $msg->exists() ) {
00122                 $descriptionText = $this->msg( 'parentheses' )->rawParams( $msg->parse() )->escaped();
00123                 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc' ),
00124                     " $descriptionText" );
00125             }
00126         }
00127 
00128         return Html::rawElement( 'tr', $trExtraParams,
00129             Html::rawElement( 'td', array(), $text ) .
00130             Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
00131         );
00132     }
00133 
00139     private function getPageStats() {
00140         return Xml::openElement( 'tr' ) .
00141             Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )->parse() ) .
00142             Xml::closeElement( 'tr' ) .
00143                 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Allpages' ),
00144                     $this->msg( 'statistics-articles' )->parse() ),
00145                     $this->getLanguage()->formatNum( $this->good ),
00146                     array( 'class' => 'mw-statistics-articles' ) ) .
00147                 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
00148                     $this->getLanguage()->formatNum( $this->total ),
00149                     array( 'class' => 'mw-statistics-pages' ),
00150                     'statistics-pages-desc' ) .
00151                 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Listfiles' ),
00152                     $this->msg( 'statistics-files' )->parse() ),
00153                     $this->getLanguage()->formatNum( $this->images ),
00154                     array( 'class' => 'mw-statistics-files' ) );
00155     }
00156 
00157     private function getEditStats() {
00158         return Xml::openElement( 'tr' ) .
00159             Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
00160             Xml::closeElement( 'tr' ) .
00161             $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
00162                 $this->getLanguage()->formatNum( $this->edits ),
00163                 array( 'class' => 'mw-statistics-edits' )
00164             ) .
00165             $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
00166                 $this->getLanguage()
00167                     ->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
00168                 array( 'class' => 'mw-statistics-edits-average' )
00169             );
00170     }
00171 
00172     private function getUserStats() {
00173         global $wgActiveUserDays;
00174 
00175         return Xml::openElement( 'tr' ) .
00176             Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-users' )->parse() ) .
00177             Xml::closeElement( 'tr' ) .
00178             $this->formatRow( $this->msg( 'statistics-users' )->parse(),
00179                 $this->getLanguage()->formatNum( $this->users ),
00180                 array( 'class' => 'mw-statistics-users' )
00181             ) .
00182             $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
00183                 Linker::linkKnown(
00184                     SpecialPage::getTitleFor( 'Activeusers' ),
00185                     $this->msg( 'listgrouprights-members' )->escaped()
00186                 ),
00187                 $this->getLanguage()->formatNum( $this->activeUsers ),
00188                 array( 'class' => 'mw-statistics-users-active' ),
00189                 'statistics-users-active-desc',
00190                 $this->getLanguage()->formatNum( $wgActiveUserDays )
00191             );
00192     }
00193 
00194     private function getGroupStats() {
00195         global $wgGroupPermissions, $wgImplicitGroups;
00196         $text = '';
00197         foreach ( $wgGroupPermissions as $group => $permissions ) {
00198             # Skip generic * and implicit groups
00199             if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
00200                 continue;
00201             }
00202             $groupname = htmlspecialchars( $group );
00203             $msg = $this->msg( 'group-' . $groupname );
00204             if ( $msg->isBlank() ) {
00205                 $groupnameLocalized = $groupname;
00206             } else {
00207                 $groupnameLocalized = $msg->text();
00208             }
00209             $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
00210             if ( $msg->isBlank() ) {
00211                 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
00212             } else {
00213                 $grouppageLocalized = $msg->text();
00214             }
00215             $linkTarget = Title::newFromText( $grouppageLocalized );
00216             $grouppage = Linker::link(
00217                 $linkTarget,
00218                 htmlspecialchars( $groupnameLocalized )
00219             );
00220             $grouplink = Linker::linkKnown(
00221                 SpecialPage::getTitleFor( 'Listusers' ),
00222                 $this->msg( 'listgrouprights-members' )->escaped(),
00223                 array(),
00224                 array( 'group' => $group )
00225             );
00226             # Add a class when a usergroup contains no members to allow hiding these rows
00227             $classZero = '';
00228             $countUsers = SiteStats::numberingroup( $groupname );
00229             if ( $countUsers == 0 ) {
00230                 $classZero = ' statistics-group-zero';
00231             }
00232             $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
00233                 $this->getLanguage()->formatNum( $countUsers ),
00234                 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
00235         }
00236 
00237         return $text;
00238     }
00239 
00240     private function getViewsStats() {
00241         return Xml::openElement( 'tr' ) .
00242             Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-views' )->parse() ) .
00243             Xml::closeElement( 'tr' ) .
00244                 $this->formatRow( $this->msg( 'statistics-views-total' )->parse(),
00245                     $this->getLanguage()->formatNum( $this->views ),
00246                         array( 'class' => 'mw-statistics-views-total' ), 'statistics-views-total-desc' ) .
00247                 $this->formatRow( $this->msg( 'statistics-views-peredit' )->parse(),
00248                     $this->getLanguage()->formatNum( sprintf( '%.2f', $this->edits ?
00249                         $this->views / $this->edits : 0 ) ),
00250                         array( 'class' => 'mw-statistics-views-peredit' ) );
00251     }
00252 
00253     private function getMostViewedPages() {
00254         $text = '';
00255         $dbr = wfGetDB( DB_SLAVE );
00256         $res = $dbr->select(
00257             'page',
00258             array(
00259                 'page_namespace',
00260                 'page_title',
00261                 'page_counter',
00262             ),
00263             array(
00264                 'page_is_redirect' => 0,
00265                 'page_counter > 0',
00266             ),
00267             __METHOD__,
00268             array(
00269                 'ORDER BY' => 'page_counter DESC',
00270                 'LIMIT' => 10,
00271             )
00272         );
00273 
00274         if ( $res->numRows() > 0 ) {
00275             $text .= Xml::openElement( 'tr' );
00276             $text .= Xml::tags(
00277                 'th',
00278                 array( 'colspan' => '2' ),
00279                 $this->msg( 'statistics-mostpopular' )->parse()
00280             );
00281             $text .= Xml::closeElement( 'tr' );
00282 
00283             foreach ( $res as $row ) {
00284                 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
00285 
00286                 if ( $title instanceof Title ) {
00287                     $text .= $this->formatRow( Linker::link( $title ),
00288                         $this->getLanguage()->formatNum( $row->page_counter ) );
00289                 }
00290             }
00291             $res->free();
00292         }
00293 
00294         return $text;
00295     }
00296 
00304     private function getOtherStats( array $stats ) {
00305         $return = '';
00306 
00307         foreach ( $stats as $header => $items ) {
00308             // Identify the structure used
00309             if ( is_array( $items ) ) {
00310 
00311                 // Ignore headers that are recursively set as legacy header
00312                 if ( $header !== 'statistics-header-hooks' ) {
00313                     $return .= $this->formatRowHeader( $header );
00314                 }
00315 
00316                 // Collect all items that belong to the same header
00317                 foreach ( $items as $key => $value ) {
00318                     $name = $this->msg( $key )->parse();
00319                     $number = htmlspecialchars( $value );
00320 
00321                     $return .= $this->formatRow(
00322                         $name,
00323                         $this->getLanguage()->formatNum( $number ),
00324                         array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
00325                     );
00326                 }
00327             } else {
00328                 // Create the legacy header only once
00329                 if ( $return === '' ) {
00330                     $return .= $this->formatRowHeader( 'statistics-header-hooks' );
00331                 }
00332 
00333                 // Recursively remap the legacy structure
00334                 $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
00335                     array( $header => $items ) ) );
00336             }
00337         }
00338 
00339         return $return;
00340     }
00341 
00348     private function formatRowHeader( $header ) {
00349         return Xml::openElement( 'tr' ) .
00350             Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
00351             Xml::closeElement( 'tr' );
00352     }
00353 
00354     protected function getGroupName() {
00355         return 'wiki';
00356     }
00357 }