3 * Created on Jan 1, 2013
5 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
24 abstract class ApiQueryContinueTestBase
extends ApiQueryTestBase
{
27 * Enable to print in-depth debugging info during the test run
29 protected $mVerbose = false;
32 * Run query() and compare against expected values
33 * @param array $expected
34 * @param array $params Api parameters
35 * @param int $expectedCount Max number of iterations
36 * @param string $id Unit test id
37 * @param bool $continue True to use smart continue
38 * @return array Merged results data array
40 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
41 $result = $this->query( $params, $expectedCount, $id, $continue );
42 $this->assertResult( $expected, $result, $id );
46 * Run query in a loop until no more values are available
47 * @param array $params Api parameters
48 * @param int $expectedCount Max number of iterations
49 * @param string $id Unit test id
50 * @param bool $useContinue True to use smart continue
51 * @return array Merged results data array
54 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
55 if ( isset( $params['action'] ) ) {
56 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
58 $params['action'] = 'query';
61 if ( !isset( $params['continue'] ) ) {
62 $params['continue'] = '';
68 $request = array_merge( $params, $continue );
69 uksort( $request, function ( $a, $b ) {
70 // put 'continue' params at the end - lazy method
71 $a = strpos( $a, 'continue' ) !== false ?
'zzz ' . $a : $a;
72 $b = strpos( $b, 'continue' ) !== false ?
'zzz ' . $b : $b;
74 return strcmp( $a, $b );
76 $reqStr = http_build_query( $request );
77 // $reqStr = str_replace( '&', ' & ', $reqStr );
78 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
79 if ( $this->mVerbose
) {
80 print "$id (#$count): $reqStr\n";
83 $data = $this->doApiRequest( $request );
84 } catch ( Exception
$e ) {
85 throw new Exception( "$id on $count", 0, $e );
88 if ( isset( $data['warnings'] ) ) {
89 $warnings = json_encode( $data['warnings'] );
90 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
92 $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
93 if ( isset( $data['continue'] ) ) {
94 $continue = $data['continue'];
95 unset( $data['continue'] );
99 if ( $this->mVerbose
) {
100 $this->printResult( $data );
102 $this->mergeResult( $result, $data );
104 if ( empty( $continue ) ) {
105 $this->assertEquals( $expectedCount, $count, "$id finished early" );
108 } elseif ( !$useContinue ) {
109 $this->assertFalse( 'Non-smart query must be requested all at once' );
117 private function printResult( $data ) {
120 if ( isset( $q['pages'] ) ) {
121 foreach ( $q['pages'] as $p ) {
123 if ( isset( $p['links'] ) ) {
124 $m .= '/[' . implode( ',', array_map(
128 $p['links'] ) ) . ']';
130 if ( isset( $p['categories'] ) ) {
131 $m .= '/(' . implode( ',', array_map(
133 return str_replace( 'Category:', '', $v['title'] );
135 $p['categories'] ) ) . ')';
140 if ( isset( $q['allcategories'] ) ) {
141 $print[] = '*Cats/(' . implode( ',', array_map(
145 $q['allcategories'] ) ) . ')';
147 self
::GetItems( $q, 'allpages', 'Pages', $print );
148 self
::GetItems( $q, 'alllinks', 'Links', $print );
149 self
::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
150 print ' ' . implode( ' ', $print ) . "\n";
153 private static function GetItems( $q, $moduleName, $name, &$print ) {
154 if ( isset( $q[$moduleName] ) ) {
155 $print[] = "*$name/[" . implode( ',',
160 $q[$moduleName] ) ) . ']';
165 * Recursively merge the new result returned from the query to the previous results.
166 * @param mixed $results
167 * @param mixed $newResult
168 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
170 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
172 is_array( $results ),
173 is_array( $newResult ),
174 'Type of result and data do not match'
176 if ( !is_array( $results ) ) {
177 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
180 foreach ( $newResult as $key => $value ) {
181 if ( !$numericIds && $sort === null ) {
182 if ( !is_array( $value ) ) {
184 } elseif ( array_key_exists( 'title', $value ) ) {
185 $sort = function ( $a, $b ) {
186 return strcmp( $a['title'], $b['title'] );
192 $keyExists = array_key_exists( $key, $results );
193 if ( is_numeric( $key ) ) {
196 $results[$key] = $value;
198 $this->mergeResult( $results[$key], $value );
203 } elseif ( !$keyExists ) {
204 $results[$key] = $value;
206 $this->mergeResult( $results[$key], $value, $key === 'pages' );
210 ksort( $results, SORT_NUMERIC
);
211 } elseif ( $sort !== null && $sort !== false ) {
212 usort( $results, $sort );