A service for read-only mode
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 5343248..8b857ea 100644 (file)
@@ -28,6 +28,7 @@ use Liuggio\StatsdClient\Sender\SocketSender;
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\Session\SessionManager;
 use Wikimedia\ScopedCallback;
+use Wikimedia\Rdbms\DBReplicationWaitError;
 
 // Hide compatibility functions from Doxygen
 /// @cond
@@ -222,18 +223,18 @@ function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
 /**
  * Merge arrays in the style of getUserPermissionsErrors, with duplicate removal
  * e.g.
- *     wfMergeErrorArrays(
- *             [ [ 'x' ] ],
- *             [ [ 'x', '2' ] ],
- *             [ [ 'x' ] ],
- *             [ [ 'y' ] ]
- *     );
+ *     wfMergeErrorArrays(
+ *       [ [ 'x' ] ],
+ *       [ [ 'x', '2' ] ],
+ *       [ [ 'x' ] ],
+ *       [ [ 'y' ] ]
+ *     );
  * returns:
- *             [
- *             [ 'x', '2' ],
- *             [ 'x' ],
- *             [ 'y' ]
- *     ]
+ *     [
+ *       [ 'x', '2' ],
+ *       [ 'x' ],
+ *       [ 'y' ]
+ *     ]
  *
  * @param array $array1,...
  * @return array
@@ -1272,7 +1273,8 @@ function wfIncrStats( $key, $count = 1 ) {
  * @return bool
  */
 function wfReadOnly() {
-       return wfReadOnlyReason() !== false;
+       return \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
+               ->isReadOnly();
 }
 
 /**
@@ -1284,19 +1286,8 @@ function wfReadOnly() {
  * @return string|bool String when in read-only mode; false otherwise
  */
 function wfReadOnlyReason() {
-       $readOnly = wfConfiguredReadOnlyReason();
-       if ( $readOnly !== false ) {
-               return $readOnly;
-       }
-
-       static $lbReadOnly = null;
-       if ( $lbReadOnly === null ) {
-               // Callers use this method to be aware that data presented to a user
-               // may be very stale and thus allowing submissions can be problematic.
-               $lbReadOnly = wfGetLB()->getReadOnlyReason();
-       }
-
-       return $lbReadOnly;
+       return \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
+               ->getReason();
 }
 
 /**
@@ -1306,18 +1297,8 @@ function wfReadOnlyReason() {
  * @since 1.27
  */
 function wfConfiguredReadOnlyReason() {
-       global $wgReadOnly, $wgReadOnlyFile;
-
-       if ( $wgReadOnly === null ) {
-               // Set $wgReadOnly for faster access next time
-               if ( is_file( $wgReadOnlyFile ) && filesize( $wgReadOnlyFile ) > 0 ) {
-                       $wgReadOnly = file_get_contents( $wgReadOnlyFile );
-               } else {
-                       $wgReadOnly = false;
-               }
-       }
-
-       return $wgReadOnly;
+       return \MediaWiki\MediaWikiServices::getInstance()->getConfiguredReadOnlyMode()
+               ->getReason();
 }
 
 /**
@@ -1787,6 +1768,7 @@ function wfHttpError( $code, $label, $desc ) {
                $wgOut->sendCacheControl();
        }
 
+       MediaWiki\HeaderCallback::warnIfHeadersSent();
        header( 'Content-type: text/html; charset=utf-8' );
        print '<!DOCTYPE html>' .
                '<html><head><title>' .
@@ -2572,8 +2554,8 @@ function wfInitShellLocale() {
  * @param string $script MediaWiki cli script path
  * @param array $parameters Arguments and options to the script
  * @param array $options Associative array of options:
- *             'php': The path to the php executable
- *             'wrapper': Path to a PHP wrapper to handle the maintenance script
+ *     'php': The path to the php executable
+ *     'wrapper': Path to a PHP wrapper to handle the maintenance script
  * @return string
  */
 function wfShellWikiCmd( $script, array $parameters = [], array $options = [] ) {
@@ -3071,7 +3053,7 @@ function wfSplitWikiID( $wiki ) {
  * @todo Replace calls to wfGetDB with calls to LoadBalancer::getConnection()
  *       on an injected instance of LoadBalancer.
  *
- * @return Database
+ * @return \Wikimedia\Rdbms\Database
  */
 function wfGetDB( $db, $groups = [], $wiki = false ) {
        return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
@@ -3084,7 +3066,7 @@ function wfGetDB( $db, $groups = [], $wiki = false ) {
  *              or MediaWikiServices::getDBLoadBalancerFactory() instead.
  *
  * @param string|bool $wiki Wiki ID, or false for the current wiki
- * @return LoadBalancer
+ * @return \Wikimedia\Rdbms\LoadBalancer
  */
 function wfGetLB( $wiki = false ) {
        if ( $wiki === false ) {
@@ -3100,7 +3082,7 @@ function wfGetLB( $wiki = false ) {
  *
  * @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead.
  *
- * @return LBFactory
+ * @return \Wikimedia\Rdbms\LBFactory
  */
 function wfGetLBFactory() {
        return \MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();