Add option for sharing info about this MediaWiki install via pingback
authorOri Livneh <ori@wikimedia.org>
Thu, 30 Jun 2016 09:29:10 +0000 (02:29 -0700)
committerReedy <reedy@wikimedia.org>
Thu, 21 Jul 2016 21:00:28 +0000 (21:00 +0000)
When $wgPingback is true, MediaWiki will periodically ping
https://www.mediawiki.org/beacon with basic information about the local
MediaWiki installation.  This data includes, for example, the type of system,
PHP version, and chosen database backend.

The pingback is sent via a deferred (post-send) update whenever $wgVersion
changes, using the updatelog table to ensure we don't send duplicate pingbacks.
A database lock ensures only one thread attempts to send the pingback, and a
cache key throttles attempts to no more than once per hour.

$wgPingback is false by default. The web installer has a checkbox for
controlling this option, and it is checked by default. This nudges new installs
to turn on pingbacks, but does not sneak this decision past sysops of existing
installs.

Change-Id: Ie43a6b46a07ebd9ccc1b9c3001f2ea02435d826b

RELEASE-NOTES-1.28
autoload.php
includes/DefaultSettings.php
includes/Pingback.php [new file with mode: 0644]
includes/Setup.php
includes/installer/Installer.php
includes/installer/LocalSettingsGenerator.php
includes/installer/WebInstallerName.php
includes/installer/i18n/en.json
includes/installer/i18n/qqq.json

index 429c5fe..a20bec0 100644 (file)
@@ -22,6 +22,10 @@ production.
 * The deprecated $wgEditEncoding variable has been removed; it was only used
   for Esperanto language character conversion. You are now recommended to use
   input methods provided by the UniversalLanguageSelector extension.
+* When $wgPingback is true, MediaWiki will periodically ping
+  https://www.mediawiki.org/beacon with basic information about the local
+  MediaWiki installation.  This data includes, for example, the type of system,
+  PHP version, and chosen database backend. This behavior is off by default.
 
 === New features in 1.28 ===
 * User::isBot() method for checking if an account is a bot role account.
index 76a329f..5da06d8 100644 (file)
@@ -1039,6 +1039,7 @@ $wgAutoloadLocalClasses = [
        'PermissionsError' => __DIR__ . '/includes/exception/PermissionsError.php',
        'PhpHttpRequest' => __DIR__ . '/includes/HttpFunctions.php',
        'PhpXmlBugTester' => __DIR__ . '/includes/installer/PhpBugTests.php',
+       'Pingback' => __DIR__ . '/includes/Pingback.php',
        'PoolCounter' => __DIR__ . '/includes/poolcounter/PoolCounter.php',
        'PoolCounterRedis' => __DIR__ . '/includes/poolcounter/PoolCounterRedis.php',
        'PoolCounterWork' => __DIR__ . '/includes/poolcounter/PoolCounterWork.php',
index 16c335c..1e60302 100644 (file)
@@ -8340,6 +8340,21 @@ $wgEventRelayerConfig = [
        ]
 ];
 
