ApiQueryFileRepoInfo.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © 2013 Mark Holmquist <mtraceur@member.fsf.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @since 1.22
  22. */
  23. /**
  24. * A query action to return meta information about the foreign file repos
  25. * configured on the wiki.
  26. *
  27. * @ingroup API
  28. */
  29. class ApiQueryFileRepoInfo extends ApiQueryBase {
  30. public function __construct( ApiQuery $query, $moduleName ) {
  31. parent::__construct( $query, $moduleName, 'fri' );
  32. }
  33. protected function getInitialisedRepoGroup() {
  34. $repoGroup = RepoGroup::singleton();
  35. $repoGroup->initialiseRepos();
  36. return $repoGroup;
  37. }
  38. public function execute() {
  39. $conf = $this->getConfig();
  40. $params = $this->extractRequestParams();
  41. $props = array_flip( $params['prop'] );
  42. $repos = [];
  43. $repoGroup = $this->getInitialisedRepoGroup();
  44. $foreignTargets = $conf->get( 'ForeignUploadTargets' );
  45. $repoGroup->forEachForeignRepo( function ( $repo ) use ( &$repos, $props, $foreignTargets ) {
  46. $repoProps = $repo->getInfo();
  47. $repoProps['canUpload'] = in_array( $repoProps['name'], $foreignTargets );
  48. $repos[] = array_intersect_key( $repoProps, $props );
  49. } );
  50. $localInfo = $repoGroup->getLocalRepo()->getInfo();
  51. $localInfo['canUpload'] = $conf->get( 'EnableUploads' );
  52. $repos[] = array_intersect_key( $localInfo, $props );
  53. $result = $this->getResult();
  54. ApiResult::setIndexedTagName( $repos, 'repo' );
  55. ApiResult::setArrayTypeRecursive( $repos, 'assoc' );
  56. ApiResult::setArrayType( $repos, 'array' );
  57. $result->addValue( [ 'query' ], 'repos', $repos );
  58. }
  59. public function getCacheMode( $params ) {
  60. return 'public';
  61. }
  62. public function getAllowedParams() {
  63. $props = $this->getProps();
  64. return [
  65. 'prop' => [
  66. ApiBase::PARAM_DFLT => implode( '|', $props ),
  67. ApiBase::PARAM_ISMULTI => true,
  68. ApiBase::PARAM_TYPE => $props,
  69. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  70. ],
  71. ];
  72. }
  73. public function getProps() {
  74. $props = [];
  75. $repoGroup = $this->getInitialisedRepoGroup();
  76. $repoGroup->forEachForeignRepo( function ( $repo ) use ( &$props ) {
  77. $props = array_merge( $props, array_keys( $repo->getInfo() ) );
  78. } );
  79. $propValues = array_values( array_unique( array_merge(
  80. $props,
  81. array_keys( $repoGroup->getLocalRepo()->getInfo() )
  82. ) ) );
  83. $propValues[] = 'canUpload';
  84. sort( $propValues );
  85. return $propValues;
  86. }
  87. protected function getExamplesMessages() {
  88. $examples = [];
  89. $props = array_intersect( [ 'apiurl', 'name', 'displayname' ], $this->getProps() );
  90. if ( $props ) {
  91. $examples['action=query&meta=filerepoinfo&friprop=' . implode( '|', $props )] =
  92. 'apihelp-query+filerepoinfo-example-simple';
  93. }
  94. return $examples;
  95. }
  96. public function getHelpUrls() {
  97. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerepoinfo';
  98. }
  99. }