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