StubObject.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Class to implement stub globals, which are globals that delay loading the
  4. * their associated module code by deferring initialisation until the first
  5. * method call.
  6. *
  7. * Note on unstub loops:
  8. *
  9. * Unstub loops (infinite recursion) sometimes occur when a constructor calls
  10. * another function, and the other function calls some method of the stub. The
  11. * best way to avoid this is to make constructors as lightweight as possible,
  12. * deferring any initialisation which depends on other modules. As a last
  13. * resort, you can use StubObject::isRealObject() to break the loop, but as a
  14. * general rule, the stub object mechanism should be transparent, and code
  15. * which refers to it should be kept to a minimum.
  16. */
  17. class StubObject {
  18. var $mGlobal, $mClass, $mParams;
  19. /**
  20. * Constructor.
  21. *
  22. * @param String $global name of the global variable.
  23. * @param String $class name of the class of the real object.
  24. * @param Array $param array of parameters to pass to contructor of the real
  25. * object.
  26. */
  27. function __construct( $global = null, $class = null, $params = array() ) {
  28. $this->mGlobal = $global;
  29. $this->mClass = $class;
  30. $this->mParams = $params;
  31. }
  32. /**
  33. * Returns a bool value whetever $obj is a stub object. Can be used to break
  34. * a infinite loop when unstubbing an object.
  35. *
  36. * @param Object $obj object to check.
  37. * @return bool true if $obj is not an instance of StubObject class.
  38. */
  39. static function isRealObject( $obj ) {
  40. return is_object( $obj ) && !($obj instanceof StubObject);
  41. }
  42. /**
  43. * Function called if any function exists with that name in this object.
  44. * It is used to unstub the object. Only used internally, PHP will call
  45. * self::__call() function and that function will call this function.
  46. * This function will also call the function with the same name in the real
  47. * object.
  48. *
  49. * @param String $name name of the function called.
  50. * @param Array $args array of arguments.
  51. */
  52. function _call( $name, $args ) {
  53. $this->_unstub( $name, 5 );
  54. return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
  55. }
  56. /**
  57. * Create a new object to replace this stub object.
  58. */
  59. function _newObject() {
  60. return wfCreateObject( $this->mClass, $this->mParams );
  61. }
  62. /**
  63. * Function called by PHP if no function with that name exists in this
  64. * object.
  65. *
  66. * @param String $name name of the function called
  67. * @param Array $args array of arguments
  68. */
  69. function __call( $name, $args ) {
  70. return $this->_call( $name, $args );
  71. }
  72. /**
  73. * This function creates a new object of the real class and replace it in
  74. * the global variable.
  75. * This is public, for the convenience of external callers wishing to access
  76. * properties, e.g. eval.php
  77. *
  78. * @param String $name name of the method called in this object.
  79. * @param Integer $level level to go in the stact trace to get the function
  80. * who called this function.
  81. */
  82. function _unstub( $name = '_unstub', $level = 2 ) {
  83. static $recursionLevel = 0;
  84. if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
  85. $fname = __METHOD__.'-'.$this->mGlobal;
  86. wfProfileIn( $fname );
  87. $caller = wfGetCaller( $level );
  88. if ( ++$recursionLevel > 2 ) {
  89. throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
  90. }
  91. wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
  92. $GLOBALS[$this->mGlobal] = $this->_newObject();
  93. --$recursionLevel;
  94. wfProfileOut( $fname );
  95. }
  96. }
  97. }
  98. /**
  99. * Stub object for the content language of this wiki. This object have to be in
  100. * $wgContLang global.
  101. */
  102. class StubContLang extends StubObject {
  103. function __construct() {
  104. parent::__construct( 'wgContLang' );
  105. }
  106. function __call( $name, $args ) {
  107. return $this->_call( $name, $args );
  108. }
  109. function _newObject() {
  110. global $wgContLanguageCode;
  111. $obj = Language::factory( $wgContLanguageCode );
  112. $obj->initEncoding();
  113. $obj->initContLang();
  114. return $obj;
  115. }
  116. }
  117. /**
  118. * Stub object for the user language. It depends of the user preferences and
  119. * "uselang" parameter that can be passed to index.php. This object have to be
  120. * in $wgLang global.
  121. */
  122. class StubUserLang extends StubObject {
  123. function __construct() {
  124. parent::__construct( 'wgLang' );
  125. }
  126. function __call( $name, $args ) {
  127. return $this->_call( $name, $args );
  128. }
  129. function _newObject() {
  130. global $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
  131. $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
  132. // if variant is explicitely selected, use it instead the one from wgUser
  133. // see bug #7605
  134. if( $wgContLang->hasVariants() && in_array($code, $wgContLang->getVariants()) ){
  135. $variant = $wgContLang->getPreferredVariant();
  136. if( $variant != $wgContLanguageCode )
  137. $code = $variant;
  138. }
  139. # Validate $code
  140. if( empty( $code ) || !preg_match( '/^[a-z-]+$/', $code ) || ( $code === 'qqq' ) ) {
  141. wfDebug( "Invalid user language code\n" );
  142. $code = $wgContLanguageCode;
  143. }
  144. if( $code === $wgContLanguageCode ) {
  145. return $wgContLang;
  146. } else {
  147. $obj = Language::factory( $code );
  148. return $obj;
  149. }
  150. }
  151. }
  152. /**
  153. * Stub object for the user. The initialisation of the will depend of
  154. * $wgCommandLineMode. If it's true, it will be an anonymous user and if it's
  155. * false, the user will be loaded from credidentails provided by cookies. This
  156. * object have to be in $wgUser global.
  157. */
  158. class StubUser extends StubObject {
  159. function __construct() {
  160. parent::__construct( 'wgUser' );
  161. }
  162. function __call( $name, $args ) {
  163. return $this->_call( $name, $args );
  164. }
  165. function _newObject() {
  166. global $wgCommandLineMode;
  167. if( $wgCommandLineMode ) {
  168. $user = new User;
  169. } else {
  170. $user = User::newFromSession();
  171. }
  172. return $user;
  173. }
  174. }