Add SVG versions of enhanced recent changes collapse/show arrows
[lhc/web/wiklou.git] / includes / Status.php
index 7ec1b0f..dc0bae0 100644 (file)
  * An operation which is not OK should have errors so that the user can be
  * informed as to what went wrong. Calling the fatal() function sets an error
  * message and simultaneously switches off the OK flag.
+ *
+ * The recommended pattern for Status objects is to return a Status object
+ * unconditionally, i.e. both on success and on failure -- so that the
+ * developer of the calling code is reminded that the function can fail, and
+ * so that a lack of error-handling will be explicit.
  */
 class Status {
-       var $ok = true;
-       var $value;
+       public $ok = true;
+       public $value;
 
        /** Counters for batch operations */
-       public $successCount = 0, $failCount = 0;
+       public $successCount = 0;
+       public $failCount = 0;
+
        /** Array to indicate which items of the batch operations were successful */
        public $success = array();
 
-       /*semi-private*/ var $errors = array();
-       /*semi-private*/ var $cleanCallback = false;
+       public $errors = array();
+       public $cleanCallback = false;
 
        /**
         * Factory function for fatal errors
@@ -76,7 +83,7 @@ class Status {
         * @param $ok Boolean: whether the operation completed
         * @param $value Mixed
         */
-       function setResult( $ok, $value = null ) {
+       public function setResult( $ok, $value = null ) {
                $this->ok = $ok;
                $this->value = $value;
        }
@@ -87,7 +94,7 @@ class Status {
         *
         * @return Boolean
         */
-       function isGood() {
+       public function isGood() {
                return $this->ok && !$this->errors;
        }
 
@@ -96,7 +103,7 @@ class Status {
         *
         * @return Boolean
         */
-       function isOK() {
+       public function isOK() {
                return $this->ok;
        }
 
@@ -105,7 +112,7 @@ class Status {
         *
         * @param string|Message $message message name or object
         */
-       function warning( $message /*, parameters... */ ) {
+       public function warning( $message /*, parameters... */ ) {
                $params = array_slice( func_get_args(), 1 );
                $this->errors[] = array(
                        'type' => 'warning',
@@ -119,7 +126,7 @@ class Status {
         *
         * @param string|Message $message message name or object
         */
-       function error( $message /*, parameters... */ ) {
+       public function error( $message /*, parameters... */ ) {
                $params = array_slice( func_get_args(), 1 );
                $this->errors[] = array(
                        'type' => 'error',
@@ -133,7 +140,7 @@ class Status {
         *
         * @param string|Message $message message name or object
         */
-       function fatal( $message /*, parameters... */ ) {
+       public function fatal( $message /*, parameters... */ ) {
                $params = array_slice( func_get_args(), 1 );
                $this->errors[] = array(
                        'type' => 'error',
@@ -145,7 +152,7 @@ class Status {
        /**
         * Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
         */
-       function __wakeup() {
+       public function __wakeup() {
                $this->cleanCallback = false;
        }
 
@@ -172,7 +179,7 @@ class Status {
         * @param string $longContext a long enclosing context message name, for a list
         * @return String
         */
-       function getWikiText( $shortContext = false, $longContext = false ) {
+       public function getWikiText( $shortContext = false, $longContext = false ) {
                if ( count( $this->errors ) == 0 ) {
                        if ( $this->ok ) {
                                $this->fatal( 'internalerror_info',
@@ -183,15 +190,18 @@ class Status {
                        }
                }
                if ( count( $this->errors ) == 1 ) {
-                       $s = $this->getErrorMessage( $this->errors[0] );
+                       $s = $this->getErrorMessage( $this->errors[0] )->plain();
                        if ( $shortContext ) {
                                $s = wfMessage( $shortContext, $s )->plain();
                        } elseif ( $longContext ) {
                                $s = wfMessage( $longContext, "* $s\n" )->plain();
                        }
                } else {
-                       $s = '* ' . implode( "\n* ",
-                               $this->getErrorMessageArray( $this->errors ) ) . "\n";
+                       $errors = $this->getErrorMessageArray( $this->errors );
+                       foreach ( $errors as &$error ) {
+                               $error = $error->plain();
+                       }
+                       $s = '* ' . implode( "\n* ", $errors ) . "\n";
                        if ( $longContext ) {
                                $s = wfMessage( $longContext, $s )->plain();
                        } elseif ( $shortContext ) {
@@ -201,6 +211,56 @@ class Status {
                return $s;
        }
 
+       /**
+        * Get the error list as a Message object
+        *
+        * @param string $shortContext a short enclosing context message name, to
+        *        be used when there is a single error
+        * @param string $longContext a long enclosing context message name, for a list
+        * @return Message
+        */
+       public function getMessage( $shortContext = false, $longContext = false ) {
+               if ( count( $this->errors ) == 0 ) {
+                       if ( $this->ok ) {
+                               $this->fatal( 'internalerror_info',
+                                       __METHOD__ . " called for a good result, this is incorrect\n" );
+                       } else {
+                               $this->fatal( 'internalerror_info',
+                                       __METHOD__ . ": Invalid result object: no error text but not OK\n" );
+                       }
+               }
+               if ( count( $this->errors ) == 1 ) {
+                       $s = $this->getErrorMessage( $this->errors[0] );
+                       if ( $shortContext ) {
+                               $s = wfMessage( $shortContext, $s );
+                       } elseif ( $longContext ) {
+                               $wrapper = new RawMessage( "* \$1\n" );
+                               $wrapper->params( $s )->parse();
+                               $s = wfMessage( $longContext, $wrapper );
+                       }
+               } else {
+                       $msgs = $this->getErrorMessageArray( $this->errors );
+                       $msgCount = count( $msgs );
+
+                       if ( $shortContext ) {
+                               $msgCount++;
+                       }
+
+                       $wrapper = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) );
+                       $s = $wrapper->params( $msgs )->parse();
+
+                       if ( $longContext ) {
+                               $s = wfMessage( $longContext, $wrapper );
+                       } elseif ( $shortContext ) {
+                               $wrapper = new RawMessage( "\n\$1\n", $wrapper );
+                               $wrapper->parse();
+                               $s = wfMessage( $shortContext, $wrapper );
+                       }
+               }
+
+               return $s;
+       }
+
        /**
         * Return the message for a single error.
         * @param $error Mixed With an array & two values keyed by
@@ -225,7 +285,7 @@ class Status {
                } else {
                        $msg = wfMessage( $error );
                }
-               return $msg->plain();
+               return $msg;
        }
 
        /**
@@ -256,7 +316,7 @@ class Status {
         * @param $other Status Other Status object
         * @param $overwriteValue Boolean: whether to override the "value" member
         */
-       function merge( $other, $overwriteValue = false ) {
+       public function merge( $other, $overwriteValue = false ) {
                $this->errors = array_merge( $this->errors, $other->errors );
                $this->ok = $this->ok && $other->ok;
                if ( $overwriteValue ) {
@@ -272,7 +332,7 @@ class Status {
         * @return array A list in which each entry is an array with a message key as its first element.
         *         The remaining array elements are the message parameters.
         */
-       function getErrorsArray() {
+       public function getErrorsArray() {
                return $this->getStatusArray( "error" );
        }
 
@@ -282,14 +342,13 @@ class Status {
         * @return array A list in which each entry is an array with a message key as its first element.
         *         The remaining array elements are the message parameters.
         */
-       function getWarningsArray() {
+       public function getWarningsArray() {
                return $this->getStatusArray( "warning" );
        }
 
        /**
         * Returns a list of status messages of the given type
         * @param $type String
-        *
         * @return Array
         */
        protected function getStatusArray( $type ) {
@@ -297,7 +356,10 @@ class Status {
                foreach ( $this->errors as $error ) {
                        if ( $error['type'] === $type ) {
                                if ( $error['message'] instanceof Message ) {
-                                       $result[] = array_merge( array( $error['message']->getKey() ), $error['message']->getParams() );
+                                       $result[] = array_merge(
+                                               array( $error['message']->getKey() ),
+                                               $error['message']->getParams()
+                                       );
                                } elseif ( $error['params'] ) {
                                        $result[] = array_merge( array( $error['message'] ), $error['params'] );
                                } else {
@@ -305,6 +367,7 @@ class Status {
                                }
                        }
                }
+
                return $result;
        }
 
@@ -335,7 +398,7 @@ class Status {
         * @param string $msg message name
         * @return Boolean
         */
-       function hasMessage( $msg ) {
+       public function hasMessage( $msg ) {
                foreach ( $this->errors as $error ) {
                        if ( $error['message'] === $msg ) {
                                return true;
@@ -355,7 +418,7 @@ class Status {
         * @param $dest Message|String: Replacement message key or object
         * @return bool Return true if the replacement was done, false otherwise.
         */
-       function replaceMessage( $source, $dest ) {
+       public function replaceMessage( $source, $dest ) {
                $replaced = false;
                foreach ( $this->errors as $index => $error ) {
                        if ( $error['message'] === $source ) {
@@ -366,15 +429,6 @@ class Status {
                return $replaced;
        }
 
-       /**
-        * Backward compatibility function for WikiError -> Status migration
-        *
-        * @return String
-        */
-       public function getMessage() {
-               return $this->getWikiText();
-       }
-
        /**
         * @return mixed
         */