From f181cdec56fb2bd6b85f39ed31ac22609bd83777 Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Thu, 30 Jun 2016 02:29:10 -0700 Subject: [PATCH] Add option for sharing info about this MediaWiki install via pingback 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 | 4 + autoload.php | 1 + includes/DefaultSettings.php | 15 ++ includes/Pingback.php | 247 ++++++++++++++++++ includes/Setup.php | 4 + includes/installer/Installer.php | 1 + includes/installer/LocalSettingsGenerator.php | 10 +- includes/installer/WebInstallerName.php | 8 +- includes/installer/i18n/en.json | 2 + includes/installer/i18n/qqq.json | 2 + 10 files changed, 291 insertions(+), 3 deletions(-) create mode 100644 includes/Pingback.php diff --git a/RELEASE-NOTES-1.28 b/RELEASE-NOTES-1.28 index 429c5feb60..a20bec0299 100644 --- a/RELEASE-NOTES-1.28 +++ b/RELEASE-NOTES-1.28 @@ -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. diff --git a/autoload.php b/autoload.php index 76a329f817..5da06d83fb 100644 --- a/autoload.php +++ b/autoload.php @@ -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', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 16c335ce05..1e6030226c 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 index 0000000000..f633029134 --- /dev/null +++ b/includes/Pingback.php @@ -0,0 +1,247 @@ +. + */ + 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 + * (). + * + * @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: + * + * + * @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: + * + * + * The schema for the data is located at: + * + */ + 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(); + } + } ); + } +} diff --git a/includes/Setup.php b/includes/Setup.php index da224a0b54..6c5de90939 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -871,6 +871,10 @@ if ( !defined( 'MW_NO_SESSION' ) && !$wgCommandLineMode ) { unset( $sessionUser ); } +if ( !$wgCommandLineMode ) { + Pingback::schedulePingback(); +} + wfDebug( "Fully initialised\n" ); $wgFullyInitialised = true; diff --git a/includes/installer/Installer.php b/includes/installer/Installer.php index 4d5aa7ad22..5e3758daf0 100644 --- a/includes/installer/Installer.php +++ b/includes/installer/Installer.php @@ -180,6 +180,7 @@ abstract class Installer { 'wgUseInstantCommons', 'wgUpgradeKey', 'wgDefaultSkin', + 'wgPingback', ]; /** diff --git a/includes/installer/LocalSettingsGenerator.php b/includes/installer/LocalSettingsGenerator.php index ced7b93e85..1d7c7f22fc 100644 --- a/includes/installer/LocalSettingsGenerator.php +++ b/includes/installer/LocalSettingsGenerator.php @@ -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 diff --git a/includes/installer/WebInstallerName.php b/includes/installer/WebInstallerName.php index dcd30cf2bc..2345d896a1 100644 --- a/includes/installer/WebInstallerName.php +++ b/includes/installer/WebInstallerName.php @@ -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' ) ) === '' ) { diff --git a/includes/installer/i18n/en.json b/includes/installer/i18n/en.json index 79383f3818..dbe4266fef 100644 --- a/includes/installer/i18n/en.json +++ b/includes/installer/i18n/en.json @@ -196,6 +196,8 @@ "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.", diff --git a/includes/installer/i18n/qqq.json b/includes/installer/i18n/qqq.json index 69a6830b1b..6a1dd08563 100644 --- a/includes/installer/i18n/qqq.json +++ b/includes/installer/i18n/qqq.json @@ -214,6 +214,8 @@ "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.", -- 2.20.1