Remove "related" searches
[mediawiki.git] / includes / context / DerivativeContext.php
blobb8966f0cf5efb01deee75449c1fb89f28ae786ed
1 <?php
2 /**
3 * Request-dependant objects containers.
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.
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.
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
20 * @since 1.19
22 * @author Daniel Friesen
23 * @file
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.
32 class DerivativeContext extends ContextSource {
33 /**
34 * @var WebRequest
36 private $request;
38 /**
39 * @var Title
41 private $title;
43 /**
44 * @var WikiPage
46 private $wikipage;
48 /**
49 * @var OutputPage
51 private $output;
53 /**
54 * @var User
56 private $user;
58 /**
59 * @var Language
61 private $lang;
63 /**
64 * @var Skin
66 private $skin;
68 /**
69 * @var Config
71 private $config;
73 /**
74 * Constructor
75 * @param IContextSource $context Context to inherit from
77 public function __construct( IContextSource $context ) {
78 $this->setContext( $context );
81 /**
82 * Set the SiteConfiguration object
84 * @param Config $s
86 public function setConfig( Config $s ) {
87 $this->config = $s;
90 /**
91 * Get the Config object
93 * @return Config
95 public function getConfig() {
96 if ( !is_null( $this->config ) ) {
97 return $this->config;
98 } else {
99 return $this->getContext()->getConfig();
104 * Set the WebRequest object
106 * @param WebRequest $r
108 public function setRequest( WebRequest $r ) {
109 $this->request = $r;
113 * Get the WebRequest object
115 * @return WebRequest
117 public function getRequest() {
118 if ( !is_null( $this->request ) ) {
119 return $this->request;
120 } else {
121 return $this->getContext()->getRequest();
126 * Set the Title object
128 * @param Title $t
130 public function setTitle( Title $t ) {
131 $this->title = $t;
135 * Get the Title object
137 * @return Title|null
139 public function getTitle() {
140 if ( !is_null( $this->title ) ) {
141 return $this->title;
142 } else {
143 return $this->getContext()->getTitle();
148 * Check whether a WikiPage object can be get with getWikiPage().
149 * Callers should expect that an exception is thrown from getWikiPage()
150 * if this method returns false.
152 * @since 1.19
153 * @return bool
155 public function canUseWikiPage() {
156 if ( $this->wikipage !== null ) {
157 return true;
158 } elseif ( $this->title !== null ) {
159 return $this->title->canExist();
160 } else {
161 return $this->getContext()->canUseWikiPage();
166 * Set the WikiPage object
168 * @since 1.19
169 * @param WikiPage $p
171 public function setWikiPage( WikiPage $p ) {
172 $this->wikipage = $p;
176 * Get the WikiPage object.
177 * May throw an exception if there's no Title object set or the Title object
178 * belongs to a special namespace that doesn't have WikiPage, so use first
179 * canUseWikiPage() to check whether this method can be called safely.
181 * @since 1.19
182 * @return WikiPage
184 public function getWikiPage() {
185 if ( !is_null( $this->wikipage ) ) {
186 return $this->wikipage;
187 } else {
188 return $this->getContext()->getWikiPage();
193 * Set the OutputPage object
195 * @param OutputPage $o
197 public function setOutput( OutputPage $o ) {
198 $this->output = $o;
202 * Get the OutputPage object
204 * @return OutputPage
206 public function getOutput() {
207 if ( !is_null( $this->output ) ) {
208 return $this->output;
209 } else {
210 return $this->getContext()->getOutput();
215 * Set the User object
217 * @param User $u
219 public function setUser( User $u ) {
220 $this->user = $u;
224 * Get the User object
226 * @return User
228 public function getUser() {
229 if ( !is_null( $this->user ) ) {
230 return $this->user;
231 } else {
232 return $this->getContext()->getUser();
237 * Set the Language object
239 * @param Language|string $l Language instance or language code
240 * @throws MWException
241 * @since 1.19
243 public function setLanguage( $l ) {
244 if ( $l instanceof Language ) {
245 $this->lang = $l;
246 } elseif ( is_string( $l ) ) {
247 $l = RequestContext::sanitizeLangCode( $l );
248 $obj = Language::factory( $l );
249 $this->lang = $obj;
250 } else {
251 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
256 * Get the Language object
258 * @return Language
259 * @since 1.19
261 public function getLanguage() {
262 if ( !is_null( $this->lang ) ) {
263 return $this->lang;
264 } else {
265 return $this->getContext()->getLanguage();
270 * Set the Skin object
272 * @param Skin $s
274 public function setSkin( Skin $s ) {
275 $this->skin = clone $s;
276 $this->skin->setContext( $this );
280 * Get the Skin object
282 * @return Skin
284 public function getSkin() {
285 if ( !is_null( $this->skin ) ) {
286 return $this->skin;
287 } else {
288 return $this->getContext()->getSkin();
293 * Get a message using the current context.
295 * This can't just inherit from ContextSource, since then
296 * it would set only the original context, and not take
297 * into account any changes.
299 * @param mixed $args,... Arguments to wfMessage
300 * @return Message
302 public function msg() {
303 $args = func_get_args();
305 return call_user_func_array( 'wfMessage', $args )->setContext( $this );