Pingback.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Send information about this MediaWiki instance to MediaWiki.org.
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. */
  22. use Psr\Log\LoggerInterface;
  23. use MediaWiki\Logger\LoggerFactory;
  24. /**
  25. * Send information about this MediaWiki instance to MediaWiki.org.
  26. *
  27. * @since 1.28
  28. */
  29. class Pingback {
  30. /**
  31. * @var int Revision ID of the JSON schema that describes the pingback
  32. * payload. The schema lives on MetaWiki, at
  33. * <https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>.
  34. */
  35. const SCHEMA_REV = 15781718;
  36. /** @var LoggerInterface */
  37. protected $logger;
  38. /** @var Config */
  39. protected $config;
  40. /** @var string updatelog key (also used as cache/db lock key) */
  41. protected $key;
  42. /** @var string Randomly-generated identifier for this wiki */
  43. protected $id;
  44. /**
  45. * @param Config $config
  46. * @param LoggerInterface $logger
  47. */
  48. public function __construct( Config $config = null, LoggerInterface $logger = null ) {
  49. $this->config = $config ?: RequestContext::getMain()->getConfig();
  50. $this->logger = $logger ?: LoggerFactory::getInstance( __CLASS__ );
  51. $this->key = 'Pingback-' . $this->config->get( 'Version' );
  52. }
  53. /**
  54. * Should a pingback be sent?
  55. * @return bool
  56. */
  57. private function shouldSend() {
  58. return $this->config->get( 'Pingback' ) && !$this->checkIfSent();
  59. }
  60. /**
  61. * Has a pingback been sent in the last month for this MediaWiki version?
  62. * @return bool
  63. */
  64. private function checkIfSent() {
  65. $dbr = wfGetDB( DB_REPLICA );
  66. $timestamp = $dbr->selectField(
  67. 'updatelog',
  68. 'ul_value',
  69. [ 'ul_key' => $this->key ],
  70. __METHOD__
  71. );
  72. if ( $timestamp === false ) {
  73. return false;
  74. }
  75. // send heartbeat ping if last ping was over a month ago
  76. if ( time() - (int)$timestamp > 60 * 60 * 24 * 30 ) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * Record the fact that we have sent a pingback for this MediaWiki version,
  83. * to ensure we don't submit data multiple times.
  84. */
  85. private function markSent() {
  86. $dbw = wfGetDB( DB_MASTER );
  87. $timestamp = time();
  88. return $dbw->upsert(
  89. 'updatelog',
  90. [ 'ul_key' => $this->key, 'ul_value' => $timestamp ],
  91. [ 'ul_key' ],
  92. [ 'ul_value' => $timestamp ],
  93. __METHOD__
  94. );
  95. }
  96. /**
  97. * Acquire lock for sending a pingback
  98. *
  99. * This ensures only one thread can attempt to send a pingback at any given
  100. * time and that we wait an hour before retrying failed attempts.
  101. *
  102. * @return bool Whether lock was acquired
  103. */
  104. private function acquireLock() {
  105. $cache = ObjectCache::getLocalClusterInstance();
  106. if ( !$cache->add( $this->key, 1, 60 * 60 ) ) {
  107. return false; // throttled
  108. }
  109. $dbw = wfGetDB( DB_MASTER );
  110. if ( !$dbw->lock( $this->key, __METHOD__, 0 ) ) {
  111. return false; // already in progress
  112. }
  113. return true;
  114. }
  115. /**
  116. * Collect basic data about this MediaWiki installation and return it
  117. * as an associative array conforming to the Pingback schema on MetaWiki
  118. * (<https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>).
  119. *
  120. * This is public so we can display it in the installer
  121. *
  122. * Developers: If you're adding a new piece of data to this, please ensure
  123. * that you update https://www.mediawiki.org/wiki/Manual:$wgPingback
  124. *
  125. * @return array
  126. */
  127. public function getSystemInfo() {
  128. $event = [
  129. 'database' => $this->config->get( 'DBtype' ),
  130. 'MediaWiki' => $this->config->get( 'Version' ),
  131. 'PHP' => PHP_VERSION,
  132. 'OS' => PHP_OS . ' ' . php_uname( 'r' ),
  133. 'arch' => PHP_INT_SIZE === 8 ? 64 : 32,
  134. 'machine' => php_uname( 'm' ),
  135. ];
  136. if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
  137. $event['serverSoftware'] = $_SERVER['SERVER_SOFTWARE'];
  138. }
  139. $limit = ini_get( 'memory_limit' );
  140. if ( $limit && $limit != -1 ) {
  141. $event['memoryLimit'] = $limit;
  142. }
  143. return $event;
  144. }
  145. /**
  146. * Get the EventLogging packet to be sent to the server
  147. *
  148. * @return array
  149. */
  150. private function getData() {
  151. return [
  152. 'schema' => 'MediaWikiPingback',
  153. 'revision' => self::SCHEMA_REV,
  154. 'wiki' => $this->getOrCreatePingbackId(),
  155. 'event' => $this->getSystemInfo(),
  156. ];
  157. }
  158. /**
  159. * Get a unique, stable identifier for this wiki
  160. *
  161. * If the identifier does not already exist, create it and save it in the
  162. * database. The identifier is randomly-generated.
  163. *
  164. * @return string 32-character hex string
  165. */
  166. private function getOrCreatePingbackId() {
  167. if ( !$this->id ) {
  168. $id = wfGetDB( DB_REPLICA )->selectField(
  169. 'updatelog', 'ul_value', [ 'ul_key' => 'PingBack' ] );
  170. if ( $id == false ) {
  171. $id = MWCryptRand::generateHex( 32 );
  172. $dbw = wfGetDB( DB_MASTER );
  173. $dbw->insert(
  174. 'updatelog',
  175. [ 'ul_key' => 'PingBack', 'ul_value' => $id ],
  176. __METHOD__,
  177. 'IGNORE'
  178. );
  179. if ( !$dbw->affectedRows() ) {
  180. $id = $dbw->selectField(
  181. 'updatelog', 'ul_value', [ 'ul_key' => 'PingBack' ] );
  182. }
  183. }
  184. $this->id = $id;
  185. }
  186. return $this->id;
  187. }
  188. /**
  189. * Serialize pingback data and send it to MediaWiki.org via a POST
  190. * to its event beacon endpoint.
  191. *
  192. * The data encoding conforms to the expectations of EventLogging,
  193. * a software suite used by the Wikimedia Foundation for logging and
  194. * processing analytic data.
  195. *
  196. * Compare:
  197. * <https://github.com/wikimedia/mediawiki-extensions-EventLogging/
  198. * blob/7e5fe4f1ef/includes/EventLogging.php#L32-L74>
  199. *
  200. * @param array $data Pingback data as an associative array
  201. * @return bool true on success, false on failure
  202. */
  203. private function postPingback( array $data ) {
  204. $json = FormatJson::encode( $data );
  205. $queryString = rawurlencode( str_replace( ' ', '\u0020', $json ) ) . ';';
  206. $url = 'https://www.mediawiki.org/beacon/event?' . $queryString;
  207. return Http::post( $url ) !== false;
  208. }
  209. /**
  210. * Send information about this MediaWiki instance to MediaWiki.org.
  211. *
  212. * The data is structured and serialized to match the expectations of
  213. * EventLogging, a software suite used by the Wikimedia Foundation for
  214. * logging and processing analytic data.
  215. *
  216. * Compare:
  217. * <https://github.com/wikimedia/mediawiki-extensions-EventLogging/
  218. * blob/7e5fe4f1ef/includes/EventLogging.php#L32-L74>
  219. *
  220. * The schema for the data is located at:
  221. * <https://meta.wikimedia.org/wiki/Schema:MediaWikiPingback>
  222. * @return bool
  223. */
  224. public function sendPingback() {
  225. if ( !$this->acquireLock() ) {
  226. $this->logger->debug( __METHOD__ . ": couldn't acquire lock" );
  227. return false;
  228. }
  229. $data = $this->getData();
  230. if ( !$this->postPingback( $data ) ) {
  231. $this->logger->warning( __METHOD__ . ": failed to send pingback; check 'http' log" );
  232. return false;
  233. }
  234. $this->markSent();
  235. $this->logger->debug( __METHOD__ . ": pingback sent OK ({$this->key})" );
  236. return true;
  237. }
  238. /**
  239. * Schedule a deferred callable that will check if a pingback should be
  240. * sent and (if so) proceed to send it.
  241. */
  242. public static function schedulePingback() {
  243. DeferredUpdates::addCallableUpdate( function () {
  244. $instance = new Pingback;
  245. if ( $instance->shouldSend() ) {
  246. $instance->sendPingback();
  247. }
  248. } );
  249. }
  250. }