4 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 namespace MediaWiki\Tests\Api\Query
;
28 abstract class ApiQueryContinueTestBase
extends ApiQueryTestBase
{
31 * @var bool Enable to print in-depth debugging info during the test run
33 protected $mVerbose = false;
36 * Run query() and compare against expected values
37 * @param array $expected
38 * @param array $params Api parameters
39 * @param int $expectedCount Max number of iterations
40 * @param string $id Unit test id
41 * @param bool $continue True to use smart continue
43 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
44 $result = $this->query( $params, $expectedCount, $id, $continue );
45 $this->assertResult( $expected, $result, $id );
49 * Run query in a loop until no more values are available
50 * @param array $params Api parameters
51 * @param int $expectedCount Max number of iterations
52 * @param string $id Unit test id
53 * @param bool $useContinue True to use smart continue
54 * @return array Merged results data array
57 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
58 if ( isset( $params['action'] ) ) {
59 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
61 $params['action'] = 'query';
67 $request = array_merge( $params, $continue );
68 uksort( $request, static function ( $a, $b ) {
69 // put 'continue' params at the end - lazy method
70 $a = str_contains( $a, 'continue' ) ?
'zzz ' . $a : $a;
71 $b = str_contains( $b, 'continue' ) ?
'zzz ' . $b : $b;
73 return strcmp( $a, $b );
75 $reqStr = http_build_query( $request );
76 // $reqStr = str_replace( '&', ' & ', $reqStr );
77 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
78 if ( $this->mVerbose
) {
79 print "$id (#$count): $reqStr\n";
82 $data = $this->doApiRequest( $request );
83 } catch ( Exception
$e ) {
84 throw new Exception( "$id on $count", 0, $e );
87 if ( isset( $data['warnings'] ) ) {
88 $warnings = json_encode( $data['warnings'] );
89 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
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'] );
98 if ( $this->mVerbose
) {
99 $this->printResult( $data );
101 $this->mergeResult( $result, $data );
103 if ( empty( $continue ) ) {
104 $this->assertEquals( $expectedCount, $count, "$id finished early" );
107 } elseif ( !$useContinue ) {
108 $this->assertFalse( 'Non-smart query must be requested all at once' );
116 private function printResult( $data ) {
119 if ( isset( $q['pages'] ) ) {
120 foreach ( $q['pages'] as $p ) {
122 if ( isset( $p['links'] ) ) {
123 $m .= '/[' . implode( ',', array_column( $p['links'], 'title' ) ) . ']';
125 if ( isset( $p['categories'] ) ) {
126 $m .= '/(' . implode( ',', array_map(
127 static function ( $v ) {
128 return str_replace( 'Category:', '', $v['title'] );
130 $p['categories'] ) ) . ')';
135 if ( isset( $q['allcategories'] ) ) {
136 $print[] = '*Cats/(' . implode( ',', array_column( $q['allcategories'], '*' ) ) . ')';
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";
144 private static function getItems( $q, $moduleName, $name, &$print ) {
145 if ( isset( $q[$moduleName] ) ) {
146 $print[] = "*$name/[" . implode( ',', array_column( $q[$moduleName], 'title' ) ) . ']';
151 * Recursively merge the new result returned from the query to the previous results.
152 * @param mixed &$results
153 * @param mixed $newResult
154 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
156 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
158 is_array( $results ),
159 is_array( $newResult ),
160 'Type of result and data do not match'
162 if ( !is_array( $results ) ) {
163 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
166 foreach ( $newResult as $key => $value ) {
167 if ( !$numericIds && $sort === null ) {
168 if ( !is_array( $value ) ) {
170 } elseif ( array_key_exists( 'title', $value ) ) {
171 $sort = static function ( $a, $b ) {
172 return strcmp( $a['title'], $b['title'] );
178 $keyExists = array_key_exists( $key, $results );
179 if ( is_numeric( $key ) ) {
182 $results[$key] = $value;
184 $this->mergeResult( $results[$key], $value );
189 } elseif ( !$keyExists ) {
190 $results[$key] = $value;
192 $this->mergeResult( $results[$key], $value, $key === 'pages' );
196 ksort( $results, SORT_NUMERIC
);
197 } elseif ( $sort !== null && $sort !== false ) {
198 usort( $results, $sort );