5 * Created on Jan 1, 2013
7 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 require_once 'ApiQueryTestBase.php';
29 abstract class ApiQueryContinueTestBase
extends ApiQueryTestBase
{
32 * Enable to print in-depth debugging info during the test run
34 protected $mVerbose = false;
37 * Run query() and compare against expected values
39 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
40 $result = $this->query( $params, $expectedCount, $id, $continue );
41 $this->assertResult( $expected, $result, $id );
45 * Run query in a loop until no more values are available
46 * @param array $params api parameters
47 * @param int $expectedCount max number of iterations
48 * @param string $id unit test id
49 * @param boolean $useContinue true to use smart continue
50 * @return mixed: merged results data array
53 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
54 if ( isset( $params['action'] ) ) {
55 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
57 $params['action'] = 'query';
59 if ( $useContinue && !isset( $params['continue'] ) ) {
60 $params['continue'] = '';
66 $request = array_merge( $params, $continue );
67 uksort( $request, function ( $a, $b ) {
68 // put 'continue' params at the end - lazy method
69 $a = strpos( $a, 'continue' ) !== false ?
'zzz ' . $a : $a;
70 $b = strpos( $b, 'continue' ) !== false ?
'zzz ' . $b : $b;
72 return strcmp( $a, $b );
74 $reqStr = http_build_query( $request );
75 //$reqStr = str_replace( '&', ' & ', $reqStr );
76 $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
77 if ( $this->mVerbose
) {
78 print "$id (#$count): $reqStr\n";
81 $data = $this->doApiRequest( $request );
82 } catch ( Exception
$e ) {
83 throw new Exception( "$id on $count", 0, $e );
86 if ( isset( $data['warnings'] ) ) {
87 $warnings = json_encode( $data['warnings'] );
88 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
90 $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
91 if ( isset( $data['continue'] ) ) {
92 $continue = $data['continue'];
93 unset( $data['continue'] );
97 if ( $this->mVerbose
) {
98 $this->printResult( $data );
100 $this->mergeResult( $result, $data );
102 if ( empty( $continue ) ) {
103 // $this->assertEquals( $expectedCount, $count, "$id finished early" );
104 if ( $expectedCount > $count ) {
105 print "***** $id Finished early in $count turns. $expectedCount was expected\n";
109 } elseif ( !$useContinue ) {
110 $this->assertFalse( 'Non-smart query must be requested all at once' );
115 private function printResult( $data ) {
118 if ( isset( $q['pages'] ) ) {
119 foreach ( $q['pages'] as $p ) {
121 if ( isset( $p['links'] ) ) {
122 $m .= '/[' . implode( ',', array_map(
126 $p['links'] ) ) . ']';
128 if ( isset( $p['categories'] ) ) {
129 $m .= '/(' . implode( ',', array_map(
131 return str_replace( 'Category:', '', $v['title'] );
133 $p['categories'] ) ) . ')';
138 if ( isset( $q['allcategories'] ) ) {
139 $print[] = '*Cats/(' . implode( ',', array_map(
143 $q['allcategories'] ) ) . ')';
145 self
::GetItems( $q, 'allpages', 'Pages', $print );
146 self
::GetItems( $q, 'alllinks', 'Links', $print );
147 self
::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
148 print ' ' . implode( ' ', $print ) . "\n";
151 private static function GetItems( $q, $moduleName, $name, &$print ) {
152 if ( isset( $q[$moduleName] ) ) {
153 $print[] = "*$name/[" . implode( ',',
154 array_map( function ( $v ) {
157 $q[$moduleName] ) ) . ']';
162 * Recursively merge the new result returned from the query to the previous results.
163 * @param mixed $results
164 * @param mixed $newResult
165 * @param bool $numericIds If true, treat keys as ids to be merged instead of appending
167 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
168 $this->assertEquals( is_array( $results ), is_array( $newResult ), 'Type of result and data do not match' );
169 if ( !is_array( $results ) ) {
170 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
173 foreach ( $newResult as $key => $value ) {
174 if ( !$numericIds && $sort === null ) {
175 if ( !is_array( $value ) ) {
177 } elseif ( array_key_exists( 'title', $value ) ) {
178 $sort = function ( $a, $b ) {
179 return strcmp( $a['title'], $b['title'] );
185 $keyExists = array_key_exists( $key, $results );
186 if ( is_numeric( $key ) ) {
189 $results[$key] = $value;
191 $this->mergeResult( $results[$key], $value );
196 } elseif ( !$keyExists ) {
197 $results[$key] = $value;
199 $this->mergeResult( $results[$key], $value, $key === 'pages' );
203 ksort( $results, SORT_NUMERIC
);
204 } elseif ( $sort !== null && $sort !== false ) {
205 uasort( $results, $sort );