(bug 24620) Add types to LogFormatter
authorjan <jan@jans-seite.de>
Mon, 22 Oct 2012 09:00:15 +0000 (11:00 +0200)
committerGerrit Code Review <gerrit@wikimedia.org>
Sun, 9 Dec 2012 23:50:41 +0000 (23:50 +0000)
This change adds types to LogFormatter like "msg" so log values
(paramaters for the log message) can be formated as e.g. a message in
user or content language.

If you want to add another type feel free to suggest it in your review.

Change-Id: I6a56c2041bcbce292cff485e706db02305aab01c

RELEASE-NOTES-1.21
includes/logging/LogFormatter.php

index 87a0058..9ff57b6 100644 (file)
@@ -60,6 +60,7 @@ production.
 * Debug message emitted by wfDebugLog() will now be prefixed with the group
   name when its logged to the default log file. That is the case whenever the
   group has no key in wgDebugLogGroups, that will help triage the default log.
+* (bug 24620) Add types to LogFormatter
 
 === Bug fixes in 1.21 ===
 * (bug 40353) SpecialDoubleRedirect should support interwiki redirects.
index 8c1e294..f049126 100644 (file)
@@ -403,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.
@@ -446,6 +446,78 @@ 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.