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
25 require_once 'ApiQueryTestBase.php';
27 abstract class ApiQueryContinueTestBase
extends ApiQueryTestBase
{
30 * Enable to print in-depth debugging info during the test run
32 protected $mVerbose = false;
35 * Run query() and compare against expected values
36 * @param array $expected
37 * @param array $params Api parameters
38 * @param int $expectedCount Max number of iterations
39 * @param string $id Unit test id
40 * @param bool $useContinue true to use smart continue
41 * @return mixed Merged results data array
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 mixed 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';
63 if ( $useContinue && !isset( $params['continue'] ) ) {
64 $params['continue'] = '';
70 $request = array_merge( $params, $continue );
71 uksort( $request, function ( $a, $b ) {
72 // put 'continue' params at the end - lazy method
73 $a = strpos( $a, 'continue' ) !== false ?
'zzz ' . $a : $a;
74 $b = strpos( $b, 'continue' ) !== false ?
'zzz ' . $b : $b;
76 return strcmp( $a, $b );
78 $reqStr = http_build_query( $request );
79 //$reqStr = str_replace( '&', ' & ', $reqStr );
80 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
81 if ( $this->mVerbose
) {
82 print "$id (#$count): $reqStr\n";
85 $data = $this->doApiRequest( $request );
86 } catch ( Exception
$e ) {
87 throw new Exception( "$id on $count", 0, $e );
90 if ( isset( $data['warnings'] ) ) {
91 $warnings = json_encode( $data['warnings'] );
92 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
94 $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
95 if ( isset( $data['continue'] ) ) {
96 $continue = $data['continue'];
97 unset( $data['continue'] );
101 if ( $this->mVerbose
) {
102 $this->printResult( $data );
104 $this->mergeResult( $result, $data );
106 if ( empty( $continue ) ) {
107 // $this->assertEquals( $expectedCount, $count, "$id finished early" );
108 if ( $expectedCount > $count ) {
109 print "***** $id Finished early in $count turns. $expectedCount was expected\n";
113 } elseif ( !$useContinue ) {
114 $this->assertFalse( 'Non-smart query must be requested all at once' );
122 private function printResult( $data ) {
125 if ( isset( $q['pages'] ) ) {
126 foreach ( $q['pages'] as $p ) {
128 if ( isset( $p['links'] ) ) {
129 $m .= '/[' . implode( ',', array_map(
133 $p['links'] ) ) . ']';
135 if ( isset( $p['categories'] ) ) {
136 $m .= '/(' . implode( ',', array_map(
138 return str_replace( 'Category:', '', $v['title'] );
140 $p['categories'] ) ) . ')';
145 if ( isset( $q['allcategories'] ) ) {
146 $print[] = '*Cats/(' . implode( ',', array_map(
150 $q['allcategories'] ) ) . ')';
152 self
::GetItems( $q, 'allpages', 'Pages', $print );
153 self
::GetItems( $q, 'alllinks', 'Links', $print );
154 self
::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
155 print ' ' . implode( ' ', $print ) . "\n";
158 private static function GetItems( $q, $moduleName, $name, &$print ) {
159 if ( isset( $q[$moduleName] ) ) {
160 $print[] = "*$name/[" . implode( ',',
165 $q[$moduleName] ) ) . ']';
170 * Recursively merge the new result returned from the query to the previous results.
171 * @param mixed $results
172 * @param mixed $newResult
173 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
175 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
177 is_array( $results ),
178 is_array( $newResult ),
179 'Type of result and data do not match'
181 if ( !is_array( $results ) ) {
182 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
185 foreach ( $newResult as $key => $value ) {
186 if ( !$numericIds && $sort === null ) {
187 if ( !is_array( $value ) ) {
189 } elseif ( array_key_exists( 'title', $value ) ) {
190 $sort = function ( $a, $b ) {
191 return strcmp( $a['title'], $b['title'] );
197 $keyExists = array_key_exists( $key, $results );
198 if ( is_numeric( $key ) ) {
201 $results[$key] = $value;
203 $this->mergeResult( $results[$key], $value );
208 } elseif ( !$keyExists ) {
209 $results[$key] = $value;
211 $this->mergeResult( $results[$key], $value, $key === 'pages' );
215 ksort( $results, SORT_NUMERIC
);
216 } elseif ( $sort !== null && $sort !== false ) {
217 usort( $results, $sort );