Added "maxPartitionsTry" option to JobQueueFederated
[lhc/web/wiklou.git] / tests / phpunit / includes / api / query / ApiQueryContinueTestBase.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jan 1, 2013
6 *
7 * Copyright © 2013 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 require_once 'ApiQueryTestBase.php';
28
29 abstract class ApiQueryContinueTestBase extends ApiQueryTestBase {
30
31 /**
32 * Enable to print in-depth debugging info during the test run
33 */
34 protected $mVerbose = false;
35
36 /**
37 * Run query() and compare against expected values
38 */
39 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
40 $result = $this->query( $params, $expectedCount, $id, $continue );
41 $this->assertResult( $expected, $result, $id );
42 }
43
44 /**
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
51 * @throws Exception
52 */
53 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
54 if ( isset( $params['action'] ) ) {
55 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
56 } else {
57 $params['action'] = 'query';
58 }
59 if ( $useContinue && !isset( $params['continue'] ) ) {
60 $params['continue'] = '';
61 }
62 $count = 0;
63 $result = array();
64 $continue = array();
65 do {
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;
71
72 return strcmp( $a, $b );
73 } );
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";
79 }
80 try {
81 $data = $this->doApiRequest( $request );
82 } catch ( Exception $e ) {
83 throw new Exception( "$id on $count", 0, $e );
84 }
85 $data = $data[0];
86 if ( isset( $data['warnings'] ) ) {
87 $warnings = json_encode( $data['warnings'] );
88 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
89 }
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'] );
94 } else {
95 $continue = array();
96 }
97 if ( $this->mVerbose ) {
98 $this->printResult( $data );
99 }
100 $this->mergeResult( $result, $data );
101 $count++;
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";
106 }
107
108 return $result;
109 } elseif ( !$useContinue ) {
110 $this->assertFalse( 'Non-smart query must be requested all at once' );
111 }
112 } while ( true );
113 }
114
115 private function printResult( $data ) {
116 $q = $data['query'];
117 $print = array();
118 if ( isset( $q['pages'] ) ) {
119 foreach ( $q['pages'] as $p ) {
120 $m = $p['title'];
121 if ( isset( $p['links'] ) ) {
122 $m .= '/[' . implode( ',', array_map(
123 function ( $v ) {
124 return $v['title'];
125 },
126 $p['links'] ) ) . ']';
127 }
128 if ( isset( $p['categories'] ) ) {
129 $m .= '/(' . implode( ',', array_map(
130 function ( $v ) {
131 return str_replace( 'Category:', '', $v['title'] );
132 },
133 $p['categories'] ) ) . ')';
134 }
135 $print[] = $m;
136 }
137 }
138 if ( isset( $q['allcategories'] ) ) {
139 $print[] = '*Cats/(' . implode( ',', array_map(
140 function ( $v ) {
141 return $v['*'];
142 },
143 $q['allcategories'] ) ) . ')';
144 }
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";
149 }
150
151 private static function GetItems( $q, $moduleName, $name, &$print ) {
152 if ( isset( $q[$moduleName] ) ) {
153 $print[] = "*$name/[" . implode( ',',
154 array_map( function ( $v ) {
155 return $v['title'];
156 },
157 $q[$moduleName] ) ) . ']';
158 }
159 }
160
161 /**
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
166 */
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' );
171 } else {
172 $sort = null;
173 foreach ( $newResult as $key => $value ) {
174 if ( !$numericIds && $sort === null ) {
175 if ( !is_array( $value ) ) {
176 $sort = false;
177 } elseif ( array_key_exists( 'title', $value ) ) {
178 $sort = function ( $a, $b ) {
179 return strcmp( $a['title'], $b['title'] );
180 };
181 } else {
182 $sort = false;
183 }
184 }
185 $keyExists = array_key_exists( $key, $results );
186 if ( is_numeric( $key ) ) {
187 if ( $numericIds ) {
188 if ( !$keyExists ) {
189 $results[$key] = $value;
190 } else {
191 $this->mergeResult( $results[$key], $value );
192 }
193 } else {
194 $results[] = $value;
195 }
196 } elseif ( !$keyExists ) {
197 $results[$key] = $value;
198 } else {
199 $this->mergeResult( $results[$key], $value, $key === 'pages' );
200 }
201 }
202 if ( $numericIds ) {
203 ksort( $results, SORT_NUMERIC );
204 } elseif ( $sort !== null && $sort !== false ) {
205 uasort( $results, $sort );
206 }
207 }
208 }
209 }