1 | <?php |
||
35 | class PageIndexerRequest |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * List of actions to perform during page rendering. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $actions = array(); |
||
44 | |||
45 | /** |
||
46 | * Parameters as sent from the Index Queue page indexer. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $parameters = array(); |
||
51 | |||
52 | /** |
||
53 | * Headers as sent from the Index Queue page indexer. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $header = array(); |
||
58 | |||
59 | /** |
||
60 | * Unique request ID. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $requestId; |
||
65 | |||
66 | /** |
||
67 | * Username to use for basic auth protected URLs. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $username = ''; |
||
72 | |||
73 | /** |
||
74 | * Password to use for basic auth protected URLs. |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $password = ''; |
||
79 | |||
80 | /** |
||
81 | * An Index Queue item related to this request. |
||
82 | * |
||
83 | * @var Item |
||
84 | */ |
||
85 | protected $indexQueueItem = null; |
||
86 | |||
87 | /** |
||
88 | * Request timeout in seconds |
||
89 | * |
||
90 | * @var float |
||
91 | */ |
||
92 | protected $timeout; |
||
93 | |||
94 | /** |
||
95 | * PageIndexerRequest constructor. |
||
96 | * |
||
97 | * @param string $jsonEncodedParameters json encoded header |
||
98 | 6 | */ |
|
99 | public function __construct($jsonEncodedParameters = null) |
||
100 | 6 | { |
|
101 | $this->requestId = uniqid(); |
||
102 | 6 | $this->timeout = (float)ini_get('default_socket_timeout'); |
|
103 | |||
104 | 6 | if (is_null($jsonEncodedParameters)) { |
|
105 | 3 | return; |
|
106 | 3 | } |
|
107 | |||
108 | 3 | $this->parameters = (array)json_decode($jsonEncodedParameters, true); |
|
109 | 3 | $this->requestId = $this->parameters['requestId']; |
|
110 | unset($this->parameters['requestId']); |
||
111 | 3 | ||
112 | 3 | $actions = explode(',', $this->parameters['actions']); |
|
113 | 3 | foreach ($actions as $action) { |
|
114 | $this->addAction($action); |
||
115 | 3 | } |
|
116 | unset($this->parameters['actions']); |
||
117 | 6 | } |
|
118 | |||
119 | /** |
||
120 | * Adds an action to perform during page rendering. |
||
121 | * |
||
122 | * @param string $action Action name. |
||
123 | */ |
||
124 | 4 | public function addAction($action) |
|
128 | |||
129 | /** |
||
130 | * Executes the request. |
||
131 | * |
||
132 | * Uses headers to submit additional data and avoiding to have these |
||
133 | * arguments integrated into the URL when created by RealURL. |
||
134 | * |
||
135 | * @param string $url The URL to request. |
||
136 | * @return PageIndexerResponse Response |
||
137 | */ |
||
138 | 1 | public function send($url) |
|
177 | |||
178 | /** |
||
179 | * This method is used to retrieve an url from the frontend and decode the response. |
||
180 | * |
||
181 | * @param string $url |
||
182 | * @param PageIndexerResponse $response |
||
183 | * @return mixed |
||
184 | 1 | */ |
|
185 | protected function getUrlAndDecodeResponse($url, PageIndexerResponse $response) |
||
210 | 1 | ||
211 | 1 | /** |
|
212 | 1 | * Generates the headers to be send with the request. |
|
213 | * |
||
214 | * @return string[] Array of HTTP headers. |
||
215 | 1 | */ |
|
216 | 1 | public function getHeaders() |
|
244 | |||
245 | /** |
||
246 | * Adds an HTTP header to be send with the request. |
||
247 | * |
||
248 | * @param string $header HTTP header |
||
249 | */ |
||
250 | public function addHeader($header) |
||
254 | |||
255 | 2 | /** |
|
256 | * Checks whether this is a legitimate request coming from the Index Queue |
||
257 | 2 | * page indexer worker task. |
|
258 | * |
||
259 | 2 | * @return bool TRUE if it's a legitimate request, FALSE otherwise. |
|
260 | 2 | */ |
|
261 | 2 | public function isAuthenticated() |
|
281 | |||
282 | /** |
||
283 | * Gets the list of actions to perform during page rendering. |
||
284 | * |
||
285 | * @return array List of actions |
||
286 | */ |
||
287 | public function getActions() |
||
291 | |||
292 | /** |
||
293 | * Gets the request's parameters. |
||
294 | * |
||
295 | * @return array Request parameters. |
||
296 | */ |
||
297 | public function getParameters() |
||
301 | 1 | ||
302 | /** |
||
303 | * Gets the request's unique ID. |
||
304 | * |
||
305 | * @return string Unique request ID. |
||
306 | */ |
||
307 | public function getRequestId() |
||
311 | |||
312 | 2 | /** |
|
313 | * Gets a specific parameter's value. |
||
314 | 2 | * |
|
315 | 2 | * @param string $parameterName The parameter to retrieve. |
|
316 | * @return mixed NULL if a parameter was not set or it's value otherwise. |
||
317 | */ |
||
318 | 2 | public function getParameter($parameterName) |
|
322 | |||
323 | /** |
||
324 | * Sets a request's parameter and its value. |
||
325 | * |
||
326 | * @param string $parameter Parameter name |
||
327 | 3 | * @param mixed $value Parameter value. |
|
328 | */ |
||
329 | 3 | public function setParameter($parameter, $value) |
|
337 | |||
338 | /** |
||
339 | * Sets username and password to be used for a basic auth request header. |
||
340 | * |
||
341 | * @param string $username username. |
||
342 | * @param string $password password. |
||
343 | */ |
||
344 | public function setAuthorizationCredentials($username, $password) |
||
349 | |||
350 | /** |
||
351 | * Sets the Index Queue item this request is related to. |
||
352 | * |
||
353 | 1 | * @param Item $item Related Index Queue item. |
|
354 | */ |
||
355 | 1 | public function setIndexQueueItem(Item $item) |
|
359 | |||
360 | /** |
||
361 | * Returns the request timeout in seconds |
||
362 | * |
||
363 | * @return float |
||
364 | */ |
||
365 | public function getTimeout() |
||
369 | |||
370 | /** |
||
371 | * Sets the request timeout in seconds |
||
372 | * |
||
373 | * @param float $timeout Timeout seconds |
||
374 | */ |
||
375 | public function setTimeout($timeout) |
||
379 | |||
380 | /** |
||
381 | * Fetches a page by sending the configured headers. |
||
382 | * |
||
383 | * @param string $url |
||
384 | * @param string[] $headers |
||
385 | * @param float $timeout |
||
386 | * @return string |
||
387 | */ |
||
388 | protected function getUrl($url, $headers, $timeout) |
||
394 | } |
||
395 |