Anomie has uploaded a new change for review.
https://gerrit.wikimedia.org/r/183315
Change subject: API: Move parameter formatting into LogFormatter
......................................................................
API: Move parameter formatting into LogFormatter
This allows for extensions to format their log entry parameters, and
keeps the code for formatting API log entry parameters in the same place
as for other formatting.
This also takes the opportunity to rearrange the output format slightly
to avoid conflicts like what's happening in T73020.
Bug: T35235
Bug: T73020
Change-Id: I6846ce09322eb404c506b5a51780a44ce9279fe2
---
M RELEASE-NOTES-1.25
M autoload.php
M includes/DefaultSettings.php
M includes/api/ApiQueryLogEvents.php
M includes/api/ApiQueryRecentChanges.php
M includes/api/ApiQueryWatchlist.php
A includes/logging/BlockLogFormatter.php
M includes/logging/DeleteLogFormatter.php
M includes/logging/LogEntry.php
M includes/logging/LogFormatter.php
M includes/logging/MergeLogFormatter.php
M includes/logging/MoveLogFormatter.php
M includes/logging/PatrolLogFormatter.php
M includes/logging/RightsLogFormatter.php
A includes/logging/UploadLogFormatter.php
M includes/revisiondelete/RevDelLogItem.php
M tests/phpunit/includes/logging/LogFormatterTest.php
17 files changed, 489 insertions(+), 138 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/15/183315/1
diff --git a/RELEASE-NOTES-1.25 b/RELEASE-NOTES-1.25
index 5574350..ef28f7a 100644
--- a/RELEASE-NOTES-1.25
+++ b/RELEASE-NOTES-1.25
@@ -75,6 +75,9 @@
on Special:Version in their own section. Extensions or skins that are
installed via composer will not be shown in this section as it is assumed
they will add the proper credits to the skins or extensions section.
+* (T35235) LogFormatter subclasses are now responsible for formatting the
+ parameters for API log event output. Extensions should implement the new
+ getParametersForApi() method in their log formatters.
==== External libraries ====
* MediaWiki now requires certain external libraries to be installed. In the
past
@@ -171,6 +174,18 @@
* (T78690) list=allimages now accepts multiple pipe-separated values
for the 'aimime' parameter.
* Added format=json2, still experimental.
+* (T73020) Log event details are now always under a 'params' subkey for
+ list=logevents, and a 'logparams' subkey for list=watchlist and
+ list=recentchanges.
+* Log event details are changing formatting:
+** block events now report flags as an array rather than as a comma-separated
+ list.
+** patrol events now report the 'auto' flag as a boolean (absent/empty string
+ for BC formats) rather than as an integer.
+** rights events now report the old and new group lists as arrays rather than
+ as comma-separated lists.
+** merge events use new-style formatting.
+** delete/event and delete/revision events use new-style formatting.
=== Action API internal changes in 1.25 ===
* ApiHelp has been rewritten to support i18n and paginated HTML output.
@@ -209,6 +224,8 @@
* ApiResult/ApiFormatBase "raw mode" is deprecated.
* ApiFormatXml now assumes defaults and so on instead of throwing errors when
metadata isn't set.
+* (T35235) LogFormatter subclasses are now responsible for formatting log event
+ parameters for the API.
* The following methods have been deprecated and may be removed in a future
release:
* ApiBase::getDescription
@@ -240,6 +257,7 @@
* ApiResult::setContinueParam
* ApiResult::setGeneratorContinueParam
* ApiQueryImageInfo::getPropertyDescriptions
+ * ApiQueryLogEvents::addLogParams
* The following classes have been deprecated and may be removed in a future
release:
* ApiQueryDeletedrevs
diff --git a/autoload.php b/autoload.php
index b36153a..7cce34a 100644
--- a/autoload.php
+++ b/autoload.php
@@ -167,6 +167,7 @@
'Blob' => __DIR__ . '/includes/db/DatabaseUtility.php',
'Block' => __DIR__ . '/includes/Block.php',
'BlockListPager' => __DIR__ . '/includes/specials/SpecialBlockList.php',
+ 'BlockLogFormatter' => __DIR__ .
'/includes/logging/BlockLogFormatter.php',
'BloomCache' => __DIR__ . '/includes/cache/bloom/BloomCache.php',
'BloomCacheRedis' => __DIR__ .
'/includes/cache/bloom/BloomCacheRedis.php',
'BloomFilterTitleHasLogs' => __DIR__ .
'/includes/cache/bloom/BloomFilters.php',
@@ -1224,6 +1225,7 @@
'UploadFromStash' => __DIR__ . '/includes/upload/UploadFromStash.php',
'UploadFromUrl' => __DIR__ . '/includes/upload/UploadFromUrl.php',
'UploadFromUrlJob' => __DIR__ .
'/includes/jobqueue/jobs/UploadFromUrlJob.php',
+ 'UploadLogFormatter' => __DIR__ .
'/includes/logging/UploadLogFormatter.php',
'UploadSourceAdapter' => __DIR__ . '/includes/Import.php',
'UploadSourceField' => __DIR__ . '/includes/specials/SpecialUpload.php',
'UploadStash' => __DIR__ . '/includes/upload/UploadStash.php',
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index 1884b5f..d65cd20 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -6654,12 +6654,17 @@
);
/**
- * The same as above, but here values are names of functions,
+ * The same as above, but here values are names of classes,
* not messages.
* @see LogPage::actionText
* @see LogFormatter
*/
$wgLogActionsHandlers = array(
+ 'block/block' => 'BlockLogFormatter',
+ 'block/unblock' => 'BlockLogFormatter',
+ 'block/reblock' => 'BlockLogFormatter',
+ 'suppress/block' => 'BlockLogFormatter',
+ 'suppress/reblock' => 'BlockLogFormatter',
'move/move' => 'MoveLogFormatter',
'move/move_redir' => 'MoveLogFormatter',
'delete/delete' => 'DeleteLogFormatter',
@@ -6672,9 +6677,9 @@
'patrol/patrol' => 'PatrolLogFormatter',
'rights/rights' => 'RightsLogFormatter',
'rights/autopromote' => 'RightsLogFormatter',
- 'upload/upload' => 'LogFormatter',
- 'upload/overwrite' => 'LogFormatter',
- 'upload/revert' => 'LogFormatter',
+ 'upload/upload' => 'UploadLogFormatter',
+ 'upload/overwrite' => 'UploadLogFormatter',
+ 'upload/revert' => 'UploadLogFormatter',
'merge/merge' => 'MergeLogFormatter',
);
diff --git a/includes/api/ApiQueryLogEvents.php
b/includes/api/ApiQueryLogEvents.php
index 0119c91..a2a5776 100644
--- a/includes/api/ApiQueryLogEvents.php
+++ b/includes/api/ApiQueryLogEvents.php
@@ -243,6 +243,7 @@
}
/**
+ * @deprecated since 1.25 Use LogFormatter::formatParametersForApi
instead
* @param ApiResult $result
* @param array $vals
* @param string $params
@@ -255,104 +256,14 @@
public static function addLogParams( $result, &$vals, $params, $type,
$action, $ts, $legacy = false
) {
- switch ( $type ) {
- case 'move':
- if ( $legacy ) {
- $targetKey = 0;
- $noredirKey = 1;
- } else {
- $targetKey = '4::target';
- $noredirKey = '5::noredir';
- }
+ wfDeprecated( __METHOD__, '1.25' );
- if ( isset( $params[$targetKey] ) ) {
- $title = Title::newFromText(
$params[$targetKey] );
- if ( $title ) {
- $vals2 = array();
- ApiQueryBase::addTitleInfo(
$vals2, $title, 'new_' );
- $vals[$type] = $vals2;
- }
- }
- if ( isset( $params[$noredirKey] ) &&
$params[$noredirKey] ) {
- $vals[$type]['suppressedredirect'] = '';
- }
- $params = null;
- break;
- case 'patrol':
- if ( $legacy ) {
- $cur = 0;
- $prev = 1;
- $auto = 2;
- } else {
- $cur = '4::curid';
- $prev = '5::previd';
- $auto = '6::auto';
- }
- $vals2 = array();
- $vals2['cur'] = $params[$cur];
- $vals2['prev'] = $params[$prev];
- $vals2['auto'] = $params[$auto];
- $vals[$type] = $vals2;
- $params = null;
- break;
- case 'rights':
- $vals2 = array();
- if ( $legacy ) {
- list( $vals2['old'], $vals2['new'] ) =
$params;
- } else {
- $vals2['new'] = implode( ', ',
$params['5::newgroups'] );
- $vals2['old'] = implode( ', ',
$params['4::oldgroups'] );
- }
- $vals[$type] = $vals2;
- $params = null;
- break;
- case 'block':
- if ( $action == 'unblock' ) {
- break;
- }
- $vals2 = array();
- list( $vals2['duration'], $vals2['flags'] ) =
$params;
-
- // Indefinite blocks have no expiry time
- if ( SpecialBlock::parseExpiryInput( $params[0]
) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
- $vals2['expiry'] = wfTimestamp(
TS_ISO_8601,
- strtotime( $params[0],
wfTimestamp( TS_UNIX, $ts ) ) );
- }
- $vals[$type] = $vals2;
- $params = null;
- break;
- case 'upload':
- if ( isset( $params['img_timestamp'] ) ) {
- $params['img_timestamp'] = wfTimestamp(
TS_ISO_8601, $params['img_timestamp'] );
- }
- break;
- case 'merge':
- // replace the named parameter with numbered
for backward compatibility
- if ( isset( $params['4::dest'] ) ) {
- $params[] = $params['4::dest'];
- unset( $params['4::dest'] );
- }
- if ( isset( $params['5::mergepoint'] ) ) {
- $params[] = $params['5::mergepoint'];
- unset( $params['5::mergepoint'] );
- }
- break;
- }
- if ( !is_null( $params ) ) {
- $logParams = array();
- // Keys like "4::paramname" can't be used for output so
we change them to "paramname"
- foreach ( $params as $key => $value ) {
- if ( strpos( $key, ':' ) === false ) {
- $logParams[$key] = $value;
- continue;
- }
- $logParam = explode( ':', $key, 3 );
- $logParams[$logParam[2]] = $value;
- }
- ApiResult::setIndexedTagName( $logParams, 'param' );
- ApiResult::setIndexedTagNameOnSubarrays( $logParams,
'param' );
- $vals = array_merge( $vals, $logParams );
- }
+ $entry = new ManualLogEntry( $type, $action );
+ $entry->setParameters( $params );
+ $entry->setTimestamp( $ts );
+ $entry->setLegacy( $legacy );
+ $formatter = LogFormatter::newFromEntry( $entry );
+ $vals['params'] = $formatter->formatParametersForApi();
return $vals;
}
@@ -385,15 +296,7 @@
$vals['logpage'] = intval(
$row->log_page );
}
if ( $this->fld_details && $row->log_params !==
'' ) {
- self::addLogParams(
- $this->getResult(),
- $vals,
- $logEntry->getParameters(),
- $logEntry->getType(),
- $logEntry->getSubtype(),
- $logEntry->getTimestamp(),
- $logEntry->isLegacy()
- );
+ $vals['params'] =
LogFormatter::newFromRow( $row )->formatParametersForApi();
}
}
}
diff --git a/includes/api/ApiQueryRecentChanges.php
b/includes/api/ApiQueryRecentChanges.php
index 7c5116c..1d8c993 100644
--- a/includes/api/ApiQueryRecentChanges.php
+++ b/includes/api/ApiQueryRecentChanges.php
@@ -534,15 +534,7 @@
$vals['logid'] = intval( $row->rc_logid );
$vals['logtype'] = $row->rc_log_type;
$vals['logaction'] = $row->rc_log_action;
- $logEntry = DatabaseLogEntry::newFromRow(
(array)$row );
- ApiQueryLogEvents::addLogParams(
- $this->getResult(),
- $vals,
- $logEntry->getParameters(),
- $logEntry->getType(),
- $logEntry->getSubtype(),
- $logEntry->getTimestamp()
- );
+ $vals['logparams'] = LogFormatter::newFromRow(
(array)$row )->formatParametersForApi();
}
}
diff --git a/includes/api/ApiQueryWatchlist.php
b/includes/api/ApiQueryWatchlist.php
index c258cfc..4efbb77 100644
--- a/includes/api/ApiQueryWatchlist.php
+++ b/includes/api/ApiQueryWatchlist.php
@@ -412,15 +412,7 @@
$vals['logid'] = intval( $row->rc_logid );
$vals['logtype'] = $row->rc_log_type;
$vals['logaction'] = $row->rc_log_action;
- $logEntry = DatabaseLogEntry::newFromRow(
(array)$row );
- ApiQueryLogEvents::addLogParams(
- $this->getResult(),
- $vals,
- $logEntry->getParameters(),
- $logEntry->getType(),
- $logEntry->getSubtype(),
- $logEntry->getTimestamp()
- );
+ $vals['logparams'] = LogFormatter::newFromRow(
(array)$row )->formatParametersForApi();
}
}
diff --git a/includes/logging/BlockLogFormatter.php
b/includes/logging/BlockLogFormatter.php
new file mode 100644
index 0000000..b3acfda
--- /dev/null
+++ b/includes/logging/BlockLogFormatter.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Formatter for block log entries.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
2.0 or later
+ * @since 1.25
+ */
+
+/**
+ * This class formats block log entries.
+ *
+ * @todo Fix this to properly extend LogFormatter
+ * @since 1.25
+ */
+class BlockLogFormatter extends LegacyLogFormatter {
+
+ protected function getParametersForApi() {
+ $entry = $this->entry;
+ $params = $entry->getParameters();
+
+ static $map = array(
+ '4::duration',
+ '5:array:flags',
+ '5::flags' => '5:array:flags',
+ );
+ foreach ( $map as $index => $key ) {
+ if ( isset( $params[$index] ) ) {
+ $params[$key] = $params[$index];
+ unset( $params[$index] );
+ }
+ }
+
+ if ( isset( $params['5:array:flags'] ) && !is_array(
$params['5:array:flags'] ) ) {
+ $params['5:array:flags'] = $params['5:array:flags'] ===
''
+ ? array()
+ : explode( ',', $params['5:array:flags'] );
+ }
+
+ if ( isset( $params['4::duration'] ) &&
+ SpecialBlock::parseExpiryInput( $params['4::duration']
) !== wfGetDB( DB_SLAVE )->getInfinity()
+ ) {
+ $ts = wfTimestamp( TS_UNIX, $entry->getTimestamp() );
+ $params[':timestamp:expiry'] = strtotime(
$params['4::duration'], $ts );
+ }
+
+ return $params;
+ }
+
+ public function formatParametersForApi() {
+ $ret = parent::formatParametersForApi();
+ if ( isset( $ret['flags'] ) ) {
+ ApiResult::setIndexedTagName( $ret['flags'], 'f' );
+ }
+ return $ret;
+ }
+
+}
diff --git a/includes/logging/DeleteLogFormatter.php
b/includes/logging/DeleteLogFormatter.php
index 8b30e9b..1b81828 100644
--- a/includes/logging/DeleteLogFormatter.php
+++ b/includes/logging/DeleteLogFormatter.php
@@ -209,4 +209,50 @@
return '';
}
}
+
+ protected function getParametersForApi() {
+ $entry = $this->entry;
+ $params = array();
+
+ $subtype = $this->entry->getSubtype();
+ if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
+ $rawParams = $entry->getParameters();
+ if ( $subtype === 'event' ) {
+ array_unshift( $rawParams, 'logging' );
+ }
+
+ static $knownTypes = array( 'logging', 'revision',
'oldimage', 'archive', 'filearchive' );
+ if ( in_array( $rawParams[0], $knownTypes ) ) {
+ $old = $this->parseBitField( $rawParams[2] );
+ $new = $this->parseBitField( $rawParams[3] );
+ $params = array(
+ '::type' => $rawParams[0],
+ ':array:ids' => explode( ',',
$rawParams[1] ),
+ ':assoc:old' => array( 'bitmask' =>
$old ),
+ ':assoc:new' => array( 'bitmask' =>
$new ),
+ );
+
+ static $fields = array(
+ Revision::DELETED_TEXT => 'content',
+ Revision::DELETED_COMMENT => 'comment',
+ Revision::DELETED_USER => 'user',
+ Revision::DELETED_RESTRICTED =>
'restricted',
+ );
+ foreach ( $fields as $bit => $key ) {
+ $params[':assoc:old'][$key] = (bool)(
$old & $bit );
+ $params[':assoc:new'][$key] = (bool)(
$new & $bit );
+ }
+ }
+ }
+
+ return $params;
+ }
+
+ public function formatParametersForApi() {
+ $ret = parent::formatParametersForApi();
+ if ( isset( $ret['ids'] ) ) {
+ ApiResult::setIndexedTagName( $ret['ids'], 'id' );
+ }
+ return $ret;
+ }
}
diff --git a/includes/logging/LogEntry.php b/includes/logging/LogEntry.php
index 46c5515..3f3a675 100644
--- a/includes/logging/LogEntry.php
+++ b/includes/logging/LogEntry.php
@@ -370,6 +370,9 @@
/** @var int ID of the log entry */
protected $id;
+ /** @var bool Whether this is a legacy log entry */
+ protected $legacy = false;
+
/**
* Constructor.
*
@@ -456,6 +459,16 @@
*/
public function setComment( $comment ) {
$this->comment = $comment;
+ }
+
+ /**
+ * Set the 'legacy' flag
+ *
+ * @since 1.25
+ * @param bool $legacy
+ */
+ public function setLegacy( $legacy = true ) {
+ $this->legacy = $legacy;
}
/**
@@ -616,6 +629,10 @@
return $this->parameters;
}
+ public function isLegacy() {
+ return $this->legacy;
+ }
+
/**
* @return User
*/
diff --git a/includes/logging/LogFormatter.php
b/includes/logging/LogFormatter.php
index c6fcdb0..09d2c0f 100644
--- a/includes/logging/LogFormatter.php
+++ b/includes/logging/LogFormatter.php
@@ -421,7 +421,9 @@
continue;
}
list( $index, $type, ) = explode( ':', $key, 3 );
- $params[$index - 1] = $this->formatParameterValue(
$type, $value );
+ if ( $index === (string)(int)$index ) {
+ $params[$index - 1] =
$this->formatParameterValue( $type, $value );
+ }
}
/* Message class doesn't like non consecutive numbering.
@@ -674,6 +676,120 @@
// problems with extensions
return $this->getMessageParameters();
}
+
+ /**
+ * Get the array of parameters, converted from legacy format if
necessary.
+ * @since 1.25
+ * @return array
+ */
+ protected function getParametersForApi() {
+ return $this->entry->getParameters();
+ }
+
+ /**
+ * Format parameters for API output
+ *
+ * The result array should generally map named keys to values. Index and
+ * type should be omitted, e.g. "4::foo" should be returned as "foo" in
the
+ * output. Values should generally be unformatted.
+ *
+ * Renames or removals of keys besides from the legacy numeric format to
+ * modern named style should be avoided. Any renames should be
announced to
+ * the mediawiki-api-announce mailing list.
+ *
+ * @since 1.25
+ * @return array
+ */
+ public function formatParametersForApi() {
+ $logParams = array();
+ foreach ( $this->getParametersForApi() as $key => $value ) {
+ $vals = explode( ':', $key, 3 );
+ if ( count( $vals ) !== 3 ) {
+ $logParams[$key] = $value;
+ continue;
+ }
+ $logParams += $this->formatParameterValueForApi(
$vals[2], $vals[1], $value );
+ }
+ ApiResult::setIndexedTagName( $logParams, 'param' );
+ ApiResult::setArrayType( $logParams, 'assoc' );
+
+ return $logParams;
+ }
+
+ /**
+ * Format a single parameter value for API output
+ *
+ * @since 1.25
+ * @param string $name
+ * @param string $type
+ * @param string $value
+ * @return array
+ */
+ protected function formatParameterValueForApi( $name, $type, $value ) {
+ $type = strtolower( trim( $type ) );
+ switch ( $type ) {
+ case 'bool':
+ $value = (bool)$value;
+ break;
+
+ case 'number':
+ if ( ctype_digit( $value ) ) {
+ $value = (int)$value;
+ } else {
+ $value = (float)$value;
+ }
+ break;
+
+ case 'array':
+ case 'assoc':
+ case 'kvp':
+ if ( is_array( $value ) ) {
+ ApiResult::setArrayType( $value, $type
);
+ }
+ break;
+
+ case 'timestamp':
+ $value = wfTimestamp( TS_ISO_8601, $value );
+ break;
+
+ case 'msg':
+ case 'msg-content':
+ $msg = $this->msg( $value );
+ if ( $type === 'msg-content' ) {
+ $msg->inContentLanguage();
+ }
+ $value = array();
+ $value["{$name}_key"] = $msg->getKey();
+ if ( $msg->getParams() ) {
+ $value["{$name}_params"] =
$msg->getParams();
+ }
+ $value["{$name}_text"] = $msg->text();
+ return $value;
+
+ case 'title':
+ case 'title-link':
+ $title = Title::newFromText( $value );
+ if ( $title ) {
+ $value = array();
+ ApiQueryBase::addTitleInfo( $value,
$title, "{$name}_" );
+ }
+ return $value;
+
+ case 'user':
+ case 'user-link':
+ $user = User::newFromName( $value );
+ if ( $user ) {
+ $value = $user->getName();
+ }
+ break;
+
+ default:
+ // do nothing
+ break;
+ }
+
+ return array( $name => $value );
+ }
}
/**
diff --git a/includes/logging/MergeLogFormatter.php
b/includes/logging/MergeLogFormatter.php
index 6763dbd..36e383b 100644
--- a/includes/logging/MergeLogFormatter.php
+++ b/includes/logging/MergeLogFormatter.php
@@ -68,4 +68,24 @@
return $this->msg( 'parentheses' )->rawParams( $revert
)->escaped();
}
+
+ protected function getParametersForApi() {
+ $entry = $this->entry;
+ $params = $entry->getParameters();
+
+ static $map = array(
+ '4:title:dest',
+ '5:timestamp:mergepoint',
+ '4::dest' => '4:title:dest',
+ '5::mergepoint' => '5:timestamp:mergepoint',
+ );
+ foreach ( $map as $index => $key ) {
+ if ( isset( $params[$index] ) ) {
+ $params[$key] = $params[$index];
+ unset( $params[$index] );
+ }
+ }
+
+ return $params;
+ }
}
diff --git a/includes/logging/MoveLogFormatter.php
b/includes/logging/MoveLogFormatter.php
index 35da113..e60708d 100644
--- a/includes/logging/MoveLogFormatter.php
+++ b/includes/logging/MoveLogFormatter.php
@@ -85,4 +85,25 @@
return $this->msg( 'parentheses' )->rawParams( $revert
)->escaped();
}
+
+ protected function getParametersForApi() {
+ $entry = $this->entry;
+ $params = $entry->getParameters();
+
+ static $map = array(
+ '4:title:target',
+ '5:bool:suppressredirect',
+ '4::target' => '4:title:target',
+ '5::noredir' => '5:bool:suppressredirect',
+ );
+ foreach ( $map as $index => $key ) {
+ if ( isset( $params[$index] ) ) {
+ $params[$key] = $params[$index];
+ unset( $params[$index] );
+ }
+ }
+
+ return $params;
+ }
+
}
diff --git a/includes/logging/PatrolLogFormatter.php
b/includes/logging/PatrolLogFormatter.php
index 2abaf17..0033743 100644
--- a/includes/logging/PatrolLogFormatter.php
+++ b/includes/logging/PatrolLogFormatter.php
@@ -62,4 +62,24 @@
return $params;
}
+
+ protected function getParametersForApi() {
+ $entry = $this->entry;
+ $params = $entry->getParameters();
+
+ static $map = array(
+ '4::curid',
+ '5::previd',
+ '6:bool:auto',
+ '6::auto' => '6:bool:auto',
+ );
+ foreach ( $map as $index => $key ) {
+ if ( isset( $params[$index] ) ) {
+ $params[$key] = $params[$index];
+ unset( $params[$index] );
+ }
+ }
+
+ return $params;
+ }
}
diff --git a/includes/logging/RightsLogFormatter.php
b/includes/logging/RightsLogFormatter.php
index ac252ae..597bec5 100644
--- a/includes/logging/RightsLogFormatter.php
+++ b/includes/logging/RightsLogFormatter.php
@@ -110,4 +110,35 @@
return $params;
}
+
+ protected function getParametersForApi() {
+ $entry = $this->entry;
+ $params = $entry->getParameters();
+
+ static $map = array(
+ '4:array:oldgroups',
+ '5:array:newgroups',
+ '4::oldgroups' => '4:array:oldgroups',
+ '5::newgroups' => '5:array:newgroups',
+ );
+ foreach ( $map as $index => $key ) {
+ if ( isset( $params[$index] ) ) {
+ $params[$key] = $params[$index];
+ unset( $params[$index] );
+ }
+ }
+
+ return $params;
+ }
+
+ public function formatParametersForApi() {
+ $ret = parent::formatParametersForApi();
+ if ( isset( $ret['oldgroups'] ) ) {
+ ApiResult::setIndexedTagName( $ret['oldgroups'], 'g' );
+ }
+ if ( isset( $ret['newgroups'] ) ) {
+ ApiResult::setIndexedTagName( $ret['newgroups'], 'g' );
+ }
+ return $ret;
+ }
}
diff --git a/includes/logging/UploadLogFormatter.php
b/includes/logging/UploadLogFormatter.php
new file mode 100644
index 0000000..52961dc
--- /dev/null
+++ b/includes/logging/UploadLogFormatter.php
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Formatter for upload log entries.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
2.0 or later
+ * @since 1.25
+ */
+
+/**
+ * This class formats upload log entries.
+ *
+ * @since 1.25
+ */
+class UploadLogFormatter extends LogFormatter {
+
+ protected function getParametersForApi() {
+ $entry = $this->entry;
+ $params = $entry->getParameters();
+
+ static $map = array(
+ 'img_timestamp' => ':timestamp:img_timestamp',
+ );
+ foreach ( $map as $index => $key ) {
+ if ( isset( $params[$index] ) ) {
+ $params[$key] = $params[$index];
+ unset( $params[$index] );
+ }
+ }
+
+ return $params;
+ }
+
+}
diff --git a/includes/revisiondelete/RevDelLogItem.php
b/includes/revisiondelete/RevDelLogItem.php
index 5c8b8c9..49adf20 100644
--- a/includes/revisiondelete/RevDelLogItem.php
+++ b/includes/revisiondelete/RevDelLogItem.php
@@ -124,15 +124,7 @@
: array();
if ( LogEventsList::userCan( $this->row,
LogPage::DELETED_ACTION, $user ) ) {
- ApiQueryLogEvents::addLogParams(
- $result,
- $ret,
- $logEntry->getParameters(),
- $logEntry->getType(),
- $logEntry->getSubtype(),
- $logEntry->getTimestamp(),
- $logEntry->isLegacy()
- );
+ $ret['params'] = LogFormatter::newFromEntry( $logEntry
)->formatParametersForApi();
}
if ( LogEventsList::userCan( $this->row, LogPage::DELETED_USER,
$user ) ) {
$ret += array(
diff --git a/tests/phpunit/includes/logging/LogFormatterTest.php
b/tests/phpunit/includes/logging/LogFormatterTest.php
index 6210d09..334094f 100644
--- a/tests/phpunit/includes/logging/LogFormatterTest.php
+++ b/tests/phpunit/includes/logging/LogFormatterTest.php
@@ -239,4 +239,58 @@
$this->assertEquals( $comment, $formatter->getComment() );
}
+
+ /**
+ * @dataProvider provideApiParamFormatting
+ * @covers LogFormatter::formatParametersForApi
+ * @covers LogFormatter::formatParameterValueForApi
+ */
+ public function testApiParamFormatting( $key, $value, $expected ) {
+ $entry = $this->newLogEntry( 'param', array( $key => $value ) );
+ $formatter = LogFormatter::newFromEntry( $entry );
+ $formatter->setContext( $this->context );
+
+ ApiResult::setIndexedTagName( $expected, 'param' );
+ ApiResult::setIndexedTagNameOnSubarrays( $expected, 'value' );
+ ApiResult::setArrayType( $expected, 'assoc' );
+
+ $this->assertEquals( $expected,
$formatter->formatParametersForApi() );
+ }
+
+ public static function provideApiParamFormatting() {
+ return array(
+ array( 0, 'value', array( 'value' ) ),
+ array( 'named', 'value', array( 'named' => 'value' ) ),
+ array( '::key', 'value', array( 'key' => 'value' ) ),
+ array( '4::key', 'value', array( 'key' => 'value' ) ),
+ array( '4:raw:key', 'value', array( 'key' => 'value' )
),
+ array( '4:plain:key', 'value', array( 'key' => 'value'
) ),
+ array( '4:bool:key', '1', array( 'key' => true ) ),
+ array( '4:bool:key', '0', array( 'key' => false ) ),
+ array( '4:number:key', '123', array( 'key' => 123 ) ),
+ array( '4:number:key', '123.5', array( 'key' => 123.5 )
),
+ array( '4:array:key', array(), array( 'key' => array(
ApiResult::META_TYPE => 'array' ) ) ),
+ array( '4:assoc:key', array(), array( 'key' => array(
ApiResult::META_TYPE => 'assoc' ) ) ),
+ array( '4:kvp:key', array(), array( 'key' => array(
ApiResult::META_TYPE => 'kvp' ) ) ),
+ array( '4:timestamp:key', '20150102030405', array(
'key' => '2015-01-02T03:04:05Z' ) ),
+ array( '4:msg:key', 'parentheses', array(
+ 'key_key' => 'parentheses',
+ 'key_text' => wfMessage( 'parentheses'
)->text(),
+ ) ),
+ array( '4:msg-content:key', 'parentheses', array(
+ 'key_key' => 'parentheses',
+ 'key_text' => wfMessage( 'parentheses'
)->inContentLanguage()->text(),
+ ) ),
+ array( '4:title:key', 'project:foo', array(
+ 'key_ns' => NS_PROJECT,
+ 'key_title' => Title::newFromText(
'project:foo' )->getFullText(),
+ ) ),
+ array( '4:title-link:key', 'project:foo', array(
+ 'key_ns' => NS_PROJECT,
+ 'key_title' => Title::newFromText(
'project:foo' )->getFullText(),
+ ) ),
+ array( '4:user:key', 'foo', array( 'key' => 'Foo' ) ),
+ array( '4:user-link:key', 'foo', array( 'key' => 'Foo'
) ),
+ );
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/183315
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6846ce09322eb404c506b5a51780a44ce9279fe2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits