| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Api; |
| 22 |
|
| 23 |
use UnexpectedValueException; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class ApiContinuationManager { |
| 31 |
|
| 32 |
private $source; |
| 33 |
|
| 34 |
|
| 35 |
private $allModules = []; |
| 36 |
|
| 37 |
private $generatedModules; |
| 38 |
|
| 39 |
|
| 40 |
private $continuationData = []; |
| 41 |
|
| 42 |
private $generatorContinuationData = []; |
| 43 |
|
| 44 |
private $generatorNonContinuationData = []; |
| 45 |
|
| 46 |
|
| 47 |
private $generatorParams = []; |
| 48 |
|
| 49 |
private $generatorDone = false; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( |
| 58 |
ApiBase $module, array $allModules = [], array $generatedModules = [] |
| 59 |
) { |
| 60 |
$this->source = get_class( $module ); |
| 61 |
$request = $module->getRequest(); |
| 62 |
|
| 63 |
$this->generatedModules = $generatedModules |
| 64 |
? array_combine( $generatedModules, $generatedModules ) |
| 65 |
: []; |
| 66 |
|
| 67 |
$skip = []; |
| 68 |
$continue = $request->getVal( 'continue', '' ); |
| 69 |
if ( $continue !== '' ) { |
| 70 |
$continue = explode( '||', $continue ); |
| 71 |
if ( count( $continue ) !== 2 ) { |
| 72 |
throw ApiUsageException::newWithMessage( $module->getMain(), 'apierror-badcontinue' ); |
| 73 |
} |
| 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 ) |
| 81 |
); |
| 82 |
} else { |
| 83 |
|
| 84 |
|
| 85 |
$skip += $this->generatedModules; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
foreach ( $allModules as $module ) { |
| 90 |
$name = $module->getModuleName(); |
| 91 |
if ( in_array( $name, $skip, true ) ) { |
| 92 |
$this->allModules[$name] = false; |
| 93 |
|
| 94 |
$module->extractRequestParams(); |
| 95 |
} else { |
| 96 |
$this->allModules[$name] = $module; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function getSource() { |
| 106 |
return $this->source; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function isGeneratorDone() { |
| 113 |
return $this->generatorDone; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function getRunModules() { |
| 121 |
return array_values( array_filter( $this->allModules ) ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function addContinueParam( ApiBase $module, $paramName, $paramValue ) { |
| 132 |
$name = $module->getModuleName(); |
| 133 |
if ( !isset( $this->allModules[$name] ) ) { |
| 134 |
throw new UnexpectedValueException( |
| 135 |
"Module '$name' called " . __METHOD__ . |
| 136 |
' but was not passed to ' . __CLASS__ . '::__construct' |
| 137 |
); |
| 138 |
} |
| 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' |
| 143 |
); |
| 144 |
} |
| 145 |
$paramName = $module->encodeParamName( $paramName ); |
| 146 |
if ( is_array( $paramValue ) ) { |
| 147 |
$paramValue = implode( '|', $paramValue ); |
| 148 |
} |
| 149 |
$this->continuationData[$name][$paramName] = $paramValue; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function addGeneratorNonContinueParam( ApiBase $module, $paramName, $paramValue ) { |
| 164 |
$name = $module->getModuleName(); |
| 165 |
$paramName = $module->encodeParamName( $paramName ); |
| 166 |
if ( is_array( $paramValue ) ) { |
| 167 |
$paramValue = implode( '|', $paramValue ); |
| 168 |
} |
| 169 |
$this->generatorNonContinuationData[$name][$paramName] = $paramValue; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function addGeneratorContinueParam( ApiBase $module, $paramName, $paramValue ) { |
| 179 |
$name = $module->getModuleName(); |
| 180 |
$paramName = $module->encodeParamName( $paramName ); |
| 181 |
if ( is_array( $paramValue ) ) { |
| 182 |
$paramValue = implode( '|', $paramValue ); |
| 183 |
} |
| 184 |
$this->generatorContinuationData[$name][$paramName] = $paramValue; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function getRawContinuation() { |
| 192 |
return array_merge_recursive( $this->continuationData, $this->generatorContinuationData ); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function getRawNonContinuation() { |
| 201 |
return $this->generatorNonContinuationData; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function getContinuation() { |
| 209 |
$data = []; |
| 210 |
$batchcomplete = false; |
| 211 |
|
| 212 |
$finishedModules = array_diff( |
| 213 |
array_keys( $this->allModules ), |
| 214 |
array_keys( $this->continuationData ) |
| 215 |
); |
| 216 |
|
| 217 |
|
| 218 |
$continuationData = array_diff_key( $this->continuationData, $this->generatedModules ); |
| 219 |
foreach ( $continuationData as $kvp ) { |
| 220 |
$data += $kvp; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
$continuationData = array_intersect_key( $this->continuationData, $this->generatedModules ); |
| 225 |
if ( $continuationData ) { |
| 226 |
|
| 227 |
|
| 228 |
foreach ( $continuationData as $kvp ) { |
| 229 |
$data += $kvp; |
| 230 |
} |
| 231 |
$generatorParams = []; |
| 232 |
foreach ( $this->generatorNonContinuationData as $kvp ) { |
| 233 |
$generatorParams += $kvp; |
| 234 |
} |
| 235 |
$generatorParams += $this->generatorParams; |
| 236 |
|
| 237 |
$data += $generatorParams; |
| 238 |
$generatorKeys = implode( '|', array_keys( $generatorParams ) ); |
| 239 |
} elseif ( $this->generatorContinuationData ) { |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
$generatorParams = []; |
| 244 |
foreach ( $this->generatorContinuationData as $kvp ) { |
| 245 |
$generatorParams += $kvp; |
| 246 |
} |
| 247 |
$data += $generatorParams; |
| 248 |
$finishedModules = array_diff( $finishedModules, $this->generatedModules ); |
| 249 |
$generatorKeys = implode( '|', array_keys( $generatorParams ) ); |
| 250 |
$batchcomplete = true; |
| 251 |
} else { |
| 252 |
|
| 253 |
$generatorKeys = '-'; |
| 254 |
$batchcomplete = true; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
if ( $data || $generatorKeys !== '-' ) { |
| 260 |
$data['continue'] = $generatorKeys . '||' . implode( '|', $finishedModules ); |
| 261 |
} |
| 262 |
|
| 263 |
return [ $data, $batchcomplete ]; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function setContinuationIntoResult( ApiResult $result ) { |
| 271 |
[ $data, $batchcomplete ] = $this->getContinuation(); |
| 272 |
if ( $data ) { |
| 273 |
$result->addValue( null, 'continue', $data, |
| 274 |
ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK ); |
| 275 |
} |
| 276 |
if ( $batchcomplete ) { |
| 277 |
$result->addValue( null, 'batchcomplete', true, |
| 278 |
ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK ); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
class_alias( ApiContinuationManager::class, 'ApiContinuationManager' ); |