23use UnexpectedValueException;
35 private $allModules = [];
37 private $generatedModules;
40 private $continuationData = [];
42 private $generatorContinuationData = [];
44 private $generatorNonContinuationData = [];
47 private $generatorParams = [];
49 private $generatorDone =
false;
58 ApiBase $module, array $allModules = [], array $generatedModules = []
60 $this->source = get_class( $module );
63 $this->generatedModules = $generatedModules
64 ? array_combine( $generatedModules, $generatedModules )
68 $continue = $request->getVal(
'continue',
'' );
69 if ( $continue !==
'' ) {
70 $continue = explode(
'||', $continue );
71 if ( count( $continue ) !== 2 ) {
74 $this->generatorDone = ( $continue[0] ===
'-' );
75 $skip = explode(
'|', $continue[1] );
76 if ( !$this->generatorDone ) {
77 $params = explode(
'|', $continue[0] );
78 $this->generatorParams = array_intersect_key(
79 $request->getValues(),
80 array_fill_keys(
$params,
true )
85 $skip += $this->generatedModules;
89 foreach ( $allModules as $module ) {
91 if ( in_array( $name, $skip,
true ) ) {
92 $this->allModules[$name] =
false;
96 $this->allModules[$name] = $module;
106 return $this->source;
113 return $this->generatorDone;
121 return array_values( array_filter( $this->allModules ) );
133 if ( !isset( $this->allModules[$name] ) ) {
134 throw new UnexpectedValueException(
135 "Module '$name' called " . __METHOD__ .
136 ' but was not passed to ' . __CLASS__ .
'::__construct'
139 if ( !$this->allModules[$name] ) {
140 throw new UnexpectedValueException(
141 "Module '$name' was not supposed to have been executed, but " .
142 'it was executed anyway'
146 if ( is_array( $paramValue ) ) {
147 $paramValue = implode(
'|', $paramValue );
149 $this->continuationData[$name][$paramName] = $paramValue;
166 if ( is_array( $paramValue ) ) {
167 $paramValue = implode(
'|', $paramValue );
169 $this->generatorNonContinuationData[$name][$paramName] = $paramValue;
181 if ( is_array( $paramValue ) ) {
182 $paramValue = implode(
'|', $paramValue );
184 $this->generatorContinuationData[$name][$paramName] = $paramValue;
192 return array_merge_recursive( $this->continuationData, $this->generatorContinuationData );
201 return $this->generatorNonContinuationData;
210 $batchcomplete =
false;
212 $finishedModules = array_diff(
213 array_keys( $this->allModules ),
214 array_keys( $this->continuationData )
218 $continuationData = array_diff_key( $this->continuationData, $this->generatedModules );
219 foreach ( $continuationData as $kvp ) {
224 $continuationData = array_intersect_key( $this->continuationData, $this->generatedModules );
225 if ( $continuationData ) {
228 foreach ( $continuationData as $kvp ) {
231 $generatorParams = [];
232 foreach ( $this->generatorNonContinuationData as $kvp ) {
233 $generatorParams += $kvp;
235 $generatorParams += $this->generatorParams;
237 $data += $generatorParams;
238 $generatorKeys = implode(
'|', array_keys( $generatorParams ) );
239 } elseif ( $this->generatorContinuationData ) {
243 $generatorParams = [];
244 foreach ( $this->generatorContinuationData as $kvp ) {
245 $generatorParams += $kvp;
247 $data += $generatorParams;
248 $finishedModules = array_diff( $finishedModules, $this->generatedModules );
249 $generatorKeys = implode(
'|', array_keys( $generatorParams ) );
250 $batchcomplete =
true;
253 $generatorKeys =
'-';
254 $batchcomplete =
true;
259 if ( $data || $generatorKeys !==
'-' ) {
260 $data[
'continue'] = $generatorKeys .
'||' . implode(
'|', $finishedModules );
263 return [ $data, $batchcomplete ];
273 $result->
addValue(
null,
'continue', $data,
276 if ( $batchcomplete ) {
277 $result->
addValue(
null,
'batchcomplete',
true,
284class_alias( ApiContinuationManager::class,
'ApiContinuationManager' );
array $params
The job parameters.