Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / includes / context / ContextSource.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @author Happy-melon
19 * @file
20 */
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * The simplest way of implementing IContextSource is to hold a RequestContext as a
25 * member variable and provide accessors to it.
26 *
27 * @since 1.18
28 */
29 abstract class ContextSource implements IContextSource {
30 /**
31 * @var IContextSource
32 */
33 private $context;
34
35 /**
36 * Get the base IContextSource object
37 * @since 1.18
38 * @return IContextSource
39 */
40 public function getContext() {
41 if ( $this->context === null ) {
42 $class = static::class;
43 wfDebug( __METHOD__ . " ($class): called and \$context is null. " .
44 "Using RequestContext::getMain() for sanity\n" );
45 $this->context = RequestContext::getMain();
46 }
47
48 return $this->context;
49 }
50
51 /**
52 * @since 1.18
53 * @param IContextSource $context
54 */
55 public function setContext( IContextSource $context ) {
56 $this->context = $context;
57 }
58
59 /**
60 * @since 1.23
61 * @return Config
62 */
63 public function getConfig() {
64 return $this->getContext()->getConfig();
65 }
66
67 /**
68 * @since 1.18
69 * @return WebRequest
70 */
71 public function getRequest() {
72 return $this->getContext()->getRequest();
73 }
74
75 /**
76 * @since 1.18
77 * @return Title|null
78 */
79 public function getTitle() {
80 return $this->getContext()->getTitle();
81 }
82
83 /**
84 * Check whether a WikiPage object can be get with getWikiPage().
85 * Callers should expect that an exception is thrown from getWikiPage()
86 * if this method returns false.
87 *
88 * @since 1.19
89 * @return bool
90 */
91 public function canUseWikiPage() {
92 return $this->getContext()->canUseWikiPage();
93 }
94
95 /**
96 * Get the WikiPage object.
97 * May throw an exception if there's no Title object set or the Title object
98 * belongs to a special namespace that doesn't have WikiPage, so use first
99 * canUseWikiPage() to check whether this method can be called safely.
100 *
101 * @since 1.19
102 * @return WikiPage
103 */
104 public function getWikiPage() {
105 return $this->getContext()->getWikiPage();
106 }
107
108 /**
109 * @since 1.18
110 * @return OutputPage
111 */
112 public function getOutput() {
113 return $this->getContext()->getOutput();
114 }
115
116 /**
117 * @since 1.18
118 * @return User
119 */
120 public function getUser() {
121 return $this->getContext()->getUser();
122 }
123
124 /**
125 * @since 1.19
126 * @return Language
127 */
128 public function getLanguage() {
129 return $this->getContext()->getLanguage();
130 }
131
132 /**
133 * @since 1.18
134 * @return Skin
135 */
136 public function getSkin() {
137 return $this->getContext()->getSkin();
138 }
139
140 /**
141 * @since 1.27
142 * @return Timing
143 */
144 public function getTiming() {
145 return $this->getContext()->getTiming();
146 }
147
148 /**
149 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
150 *
151 * @since 1.25
152 * @return IBufferingStatsdDataFactory
153 */
154 public function getStats() {
155 return MediaWikiServices::getInstance()->getStatsdDataFactory();
156 }
157
158 /**
159 * Get a Message object with context set
160 * Parameters are the same as wfMessage()
161 *
162 * @since 1.18
163 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
164 * or a MessageSpecifier.
165 * @param mixed $args,...
166 * @return Message
167 */
168 public function msg( $key /* $args */ ) {
169 $args = func_get_args();
170
171 return call_user_func_array( [ $this->getContext(), 'msg' ], $args );
172 }
173
174 /**
175 * Export the resolved user IP, HTTP headers, user ID, and session ID.
176 * The result will be reasonably sized to allow for serialization.
177 *
178 * @return array
179 * @since 1.21
180 */
181 public function exportSession() {
182 return $this->getContext()->exportSession();
183 }
184 }