ContextSource.php 4.2 KB

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