Merge "includes/libs: Replace implicit Bugzilla bug numbers with Phab ones"
[lhc/web/wiklou.git] / includes / libs / HttpStatus.php
index 4f626b2..27f8728 100644 (file)
 class HttpStatus {
 
        /**
-        * Get the message associated with HTTP response code $code
+        * Get the message associated with an HTTP response status code
         *
-        * Replace OutputPage::getStatusMessage( $code )
-        *
-        * @param $code Integer: status code
-        * @return String or null: message or null if $code is not in the list of
-        *         messages
+        * @param int $code Status code
+        * @return string|null Message, or null if $code is not known
         */
        public static function getMessage( $code ) {
-               static $statusMessage = array(
+               static $statusMessage = [
                        100 => 'Continue',
                        101 => 'Switching Protocols',
                        102 => 'Processing',
@@ -75,15 +72,44 @@ class HttpStatus {
                        422 => 'Unprocessable Entity',
                        423 => 'Locked',
                        424 => 'Failed Dependency',
+                       428 => 'Precondition Required',
+                       429 => 'Too Many Requests',
+                       431 => 'Request Header Fields Too Large',
                        500 => 'Internal Server Error',
                        501 => 'Not Implemented',
                        502 => 'Bad Gateway',
                        503 => 'Service Unavailable',
                        504 => 'Gateway Timeout',
                        505 => 'HTTP Version Not Supported',
-                       507 => 'Insufficient Storage'
-               );
+                       507 => 'Insufficient Storage',
+                       511 => 'Network Authentication Required',
+               ];
                return isset( $statusMessage[$code] ) ? $statusMessage[$code] : null;
        }
 
+       /**
+        * Output an HTTP status code header
+        *
+        * @since 1.26
+        * @param int $code Status code
+        */
+       public static function header( $code ) {
+               static $version = null;
+               $message = self::getMessage( $code );
+               if ( $message === null ) {
+                       trigger_error( "Unknown HTTP status code $code", E_USER_WARNING );
+                       return false;
+               }
+
+               MediaWiki\HeaderCallback::warnIfHeadersSent();
+               if ( $version === null ) {
+                       $version = isset( $_SERVER['SERVER_PROTOCOL'] ) &&
+                               $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.0' ?
+                                       '1.0' :
+                                       '1.1';
+               }
+
+               header( "HTTP/$version $code $message" );
+       }
+
 }