MediaWiki  REL1_24
StatCounter.php
Go to the documentation of this file.
00001 <?php
00038 class StatCounter {
00040     protected $deltas = array(); // (key => count)
00041 
00043     protected $config;
00044 
00045     protected function __construct( Config $config ) {
00046         $this->config = $config;
00047     }
00048 
00052     public static function singleton() {
00053         static $instance = null;
00054         if ( !$instance ) {
00055             $instance = new self(
00056                 ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
00057             );
00058         }
00059         return $instance;
00060     }
00061 
00069     public function incr( $key, $count = 1 ) {
00070         $this->deltas[$key] = isset( $this->deltas[$key] ) ? $this->deltas[$key] : 0;
00071         $this->deltas[$key] += $count;
00072         if ( PHP_SAPI === 'cli' ) {
00073             $this->flush();
00074         }
00075     }
00076 
00082     public function flush() {
00083         $statsMethod = $this->config->get( 'StatsMethod' );
00084         $deltas = array_filter( $this->deltas ); // remove 0 valued entries
00085         if ( $statsMethod === 'udp' ) {
00086             $this->sendDeltasUDP( $deltas );
00087         } elseif ( $statsMethod === 'cache' ) {
00088             $this->sendDeltasMemc( $deltas );
00089         } else {
00090             // disabled
00091         }
00092         $this->deltas = array();
00093     }
00094 
00099     protected function sendDeltasUDP( array $deltas ) {
00100         $aggregateStatsID = $this->config->get( 'AggregateStatsID' );
00101         $id = strlen( $aggregateStatsID ) ? $aggregateStatsID : wfWikiID();
00102 
00103         $lines = array();
00104         foreach ( $deltas as $key => $count ) {
00105             $lines[] = sprintf( $this->config->get( 'StatsFormatString' ), $id, $count, $key );
00106         }
00107 
00108         if ( count( $lines ) ) {
00109             static $socket = null;
00110             if ( !$socket ) {
00111                 $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
00112             }
00113             $packet = '';
00114             $packets = array();
00115             foreach ( $lines as $line ) {
00116                 if ( ( strlen( $packet ) + strlen( $line ) ) > 1450 ) {
00117                     $packets[] = $packet;
00118                     $packet = '';
00119                 }
00120                 $packet .= $line;
00121             }
00122             if ( $packet != '' ) {
00123                 $packets[] = $packet;
00124             }
00125             foreach ( $packets as $packet ) {
00126                 wfSuppressWarnings();
00127                 socket_sendto(
00128                     $socket,
00129                     $packet,
00130                     strlen( $packet ),
00131                     0,
00132                     $this->config->get( 'UDPProfilerHost' ),
00133                     $this->config->get( 'UDPProfilerPort' )
00134                 );
00135                 wfRestoreWarnings();
00136             }
00137         }
00138     }
00139 
00144     protected function sendDeltasMemc( array $deltas ) {
00145         global $wgMemc;
00146 
00147         foreach ( $deltas as $key => $count ) {
00148             $ckey = wfMemcKey( 'stats', $key );
00149             if ( $wgMemc->incr( $ckey, $count ) === null ) {
00150                 $wgMemc->add( $ckey, $count );
00151             }
00152         }
00153     }
00154 }