Whitespace fixup under tha maint directory.
[lhc/web/wiklou.git] / maintenance / importImages.inc
1 <?php
2
3 /**
4 * Support functions for the importImages script
5 *
6 * @file
7 * @ingroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 * @author Mij <mij@bitchx.it>
10 */
11
12 /**
13 * Search a directory for files with one of a set of extensions
14 *
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
18 */
19 function findFiles( $dir, $exts ) {
20 if ( is_dir( $dir ) ) {
21 $dhl = opendir( $dir );
22 if ( $dhl ) {
23 $files = array();
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;
29 }
30 }
31 return $files;
32 } else {
33 return array();
34 }
35 } else {
36 return array();
37 }
38 }
39
40 /**
41 * Split a filename into filename and extension
42 *
43 * @param $filename Filename
44 * @return array
45 */
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 );
52 }
53
54 /**
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.
62 *
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
67 */
68 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
69 if ( strpos( $auxExtension, '.' ) !== 0 ) {
70 $auxExtension = '.' . $auxExtension;
71 }
72
73 $d = dirname( $file );
74 $n = basename( $file );
75
76 while ( $maxStrip >= 0 ) {
77 $f = $d . '/' . $n . $auxExtension;
78
79 if ( file_exists( $f ) ) {
80 return $f;
81 }
82
83 $idx = strrpos( $n, '.' );
84 if ( !$idx ) break;
85
86 $n = substr( $n, 0, $idx );
87 $maxStrip -= 1;
88 }
89
90 return false;
91 }
92
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 ) {
98 return false;
99 }
100
101 return html_entity_decode( $matches[1] );
102 }
103
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 ) {
108 return false;
109 }
110
111 return html_entity_decode( $matches[1] );
112 }
113