MediaWiki  REL1_24
importImages.inc
Go to the documentation of this file.
00001 <?php
00034 function findFiles( $dir, $exts, $recurse = false ) {
00035     if ( is_dir( $dir ) ) {
00036         $dhl = opendir( $dir );
00037         if ( $dhl ) {
00038             $files = array();
00039             while ( ( $file = readdir( $dhl ) ) !== false ) {
00040                 if ( is_file( $dir . '/' . $file ) ) {
00041                     list( /* $name */, $ext ) = splitFilename( $dir . '/' . $file );
00042                     if ( array_search( strtolower( $ext ), $exts ) !== false ) {
00043                         $files[] = $dir . '/' . $file;
00044                     }
00045                 } elseif ( $recurse && is_dir( $dir . '/' . $file ) && $file !== '..' && $file !== '.' ) {
00046                     $files = array_merge( $files, findFiles( $dir . '/' . $file, $exts, true ) );
00047                 }
00048             }
00049 
00050             return $files;
00051         } else {
00052             return array();
00053         }
00054     } else {
00055         return array();
00056     }
00057 }
00058 
00065 function splitFilename( $filename ) {
00066     $parts = explode( '.', $filename );
00067     $ext = $parts[count( $parts ) - 1];
00068     unset( $parts[count( $parts ) - 1] );
00069     $fname = implode( '.', $parts );
00070 
00071     return array( $fname, $ext );
00072 }
00073 
00088 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
00089     if ( strpos( $auxExtension, '.' ) !== 0 ) {
00090         $auxExtension = '.' . $auxExtension;
00091     }
00092 
00093     $d = dirname( $file );
00094     $n = basename( $file );
00095 
00096     while ( $maxStrip >= 0 ) {
00097         $f = $d . '/' . $n . $auxExtension;
00098 
00099         if ( file_exists( $f ) ) {
00100             return $f;
00101         }
00102 
00103         $idx = strrpos( $n, '.' );
00104         if ( !$idx ) {
00105             break;
00106         }
00107 
00108         $n = substr( $n, 0, $idx );
00109         $maxStrip -= 1;
00110     }
00111 
00112     return false;
00113 }
00114 
00115 # @todo FIXME: Access the api in a saner way and performing just one query
00116 # (preferably batching files too).
00117 function getFileCommentFromSourceWiki( $wiki_host, $file ) {
00118     $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:'
00119         . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment';
00120     $body = Http::get( $url );
00121     if ( preg_match( '#<ii comment="([^"]*)" />#', $body, $matches ) == 0 ) {
00122         return false;
00123     }
00124 
00125     return html_entity_decode( $matches[1] );
00126 }
00127 
00128 function getFileUserFromSourceWiki( $wiki_host, $file ) {
00129     $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:'
00130         . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user';
00131     $body = Http::get( $url );
00132     if ( preg_match( '#<ii user="([^"]*)" />#', $body, $matches ) == 0 ) {
00133         return false;
00134     }
00135 
00136     return html_entity_decode( $matches[1] );
00137 }
00138