MediaWiki  1.23.0
ApiQueryContinueTestBase.php
Go to the documentation of this file.
1 <?php
25 require_once 'ApiQueryTestBase.php';
26 
28 
32  protected $mVerbose = false;
33 
37  protected function checkC( $expected, $params, $expectedCount, $id, $continue = true ) {
38  $result = $this->query( $params, $expectedCount, $id, $continue );
39  $this->assertResult( $expected, $result, $id );
40  }
41 
51  protected function query( $params, $expectedCount, $id, $useContinue = true ) {
52  if ( isset( $params['action'] ) ) {
53  $this->assertEquals( 'query', $params['action'], 'Invalid query action' );
54  } else {
55  $params['action'] = 'query';
56  }
57  if ( $useContinue && !isset( $params['continue'] ) ) {
58  $params['continue'] = '';
59  }
60  $count = 0;
61  $result = array();
62  $continue = array();
63  do {
64  $request = array_merge( $params, $continue );
65  uksort( $request, function ( $a, $b ) {
66  // put 'continue' params at the end - lazy method
67  $a = strpos( $a, 'continue' ) !== false ? 'zzz ' . $a : $a;
68  $b = strpos( $b, 'continue' ) !== false ? 'zzz ' . $b : $b;
69 
70  return strcmp( $a, $b );
71  } );
72  $reqStr = http_build_query( $request );
73  //$reqStr = str_replace( '&', ' & ', $reqStr );
74  $this->assertLessThan( $expectedCount, $count, "$id more data: $reqStr" );
75  if ( $this->mVerbose ) {
76  print "$id (#$count): $reqStr\n";
77  }
78  try {
79  $data = $this->doApiRequest( $request );
80  } catch ( Exception $e ) {
81  throw new Exception( "$id on $count", 0, $e );
82  }
83  $data = $data[0];
84  if ( isset( $data['warnings'] ) ) {
85  $warnings = json_encode( $data['warnings'] );
86  $this->fail( "$id Warnings on #$count in $reqStr\n$warnings" );
87  }
88  $this->assertArrayHasKey( 'query', $data, "$id no 'query' on #$count in $reqStr" );
89  if ( isset( $data['continue'] ) ) {
90  $continue = $data['continue'];
91  unset( $data['continue'] );
92  } else {
93  $continue = array();
94  }
95  if ( $this->mVerbose ) {
96  $this->printResult( $data );
97  }
98  $this->mergeResult( $result, $data );
99  $count++;
100  if ( empty( $continue ) ) {
101  // $this->assertEquals( $expectedCount, $count, "$id finished early" );
102  if ( $expectedCount > $count ) {
103  print "***** $id Finished early in $count turns. $expectedCount was expected\n";
104  }
105 
106  return $result;
107  } elseif ( !$useContinue ) {
108  $this->assertFalse( 'Non-smart query must be requested all at once' );
109  }
110  } while ( true );
111  }
112 
116  private function printResult( $data ) {
117  $q = $data['query'];
118  $print = array();
119  if ( isset( $q['pages'] ) ) {
120  foreach ( $q['pages'] as $p ) {
121  $m = $p['title'];
122  if ( isset( $p['links'] ) ) {
123  $m .= '/[' . implode( ',', array_map(
124  function ( $v ) {
125  return $v['title'];
126  },
127  $p['links'] ) ) . ']';
128  }
129  if ( isset( $p['categories'] ) ) {
130  $m .= '/(' . implode( ',', array_map(
131  function ( $v ) {
132  return str_replace( 'Category:', '', $v['title'] );
133  },
134  $p['categories'] ) ) . ')';
135  }
136  $print[] = $m;
137  }
138  }
139  if ( isset( $q['allcategories'] ) ) {
140  $print[] = '*Cats/(' . implode( ',', array_map(
141  function ( $v ) {
142  return $v['*'];
143  },
144  $q['allcategories'] ) ) . ')';
145  }
146  self::GetItems( $q, 'allpages', 'Pages', $print );
147  self::GetItems( $q, 'alllinks', 'Links', $print );
148  self::GetItems( $q, 'alltransclusions', 'Trnscl', $print );
149  print ' ' . implode( ' ', $print ) . "\n";
150  }
151 
152  private static function GetItems( $q, $moduleName, $name, &$print ) {
153  if ( isset( $q[$moduleName] ) ) {
154  $print[] = "*$name/[" . implode( ',',
155  array_map(
156  function ( $v ) {
157  return $v['title'];
158  },
159  $q[$moduleName] ) ) . ']';
160  }
161  }
162 
169  protected function mergeResult( &$results, $newResult, $numericIds = false ) {
170  $this->assertEquals( is_array( $results ), is_array( $newResult ), 'Type of result and data do not match' );
171  if ( !is_array( $results ) ) {
172  $this->assertEquals( $results, $newResult, 'Repeated result must be the same as before' );
173  } else {
174  $sort = null;
175  foreach ( $newResult as $key => $value ) {
176  if ( !$numericIds && $sort === null ) {
177  if ( !is_array( $value ) ) {
178  $sort = false;
179  } elseif ( array_key_exists( 'title', $value ) ) {
180  $sort = function ( $a, $b ) {
181  return strcmp( $a['title'], $b['title'] );
182  };
183  } else {
184  $sort = false;
185  }
186  }
187  $keyExists = array_key_exists( $key, $results );
188  if ( is_numeric( $key ) ) {
189  if ( $numericIds ) {
190  if ( !$keyExists ) {
191  $results[$key] = $value;
192  } else {
193  $this->mergeResult( $results[$key], $value );
194  }
195  } else {
196  $results[] = $value;
197  }
198  } elseif ( !$keyExists ) {
199  $results[$key] = $value;
200  } else {
201  $this->mergeResult( $results[$key], $value, $key === 'pages' );
202  }
203  }
204  if ( $numericIds ) {
205  ksort( $results, SORT_NUMERIC );
206  } elseif ( $sort !== null && $sort !== false ) {
207  usort( $results, $sort );
208  }
209  }
210  }
211 }
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$params
$params
Definition: styleTest.css.php:40
fail
as a message key or array as accepted by ApiBase::dieUsageMsg after processing request parameters Return false to let the request fail
Definition: hooks.txt:375
ApiQueryContinueTestBase\$mVerbose
$mVerbose
Enable to print in-depth debugging info during the test run.
Definition: ApiQueryContinueTestBase.php:32
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiQueryContinueTestBase\GetItems
static GetItems( $q, $moduleName, $name, &$print)
Definition: ApiQueryContinueTestBase.php:152
ApiQueryTestBase\assertResult
assertResult( $exp, $result, $message='')
Definition: ApiQueryTestBase.php:103
$sort
$sort
Definition: profileinfo.php:301
ApiQueryContinueTestBase\checkC
checkC( $expected, $params, $expectedCount, $id, $continue=true)
Run query() and compare against expected values.
Definition: ApiQueryContinueTestBase.php:37
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
ApiQueryTestBase
This class has some common functionality for testing query module.
Definition: ApiQueryTestBase.php:27
ApiQueryContinueTestBase\printResult
printResult( $data)
Definition: ApiQueryContinueTestBase.php:116
$count
$count
Definition: UtfNormalTest2.php:96
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:74
ApiQueryContinueTestBase
Definition: ApiQueryContinueTestBase.php:27
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiQueryContinueTestBase\mergeResult
mergeResult(&$results, $newResult, $numericIds=false)
Recursively merge the new result returned from the query to the previous results.
Definition: ApiQueryContinueTestBase.php:169
ApiQueryContinueTestBase\query
query( $params, $expectedCount, $id, $useContinue=true)
Run query in a loop until no more values are available.
Definition: ApiQueryContinueTestBase.php:51
$e
if( $useReadline) $e
Definition: eval.php:66