MediaWiki  1.23.15
StatCounter.php
Go to the documentation of this file.
1 <?php
38 class StatCounter {
40  protected $deltas = array(); // (key => count)
41 
42  protected function __construct() {}
43 
47  public static function singleton() {
48  static $instance = null;
49  if ( !$instance ) {
50  $instance = new self();
51  }
52  return $instance;
53  }
54 
62  public function incr( $key, $count = 1 ) {
63  $this->deltas[$key] = isset( $this->deltas[$key] ) ? $this->deltas[$key] : 0;
64  $this->deltas[$key] += $count;
65  if ( PHP_SAPI === 'cli' ) {
66  $this->flush();
67  }
68  }
69 
75  public function flush() {
76  global $wgStatsMethod;
77 
78  $deltas = array_filter( $this->deltas ); // remove 0 valued entries
79  if ( $wgStatsMethod === 'udp' ) {
80  $this->sendDeltasUDP( $deltas );
81  } elseif ( $wgStatsMethod === 'cache' ) {
82  $this->sendDeltasMemc( $deltas );
83  } else {
84  // disabled
85  }
86  $this->deltas = array();
87  }
88 
93  protected function sendDeltasUDP( array $deltas ) {
94  global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgAggregateStatsID,
95  $wgStatsFormatString;
96 
97  $id = strlen( $wgAggregateStatsID ) ? $wgAggregateStatsID : wfWikiID();
98 
99  $lines = array();
100  foreach ( $deltas as $key => $count ) {
101  $lines[] = sprintf( $wgStatsFormatString, $id, $count, $key );
102  }
103 
104  if ( count( $lines ) ) {
105  static $socket = null;
106  if ( !$socket ) {
107  $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
108  }
109  $packet = '';
110  $packets = array();
111  foreach ( $lines as $line ) {
112  if ( ( strlen( $packet ) + strlen( $line ) ) > 1450 ) {
113  $packets[] = $packet;
114  $packet = '';
115  }
116  $packet .= $line;
117  }
118  if ( $packet != '' ) {
119  $packets[] = $packet;
120  }
121  foreach ( $packets as $packet ) {
123  socket_sendto(
124  $socket,
125  $packet,
126  strlen( $packet ),
127  0,
128  $wgUDPProfilerHost,
129  $wgUDPProfilerPort
130  );
132  }
133  }
134  }
135 
140  protected function sendDeltasMemc( array $deltas ) {
141  global $wgMemc;
142 
143  foreach ( $deltas as $key => $count ) {
144  $ckey = wfMemcKey( 'stats', $key );
145  if ( $wgMemc->incr( $ckey, $count ) === null ) {
146  $wgMemc->add( $ckey, $count );
147  }
148  }
149  }
150 }
StatCounter\sendDeltasMemc
sendDeltasMemc(array $deltas)
Definition: StatCounter.php:139
StatCounter\singleton
static singleton()
Definition: StatCounter.php:46
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2434
StatCounter\incr
incr( $key, $count=1)
Increment a key by delta $count.
Definition: StatCounter.php:61
StatCounter\flush
flush()
Flush all pending deltas to persistent storage.
Definition: StatCounter.php:74
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3635
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2464
$lines
$lines
Definition: router.php:65
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
StatCounter
Aggregator for wfIncrStats() that batches updates per request.
Definition: StatCounter.php:38
$line
$line
Definition: cdb.php:57
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3668
$count
$count
Definition: UtfNormalTest2.php:96
StatCounter\sendDeltasUDP
sendDeltasUDP(array $deltas)
Definition: StatCounter.php:92
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
StatCounter\$deltas
Array $deltas
Definition: StatCounter.php:39
StatCounter\__construct
__construct()
Definition: StatCounter.php:41