ApiQueryLanguageinfo.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * API module to enumerate language information.
  22. *
  23. * @ingroup API
  24. */
  25. class ApiQueryLanguageinfo extends ApiQueryBase {
  26. /**
  27. * The maximum time for {@link execute()};
  28. * if execution takes longer than this, apply continuation.
  29. *
  30. * If the localization cache is used, this time is not expected to ever be
  31. * exceeded; on the other hand, if it is not used, a typical request will
  32. * not yield more than a handful of languages before the time is exceeded
  33. * and continuation is applied, if one of the expensive props is requested.
  34. *
  35. * @var float
  36. */
  37. const MAX_EXECUTE_SECONDS = 2.0;
  38. /** @var callable|null */
  39. private $microtimeFunction;
  40. /**
  41. * @param ApiQuery $queryModule
  42. * @param string $moduleName
  43. * @param callable|null $microtimeFunction Function to use instead of microtime(), for testing.
  44. * Should accept no arguments and return float seconds. (null means real microtime().)
  45. */
  46. public function __construct(
  47. ApiQuery $queryModule,
  48. $moduleName,
  49. $microtimeFunction = null
  50. ) {
  51. parent::__construct( $queryModule, $moduleName, 'li' );
  52. $this->microtimeFunction = $microtimeFunction;
  53. }
  54. /** @return float */
  55. private function microtime() {
  56. if ( $this->microtimeFunction ) {
  57. return ( $this->microtimeFunction )();
  58. } else {
  59. return microtime( true );
  60. }
  61. }
  62. public function execute() {
  63. $endTime = $this->microtime() + self::MAX_EXECUTE_SECONDS;
  64. $props = array_flip( $this->getParameter( 'prop' ) );
  65. $includeCode = isset( $props['code'] );
  66. $includeBcp47 = isset( $props['bcp47'] );
  67. $includeDir = isset( $props['dir'] );
  68. $includeAutonym = isset( $props['autonym'] );
  69. $includeName = isset( $props['name'] );
  70. $includeFallbacks = isset( $props['fallbacks'] );
  71. $includeVariants = isset( $props['variants'] );
  72. $targetLanguageCode = $this->getLanguage()->getCode();
  73. $include = 'all';
  74. $availableLanguageCodes = array_keys( Language::fetchLanguageNames(
  75. // MediaWiki and extensions may return different sets of language codes
  76. // when asked for language names in different languages;
  77. // asking for English language names is most likely to give us the full set,
  78. // even though we may not need those at all
  79. 'en',
  80. $include
  81. ) );
  82. $selectedLanguageCodes = $this->getParameter( 'code' );
  83. if ( $selectedLanguageCodes === [ '*' ] ) {
  84. $languageCodes = $availableLanguageCodes;
  85. } else {
  86. $languageCodes = array_values( array_intersect(
  87. $availableLanguageCodes,
  88. $selectedLanguageCodes
  89. ) );
  90. $unrecognizedCodes = array_values( array_diff(
  91. $selectedLanguageCodes,
  92. $availableLanguageCodes
  93. ) );
  94. if ( $unrecognizedCodes !== [] ) {
  95. $this->addWarning( [
  96. 'apiwarn-unrecognizedvalues',
  97. $this->encodeParamName( 'code' ),
  98. Message::listParam( $unrecognizedCodes, 'comma' ),
  99. count( $unrecognizedCodes ),
  100. ] );
  101. }
  102. }
  103. // order of $languageCodes is guaranteed by Language::fetchLanguageNames()
  104. // and preserved by array_values() + array_intersect()
  105. $continue = $this->getParameter( 'continue' );
  106. if ( $continue === null ) {
  107. $continue = reset( $languageCodes );
  108. }
  109. $result = $this->getResult();
  110. $rootPath = [
  111. $this->getQuery()->getModuleName(),
  112. $this->getModuleName(),
  113. ];
  114. $result->addArrayType( $rootPath, 'assoc' );
  115. foreach ( $languageCodes as $languageCode ) {
  116. if ( $languageCode < $continue ) {
  117. continue;
  118. }
  119. $now = $this->microtime();
  120. if ( $now >= $endTime ) {
  121. $this->setContinueEnumParameter( 'continue', $languageCode );
  122. break;
  123. }
  124. $info = [];
  125. ApiResult::setArrayType( $info, 'assoc' );
  126. if ( $includeCode ) {
  127. $info['code'] = $languageCode;
  128. }
  129. if ( $includeBcp47 ) {
  130. $bcp47 = LanguageCode::bcp47( $languageCode );
  131. $info['bcp47'] = $bcp47;
  132. }
  133. if ( $includeDir ) {
  134. $dir = Language::factory( $languageCode )->getDir();
  135. $info['dir'] = $dir;
  136. }
  137. if ( $includeAutonym ) {
  138. $autonym = Language::fetchLanguageName(
  139. $languageCode,
  140. Language::AS_AUTONYMS,
  141. $include
  142. );
  143. $info['autonym'] = $autonym;
  144. }
  145. if ( $includeName ) {
  146. $name = Language::fetchLanguageName(
  147. $languageCode,
  148. $targetLanguageCode,
  149. $include
  150. );
  151. $info['name'] = $name;
  152. }
  153. if ( $includeFallbacks ) {
  154. $fallbacks = Language::getFallbacksFor(
  155. $languageCode,
  156. // allow users to distinguish between implicit and explicit 'en' fallbacks
  157. Language::STRICT_FALLBACKS
  158. );
  159. ApiResult::setIndexedTagName( $fallbacks, 'fb' );
  160. $info['fallbacks'] = $fallbacks;
  161. }
  162. if ( $includeVariants ) {
  163. $variants = Language::factory( $languageCode )->getVariants();
  164. ApiResult::setIndexedTagName( $variants, 'var' );
  165. $info['variants'] = $variants;
  166. }
  167. $fit = $result->addValue( $rootPath, $languageCode, $info );
  168. if ( !$fit ) {
  169. $this->setContinueEnumParameter( 'continue', $languageCode );
  170. break;
  171. }
  172. }
  173. }
  174. public function getCacheMode( $params ) {
  175. return 'public';
  176. }
  177. public function getAllowedParams() {
  178. return [
  179. 'prop' => [
  180. self::PARAM_DFLT => 'code',
  181. self::PARAM_ISMULTI => true,
  182. self::PARAM_TYPE => [
  183. 'code',
  184. 'bcp47',
  185. 'dir',
  186. 'autonym',
  187. 'name',
  188. 'fallbacks',
  189. 'variants',
  190. ],
  191. self::PARAM_HELP_MSG_PER_VALUE => [],
  192. ],
  193. 'code' => [
  194. self::PARAM_DFLT => '*',
  195. self::PARAM_ISMULTI => true,
  196. ],
  197. 'continue' => [
  198. self::PARAM_HELP_MSG => 'api-help-param-continue',
  199. ],
  200. ];
  201. }
  202. protected function getExamplesMessages() {
  203. $pathUrl = 'action=' . $this->getQuery()->getModuleName() .
  204. '&meta=' . $this->getModuleName();
  205. $pathMsg = $this->getModulePath();
  206. $prefix = $this->getModulePrefix();
  207. return [
  208. "$pathUrl"
  209. => "apihelp-$pathMsg-example-simple",
  210. "$pathUrl&{$prefix}prop=autonym|name&uselang=de"
  211. => "apihelp-$pathMsg-example-autonym-name-de",
  212. "$pathUrl&{$prefix}prop=fallbacks|variants&{$prefix}code=oc"
  213. => "apihelp-$pathMsg-example-fallbacks-variants-oc",
  214. "$pathUrl&{$prefix}prop=bcp47|dir"
  215. => "apihelp-$pathMsg-example-bcp47-dir",
  216. ];
  217. }
  218. }