MediaWiki  REL1_22
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 ) {
00103             break;
00104         }
00105 
00106         $n = substr( $n, 0, $idx );
00107         $maxStrip -= 1;
00108     }
00109 
00110     return false;
00111 }
00112 
00113 # FIXME: Access the api in a saner way and performing just one query (preferably batching files too).
00114 function getFileCommentFromSourceWiki( $wiki_host, $file ) {
00115     $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=comment';
00116     $body = Http::get( $url );
00117     if ( preg_match( '#<ii comment="([^"]*)" />#', $body, $matches ) == 0 ) {
00118         return false;
00119     }
00120 
00121     return html_entity_decode( $matches[1] );
00122 }
00123 
00124 function getFileUserFromSourceWiki( $wiki_host, $file ) {
00125     $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . rawurlencode( $file ) . '&prop=imageinfo&&iiprop=user';
00126     $body = Http::get( $url );
00127     if ( preg_match( '#<ii user="([^"]*)" />#', $body, $matches ) == 0 ) {
00128         return false;
00129     }
00130 
00131     return html_entity_decode( $matches[1] );
00132 }
00133