Merge "jquery.makeCollapsible: clean up the handler toggling logic"
[lhc/web/wiklou.git] / includes / context / DerivativeContext.php
1 <?php
2 /**
3 * Request-dependant objects containers.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @since 1.19
21 *
22 * @author Daniel Friesen
23 * @file
24 */
25
26 /**
27 * An IContextSource implementation which will inherit context from another source
28 * but allow individual pieces of context to be changed locally
29 * eg: A ContextSource that can inherit from the main RequestContext but have
30 * a different Title instance set on it.
31 */
32 class DerivativeContext extends ContextSource {
33 /**
34 * @var WebRequest
35 */
36 private $request;
37
38 /**
39 * @var Title
40 */
41 private $title;
42
43 /**
44 * @var WikiPage
45 */
46 private $wikipage;
47
48 /**
49 * @var OutputPage
50 */
51 private $output;
52
53 /**
54 * @var User
55 */
56 private $user;
57
58 /**
59 * @var Language
60 */
61 private $lang;
62
63 /**
64 * @var Skin
65 */
66 private $skin;
67
68 /**
69 * Constructor
70 * @param IContextSource $context Context to inherit from
71 */
72 public function __construct( IContextSource $context ) {
73 $this->setContext( $context );
74 }
75
76 /**
77 * Set the WebRequest object
78 *
79 * @param WebRequest $r
80 */
81 public function setRequest( WebRequest $r ) {
82 $this->request = $r;
83 }
84
85 /**
86 * Get the WebRequest object
87 *
88 * @return WebRequest
89 */
90 public function getRequest() {
91 if ( !is_null( $this->request ) ) {
92 return $this->request;
93 } else {
94 return $this->getContext()->getRequest();
95 }
96 }
97
98 /**
99 * Set the Title object
100 *
101 * @param Title $t
102 */
103 public function setTitle( Title $t ) {
104 $this->title = $t;
105 }
106
107 /**
108 * Get the Title object
109 *
110 * @return Title
111 */
112 public function getTitle() {
113 if ( !is_null( $this->title ) ) {
114 return $this->title;
115 } else {
116 return $this->getContext()->getTitle();
117 }
118 }
119
120 /**
121 * Check whether a WikiPage object can be get with getWikiPage().
122 * Callers should expect that an exception is thrown from getWikiPage()
123 * if this method returns false.
124 *
125 * @since 1.19
126 * @return bool
127 */
128 public function canUseWikiPage() {
129 if ( $this->wikipage !== null ) {
130 return true;
131 } elseif ( $this->title !== null ) {
132 return $this->title->canExist();
133 } else {
134 return $this->getContext()->canUseWikiPage();
135 }
136 }
137
138 /**
139 * Set the WikiPage object
140 *
141 * @since 1.19
142 * @param WikiPage $p
143 */
144 public function setWikiPage( WikiPage $p ) {
145 $this->wikipage = $p;
146 }
147
148 /**
149 * Get the WikiPage object.
150 * May throw an exception if there's no Title object set or the Title object
151 * belongs to a special namespace that doesn't have WikiPage, so use first
152 * canUseWikiPage() to check whether this method can be called safely.
153 *
154 * @since 1.19
155 * @return WikiPage
156 */
157 public function getWikiPage() {
158 if ( !is_null( $this->wikipage ) ) {
159 return $this->wikipage;
160 } else {
161 return $this->getContext()->getWikiPage();
162 }
163 }
164
165 /**
166 * Set the OutputPage object
167 *
168 * @param OutputPage $o
169 */
170 public function setOutput( OutputPage $o ) {
171 $this->output = $o;
172 }
173
174 /**
175 * Get the OutputPage object
176 *
177 * @return OutputPage
178 */
179 public function getOutput() {
180 if ( !is_null( $this->output ) ) {
181 return $this->output;
182 } else {
183 return $this->getContext()->getOutput();
184 }
185 }
186
187 /**
188 * Set the User object
189 *
190 * @param User $u
191 */
192 public function setUser( User $u ) {
193 $this->user = $u;
194 }
195
196 /**
197 * Get the User object
198 *
199 * @return User
200 */
201 public function getUser() {
202 if ( !is_null( $this->user ) ) {
203 return $this->user;
204 } else {
205 return $this->getContext()->getUser();
206 }
207 }
208
209 /**
210 * Set the Language object
211 *
212 * @deprecated 1.19 Use setLanguage instead
213 * @param Language|string $l Language instance or language code
214 */
215 public function setLang( $l ) {
216 wfDeprecated( __METHOD__, '1.19' );
217 $this->setLanguage( $l );
218 }
219
220 /**
221 * Set the Language object
222 *
223 * @param Language|string $l Language instance or language code
224 * @throws MWException
225 * @since 1.19
226 */
227 public function setLanguage( $l ) {
228 if ( $l instanceof Language ) {
229 $this->lang = $l;
230 } elseif ( is_string( $l ) ) {
231 $l = RequestContext::sanitizeLangCode( $l );
232 $obj = Language::factory( $l );
233 $this->lang = $obj;
234 } else {
235 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
236 }
237 }
238
239 /**
240 * @deprecated 1.19 Use getLanguage instead
241 * @return Language
242 */
243 public function getLang() {
244 wfDeprecated( __METHOD__, '1.19' );
245 $this->getLanguage();
246 }
247
248 /**
249 * Get the Language object
250 *
251 * @return Language
252 * @since 1.19
253 */
254 public function getLanguage() {
255 if ( !is_null( $this->lang ) ) {
256 return $this->lang;
257 } else {
258 return $this->getContext()->getLanguage();
259 }
260 }
261
262 /**
263 * Set the Skin object
264 *
265 * @param Skin $s
266 */
267 public function setSkin( Skin $s ) {
268 $this->skin = clone $s;
269 $this->skin->setContext( $this );
270 }
271
272 /**
273 * Get the Skin object
274 *
275 * @return Skin
276 */
277 public function getSkin() {
278 if ( !is_null( $this->skin ) ) {
279 return $this->skin;
280 } else {
281 return $this->getContext()->getSkin();
282 }
283 }
284 }