4 * Support functions for the importImages script
8 * @author Rob Church <robchur@gmail.com>
9 * @author Mij <mij@bitchx.it>
13 * Search a directory for files with one of a set of extensions
15 * @param $dir Path to directory to search
16 * @param $exts Array of extensions to search for
17 * @return mixed Array of filenames on success, or false on failure
19 function findFiles( $dir, $exts ) {
20 if ( is_dir( $dir ) ) {
21 $dhl = opendir( $dir );
24 while ( ( $file = readdir( $dhl ) ) !== false ) {
25 if ( is_file( $dir . '/' . $file ) ) {
26 list( /* $name */, $ext ) = splitFilename( $dir . '/' . $file );
27 if ( array_search( strtolower( $ext ), $exts ) !== false )
28 $files[] = $dir . '/' . $file;
41 * Split a filename into filename and extension
43 * @param $filename Filename
46 function splitFilename( $filename ) {
47 $parts = explode( '.', $filename );
48 $ext = $parts[ count( $parts ) - 1 ];
49 unset( $parts[ count( $parts ) - 1 ] );
50 $fname = implode( '.', $parts );
51 return array( $fname, $ext );
55 * Find an auxilliary file with the given extension, matching
56 * the give base file path. $maxStrip determines how many extensions
57 * may be stripped from the original file name before appending the
58 * new extension. For example, with $maxStrip = 1 (the default),
59 * file files acme.foo.bar.txt and acme.foo.txt would be auxilliary
60 * files for acme.foo.bar and the extension ".txt". With $maxStrip = 2,
61 * acme.txt would also be acceptable.
63 * @param $file base path
64 * @param $auxExtension the extension to be appended to the base path
65 * @param $maxStrip the maximum number of extensions to strip from the base path (default: 1)
66 * @return string or false
68 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
69 if ( strpos( $auxExtension, '.' ) !== 0 ) {
70 $auxExtension = '.' . $auxExtension;
73 $d = dirname( $file );
74 $n = basename( $file );
76 while ( $maxStrip >= 0 ) {
77 $f = $d . '/' . $n . $auxExtension;
79 if ( file_exists( $f ) ) {
83 $idx = strrpos( $n, '.' );
86 $n = substr( $n, 0, $idx );
93 # FIXME: Access the api in a saner way and performing just one query (preferably batching files too).
94 function getFileCommentFromSourceWiki( $wiki_host, $file ) {
95 $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment';
96 $body = Http::get( $url );
97 if ( preg_match( '#<ii comment="([^"]*)" />#', $body, $matches ) == 0 ) {
101 return html_entity_decode( $matches[1] );
104 function getFileUserFromSourceWiki( $wiki_host, $file ) {
105 $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user';
106 $body = Http::get( $url );
107 if ( preg_match( '#<ii user="([^"]*)" />#', $body, $matches ) == 0 ) {
111 return html_entity_decode( $matches[1] );