1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage MediaWiki
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * HTTP client class for connecting to a MediaWiki instance.
16 *
17 * @since 12.3
18 */
19 class JMediawikiHttp extends JHttp
20 {
21 /**
22 * Constructor.
23 *
24 * @param Registry $options Client options object.
25 * @param JHttpTransport $transport The HTTP transport object.
26 *
27 * @since 12.3
28 */
29 public function __construct(Registry $options = null, JHttpTransport $transport = null)
30 {
31 // Override the JHttp contructor to use JHttpTransportStream.
32 $this->options = isset($options) ? $options : new Registry;
33 $this->transport = isset($transport) ? $transport : new JHttpTransportStream($this->options);
34
35 // Make sure the user agent string is defined.
36 $this->options->def('api.useragent', 'JMediawiki/1.0');
37
38 // Set the default timeout to 120 seconds.
39 $this->options->def('api.timeout', 120);
40 }
41
42 /**
43 * Method to send the GET command to the server.
44 *
45 * @param string $url Path to the resource.
46 * @param array $headers An array of name-value pairs to include in the header of the request.
47 * @param integer $timeout Read timeout in seconds.
48 *
49 * @return JHttpResponse
50 *
51 * @since 12.3
52 */
53 public function get($url, array $headers = null, $timeout = null)
54 {
55 // Look for headers set in the options.
56 $temp = (array) $this->options->get('headers');
57
58 foreach ($temp as $key => $val)
59 {
60 if (!isset($headers[$key]))
61 {
62 $headers[$key] = $val;
63 }
64 }
65
66 // Look for timeout set in the options.
67 if ($timeout === null && $this->options->exists('api.timeout'))
68 {
69 $timeout = $this->options->get('api.timeout');
70 }
71
72 return $this->transport->request('GET', new JUri($url), null, $headers, $timeout, $this->options->get('api.useragent'));
73 }
74
75 /**
76 * Method to send the POST command to the server.
77 *
78 * @param string $url Path to the resource.
79 * @param mixed $data Either an associative array or a string to be sent with the request.
80 * @param array $headers An array of name-value pairs to include in the header of the request
81 * @param integer $timeout Read timeout in seconds.
82 *
83 * @return JHttpResponse
84 *
85 * @since 12.3
86 */
87 public function post($url, $data, array $headers = null, $timeout = null)
88 {
89 // Look for headers set in the options.
90 $temp = (array) $this->options->get('headers');
91
92 foreach ($temp as $key => $val)
93 {
94 if (!isset($headers[$key]))
95 {
96 $headers[$key] = $val;
97 }
98 }
99
100 // Look for timeout set in the options.
101 if ($timeout === null && $this->options->exists('api.timeout'))
102 {
103 $timeout = $this->options->get('api.timeout');
104 }
105
106 return $this->transport->request('POST', new JUri($url), $data, $headers, $timeout, $this->options->get('api.useragent'));
107 }
108 }
109