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