From: Aaron Schulz Date: Wed, 16 Mar 2016 10:24:10 +0000 (-0700) Subject: Make Job::toString() handle array parameters better X-Git-Tag: 1.31.0-rc.0~7596^2 X-Git-Url: http://git.cyclocoop.org//%27%40script%40/%27?a=commitdiff_plain;h=4a28737c32dfef53f311a503367a977aa946a320;p=lhc%2Fweb%2Fwiklou.git Make Job::toString() handle array parameters better Previously it would result in array(x) in fairly simple cases. Change-Id: I6bfe67faa45835babb2c7e259b5cd158e0f0a649 --- diff --git a/includes/jobqueue/Job.php b/includes/jobqueue/Job.php index 48d38d9c49..9ccf6f8465 100644 --- a/includes/jobqueue/Job.php +++ b/includes/jobqueue/Job.php @@ -315,14 +315,6 @@ abstract class Job implements IJobSpecification { * @return string */ public function toString() { - $truncFunc = function ( $value ) { - $value = (string)$value; - if ( mb_strlen( $value ) > 1024 ) { - $value = "string(" . mb_strlen( $value ) . ")"; - } - return $value; - }; - $paramString = ''; if ( $this->params ) { foreach ( $this->params as $key => $value ) { @@ -332,14 +324,14 @@ abstract class Job implements IJobSpecification { if ( is_array( $value ) ) { $filteredValue = []; foreach ( $value as $k => $v ) { - if ( is_scalar( $v ) ) { - $filteredValue[$k] = $truncFunc( $v ); + $json = FormatJson::encode( $v ); + if ( $json === false || mb_strlen( $json ) > 512 ) { + $filteredValue[$k] = gettype( $v ) . '(...)'; } else { - $filteredValue = null; - break; + $filteredValue[$k] = $v; } } - if ( $filteredValue && count( $filteredValue ) < 10 ) { + if ( count( $filteredValue ) <= 10 ) { $value = FormatJson::encode( $filteredValue ); } else { $value = "array(" . count( $value ) . ")"; @@ -348,7 +340,12 @@ abstract class Job implements IJobSpecification { $value = "object(" . get_class( $value ) . ")"; } - $paramString .= "$key={$truncFunc( $value )}"; + $flatValue = (string)$value; + if ( mb_strlen( $value ) > 1024 ) { + $flatValue = "string(" . mb_strlen( $value ) . ")"; + } + + $paramString .= "$key={$flatValue}"; } } diff --git a/tests/phpunit/includes/jobqueue/JobTest.php b/tests/phpunit/includes/jobqueue/JobTest.php index 65ffa49c8b..3c648f9bce 100644 --- a/tests/phpunit/includes/jobqueue/JobTest.php +++ b/tests/phpunit/includes/jobqueue/JobTest.php @@ -52,6 +52,29 @@ class JobTest extends MediaWikiTestCase { $this->getMockJob( [ $mockToStringObj ] ), 'someCommand 0={STRING_OBJ_VAL}' ], + [ + $this->getMockJob( [ + "pages" => [ + "932737" => [ + 0, + "Robert_James_Waller" + ] + ], + "rootJobSignature" => "45868e99bba89064e4483743ebb9b682ef95c1a7", + "rootJobTimestamp" => "20160309110158", + "masterPos" => [ + "file" => "db1023-bin.001288", + "pos" => "308257743", + "asOfTime" => 1457521464.3814 + ], + "triggeredRecursive" => true + ] ), + 'someCommand pages={"932737":[0,"Robert_James_Waller"]} ' . + 'rootJobSignature=45868e99bba89064e4483743ebb9b682ef95c1a7 ' . + 'rootJobTimestamp=20160309110158 masterPos=' . + '{"file":"db1023-bin.001288","pos":"308257743","asOfTime":1457521464.3814} ' . + 'triggeredRecursive=1' + ], ]; }