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 * MediaWiki API object class for the Joomla Platform.
16 *
17 * @since 12.3
18 */
19 abstract class JMediawikiObject
20 {
21 /**
22 * @var Registry Options for the MediaWiki object.
23 * @since 12.3
24 */
25 protected $options;
26
27 /**
28 * @var JMediawikiHttp The HTTP client object to use in sending HTTP requests.
29 * @since 12.3
30 */
31 protected $client;
32
33 /**
34 * Constructor.
35 *
36 * @param Registry $options Mediawiki options object.
37 * @param JMediawikiHttp $client The HTTP client object.
38 *
39 * @since 12.3
40 */
41 public function __construct(Registry $options = null, JMediawikiHttp $client = null)
42 {
43 $this->options = isset($options) ? $options : new Registry;
44 $this->client = isset($client) ? $client : new JMediawikiHttp($this->options);
45 }
46
47 /**
48 * Method to build and return a full request URL for the request.
49 *
50 * @param string $path URL to inflect
51 *
52 * @return string The request URL.
53 *
54 * @since 12.3
55 */
56 protected function fetchUrl($path)
57 {
58 // Append the path with output format
59 $path .= '&format=xml';
60
61 $uri = new JUri($this->options->get('api.url') . '/api.php' . $path);
62
63 if ($this->options->get('api.username', false))
64 {
65 $uri->setUser($this->options->get('api.username'));
66 }
67
68 if ($this->options->get('api.password', false))
69 {
70 $uri->setPass($this->options->get('api.password'));
71 }
72
73 return (string) $uri;
74 }
75
76 /**
77 * Method to build request parameters from a string array.
78 *
79 * @param array $params string array that contains the parameters
80 *
81 * @return string request parameter
82 *
83 * @since 12.3
84 */
85 public function buildParameter(array $params)
86 {
87 $path = '';
88
89 foreach ($params as $param)
90 {
91 $path .= $param;
92
93 if (next($params) == true)
94 {
95 $path .= '|';
96 }
97 }
98
99 return $path;
100 }
101
102 /**
103 * Method to validate response for errors
104 *
105 * @param JHttpresponse $response reponse from the mediawiki server
106 *
107 * @return Object
108 *
109 * @since 12.3
110 *
111 * @throws DomainException
112 */
113 public function validateResponse($response)
114 {
115 $xml = simplexml_load_string($response->body);
116
117 if (isset($xml->warnings))
118 {
119 throw new DomainException($xml->warnings->info);
120 }
121
122 if (isset($xml->error))
123 {
124 throw new DomainException($xml->error['info']);
125 }
126
127 return $xml;
128 }
129 }
130