MediaWiki  1.33.0
Pingback.php
Go to the documentation of this file.
1 <?php
23 use Psr\Log\LoggerInterface;
25 
31 class Pingback {
32 
38  const SCHEMA_REV = 15781718;
39 
41  protected $logger;
42 
44  protected $config;
45 
47  protected $key;
48 
50  protected $id;
51 
56  public function __construct( Config $config = null, LoggerInterface $logger = null ) {
57  $this->config = $config ?: RequestContext::getMain()->getConfig();
58  $this->logger = $logger ?: LoggerFactory::getInstance( __CLASS__ );
59  $this->key = 'Pingback-' . $this->config->get( 'Version' );
60  }
61 
66  private function shouldSend() {
67  return $this->config->get( 'Pingback' ) && !$this->checkIfSent();
68  }
69 
74  private function checkIfSent() {
75  $dbr = wfGetDB( DB_REPLICA );
76  $timestamp = $dbr->selectField(
77  'updatelog',
78  'ul_value',
79  [ 'ul_key' => $this->key ],
80  __METHOD__
81  );
82  if ( $timestamp === false ) {
83  return false;
84  }
85  // send heartbeat ping if last ping was over a month ago
86  if ( time() - (int)$timestamp > 60 * 60 * 24 * 30 ) {
87  return false;
88  }
89  return true;
90  }
91 
96  private function markSent() {
97  $dbw = wfGetDB( DB_MASTER );
98  $timestamp = time();
99  return $dbw->upsert(
100  'updatelog',
101  [ 'ul_key' => $this->key, 'ul_value' => $timestamp ],
102  [ 'ul_key' ],
103  [ 'ul_value' => $timestamp ],
104  __METHOD__
105  );
106  }
107 
116  private function acquireLock() {
118  if ( !$cache->add( $this->key, 1, 60 * 60 ) ) {
119  return false; // throttled
120  }
121 
122  $dbw = wfGetDB( DB_MASTER );
123  if ( !$dbw->lock( $this->key, __METHOD__, 0 ) ) {
124  return false; // already in progress
125  }
126 
127  return true;
128  }
129 
142  public function getSystemInfo() {
143  $event = [
144  'database' => $this->config->get( 'DBtype' ),
145  'MediaWiki' => $this->config->get( 'Version' ),
146  'PHP' => PHP_VERSION,
147  'OS' => PHP_OS . ' ' . php_uname( 'r' ),
148  'arch' => PHP_INT_SIZE === 8 ? 64 : 32,
149  'machine' => php_uname( 'm' ),
150  ];
151 
152  if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
153  $event['serverSoftware'] = $_SERVER['SERVER_SOFTWARE'];
154  }
155 
156  $limit = ini_get( 'memory_limit' );
157  if ( $limit && $limit != -1 ) {
158  $event['memoryLimit'] = $limit;
159  }
160 
161  return $event;
162  }
163 
169  private function getData() {
170  return [
171  'schema' => 'MediaWikiPingback',
172  'revision' => self::SCHEMA_REV,
173  'wiki' => $this->getOrCreatePingbackId(),
174  'event' => $this->getSystemInfo(),
175  ];
176  }
177 
186  private function getOrCreatePingbackId() {
187  if ( !$this->id ) {
188  $id = wfGetDB( DB_REPLICA )->selectField(
189  'updatelog', 'ul_value', [ 'ul_key' => 'PingBack' ] );
190 
191  if ( $id == false ) {
193  $dbw = wfGetDB( DB_MASTER );
194  $dbw->insert(
195  'updatelog',
196  [ 'ul_key' => 'PingBack', 'ul_value' => $id ],
197  __METHOD__,
198  'IGNORE'
199  );
200 
201  if ( !$dbw->affectedRows() ) {
202  $id = $dbw->selectField(
203  'updatelog', 'ul_value', [ 'ul_key' => 'PingBack' ] );
204  }
205  }
206 
207  $this->id = $id;
208  }
209 
210  return $this->id;
211  }
212 
228  private function postPingback( array $data ) {
229  $json = FormatJson::encode( $data );
230  $queryString = rawurlencode( str_replace( ' ', '\u0020', $json ) ) . ';';
231  $url = 'https://www.mediawiki.org/beacon/event?' . $queryString;
232  return Http::post( $url ) !== false;
233  }
234 
250  public function sendPingback() {
251  if ( !$this->acquireLock() ) {
252  $this->logger->debug( __METHOD__ . ": couldn't acquire lock" );
253  return false;
254  }
255 
256  $data = $this->getData();
257  if ( !$this->postPingback( $data ) ) {
258  $this->logger->warning( __METHOD__ . ": failed to send pingback; check 'http' log" );
259  return false;
260  }
261 
262  $this->markSent();
263  $this->logger->debug( __METHOD__ . ": pingback sent OK ({$this->key})" );
264  return true;
265  }
266 
271  public static function schedulePingback() {
273  $instance = new Pingback;
274  if ( $instance->shouldSend() ) {
275  $instance->sendPingback();
276  }
277  } );
278  }
279 }
Pingback\$logger
LoggerInterface $logger
Definition: Pingback.php:33
ObjectCache\getLocalClusterInstance
static getLocalClusterInstance()
Get the main cluster-local cache object.
Definition: ObjectCache.php:356
Pingback\getSystemInfo
getSystemInfo()
Collect basic data about this MediaWiki installation and return it as an associative array conforming...
Definition: Pingback.php:142
Pingback\$config
Config $config
Definition: Pingback.php:44
Http\post
static post( $url, array $options=[], $caller=__METHOD__)
Simple wrapper for Http::request( 'POST' )
Definition: Http.php:121
Pingback\checkIfSent
checkIfSent()
Has a pingback been sent in the last month for this MediaWiki version?
Definition: Pingback.php:74
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$dbr
$dbr
Definition: testCompression.php:50
Config
Interface for configuration instances.
Definition: Config.php:28
Pingback\sendPingback
sendPingback()
Send information about this MediaWiki instance to MediaWiki.org.
Definition: Pingback.php:250
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
Pingback
Send information about this MediaWiki instance to MediaWiki.org.
Definition: Pingback.php:31
Pingback\$id
string $id
Randomly-generated identifier for this wiki.
Definition: Pingback.php:50
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
Pingback\acquireLock
acquireLock()
Acquire lock for sending a pingback.
Definition: Pingback.php:116
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Pingback\markSent
markSent()
Record the fact that we have sent a pingback for this MediaWiki version, to ensure we don't submit da...
Definition: Pingback.php:96
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Pingback\shouldSend
shouldSend()
Should a pingback be sent?
Definition: Pingback.php:66
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
key
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition: hooks.txt:2154
Pingback\getData
getData()
Get the EventLogging packet to be sent to the server.
Definition: Pingback.php:169
Pingback\__construct
__construct(Config $config=null, LoggerInterface $logger=null)
Definition: Pingback.php:56
MWCryptRand\generateHex
static generateHex( $chars)
Generate a run of cryptographically random data and return it in hexadecimal string format.
Definition: MWCryptRand.php:74
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:430
Pingback\$key
string $key
updatelog key (also used as cache/db lock key)
Definition: Pingback.php:47
$cache
$cache
Definition: mcc.php:33
Pingback\postPingback
postPingback(array $data)
Serialize pingback data and send it to MediaWiki.org via a POST to its event beacon endpoint.
Definition: Pingback.php:228
LoggerFactory
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
Pingback\schedulePingback
static schedulePingback()
Schedule a deferred callable that will check if a pingback should be sent and (if so) proceed to send...
Definition: Pingback.php:271
Pingback\getOrCreatePingbackId
getOrCreatePingbackId()
Get a unique, stable identifier for this wiki.
Definition: Pingback.php:186
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:118