X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fjobqueue%2FJob.php;h=9ccf6f84657c3ec4c386323b2c304e7477fa8ec5;hp=2d13c7e5e5b03706a7e6b09226fb2587d9adaa5d;hb=e3bd13db0c285f312e31bb1b7271af4628cca80c;hpb=38ba6b620be9f6333d902055ae1c0c610af4985e diff --git a/includes/jobqueue/Job.php b/includes/jobqueue/Job.php index 2d13c7e5e5..9ccf6f8465 100644 --- a/includes/jobqueue/Job.php +++ b/includes/jobqueue/Job.php @@ -36,7 +36,7 @@ abstract class Job implements IJobSpecification { public $params; /** @var array Additional queue metadata */ - public $metadata = array(); + public $metadata = []; /** @var Title */ protected $title; @@ -47,6 +47,9 @@ abstract class Job implements IJobSpecification { /** @var string Text for error that occurred last */ protected $error; + /** @var callable[] */ + protected $teardownCallbacks = []; + /** * Run the job * @return bool Success @@ -62,7 +65,7 @@ abstract class Job implements IJobSpecification { * @throws MWException * @return Job */ - public static function factory( $command, Title $title, $params = array() ) { + public static function factory( $command, Title $title, $params = [] ) { global $wgJobClasses; if ( isset( $wgJobClasses[$command] ) ) { @@ -85,7 +88,7 @@ abstract class Job implements IJobSpecification { public function __construct( $command, $title, $params = false ) { $this->command = $command; $this->title = $title; - $this->params = is_array( $params ) ? $params : array(); // sanity + $this->params = is_array( $params ) ? $params : []; // sanity // expensive jobs may set this to true $this->removeDuplicates = false; @@ -199,12 +202,12 @@ abstract class Job implements IJobSpecification { * @since 1.21 */ public function getDeduplicationInfo() { - $info = array( + $info = [ 'type' => $this->getType(), 'namespace' => $this->getTitle()->getNamespace(), 'title' => $this->getTitle()->getDBkey(), 'params' => $this->getParams() - ); + ]; if ( is_array( $info['params'] ) ) { // Identical jobs with different "root" jobs should count as duplicates unset( $info['params']['rootJobSignature'] ); @@ -238,11 +241,11 @@ abstract class Job implements IJobSpecification { * @since 1.21 */ public static function newRootJobParams( $key ) { - return array( + return [ 'rootJobIsSelf' => true, 'rootJobSignature' => sha1( $key ), 'rootJobTimestamp' => wfTimestampNow() - ); + ]; } /** @@ -251,14 +254,14 @@ abstract class Job implements IJobSpecification { * @since 1.21 */ public function getRootJobParams() { - return array( + return [ 'rootJobSignature' => isset( $this->params['rootJobSignature'] ) ? $this->params['rootJobSignature'] : null, 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] ) ? $this->params['rootJobTimestamp'] : null - ); + ]; } /** @@ -279,6 +282,25 @@ abstract class Job implements IJobSpecification { return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] ); } + /** + * @param callable $callback + * @since 1.27 + */ + protected function addTeardownCallback( $callback ) { + $this->teardownCallbacks[] = $callback; + } + + /** + * Do any final cleanup after run(), deferred updates, and all DB commits happen + * + * @since 1.27 + */ + public function teardown() { + foreach ( $this->teardownCallbacks as $callback ) { + call_user_func( $callback ); + } + } + /** * Insert a single job into the queue. * @return bool True on success @@ -293,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 ) { @@ -308,16 +322,16 @@ abstract class Job implements IJobSpecification { $paramString .= ' '; } if ( is_array( $value ) ) { - $filteredValue = array(); + $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 ) . ")"; @@ -326,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}"; } }