9use UnexpectedValueException;
21 private $allModules = [];
23 private $generatedModules;
26 private $continuationData = [];
28 private $generatorContinuationData = [];
30 private $generatorNonContinuationData = [];
33 private $generatorParams = [];
35 private $generatorDone =
false;
44 ApiBase $module, array $allModules = [], array $generatedModules = []
46 $this->source = get_class( $module );
49 $this->generatedModules = $generatedModules
50 ? array_combine( $generatedModules, $generatedModules )
54 $continue = $request->getVal(
'continue',
'' );
55 if ( $continue !==
'' ) {
56 $continue = explode(
'||', $continue );
57 if ( count( $continue ) !== 2 ) {
60 $this->generatorDone = ( $continue[0] ===
'-' );
61 $skip = explode(
'|', $continue[1] );
62 if ( !$this->generatorDone ) {
63 $params = explode(
'|', $continue[0] );
64 $this->generatorParams = array_intersect_key(
65 $request->getValues(),
66 array_fill_keys( $params,
true )
71 $skip += $this->generatedModules;
75 foreach ( $allModules as $module ) {
77 if ( in_array( $name, $skip,
true ) ) {
78 $this->allModules[$name] =
false;
82 $this->allModules[$name] = $module;
99 return $this->generatorDone;
107 return array_values( array_filter( $this->allModules ) );
119 if ( !isset( $this->allModules[$name] ) ) {
120 throw new UnexpectedValueException(
121 "Module '$name' called " . __METHOD__ .
122 ' but was not passed to ' . __CLASS__ .
'::__construct'
125 if ( !$this->allModules[$name] ) {
126 throw new UnexpectedValueException(
127 "Module '$name' was not supposed to have been executed, but " .
128 'it was executed anyway'
132 if ( is_array( $paramValue ) ) {
133 $paramValue = implode(
'|', $paramValue );
135 $this->continuationData[$name][$paramName] = $paramValue;
152 if ( is_array( $paramValue ) ) {
153 $paramValue = implode(
'|', $paramValue );
155 $this->generatorNonContinuationData[$name][$paramName] = $paramValue;
167 if ( is_array( $paramValue ) ) {
168 $paramValue = implode(
'|', $paramValue );
170 $this->generatorContinuationData[$name][$paramName] = $paramValue;
178 return array_merge_recursive( $this->continuationData, $this->generatorContinuationData );
187 return $this->generatorNonContinuationData;
196 $batchcomplete =
false;
198 $finishedModules = array_diff(
199 array_keys( $this->allModules ),
200 array_keys( $this->continuationData )
204 $continuationData = array_diff_key( $this->continuationData, $this->generatedModules );
205 foreach ( $continuationData as $kvp ) {
210 $continuationData = array_intersect_key( $this->continuationData, $this->generatedModules );
211 if ( $continuationData ) {
214 foreach ( $continuationData as $kvp ) {
217 $generatorParams = [];
218 foreach ( $this->generatorNonContinuationData as $kvp ) {
219 $generatorParams += $kvp;
221 $generatorParams += $this->generatorParams;
223 $data += $generatorParams;
224 $generatorKeys = implode(
'|', array_keys( $generatorParams ) );
225 } elseif ( $this->generatorContinuationData ) {
229 $generatorParams = [];
230 foreach ( $this->generatorContinuationData as $kvp ) {
231 $generatorParams += $kvp;
233 $data += $generatorParams;
234 $finishedModules = array_diff( $finishedModules, $this->generatedModules );
235 $generatorKeys = implode(
'|', array_keys( $generatorParams ) );
236 $batchcomplete =
true;
239 $generatorKeys =
'-';
240 $batchcomplete =
true;
245 if ( $data || $generatorKeys !==
'-' ) {
246 $data[
'continue'] = $generatorKeys .
'||' . implode(
'|', $finishedModules );
249 return [ $data, $batchcomplete ];
258 $result->
addValue(
null,
'continue', $data,
261 if ( $batchcomplete ) {
262 $result->
addValue(
null,
'batchcomplete',
true,
269class_alias( ApiContinuationManager::class,
'ApiContinuationManager' );