Make Job::toString() handle array parameters better
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 16 Mar 2016 10:24:10 +0000 (03:24 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 16 Mar 2016 10:45:35 +0000 (03:45 -0700)
Previously it would result in array(x) in fairly simple cases.

Change-Id: I6bfe67faa45835babb2c7e259b5cd158e0f0a649

includes/jobqueue/Job.php
tests/phpunit/includes/jobqueue/JobTest.php

index 48d38d9..9ccf6f8 100644 (file)
@@ -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}";
                        }
                }
 
index 65ffa49..3c648f9 100644 (file)
@@ -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'
+                       ],
                ];
        }