MediaWiki REL1_32
ApiQueryContinueTestBase.php
Go to the documentation of this file.
1<?php
23
27 protected $mVerbose = false;
28
38 protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
39 $result = $this->query( $params, $expectedCount, $id, $continue );
40 $this->assertResult( $expected, $result, $id );
41 }
42
52 protected function query( $params, $expectedCount, $id, $useContinue = true ) {
53 if ( isset( $params['action'] ) ) {
54 $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
55 } else {
56 $params['action'] = 'query';
57 }
58 $count = 0;
59 $result = [];
60 $continue = [];
61 do {
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;
67
68 return strcmp( $a, $b );
69 } );
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";
75 }
76 try {
77 $data = $this->doApiRequest( $request );
78 } catch ( Exception $e ) {
79 throw new Exception( "$id on $count", 0, $e );
80 }
81 $data = $data[0];
82 if ( isset( $data['warnings'] ) ) {
83 $warnings = json_encode( $data['warnings'] );
84 $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
85 }
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'] );
90 } else {
91 $continue = [];
92 }
93 if ( $this->mVerbose ) {
94 $this->printResult( $data );
95 }
96 $this->mergeResult( $result, $data );
97 $count++;
98 if ( empty( $continue ) ) {
99 $this->assertEquals( $expectedCount, $count, "$id finished early" );
100
101 return $result;
102 } elseif ( !$useContinue ) {
103 $this->assertFalse( 'Non-smart query must be requested all at once' );
104 }
105 } while ( true );
106 }
107
111 private function printResult( $data ) {
112 $q = $data['query'];
113 $print = [];
114 if ( isset( $q['pages'] ) ) {
115 foreach ( $q['pages'] as $p ) {
116 $m = $p['title'];
117 if ( isset( $p['links'] ) ) {
118 $m .= '/[' . implode( ',', array_map(
119 function ( $v ) {
120 return $v['title'];
121 },
122 $p['links'] ) ) . ']';
123 }
124 if ( isset( $p['categories'] ) ) {
125 $m .= '/(' . implode( ',', array_map(
126 function ( $v ) {
127 return str_replace( 'Category:', '', $v['title'] );
128 },
129 $p['categories'] ) ) . ')';
130 }
131 $print[] = $m;
132 }
133 }
134 if ( isset( $q['allcategories'] ) ) {
135 $print[] = '*Cats/(' . implode( ',', array_map(
136 function ( $v ) {
137 return $v['*'];
138 },
139 $q['allcategories'] ) ) . ')';
140 }
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";
145 }
146
147 private static function GetItems( $q, $moduleName, $name, &$print ) {
148 if ( isset( $q[$moduleName] ) ) {
149 $print[] = "*$name/[" . implode( ',',
150 array_map(
151 function ( $v ) {
152 return $v['title'];
153 },
154 $q[$moduleName] ) ) . ']';
155 }
156 }
157
164 protected function mergeResult( &$results, $newResult, $numericIds = false ) {
165 $this->assertEquals(
166 is_array( $results ),
167 is_array( $newResult ),
168 'Type of result and data do not match'
169 );
170 if ( !is_array( $results ) ) {
171 $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
172 } else {
173 $sort = null;
174 foreach ( $newResult as $key => $value ) {
175 if ( !$numericIds && $sort === null ) {
176 if ( !is_array( $value ) ) {
177 $sort = false;
178 } elseif ( array_key_exists( 'title', $value ) ) {
179 $sort = function ( $a, $b ) {
180 return strcmp( $a['title'], $b['title'] );
181 };
182 } else {
183 $sort = false;
184 }
185 }
186 $keyExists = array_key_exists( $key, $results );
187 if ( is_numeric( $key ) ) {
188 if ( $numericIds ) {
189 if ( !$keyExists ) {
190 $results[$key] = $value;
191 } else {
192 $this->mergeResult( $results[$key], $value );
193 }
194 } else {
195 $results[] = $value;
196 }
197 } elseif ( !$keyExists ) {
198 $results[$key] = $value;
199 } else {
200 $this->mergeResult( $results[$key], $value, $key === 'pages' );
201 }
202 }
203 if ( $numericIds ) {
204 ksort( $results, SORT_NUMERIC );
205 } elseif ( $sort !== null && $sort !== false ) {
206 usort( $results, $sort );
207 }
208 }
209 }
210}
query( $params, $expectedCount, $id, $useContinue=true)
Run query in a loop until no more values are available.
static GetItems( $q, $moduleName, $name, &$print)
mergeResult(&$results, $newResult, $numericIds=false)
Recursively merge the new result returned from the query to the previous results.
$mVerbose
Enable to print in-depth debugging info during the test run.
checkC( $expected, $params, $expectedCount, $id, $continue=true)
Run query() and compare against expected values.
This class has some common functionality for testing query module.
assertResult( $exp, $result, $message='')
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
For a write query
Definition database.txt:26
namespace being checked & $result
Definition hooks.txt:2385
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2880
returning false will NOT prevent logging $e
Definition hooks.txt:2226
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
$sort
$params