MediaWiki  REL1_21
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                         return $files;
00050                 } else {
00051                         return array();
00052                 }
00053         } else {
00054                 return array();
00055         }
00056 }
00057 
00064 function splitFilename( $filename ) {
00065         $parts = explode( '.', $filename );
00066         $ext = $parts[ count( $parts ) - 1 ];
00067         unset( $parts[ count( $parts ) - 1 ] );
00068         $fname = implode( '.', $parts );
00069         return array( $fname, $ext );
00070 }
00071 
00086 function findAuxFile( $file, $auxExtension, $maxStrip = 1 ) {
00087         if ( strpos( $auxExtension, '.' ) !== 0 ) {
00088                 $auxExtension = '.' . $auxExtension;
00089         }
00090 
00091         $d = dirname( $file );
00092         $n = basename( $file );
00093 
00094         while ( $maxStrip >= 0 ) {
00095                 $f = $d . '/' . $n . $auxExtension;
00096 
00097                 if ( file_exists( $f ) ) {
00098                         return $f;
00099                 }
00100 
00101                 $idx = strrpos( $n, '.' );
00102                 if ( !$idx ) break;
00103 
00104                 $n = substr( $n, 0, $idx );
00105                 $maxStrip -= 1;
00106         }
00107 
00108         return false;
00109 }
00110 
00111 # FIXME: Access the api in a saner way and performing just one query (preferably batching files too).
00112 function getFileCommentFromSourceWiki( $wiki_host, $file ) {
00113         $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment';
00114         $body = Http::get( $url );
00115         if ( preg_match( '#<ii comment="([^"]*)" />#', $body, $matches ) == 0 ) {
00116                 return false;
00117         }
00118 
00119         return html_entity_decode( $matches[1] );
00120 }
00121 
00122 function getFileUserFromSourceWiki( $wiki_host, $file ) {
00123         $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user';
00124         $body = Http::get( $url );
00125         if ( preg_match( '#<ii user="([^"]*)" />#', $body, $matches ) == 0 ) {
00126                 return false;
00127         }
00128 
00129         return html_entity_decode( $matches[1] );
00130 }
00131