00001 <?php
00002
00019 function findFiles( $dir, $exts ) {
00020 if( is_dir( $dir ) ) {
00021 if( $dhl = opendir( $dir ) ) {
00022 $files = array();
00023 while( ( $file = readdir( $dhl ) ) !== false ) {
00024 if( is_file( $dir . '/' . $file ) ) {
00025 list( , $ext ) = splitFilename( $dir . '/' . $file );
00026 if( array_search( strtolower( $ext ), $exts ) !== false )
00027 $files[] = $dir . '/' . $file;
00028 }
00029 }
00030 return $files;
00031 } else {
00032 return array();
00033 }
00034 } else {
00035 return array();
00036 }
00037 }
00038
00045 function splitFilename( $filename ) {
00046 $parts = explode( '.', $filename );
00047 $ext = $parts[ count( $parts ) - 1 ];
00048 unset( $parts[ count( $parts ) - 1 ] );
00049 $fname = implode( '.', $parts );
00050 return array( $fname, $ext );
00051 }
00052
00067 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
00068 if ( strpos( $auxExtension, '.' ) !== 0 ) {
00069 $auxExtension = '.' . $auxExtension;
00070 }
00071
00072 $d = dirname( $file );
00073 $n = basename( $file );
00074
00075 while ( $maxStrip >= 0 ) {
00076 $f = $d . '/' . $n . $auxExtension;
00077
00078 if ( file_exists( $f ) ) {
00079 return $f;
00080 }
00081
00082 $idx = strrpos( $n, '.' );
00083 if ( !$idx ) break;
00084
00085 $n = substr( $n, 0, $idx );
00086 $maxStrip -= 1;
00087 }
00088
00089 return false;
00090 }
00091
00092 # FIXME: Access the api in a saner way and performing just one query (preferably batching files too).
00093 function getFileCommentFromSourceWiki($wiki_host, $file) {
00094 $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment';
00095 $body = Http::get($url);
00096 if (preg_match('#<ii comment="([^"]*)" />#', $body, $matches) == 0) {
00097 return false;
00098 }
00099
00100 return html_entity_decode( $matches[1] );
00101 }
00102
00103 function getFileUserFromSourceWiki($wiki_host, $file) {
00104 $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user';
00105 $body = Http::get($url);
00106 if (preg_match('#<ii user="([^"]*)" />#', $body, $matches) == 0) {
00107 return false;
00108 }
00109
00110 return html_entity_decode( $matches[1] );
00111 }
00112