Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryContinueTestBase.php
blob1ba514473d1f97987c3701e491a5e27fadf89be3
1 <?php
3 /**
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
21 * @file
24 namespace MediaWiki\Tests\Api\Query;
26 use Exception;
28 abstract class ApiQueryContinueTestBase extends ApiQueryTestBase {
30 /**
31 * @var bool Enable to print in-depth debugging info during the test run
33 protected $mVerbose = false;
35 /**
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 );
48 /**
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
55 * @throws Exception
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';
63 $count = 0;
64 $result = [];
65 $continue = [];
66 do {
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 );
74 } );
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";
81 try {
82 $data = $this->doApiRequest( $request );
83 } catch ( Exception $e ) {
84 throw new Exception( "$id on $count", 0, $e );
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" );
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 = [];
98 if ( $this->mVerbose ) {
99 $this->printResult( $data );
101 $this->mergeResult( $result, $data );
102 $count++;
103 if ( empty( $continue ) ) {
104 $this->assertEquals( $expectedCount, $count, "$id finished early" );
106 return $result;
107 } elseif ( !$useContinue ) {
108 $this->assertFalse( 'Non-smart query must be requested all at once' );
110 } while ( true );
114 * @param array $data
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' ) ) . ']';
125 if ( isset( $p['categories'] ) ) {
126 $m .= '/(' . implode( ',', array_map(
127 static function ( $v ) {
128 return str_replace( 'Category:', '', $v['title'] );
130 $p['categories'] ) ) . ')';
132 $print[] = $m;
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 ) {
157 $this->assertEquals(
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' );
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'] );
174 } else {
175 $sort = false;
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 );
186 } else {
187 $results[] = $value;
189 } elseif ( !$keyExists ) {
190 $results[$key] = $value;
191 } else {
192 $this->mergeResult( $results[$key], $value, $key === 'pages' );
195 if ( $numericIds ) {
196 ksort( $results, SORT_NUMERIC );
197 } elseif ( $sort !== null && $sort !== false ) {
198 usort( $results, $sort );