1 | <?php |
||
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 ) { |
||
61 | |||
62 | /** |
||
63 | * Should a pingback be sent? |
||
64 | * @return bool |
||
65 | */ |
||
66 | private function shouldSend() { |
||
69 | |||
70 | /** |
||
71 | * Has a pingback already been sent for this MediaWiki version? |
||
72 | * @return bool |
||
73 | */ |
||
74 | private function checkIfSent() { |
||
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() { |
||
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() { |
||
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() { |
||
143 | |||
144 | /** |
||
145 | * Get the EventLogging packet to be sent to the server |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | private function getData() { |
||
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() { |
||
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 ) { |
||
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() { |
||
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() { |
||
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: