3 * Send information about this MediaWiki instance to MediaWiki.org.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use Psr\Log\LoggerInterface
;
24 use MediaWiki\Logger\LoggerFactory
;
25 use MediaWiki\MediaWikiServices
;
28 * Send information about this MediaWiki instance to MediaWiki.org.
35 * @var int Revision ID of the JSON schema that describes the pingback
36 * payload. The schema lives on MetaWiki, at
37 * <https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>.
39 const SCHEMA_REV
= 15781718;
41 /** @var LoggerInterface */
47 /** @var string updatelog key (also used as cache/db lock key) */
50 /** @var string Randomly-generated identifier for this wiki */
54 * @param Config|null $config
55 * @param LoggerInterface|null $logger
57 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
58 $this->config
= $config ?
: RequestContext
::getMain()->getConfig();
59 $this->logger
= $logger ?
: LoggerFactory
::getInstance( __CLASS__
);
60 $this->key
= 'Pingback-' . $this->config
->get( 'Version' );
64 * Should a pingback be sent?
67 private function shouldSend() {
68 return $this->config
->get( 'Pingback' ) && !$this->checkIfSent();
72 * Has a pingback been sent in the last month for this MediaWiki version?
75 private function checkIfSent() {
76 $dbr = wfGetDB( DB_REPLICA
);
77 $timestamp = $dbr->selectField(
80 [ 'ul_key' => $this->key
],
83 if ( $timestamp === false ) {
86 // send heartbeat ping if last ping was over a month ago
87 if ( time() - (int)$timestamp > 60 * 60 * 24 * 30 ) {
94 * Record the fact that we have sent a pingback for this MediaWiki version,
95 * to ensure we don't submit data multiple times.
97 private function markSent() {
98 $dbw = wfGetDB( DB_MASTER
);
102 [ 'ul_key' => $this->key
, 'ul_value' => $timestamp ],
104 [ 'ul_value' => $timestamp ],
110 * Acquire lock for sending a pingback
112 * This ensures only one thread can attempt to send a pingback at any given
113 * time and that we wait an hour before retrying failed attempts.
115 * @return bool Whether lock was acquired
117 private function acquireLock() {
118 $cache = ObjectCache
::getLocalClusterInstance();
119 if ( !$cache->add( $this->key
, 1, 60 * 60 ) ) {
120 return false; // throttled
123 $dbw = wfGetDB( DB_MASTER
);
124 if ( !$dbw->lock( $this->key
, __METHOD__
, 0 ) ) {
125 return false; // already in progress
132 * Collect basic data about this MediaWiki installation and return it
133 * as an associative array conforming to the Pingback schema on MetaWiki
134 * (<https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>).
136 * This is public so we can display it in the installer
138 * Developers: If you're adding a new piece of data to this, please ensure
139 * that you update https://www.mediawiki.org/wiki/Manual:$wgPingback
143 public function getSystemInfo() {
145 'database' => $this->config
->get( 'DBtype' ),
146 'MediaWiki' => $this->config
->get( 'Version' ),
147 'PHP' => PHP_VERSION
,
148 'OS' => PHP_OS
. ' ' . php_uname( 'r' ),
149 'arch' => PHP_INT_SIZE
=== 8 ?
64 : 32,
150 'machine' => php_uname( 'm' ),
153 if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
154 $event['serverSoftware'] = $_SERVER['SERVER_SOFTWARE'];
157 $limit = ini_get( 'memory_limit' );
158 if ( $limit && $limit != -1 ) {
159 $event['memoryLimit'] = $limit;
166 * Get the EventLogging packet to be sent to the server
170 private function getData() {
172 'schema' => 'MediaWikiPingback',
173 'revision' => self
::SCHEMA_REV
,
174 'wiki' => $this->getOrCreatePingbackId(),
175 'event' => $this->getSystemInfo(),
180 * Get a unique, stable identifier for this wiki
182 * If the identifier does not already exist, create it and save it in the
183 * database. The identifier is randomly-generated.
185 * @return string 32-character hex string
187 private function getOrCreatePingbackId() {
189 $id = wfGetDB( DB_REPLICA
)->selectField(
190 'updatelog', 'ul_value', [ 'ul_key' => 'PingBack' ] );
192 if ( $id == false ) {
193 $id = MWCryptRand
::generateHex( 32 );
194 $dbw = wfGetDB( DB_MASTER
);
197 [ 'ul_key' => 'PingBack', 'ul_value' => $id ],
202 if ( !$dbw->affectedRows() ) {
203 $id = $dbw->selectField(
204 'updatelog', 'ul_value', [ 'ul_key' => 'PingBack' ] );
215 * Serialize pingback data and send it to MediaWiki.org via a POST
216 * to its event beacon endpoint.
218 * The data encoding conforms to the expectations of EventLogging,
219 * a software suite used by the Wikimedia Foundation for logging and
220 * processing analytic data.
223 * <https://github.com/wikimedia/mediawiki-extensions-EventLogging/
224 * blob/7e5fe4f1ef/includes/EventLogging.php#L32-L74>
226 * @param array $data Pingback data as an associative array
227 * @return bool true on success, false on failure
229 private function postPingback( array $data ) {
230 $json = FormatJson
::encode( $data );
231 $queryString = rawurlencode( str_replace( ' ', '\u0020', $json ) ) . ';';
232 $url = 'https://www.mediawiki.org/beacon/event?' . $queryString;
233 return MediaWikiServices
::getInstance()->getHttpRequestFactory()->post( $url ) !== null;
237 * Send information about this MediaWiki instance to MediaWiki.org.
239 * The data is structured and serialized to match the expectations of
240 * EventLogging, a software suite used by the Wikimedia Foundation for
241 * logging and processing analytic data.
244 * <https://github.com/wikimedia/mediawiki-extensions-EventLogging/
245 * blob/7e5fe4f1ef/includes/EventLogging.php#L32-L74>
247 * The schema for the data is located at:
248 * <https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>
251 public function sendPingback() {
252 if ( !$this->acquireLock() ) {
253 $this->logger
->debug( __METHOD__
. ": couldn't acquire lock" );
257 $data = $this->getData();
258 if ( !$this->postPingback( $data ) ) {
259 $this->logger
->warning( __METHOD__
. ": failed to send pingback; check 'http' log" );
264 $this->logger
->debug( __METHOD__
. ": pingback sent OK ({$this->key})" );
269 * Schedule a deferred callable that will check if a pingback should be
270 * sent and (if so) proceed to send it.
272 public static function schedulePingback() {
273 DeferredUpdates
::addCallableUpdate( function () {
274 $instance = new Pingback
;
275 if ( $instance->shouldSend() ) {
276 $instance->sendPingback();