+/**
+ * Share data about this installation with MediaWiki developers
+ *
+ * When set to true, MediaWiki will periodically ping https://www.mediawiki.org/ with basic
+ * data about this MediaWiki instance. This data includes, for example, the type of system,
+ * PHP version, and chosen database backend. The Wikimedia Foundation shares this data with
+ * MediaWiki developers to help guide future development efforts.
+ *
+ * For details about what data is sent, see: https://www.mediawiki.org/wiki/Pingback
+ *
+ * @var bool
+ * @since 1.28
+ */
+$wgPingback = false;
+
 /**
  * For really cool vim folding this needs to be at the end:
  * vim: foldmarker=@{,@} foldmethod=marker
diff --git a/includes/Pingback.php b/includes/Pingback.php
new file mode 100644 (file)
index 0000000..f633029
--- /dev/null
@@ -0,0 +1,247 @@
+<?php
+/**
+ * Send information about this MediaWiki instance to MediaWiki.org.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+use Psr\Log\LoggerInterface;
+use MediaWiki\Logger\LoggerFactory;
+
+/**
+ * Send information about this MediaWiki instance to MediaWiki.org.
+ *
+ * @since 1.28
+ */
+class Pingback {
+
+       /**
+        * @var int Revision ID of the JSON schema that describes the pingback
+        *   payload. The schema lives on MetaWiki, at
+        *   <https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>.
+        */
+       const SCHEMA_REV = 15781718;
+
+       /** @var LoggerInterface */
+       protected $logger;
+
+       /** @var Config */
+       protected $config;
+
+       /** @var string updatelog key (also used as cache/db lock key) */
+       protected $key;
+
+       /** @var string Randomly-generated identifier for this wiki */
+       protected $id;
+
+       /**
+        * @param Config $config
+        * @param LoggerInterface $logger
+        */
+       public function __construct( Config $config = null, LoggerInterface $logger = null ) {
+               $this->config = $config ?: RequestContext::getMain()->getConfig();
+               $this->logger = $logger ?: LoggerFactory::getInstance( __CLASS__ );
+               $this->key = 'Pingback-' . $this->config->get( 'Version' );
+       }
+
+       /**
+        * Should a pingback be sent?
+        * @return bool
+        */
+       private function shouldSend() {
+               return $this->config->get( 'Pingback' ) && !$this->checkIfSent();
+       }
+
+       /**
+        * Has a pingback already been sent for this MediaWiki version?
+        * @return bool
+        */
+       private function checkIfSent() {
+               $dbr = wfGetDB( DB_SLAVE );
+               $sent = $dbr->selectField(
+                       'updatelog', '1', [ 'ul_key' => $this->key ], __METHOD__ );
+               return $sent !== false;
+       }
+
+       /**
+        * Record the fact that we have sent a pingback for this MediaWiki version,
+        * to ensure we don't submit data multiple times.
+        */
+       private function markSent() {
+               $dbw = wfGetDB( DB_MASTER );
+               return $dbw->insert(
+                       'updatelog', [ 'ul_key' => $this->key ], __METHOD__, 'IGNORE' );
+       }
+
+       /**
+        * Acquire lock for sending a pingback
+        *
+        * This ensures only one thread can attempt to send a pingback at any given
+        * time and that we wait an hour before retrying failed attempts.
+        *
+        * @return bool Whether lock was acquired
+        */
+       private function acquireLock() {
+               $cache = ObjectCache::getLocalClusterInstance();
+               if ( !$cache->add( $this->key, 1, 60 * 60 ) ) {
+                       return false;  // throttled
+               }
+
+               $dbw = wfGetDB( DB_MASTER );
+               if ( !$dbw->lock( $this->key, __METHOD__, 0 ) ) {
+                       return false;  // already in progress
+               }
+
+               return true;
+       }
+
+       /**
+        * Collect basic data about this MediaWiki installation and return it
+        * as an associative array conforming to the Pingback schema on MetaWiki
+        * (<https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>).
+        *
+        * @return array
+        */
+       private function getData() {
+               $event = [
+                       'database'   => $this->config->get( 'DBtype' ),
+                       'MediaWiki'  => $this->config->get( 'Version' ),
+                       'PHP'        => PHP_VERSION,
+                       'OS'         => PHP_OS . ' ' . php_uname( 'r' ),
+                       'arch'       => PHP_INT_SIZE === 8 ? 64 : 32,
+                       'machine'    => php_uname( 'm' ),
+               ];
+
+               if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
+                       $event['serverSoftware'] = $_SERVER['SERVER_SOFTWARE'];
+               }
+
+               $limit = ini_get( 'memory_limit' );
+               if ( $limit && $limit != -1 ) {
+                       $event['memoryLimit'] = $limit;
+               }
+
+               return [
+                       'schema'           => 'MediaWikiPingback',
+                       'revision'         => self::SCHEMA_REV,
+                       'wiki'             => $this->getOrCreatePingbackId(),
+                       'event'            => $event,
+               ];
+       }
+
+       /**
+        * Get a unique, stable identifier for this wiki
+        *
+        * If the identifier does not already exist, create it and save it in the
+        * database. The identifier is randomly-generated.
+        *
+        * @return string 32-character hex string
+        */
+       private function getOrCreatePingbackId() {
+               if ( !$this->id ) {
+                       $id = wfGetDB( DB_SLAVE )->selectField(
+                               'updatelog', 'ul_value', [ 'ul_key' => 'PingBack' ] );
+
+                       if ( $id == false ) {
+                               $id = MWCryptRand::generateHex( 32 );
+                               $dbw = wfGetDB( DB_MASTER );
+                               $dbw->insert(
+                                       'updatelog',
+                                       [ 'ul_key' => 'PingBack', 'ul_value' => $id ],
+                                       __METHOD__,
+                                       'IGNORE'
+                               );
+
+                               if ( !$dbw->affectedRows() ) {
+                                       $id = $dbw->selectField(
+                                               'updatelog', 'ul_value', [ 'ul_key' => 'PingBack' ] );
+                               }
+                       }
+
+                       $this->id = $id;
+               }
+
+               return $this->id;
+       }
+
+       /**
+        * Serialize pingback data and send it to MediaWiki.org via a POST
+        * to its event beacon endpoint.
+        *
+        * The data encoding conforms to the expectations of EventLogging,
+        * a software suite used by the Wikimedia Foundation for logging and
+        * processing analytic data.
+        *
+        * Compare:
+        * <https://github.com/wikimedia/mediawiki-extensions-EventLogging/
+        *   blob/7e5fe4f1ef/includes/EventLogging.php#L32-L74>
+        *
+        * @param data Pingback data as an associative array
+        * @return bool true on success, false on failure
+        */
+       private function postPingback( array $data ) {
+               $json = FormatJson::encode( $data );
+               $queryString = rawurlencode( str_replace( ' ', '\u0020', $json ) ) . ';';
+               $url = 'https://www.mediawiki.org/beacon/event?' . $queryString;
+               return Http::post( $url ) !== false;
+       }
+
+       /**
+        * Send information about this MediaWiki instance to MediaWiki.org.
+        *
+        * The data is structured and serialized to match the expectations of
+        * EventLogging, a software suite used by the Wikimedia Foundation for
+        * logging and processing analytic data.
+        *
+        * Compare:
+        * <https://github.com/wikimedia/mediawiki-extensions-EventLogging/
+        *   blob/7e5fe4f1ef/includes/EventLogging.php#L32-L74>
+        *
+        * The schema for the data is located at:
+        * <https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>
+        */
+       public function sendPingback() {
+               if ( !$this->acquireLock() ) {
+                       $this->logger->debug( __METHOD__ . ": couldn't acquire lock" );
+                       return false;
+               }
+
+               $data = $this->getData();
+               if ( !$this->postPingback( $data ) ) {
+                       $this->logger->warning( __METHOD__ . ": failed to send pingback; check 'http' log" );
+                       return false;
+               }
+
+               $this->markSent();
+               $this->logger->debug( __METHOD__ . ": pingback sent OK ({$this->key})" );
+               return true;
+       }
+
+       /**
+        * Schedule a deferred callable that will check if a pingback should be
+        * sent and (if so) proceed to send it.
+        */
+       public static function schedulePingback() {
+               DeferredUpdates::addCallableUpdate( function () {
+                       $instance = new Pingback;
+                       if ( $instance->shouldSend() ) {
+                               $instance->sendPingback();
+                       }
+               } );
+       }
+}
index da224a0..6c5de90 100644 (file)
@@ -871,6 +871,10 @@ if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) {
        unset( $sessionUser );
 }
 
