importImages.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Support functions for the importImages.php script
  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. * @ingroup Maintenance
  22. * @author Rob Church <robchur@gmail.com>
  23. * @author Mij <mij@bitchx.it>
  24. */
  25. /**
  26. * Search a directory for files with one of a set of extensions
  27. *
  28. * @param string $dir Path to directory to search
  29. * @param array $exts Array of extensions to search for
  30. * @param bool $recurse Search subdirectories recursively
  31. * @return array|bool Array of filenames on success, or false on failure
  32. */
  33. function findFiles( $dir, $exts, $recurse = false ) {
  34. if ( is_dir( $dir ) ) {
  35. $dhl = opendir( $dir );
  36. if ( $dhl ) {
  37. $files = [];
  38. while ( ( $file = readdir( $dhl ) ) !== false ) {
  39. if ( is_file( $dir . '/' . $file ) ) {
  40. list( /* $name */, $ext ) = splitFilename( $dir . '/' . $file );
  41. if ( array_search( strtolower( $ext ), $exts ) !== false ) {
  42. $files[] = $dir . '/' . $file;
  43. }
  44. } elseif ( $recurse && is_dir( $dir . '/' . $file ) && $file !== '..' && $file !== '.' ) {
  45. $files = array_merge( $files, findFiles( $dir . '/' . $file, $exts, true ) );
  46. }
  47. }
  48. return $files;
  49. } else {
  50. return [];
  51. }
  52. } else {
  53. return [];
  54. }
  55. }
  56. /**
  57. * Split a filename into filename and extension
  58. *
  59. * @param string $filename Filename
  60. * @return array
  61. */
  62. function splitFilename( $filename ) {
  63. $parts = explode( '.', $filename );
  64. $ext = $parts[count( $parts ) - 1];
  65. unset( $parts[count( $parts ) - 1] );
  66. $fname = implode( '.', $parts );
  67. return [ $fname, $ext ];
  68. }
  69. /**
  70. * Find an auxilliary file with the given extension, matching
  71. * the give base file path. $maxStrip determines how many extensions
  72. * may be stripped from the original file name before appending the
  73. * new extension. For example, with $maxStrip = 1 (the default),
  74. * file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
  75. * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
  76. * acme.txt would also be acceptable.
  77. *
  78. * @param string $file Base path
  79. * @param string $auxExtension The extension to be appended to the base path
  80. * @param int $maxStrip The maximum number of extensions to strip from the base path (default: 1)
  81. * @return string|bool
  82. */
  83. function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
  84. if ( strpos( $auxExtension, '.' ) !== 0 ) {
  85. $auxExtension = '.' . $auxExtension;
  86. }
  87. $d = dirname( $file );
  88. $n = basename( $file );
  89. while ( $maxStrip >= 0 ) {
  90. $f = $d . '/' . $n . $auxExtension;
  91. if ( file_exists( $f ) ) {
  92. return $f;
  93. }
  94. $idx = strrpos( $n, '.' );
  95. if ( !$idx ) {
  96. break;
  97. }
  98. $n = substr( $n, 0, $idx );
  99. $maxStrip -= 1;
  100. }
  101. return false;
  102. }
  103. # @todo FIXME: Access the api in a saner way and performing just one query
  104. # (preferably batching files too).
  105. function getFileCommentFromSourceWiki( $wiki_host, $file ) {
  106. $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:'
  107. . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment';
  108. $body = Http::get( $url, [], __METHOD__ );
  109. if ( preg_match( '#<ii comment="([^"]*)" />#', $body, $matches ) == 0 ) {
  110. return false;
  111. }
  112. return html_entity_decode( $matches[1] );
  113. }
  114. function getFileUserFromSourceWiki( $wiki_host, $file ) {
  115. $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:'
  116. . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user';
  117. $body = Http::get( $url, [], __METHOD__ );
  118. if ( preg_match( '#<ii user="([^"]*)" />#', $body, $matches ) == 0 ) {
  119. return false;
  120. }
  121. return html_entity_decode( $matches[1] );
  122. }