Merge "Support rate-limiting thumbnail generation"
[lhc/web/wiklou.git] / includes / context / ContextSource.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.18
21 *
22 * @author Happy-melon
23 * @file
24 */
25
26 /**
27 * The simplest way of implementing IContextSource is to hold a RequestContext as a
28 * member variable and provide accessors to it.
29 */
30 abstract class ContextSource implements IContextSource {
31 /**
32 * @var IContextSource
33 */
34 private $context;
35
36 /**
37 * Get the RequestContext object
38 * @since 1.18
39 * @return RequestContext
40 */
41 public function getContext() {
42 if ( $this->context === null ) {
43 $class = get_class( $this );
44 wfDebug( __METHOD__ . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
45 $this->context = RequestContext::getMain();
46 }
47 return $this->context;
48 }
49
50 /**
51 * Set the IContextSource object
52 *
53 * @since 1.18
54 * @param IContextSource $context
55 */
56 public function setContext( IContextSource $context ) {
57 $this->context = $context;
58 }
59
60 /**
61 * Get the WebRequest object
62 *
63 * @since 1.18
64 * @return WebRequest
65 */
66 public function getRequest() {
67 return $this->getContext()->getRequest();
68 }
69
70 /**
71 * Get the Title object
72 *
73 * @since 1.18
74 * @return Title
75 */
76 public function getTitle() {
77 return $this->getContext()->getTitle();
78 }
79
80 /**
81 * Check whether a WikiPage object can be get with getWikiPage().
82 * Callers should expect that an exception is thrown from getWikiPage()
83 * if this method returns false.
84 *
85 * @since 1.19
86 * @return bool
87 */
88 public function canUseWikiPage() {
89 return $this->getContext()->canUseWikiPage();
90 }
91
92 /**
93 * Get the WikiPage object.
94 * May throw an exception if there's no Title object set or the Title object
95 * belongs to a special namespace that doesn't have WikiPage, so use first
96 * canUseWikiPage() to check whether this method can be called safely.
97 *
98 * @since 1.19
99 * @return WikiPage
100 */
101 public function getWikiPage() {
102 return $this->getContext()->getWikiPage();
103 }
104
105 /**
106 * Get the OutputPage object
107 *
108 * @since 1.18
109 * @return OutputPage
110 */
111 public function getOutput() {
112 return $this->getContext()->getOutput();
113 }
114
115 /**
116 * Get the User object
117 *
118 * @since 1.18
119 * @return User
120 */
121 public function getUser() {
122 return $this->getContext()->getUser();
123 }
124
125 /**
126 * Get the Language object
127 *
128 * @deprecated since 1.19 Use getLanguage instead
129 * @return Language
130 */
131 public function getLang() {
132 wfDeprecated( __METHOD__, '1.19' );
133 return $this->getLanguage();
134 }
135
136 /**
137 * Get the Language object
138 *
139 * @since 1.19
140 * @return Language
141 */
142 public function getLanguage() {
143 return $this->getContext()->getLanguage();
144 }
145
146 /**
147 * Get the Skin object
148 *
149 * @since 1.18
150 * @return Skin
151 */
152 public function getSkin() {
153 return $this->getContext()->getSkin();
154 }
155
156 /**
157 * Get a Message object with context set
158 * Parameters are the same as wfMessage()
159 *
160 * @since 1.18
161 * @return Message
162 */
163 public function msg( /* $args */ ) {
164 $args = func_get_args();
165 return call_user_func_array( array( $this->getContext(), 'msg' ), $args );
166 }
167
168 /**
169 * Export the resolved user IP, HTTP headers, user ID, and session ID.
170 * The result will be reasonably sized to allow for serialization.
171 *
172 * @return Array
173 * @since 1.21
174 */
175 public function exportSession() {
176 return $this->getContext()->exportSession();
177 }
178 }