X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=maintenance%2FimportImages.inc;h=fc9428d7aa0568e4fdb2a3b657c702a807f7b411;hb=5b66e4504dd543faa3a85d4a0a88daca991c223d;hp=ac5d14432b2aba4d688dab8e8d2050a55a14c91c;hpb=c0b50eb47d2f73cb47a8f1c2e09fb089f7319587;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/importImages.inc b/maintenance/importImages.inc index ac5d14432b..fc9428d7aa 100644 --- a/maintenance/importImages.inc +++ b/maintenance/importImages.inc @@ -26,43 +26,49 @@ /** * Search a directory for files with one of a set of extensions * - * @param $dir string Path to directory to search - * @param $exts Array of extensions to search for - * @return mixed Array of filenames on success, or false on failure + * @param string $dir Path to directory to search + * @param array $exts Array of extensions to search for + * @param bool $recurse Search subdirectories recursively + * @return array|bool Array of filenames on success, or false on failure */ -function findFiles( $dir, $exts ) { +function findFiles( $dir, $exts, $recurse = false ) { if ( is_dir( $dir ) ) { $dhl = opendir( $dir ); if ( $dhl ) { - $files = array(); + $files = []; while ( ( $file = readdir( $dhl ) ) !== false ) { if ( is_file( $dir . '/' . $file ) ) { list( /* $name */, $ext ) = splitFilename( $dir . '/' . $file ); - if ( array_search( strtolower( $ext ), $exts ) !== false ) + if ( array_search( strtolower( $ext ), $exts ) !== false ) { $files[] = $dir . '/' . $file; + } + } elseif ( $recurse && is_dir( $dir . '/' . $file ) && $file !== '..' && $file !== '.' ) { + $files = array_merge( $files, findFiles( $dir . '/' . $file, $exts, true ) ); } } + return $files; } else { - return array(); + return []; } } else { - return array(); + return []; } } /** * Split a filename into filename and extension * - * @param $filename string Filename + * @param string $filename Filename * @return array */ function splitFilename( $filename ) { $parts = explode( '.', $filename ); - $ext = $parts[ count( $parts ) - 1 ]; - unset( $parts[ count( $parts ) - 1 ] ); + $ext = $parts[count( $parts ) - 1]; + unset( $parts[count( $parts ) - 1] ); $fname = implode( '.', $parts ); - return array( $fname, $ext ); + + return [ $fname, $ext ]; } /** @@ -74,10 +80,10 @@ function splitFilename( $filename ) { * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2, * acme.txt would also be acceptable. * - * @param $file string base path - * @param $auxExtension string the extension to be appended to the base path - * @param $maxStrip int the maximum number of extensions to strip from the base path (default: 1) - * @return string or false + * @param string $file Base path + * @param string $auxExtension The extension to be appended to the base path + * @param int $maxStrip The maximum number of extensions to strip from the base path (default: 1) + * @return string|bool */ function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) { if ( strpos( $auxExtension, '.' ) !== 0 ) { @@ -95,7 +101,9 @@ function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) { } $idx = strrpos( $n, '.' ); - if ( !$idx ) break; + if ( !$idx ) { + break; + } $n = substr( $n, 0, $idx ); $maxStrip -= 1; @@ -104,10 +112,12 @@ function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) { return false; } -# FIXME: Access the api in a saner way and performing just one query (preferably batching files too). +# @todo FIXME: Access the api in a saner way and performing just one query +# (preferably batching files too). function getFileCommentFromSourceWiki( $wiki_host, $file ) { - $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment'; - $body = Http::get( $url ); + $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' + . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment'; + $body = Http::get( $url, [], __METHOD__ ); if ( preg_match( '##', $body, $matches ) == 0 ) { return false; } @@ -116,12 +126,12 @@ function getFileCommentFromSourceWiki( $wiki_host, $file ) { } function getFileUserFromSourceWiki( $wiki_host, $file ) { - $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user'; - $body = Http::get( $url ); + $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' + . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user'; + $body = Http::get( $url, [], __METHOD__ ); if ( preg_match( '##', $body, $matches ) == 0 ) { return false; } return html_entity_decode( $matches[1] ); } -