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 * Joomla Platform class for interacting with a Mediawiki server instance.
16 *
17 * @property-read JMediawikiSites $sites MediaWiki API object for sites.
18 * @property-read JMediawikiPages $pages MediaWiki API object for pages.
19 * @property-read JMediawikiUsers $users MediaWiki API object for users.
20 * @property-read JMediawikiLinks $links MediaWiki API object for links.
21 * @property-read JMediawikiCategories $categories MediaWiki API object for categories.
22 * @property-read JMediawikiImages $images MediaWiki API object for images.
23 * @property-read JMediawikiSearch $search MediaWiki API object for search.
24 *
25 * @since 12.3
26 */
27 class JMediawiki
28 {
29 /**
30 * @var Registry Options for the MediaWiki object.
31 * @since 12.1
32 */
33 protected $options;
34
35 /**
36 * @var JMediawikiHttp The HTTP client object to use in sending HTTP requests.
37 * @since 12.3
38 */
39 protected $client;
40
41 /**
42 * @var JMediawikiSites MediaWiki API object for Site.
43 * @since 12.3
44 */
45 protected $sites;
46
47 /**
48 * @var JMediawikiPages MediaWiki API object for pages.
49 * @since 12.1
50 */
51 protected $pages;
52
53 /**
54 * @var JMediawikiUsers MediaWiki API object for users.
55 * @since 12.3
56 */
57 protected $users;
58
59 /**
60 * @var JMediawikiLinks MediaWiki API object for links.
61 * @since 12.3
62 */
63 protected $links;
64
65 /**
66 * @var JMediawikiCategories MediaWiki API object for categories.
67 * @since 12.3
68 */
69 protected $categories;
70
71 /**
72 * @var JMediawikiImages MediaWiki API object for images.
73 * @since 12.3
74 */
75 protected $images;
76
77 /**
78 * @var JMediawikiSearch MediaWiki API object for search.
79 * @since 12.1
80 */
81 protected $search;
82
83 /**
84 * Constructor.
85 *
86 * @param Registry $options MediaWiki options object.
87 * @param JMediawikiHttp $client The HTTP client object.
88 *
89 * @since 12.3
90 */
91 public function __construct(Registry $options = null, JMediawikiHttp $client = null)
92 {
93 $this->options = isset($options) ? $options : new Registry;
94 $this->client = isset($client) ? $client : new JMediawikiHttp($this->options);
95 }
96
97 /**
98 * Magic method to lazily create API objects
99 *
100 * @param string $name Name of property to retrieve
101 *
102 * @return JMediaWikiObject MediaWiki API object (users, reviews, etc).
103 *
104 * @since 12.3
105 * @throws InvalidArgumentException
106 */
107 public function __get($name)
108 {
109 $name = strtolower($name);
110 $class = 'JMediawiki' . ucfirst($name);
111 $accessible = array(
112 'categories',
113 'images',
114 'links',
115 'pages',
116 'search',
117 'sites',
118 'users',
119 );
120
121 if (class_exists($class) && in_array($name, $accessible))
122 {
123 if (!isset($this->$name))
124 {
125 $this->$name = new $class($this->options, $this->client);
126 }
127
128 return $this->$name;
129 }
130
131 throw new InvalidArgumentException(sprintf('Property %s is not accessible.', $name));
132 }
133
134 /**
135 * Get an option from the JMediawiki instance.
136 *
137 * @param string $key The name of the option to get.
138 *
139 * @return mixed The option value.
140 *
141 * @since 12.3
142 */
143 public function getOption($key)
144 {
145 return $this->options->get($key);
146 }
147
148 /**
149 * Set an option for the JMediawiki instance.
150 *
151 * @param string $key The name of the option to set.
152 * @param mixed $value The option value to set.
153 *
154 * @return JMediawiki This object for method chaining.
155 *
156 * @since 12.3
157 */
158 public function setOption($key, $value)
159 {
160 $this->options->set($key, $value);
161
162 return $this;
163 }
164 }
165