1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* This manages continuation state.
* @since 1.25 this is no longer a subclass of ApiBase
* @ingroup API
*/
class ApiContinuationManager {
private $source;
private $allModules = [];
private $generatedModules = [];
private $continuationData = [];
private $generatorContinuationData = [];
private $generatorParams = [];
private $generatorDone = false;
/**
* @param ApiBase $module Module starting the continuation
* @param ApiBase[] $allModules Contains ApiBase instances that will be executed
* @param array $generatedModules Names of modules that depend on the generator
* @throws UsageException
*/
public function __construct(
ApiBase $module, array $allModules = [], array $generatedModules = []
) {
$this->source = get_class( $module );
$request = $module->getRequest();
$this->generatedModules = $generatedModules
? array_combine( $generatedModules, $generatedModules )
: [];
$skip = [];
$continue = $request->getVal( 'continue', '' );
if ( $continue !== '' ) {
$continue = explode( '||', $continue );
if ( count( $continue ) !== 2 ) {
throw new UsageException(
'Invalid continue param. You should pass the original value returned by the previous query',
'badcontinue'
);
}
$this->generatorDone = ( $continue[0] === '-' );
$skip = explode( '|', $continue[1] );
if ( !$this->generatorDone ) {
$params = explode( '|', $continue[0] );
if ( $params ) {
$this->generatorParams = array_intersect_key(
$request->getValues(),
array_flip( $params )
);
}
} else {
// When the generator is complete, don't run any modules that
// depend on it.
$skip += $this->generatedModules;
}
}
foreach ( $allModules as $module ) {
$name = $module->getModuleName();
if ( in_array( $name, $skip, true ) ) {
$this->allModules[$name] = false;
// Prevent spurious "unused parameter" warnings
$module->extractRequestParams();
} else {
$this->allModules[$name] = $module;
}
}
}
/**
* Get the class that created this manager
* @return string
*/
public function getSource() {
return $this->source;
}
/**
* Is the generator done?
* @return bool
*/
public function isGeneratorDone() {
return $this->generatorDone;
}
/**
* Get the list of modules that should actually be run
* @return ApiBase[]
*/
public function getRunModules() {
return array_values( array_filter( $this->allModules ) );
}
/**
* Set the continuation parameter for a module
* @param ApiBase $module
* @param string $paramName
* @param string|array $paramValue
* @throws UnexpectedValueException
*/
public function addContinueParam( ApiBase $module, $paramName, $paramValue ) {
$name = $module->getModuleName();
if ( !isset( $this->allModules[$name] ) ) {
throw new UnexpectedValueException(
"Module '$name' called " . __METHOD__ .
' but was not passed to ' . __CLASS__ . '::__construct'
);
}
if ( !$this->allModules[$name] ) {
throw new UnexpectedValueException(
"Module '$name' was not supposed to have been executed, but " .
'it was executed anyway'
);
}
$paramName = $module->encodeParamName( $paramName );
if ( is_array( $paramValue ) ) {
$paramValue = implode( '|', $paramValue );
}
$this->continuationData[$name][$paramName] = $paramValue;
}
/**
* Set the continuation parameter for the generator module
* @param ApiBase $module
* @param string $paramName
* @param string|array $paramValue
*/
public function addGeneratorContinueParam( ApiBase $module, $paramName, $paramValue ) {
$name = $module->getModuleName();
$paramName = $module->encodeParamName( $paramName );
if ( is_array( $paramValue ) ) {
$paramValue = implode( '|', $paramValue );
}
$this->generatorContinuationData[$name][$paramName] = $paramValue;
}
/**
* Fetch raw continuation data
* @return array
*/
public function getRawContinuation() {
return array_merge_recursive( $this->continuationData, $this->generatorContinuationData );
}
/**
* Fetch continuation result data
* @return array Array( (array)$data, (bool)$batchcomplete )
*/
public function getContinuation() {
$data = [];
$batchcomplete = false;
$finishedModules = array_diff(
array_keys( $this->allModules ),
array_keys( $this->continuationData )
);
// First, grab the non-generator-using continuation data
$continuationData = array_diff_key( $this->continuationData, $this->generatedModules );
foreach ( $continuationData as $module => $kvp ) {
$data += $kvp;
}
// Next, handle the generator-using continuation data
$continuationData = array_intersect_key( $this->continuationData, $this->generatedModules );
if ( $continuationData ) {
// Some modules are unfinished: include those params, and copy
// the generator params.
foreach ( $continuationData as $module => $kvp ) {
$data += $kvp;
}
$data += $this->generatorParams;
$generatorKeys = implode( '|', array_keys( $this->generatorParams ) );
} elseif ( $this->generatorContinuationData ) {
// All the generator-using modules are complete, but the
// generator isn't. Continue the generator and restart the
// generator-using modules
$generatorParams = [];
foreach ( $this->generatorContinuationData as $kvp ) {
$generatorParams += $kvp;
}
$data += $generatorParams;
$finishedModules = array_diff( $finishedModules, $this->generatedModules );
$generatorKeys = implode( '|', array_keys( $generatorParams ) );
$batchcomplete = true;
} else {
// Generator and prop modules are all done. Mark it so.
$generatorKeys = '-';
$batchcomplete = true;
}
// Set 'continue' if any continuation data is set or if the generator
// still needs to run
if ( $data || $generatorKeys !== '-' ) {
$data['continue'] = $generatorKeys . '||' . implode( '|', $finishedModules );
}
return [ $data, $batchcomplete ];
}
/**
* Store the continuation data into the result
* @param ApiResult $result
*/
public function setContinuationIntoResult( ApiResult $result ) {
list( $data, $batchcomplete ) = $this->getContinuation();
if ( $data ) {
$result->addValue( null, 'continue', $data,
ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
}
if ( $batchcomplete ) {
$result->addValue( null, 'batchcomplete', true,
ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
}
}
}
|