| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Tests\Api\Query; |
| 25 |
|
| 26 |
use Exception; |
| 27 |
|
| 28 |
abstract class ApiQueryContinueTestBase extends ApiQueryTestBase { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected $mVerbose = false; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) { |
| 44 |
$result = $this->query( $params, $expectedCount, $id, $continue ); |
| 45 |
$this->assertResult( $expected, $result, $id ); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
protected function query( $params, $expectedCount, $id, $useContinue = true ) { |
| 58 |
if ( isset( $params['action'] ) ) { |
| 59 |
$this->assertEquals( 'query', $params['action'], 'Invalid query action' ); |
| 60 |
} else { |
| 61 |
$params['action'] = 'query'; |
| 62 |
} |
| 63 |
$count = 0; |
| 64 |
$result = []; |
| 65 |
$continue = []; |
| 66 |
do { |
| 67 |
$request = array_merge( $params, $continue ); |
| 68 |
uksort( $request, static function ( $a, $b ) { |
| 69 |
|
| 70 |
$a = str_contains( $a, 'continue' ) ? 'zzz ' . $a : $a; |
| 71 |
$b = str_contains( $b, 'continue' ) ? 'zzz ' . $b : $b; |
| 72 |
|
| 73 |
return strcmp( $a, $b ); |
| 74 |
} ); |
| 75 |
$reqStr = http_build_query( $request ); |
| 76 |
|
| 77 |
$this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" ); |
| 78 |
if ( $this->mVerbose ) { |
| 79 |
print "$id (#$count): $reqStr\n"; |
| 80 |
} |
| 81 |
try { |
| 82 |
$data = $this->doApiRequest( $request ); |
| 83 |
} catch ( Exception $e ) { |
| 84 |
throw new Exception( "$id on $count", 0, $e ); |
| 85 |
} |
| 86 |
$data = $data[0]; |
| 87 |
if ( isset( $data['warnings'] ) ) { |
| 88 |
$warnings = json_encode( $data['warnings'] ); |
| 89 |
$this->fail( "$id Warnings on #$count in $reqStr\n$warnings" ); |
| 90 |
} |
| 91 |
$this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" ); |
| 92 |
if ( isset( $data['continue'] ) ) { |
| 93 |
$continue = $data['continue']; |
| 94 |
unset( $data['continue'] ); |
| 95 |
} else { |
| 96 |
$continue = []; |
| 97 |
} |
| 98 |
if ( $this->mVerbose ) { |
| 99 |
$this->printResult( $data ); |
| 100 |
} |
| 101 |
$this->mergeResult( $result, $data ); |
| 102 |
$count++; |
| 103 |
if ( empty( $continue ) ) { |
| 104 |
$this->assertEquals( $expectedCount, $count, "$id finished early" ); |
| 105 |
|
| 106 |
return $result; |
| 107 |
} elseif ( !$useContinue ) { |
| 108 |
$this->assertFalse( 'Non-smart query must be requested all at once' ); |
| 109 |
} |
| 110 |
} while ( true ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
private function printResult( $data ) { |
| 117 |
$q = $data['query']; |
| 118 |
$print = []; |
| 119 |
if ( isset( $q['pages'] ) ) { |
| 120 |
foreach ( $q['pages'] as $p ) { |
| 121 |
$m = $p['title']; |
| 122 |
if ( isset( $p['links'] ) ) { |
| 123 |
$m .= '/[' . implode( ',', array_column( $p['links'], 'title' ) ) . ']'; |
| 124 |
} |
| 125 |
if ( isset( $p['categories'] ) ) { |
| 126 |
$m .= '/(' . implode( ',', array_map( |
| 127 |
static function ( $v ) { |
| 128 |
return str_replace( 'Category:', '', $v['title'] ); |
| 129 |
}, |
| 130 |
$p['categories'] ) ) . ')'; |
| 131 |
} |
| 132 |
$print[] = $m; |
| 133 |
} |
| 134 |
} |
| 135 |
if ( isset( $q['allcategories'] ) ) { |
| 136 |
$print[] = '*Cats/(' . implode( ',', array_column( $q['allcategories'], '*' ) ) . ')'; |
| 137 |
} |
| 138 |
self::getItems( $q, 'allpages', 'Pages', $print ); |
| 139 |
self::getItems( $q, 'alllinks', 'Links', $print ); |
| 140 |
self::getItems( $q, 'alltransclusions', 'Trnscl', $print ); |
| 141 |
print ' ' . implode( ' ', $print ) . "\n"; |
| 142 |
} |
| 143 |
|
| 144 |
private static function getItems( $q, $moduleName, $name, &$print ) { |
| 145 |
if ( isset( $q[$moduleName] ) ) { |
| 146 |
$print[] = "*$name/[" . implode( ',', array_column( $q[$moduleName], 'title' ) ) . ']'; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
protected function mergeResult( &$results, $newResult, $numericIds = false ) { |
| 157 |
$this->assertEquals( |
| 158 |
is_array( $results ), |
| 159 |
is_array( $newResult ), |
| 160 |
'Type of result and data do not match' |
| 161 |
); |
| 162 |
if ( !is_array( $results ) ) { |
| 163 |
$this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' ); |
| 164 |
} else { |
| 165 |
$sort = null; |
| 166 |
foreach ( $newResult as $key => $value ) { |
| 167 |
if ( !$numericIds && $sort === null ) { |
| 168 |
if ( !is_array( $value ) ) { |
| 169 |
$sort = false; |
| 170 |
} elseif ( array_key_exists( 'title', $value ) ) { |
| 171 |
$sort = static function ( $a, $b ) { |
| 172 |
return strcmp( $a['title'], $b['title'] ); |
| 173 |
}; |
| 174 |
} else { |
| 175 |
$sort = false; |
| 176 |
} |
| 177 |
} |
| 178 |
$keyExists = array_key_exists( $key, $results ); |
| 179 |
if ( is_numeric( $key ) ) { |
| 180 |
if ( $numericIds ) { |
| 181 |
if ( !$keyExists ) { |
| 182 |
$results[$key] = $value; |
| 183 |
} else { |
| 184 |
$this->mergeResult( $results[$key], $value ); |
| 185 |
} |
| 186 |
} else { |
| 187 |
$results[] = $value; |
| 188 |
} |
| 189 |
} elseif ( !$keyExists ) { |
| 190 |
$results[$key] = $value; |
| 191 |
} else { |
| 192 |
$this->mergeResult( $results[$key], $value, $key === 'pages' ); |
| 193 |
} |
| 194 |
} |
| 195 |
if ( $numericIds ) { |
| 196 |
ksort( $results, SORT_NUMERIC ); |
| 197 |
} elseif ( $sort !== null && $sort !== false ) { |
| 198 |
usort( $results, $sort ); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |