3 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 abstract class ApiQueryContinueTestBase
extends ApiQueryTestBase
{
25 * Enable to print in-depth debugging info during the test run
27 protected $mVerbose = false;
30 * Run query() and compare against expected values
31 * @param array $expected
32 * @param array $params Api parameters
33 * @param int $expectedCount Max number of iterations
34 * @param string $id Unit test id
35 * @param bool $continue True to use smart continue
36 * @return array Merged results data array
38 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
39 $result = $this->query( $params, $expectedCount, $id, $continue );
40 $this->assertResult( $expected, $result, $id );
44 * Run query in a loop until no more values are available
45 * @param array $params Api parameters
46 * @param int $expectedCount Max number of iterations
47 * @param string $id Unit test id
48 * @param bool $useContinue True to use smart continue
49 * @return array Merged results data array
52 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
53 if ( isset( $params['action'] ) ) {
54 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
56 $params['action'] = 'query';
62 $request = array_merge( $params, $continue );
63 uksort( $request, function ( $a, $b ) {
64 // put 'continue' params at the end - lazy method
65 $a = strpos( $a, 'continue' ) !== false ?
'zzz ' . $a : $a;
66 $b = strpos( $b, 'continue' ) !== false ?
'zzz ' . $b : $b;
68 return strcmp( $a, $b );
70 $reqStr = http_build_query( $request );
71 // $reqStr = str_replace( '&', ' & ', $reqStr );
72 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
73 if ( $this->mVerbose
) {
74 print "$id (#$count): $reqStr\n";
77 $data = $this->doApiRequest( $request );
78 } catch ( Exception
$e ) {
79 throw new Exception( "$id on $count", 0, $e );
82 if ( isset( $data['warnings'] ) ) {
83 $warnings = json_encode( $data['warnings'] );
84 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
86 $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
87 if ( isset( $data['continue'] ) ) {
88 $continue = $data['continue'];
89 unset( $data['continue'] );
93 if ( $this->mVerbose
) {
94 $this->printResult( $data );
96 $this->mergeResult( $result, $data );
98 if ( empty( $continue ) ) {
99 $this->assertEquals( $expectedCount, $count, "$id finished early" );
102 } elseif ( !$useContinue ) {
103 $this->assertFalse( 'Non-smart query must be requested all at once' );
111 private function printResult( $data ) {
114 if ( isset( $q['pages'] ) ) {
115 foreach ( $q['pages'] as $p ) {
117 if ( isset( $p['links'] ) ) {
118 $m .= '/[' . implode( ',', array_map(
122 $p['links'] ) ) . ']';
124 if ( isset( $p['categories'] ) ) {
125 $m .= '/(' . implode( ',', array_map(
127 return str_replace( 'Category:', '', $v['title'] );
129 $p['categories'] ) ) . ')';
134 if ( isset( $q['allcategories'] ) ) {
135 $print[] = '*Cats/(' . implode( ',', array_map(
139 $q['allcategories'] ) ) . ')';
141 self
::GetItems( $q, 'allpages', 'Pages', $print );
142 self
::GetItems( $q, 'alllinks', 'Links', $print );
143 self
::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
144 print ' ' . implode( ' ', $print ) . "\n";
147 private static function GetItems( $q, $moduleName, $name, &$print ) {
148 if ( isset( $q[$moduleName] ) ) {
149 $print[] = "*$name/[" . implode( ',',
154 $q[$moduleName] ) ) . ']';
159 * Recursively merge the new result returned from the query to the previous results.
160 * @param mixed $results
161 * @param mixed $newResult
162 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
164 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
166 is_array( $results ),
167 is_array( $newResult ),
168 'Type of result and data do not match'
170 if ( !is_array( $results ) ) {
171 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
174 foreach ( $newResult as $key => $value ) {
175 if ( !$numericIds && $sort === null ) {
176 if ( !is_array( $value ) ) {
178 } elseif ( array_key_exists( 'title', $value ) ) {
179 $sort = function ( $a, $b ) {
180 return strcmp( $a['title'], $b['title'] );
186 $keyExists = array_key_exists( $key, $results );
187 if ( is_numeric( $key ) ) {
190 $results[$key] = $value;
192 $this->mergeResult( $results[$key], $value );
197 } elseif ( !$keyExists ) {
198 $results[$key] = $value;
200 $this->mergeResult( $results[$key], $value, $key === 'pages' );
204 ksort( $results, SORT_NUMERIC
);
205 } elseif ( $sort !== null && $sort !== false ) {
206 usort( $results, $sort );