(bug 24620) Add tests for LogFormatter
[lhc/web/wiklou.git] / includes / logging / LogFormatter.php
index 7586bb6..1d8adf9 100644 (file)
@@ -293,6 +293,28 @@ class LogFormatter {
                                }
                                break;
 
+                       case 'rights':
+                               if ( count( $parameters['4::oldgroups'] ) ) {
+                                       $oldgroups = implode( ', ', $parameters['4::oldgroups'] );
+                               } else {
+                                       $oldgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
+                               }
+                               if ( count( $parameters['5::newgroups'] ) ) {
+                                       $newgroups = implode( ', ', $parameters['5::newgroups'] );
+                               } else {
+                                       $newgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
+                               }
+                               switch( $entry->getSubtype() ) {
+                                       case 'rights':
+                                               $text = wfMessage( 'rightslogentry' )
+                                                       ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
+                                               break;
+                                       case 'autopromote':
+                                               $text = wfMessage( 'rightslogentry-autopromote' )
+                                                       ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
+                                               break;
+                               }
+                               break;
 
                        // case 'suppress' --private log -- aaron  (sign your messages so we know who to blame in a few years :-D)
                        // default:
@@ -381,7 +403,7 @@ class LogFormatter {
                foreach ( $entry->getParameters() as $key => $value ) {
                        if ( strpos( $key, ':' ) === false ) continue;
                        list( $index, $type, $name ) = explode( ':', $key, 3 );
-                       $params[$index - 1] = $value;
+                       $params[$index - 1] = $this->formatParameterValue( $type, $value );
                }
 
                /* Message class doesn't like non consecutive numbering.
@@ -408,7 +430,7 @@ class LogFormatter {
         *  - 3: target page with premade link
         * @return array
         */
-       protected function getMessageParameters() {
+       public function getMessageParameters() {
                if ( isset( $this->parsedParameters ) ) {
                        return $this->parsedParameters;
                }
@@ -424,11 +446,84 @@ class LogFormatter {
                return $this->parsedParameters = $params;
        }
 
+       /**
+        * Formats parameters values dependent to their type
+        * @param $type string The type of the value.
+        *   Valid are currently:
+        *     * - (empty) or plain: The value is returned as-is
+        *     * raw: The value will be added to the log message
+        *            as raw parameter (e.g. no escaping)
+        *            Use this only if there is no other working
+        *            type like user-link or title-link
+        *     * msg: The value is a message-key, the output is
+        *            the message in user language
+        *     * msg-content: The value is a message-key, the output
+        *                    is the message in content language
+        *     * user: The value is a user name, e.g. for GENDER
+        *     * user-link: The value is a user name, returns a
+        *                  link for the user
+        *     * title: The value is a page title,
+        *              returns name of page
+        *     * title-link: The value is a page title,
+        *                   returns link to this page
+        *     * number: Format value as number
+        * @param $value string The parameter value that should
+        *                      be formated
+        * @return string or Message::numParam or Message::rawParam
+        *         Formated value
+        * @since 1.21
+        */
+       protected function formatParameterValue( $type, $value ) {
+               $saveLinkFlood = $this->linkFlood;
+
+               switch( strtolower( trim( $type ) ) ) {
+                       case 'raw':
+                               $value = Message::rawParam( $value );
+                               break;
+                       case 'msg':
+                               $value = $this->msg( $value )->text();
+                               break;
+                       case 'msg-content':
+                               $value = $this->msg( $value )->inContentLanguage()->text();
+                               break;
+                       case 'number':
+                               $value = Message::numParam( $value );
+                               break;
+                       case 'user':
+                               $user = User::newFromName( $value );
+                               $value = $user->getName();
+                               break;
+                       case 'user-link':
+                               $this->setShowUserToolLinks( false );
+
+                               $user = User::newFromName( $value );
+                               $value = Message::rawParam( $this->makeUserLink( $user ) );
+
+                               $this->setShowUserToolLinks( $saveLinkFlood );
+                               break;
+                       case 'title':
+                               $title = Title::newFromText( $value );
+                               $value = $title->getPrefixedText();
+                               break;
+                       case 'title-link':
+                               $title = Title::newFromText( $value );
+                               $value = Message::rawParam( $this->makePageLink( $title ) );
+                               break;
+                       case 'plain':
+                               // Plain text, nothing to do
+                       default:
+                               // Catch other types and use the old behavior (return as-is)
+               }
+
+               return $value;
+       }
+
        /**
         * Helper to make a link to the page, taking the plaintext
         * value in consideration.
         * @param $title Title the page
         * @param $parameters array query parameters
+        * @throws MWException
         * @return String
         */
        protected function makePageLink( Title $title = null, $parameters = array() ) {
@@ -725,7 +820,7 @@ class MoveLogFormatter extends LogFormatter {
                return $key;
        }
 
-       protected function getMessageParameters() {
+       public function getMessageParameters() {
                $params = parent::getMessageParameters();
                $oldname = $this->makePageLink( $this->entry->getTarget(), array( 'redirect' => 'no' ) );
                $newname = $this->makePageLink( Title::newFromText( $params[3] ) );
@@ -778,7 +873,7 @@ class DeleteLogFormatter extends LogFormatter {
                return $key;
        }
 
-       protected function getMessageParameters() {
+       public function getMessageParameters() {
                if ( isset( $this->parsedParametersDeleteLog ) ) {
                        return $this->parsedParametersDeleteLog;
                }
@@ -947,7 +1042,7 @@ class PatrolLogFormatter extends LogFormatter {
                return $key;
        }
 
-       protected function getMessageParameters() {
+       public function getMessageParameters() {
                $params = parent::getMessageParameters();
 
                $target = $this->entry->getTarget();
@@ -976,7 +1071,7 @@ class PatrolLogFormatter extends LogFormatter {
  * @since 1.19
  */
 class NewUsersLogFormatter extends LogFormatter {
-       protected function getMessageParameters() {
+       public function getMessageParameters() {
                $params = parent::getMessageParameters();
                if ( $this->entry->getSubtype() === 'create2' ) {
                        if ( isset( $params[3] ) ) {
@@ -1008,3 +1103,88 @@ class NewUsersLogFormatter extends LogFormatter {
                return array();
        }
 }
+
+/**
+ * This class formats rights log entries.
+ * @since 1.21
+ */
+class RightsLogFormatter extends LogFormatter {
+       protected function makePageLink( Title $title = null, $parameters = array() ) {
+               global $wgContLang, $wgUserrightsInterwikiDelimiter;
+
+               if ( !$this->plaintext ) {
+                       $text = $wgContLang->ucfirst( $title->getText() );
+                       $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
+
+                       if ( count( $parts ) === 2 ) {
+                               $titleLink = WikiMap::foreignUserLink( $parts[1], $parts[0],
+                                       htmlspecialchars( $title->getPrefixedText() ) );
+
+                               if ( $titleLink !== false ) {
+                                       return $titleLink;
+                               }
+                       }
+               }
+
+               return parent::makePageLink( $title, $parameters );
+       }
+
+       protected function getMessageKey() {
+               $key = parent::getMessageKey();
+               $params = $this->getMessageParameters();
+               if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
+                       $key .= '-legacy';
+               }
+               return $key;
+       }
+
+       public function getMessageParameters() {
+               $params = parent::getMessageParameters();
+
+               // Really old entries
+               if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
+                       return $params;
+               }
+
+               $oldGroups = $params[3];
+               $newGroups = $params[4];
+
+               // Less old entries
+               if ( $oldGroups === '' ) {
+                       $oldGroups = array();
+               } elseif ( is_string( $oldGroups ) ) {
+                       $oldGroups = array_map( 'trim', explode( ',', $oldGroups ) );
+               }
+               if ( $newGroups === '' ) {
+                       $newGroups = array();
+               } elseif ( is_string( $newGroups ) ) {
+                       $newGroups = array_map( 'trim', explode( ',', $newGroups ) );
+               }
+
+               $userName = $this->entry->getTarget()->getText();
+               if ( !$this->plaintext && count( $oldGroups ) ) {
+                       foreach ( $oldGroups as &$group ) {
+                               $group = User::getGroupMember( $group, $userName );
+                       }
+               }
+               if ( !$this->plaintext && count( $newGroups ) ) {
+                       foreach ( $newGroups as &$group ) {
+                               $group = User::getGroupMember( $group, $userName );
+                       }
+               }
+
+               $lang = $this->context->getLanguage();
+               if ( count( $oldGroups ) ) {
+                       $params[3] = $lang->listToText( $oldGroups );
+               } else {
+                       $params[3] = $this->msg( 'rightsnone' )->text();
+               }
+               if ( count( $newGroups ) ) {
+                       $params[4] = $lang->listToText( $newGroups );
+               } else {
+                       $params[4] = $this->msg( 'rightsnone' )->text();
+               }
+
+               return $params;
+       }
+}