+if ( !$wgCommandLineMode ) {
+       Pingback::schedulePingback();
+}
+
 wfDebug( "Fully initialised\n" );
 $wgFullyInitialised = true;
 
index 4d5aa7a..5e3758d 100644 (file)
@@ -180,6 +180,7 @@ abstract class Installer {
                'wgUseInstantCommons',
                'wgUpgradeKey',
                'wgDefaultSkin',
+               'wgPingback',
        ];
 
        /**
index ced7b93..1d7c7f2 100644 (file)
@@ -64,7 +64,7 @@ class LocalSettingsGenerator {
                                'wgRightsText', '_MainCacheType', 'wgEnableUploads',
                                '_MemCachedServers', 'wgDBserver', 'wgDBuser',
                                'wgDBpassword', 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin',
-                               'wgMetaNamespace', 'wgLogo', 'wgAuthenticationTokenVersion',
+                               'wgMetaNamespace', 'wgLogo', 'wgAuthenticationTokenVersion', 'wgPingback',
                        ],
                        $db->getGlobalNames()
                );
@@ -72,7 +72,8 @@ class LocalSettingsGenerator {
                $unescaped = [ 'wgRightsIcon', 'wgLogo' ];
                $boolItems = [
                        'wgEnableEmail', 'wgEnableUserEmail', 'wgEnotifUserTalk',
-                       'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons'
+                       'wgEnotifWatchlist', 'wgEmailAuthentication', 'wgEnableUploads', 'wgUseInstantCommons',
+                       'wgPingback',
                ];
 
                foreach ( $confItems as $c ) {
@@ -372,6 +373,11 @@ ${serverSetting}
 # InstantCommons allows wiki to use images from https://commons.wikimedia.org
 \$wgUseInstantCommons = {$this->values['wgUseInstantCommons']};
 
+# Periodically send a pingback to https://www.mediawiki.org/ with basic data
+# about this MediaWiki instance. The Wikimedia Foundation shares this data
+# with MediaWiki developers to help guide future development efforts.
+\$wgPingback = {$this->values['wgPingback']};
+
 ## If you use ImageMagick (or any other shell command) on a
 ## Linux server, this will need to be set to the name of an
 ## available UTF-8 locale
index dcd30cf..2345d89 100644 (file)
@@ -100,6 +100,12 @@ class WebInstallerName extends WebInstallerPage {
                                'label' => 'config-subscribe',
                                'help' => $this->parent->getHelpBox( 'config-subscribe-help' )
                        ] ) .
+                       $this->parent->getCheckBox( [
+                               'var' => 'wgPingback',
+                               'label' => 'config-pingback',
+                               'help' => $this->parent->getHelpBox( 'config-pingback-help' ),
+                               'value' => true,
+                       ] ) .
                        $this->getFieldsetEnd() .
                        $this->parent->getInfoBox( wfMessage( 'config-almost-done' )->text() ) .
                        // getRadioSet() builds a set of labeled radio buttons.
@@ -129,7 +135,7 @@ class WebInstallerName extends WebInstallerPage {
                $retVal = true;
                $this->parent->setVarsFromRequest( [ 'wgSitename', '_NamespaceType',
                        '_AdminName', '_AdminPassword', '_AdminPasswordConfirm', '_AdminEmail',
-                       '_Subscribe', '_SkipOptional', 'wgMetaNamespace' ] );
+                       '_Subscribe', '_SkipOptional', 'wgMetaNamespace', 'wgPingback' ] );
 
                // Validate site name
                if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) {
index 79383f3..dbe4266 100644 (file)
        "config-subscribe": "Subscribe to the [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce release announcements mailing list].",
        "config-subscribe-help": "This is a low-volume mailing list used for release announcements, including important security announcements.\nYou should subscribe to it and update your MediaWiki installation when new versions come out.",
        "config-subscribe-noemail": "You tried to subscribe to the release announcements mailing list without providing an email address.\nPlease provide an email address if you wish to subscribe to the mailing list.",
+       "config-pingback": "Share data about this installation with MediaWiki developers.",
+       "config-pingback-help": "If you select this option, MediaWiki will periodically ping https://www.mediawiki.org with basic data about this MediaWiki instance. This data includes, for example, the type of system, PHP version, and chosen database backend. The Wikimedia Foundation shares this data with MediaWiki developers to help guide future development efforts.",
        "config-almost-done": "You are almost done!\nYou can now skip the remaining configuration and install the wiki right now.",
        "config-optional-continue": "Ask me more questions.",
        "config-optional-skip": "I'm bored already, just install the wiki.",
index 69a6830..6a1dd08 100644 (file)
        "config-subscribe": "Used as label for the installer checkbox",
        "config-subscribe-help": "\"Low-volume\" in this context means that there will be few e-mails to that mailing list per time period.",
        "config-subscribe-noemail": "Error text in MediaWiki installer.",
+       "config-pingback": "Option in the MediaWiki installer to submit data about this installation to MediaWiki.org.",
+       "config-pingback-help": "Explains what data will be shared if the user chooses to submit data to MediaWiki.org.",
        "config-almost-done": "Status message in the MediaWiki installer.",
        "config-optional-continue": "Option in the MediaWiki installer to make a more fine-tuned installation.",
        "config-optional-skip": "Option in the MediaWiki installer to start executing the actual installation and stop asking questions.",