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';
60 if ( $useContinue && !isset( $params['continue'] ) ) {
61 $params['continue'] = '';
63 $params['rawcontinue'] = '1';
69 $request = array_merge( $params, $continue );
70 uksort( $request, function ( $a, $b ) {
71 // put 'continue' params at the end - lazy method
72 $a = strpos( $a, 'continue' ) !== false ?
'zzz ' . $a : $a;
73 $b = strpos( $b, 'continue' ) !== false ?
'zzz ' . $b : $b;
75 return strcmp( $a, $b );
77 $reqStr = http_build_query( $request );
78 //$reqStr = str_replace( '&', ' & ', $reqStr );
79 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
80 if ( $this->mVerbose
) {
81 print "$id (#$count): $reqStr\n";
84 $data = $this->doApiRequest( $request );
85 } catch ( Exception
$e ) {
86 throw new Exception( "$id on $count", 0, $e );
89 if ( isset( $data['warnings'] ) ) {
90 $warnings = json_encode( $data['warnings'] );
91 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
93 $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
94 if ( isset( $data['continue'] ) ) {
95 $continue = $data['continue'];
96 unset( $data['continue'] );
100 if ( $this->mVerbose
) {
101 $this->printResult( $data );
103 $this->mergeResult( $result, $data );
105 if ( empty( $continue ) ) {
106 $this->assertEquals( $expectedCount, $count, "$id finished early" );
109 } elseif ( !$useContinue ) {
110 $this->assertFalse( 'Non-smart query must be requested all at once' );
118 private function printResult( $data ) {
121 if ( isset( $q['pages'] ) ) {
122 foreach ( $q['pages'] as $p ) {
124 if ( isset( $p['links'] ) ) {
125 $m .= '/[' . implode( ',', array_map(
129 $p['links'] ) ) . ']';
131 if ( isset( $p['categories'] ) ) {
132 $m .= '/(' . implode( ',', array_map(
134 return str_replace( 'Category:', '', $v['title'] );
136 $p['categories'] ) ) . ')';
141 if ( isset( $q['allcategories'] ) ) {
142 $print[] = '*Cats/(' . implode( ',', array_map(
146 $q['allcategories'] ) ) . ')';
148 self
::GetItems( $q, 'allpages', 'Pages', $print );
149 self
::GetItems( $q, 'alllinks', 'Links', $print );
150 self
::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
151 print ' ' . implode( ' ', $print ) . "\n";
154 private static function GetItems( $q, $moduleName, $name, &$print ) {
155 if ( isset( $q[$moduleName] ) ) {
156 $print[] = "*$name/[" . implode( ',',
161 $q[$moduleName] ) ) . ']';
166 * Recursively merge the new result returned from the query to the previous results.
167 * @param mixed $results
168 * @param mixed $newResult
169 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
171 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
173 is_array( $results ),
174 is_array( $newResult ),
175 'Type of result and data do not match'
177 if ( !is_array( $results ) ) {
178 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
181 foreach ( $newResult as $key => $value ) {
182 if ( !$numericIds && $sort === null ) {
183 if ( !is_array( $value ) ) {
185 } elseif ( array_key_exists( 'title', $value ) ) {
186 $sort = function ( $a, $b ) {
187 return strcmp( $a['title'], $b['title'] );
193 $keyExists = array_key_exists( $key, $results );
194 if ( is_numeric( $key ) ) {
197 $results[$key] = $value;
199 $this->mergeResult( $results[$key], $value );
204 } elseif ( !$keyExists ) {
205 $results[$key] = $value;
207 $this->mergeResult( $results[$key], $value, $key === 'pages' );
211 ksort( $results, SORT_NUMERIC
);
212 } elseif ( $sort !== null && $sort !== false ) {
213 usort( $results, $sort );