[Mediawiki-l] Re: batch image upload

Jan 2036 at gmx.de
Thu Jan 5 15:28:25 UTC 2006


Hi Community,

this answer is related to an email from Feb 22, 2005 from FxParlant.

Full Email-Text in Archiv online: 
http://mail.wikipedia.org/pipermail/mediawiki-l/2005-February/003673.html

He postet a php-file to to a batchupload of images.

Now, I have to upload appr. 400 images to my Wiki; all images are in one Folder 
(and have different filenames).

I have access to my wiki my FTP, and to the database my phpMyAdmin or ODBC.

1. Where I have to place the quoted php-File?
2. What I need to change in the Code of that file?
3. Is it compatible with MediaWiki 1.5.4 ?

Thanks for your attention,
Jan

--------------------------------------------------

QUOTE: Code of the postet php-File:

<html>
<body>
<?php
/*
Help the batch upload images from Dokuwiki to Mediawiki
INspired from:
1 - mediawiki/include/specialUpload.php
2 - mediawiki/include/globalFunctions.php line 480 function wfImageDir( $fname )

You need:
1) to put all the image files in the same directory (no more sub directories). 
That means that you must rename your file who have the same names, to avoid any 
conflict. The best way is maybe to create a script which will add the 
subdirectories names to the image name.
*/
$filePathIn = "C:/Fxp/Prog/wikis/Mywiki/media";
$baseFileIn = "C:/Fxp/Prog/wikis/Mywiki/media";
$baseFileOut = "C:/apachefriends/xampp/htdocs/wikis/media4";
recurseAction($filePathIn);

function recurseAction($filePathIn){

global $baseFileIn,$baseFileOut;

$replace = false;
$target = "db"; // file oder db
$dh = opendir($filePathIn) or die("couldn't open directory");


     while (!(($file = readdir($dh)) === false)) {
         if (is_dir("$filePathIn/$file") & (!(($file==".")|($file=="..")))) {
             print "<b>$file</b><br />\n";
             print "<u>$filePathIn/$file</u><br />\n";
             recurseAction("$filePathIn/$file");
             }

         elseif (is_file("$filePathIn/$file") & (!(($file==".")|($file=="..")))) {
          $fileName = "$file";
          //$filePathIn = "C:/Fxp/Prog/wikis/Mywiki/media";
          list($filename, $fileext) = explode(".", $fileName);
          $file = "$filePathIn/$file";

             if ($fp = fopen($file, "r")) {
             $count++;
             //$baseName = basename($file);


             $baseName = str_replace($baseFileIn."/","", $file);
             $baseName = str_replace('cib/', '', $baseName);
             $baseName = str_replace('/', ' ', $baseName);
             $baseName = preg_replace("/([a-z])([A-Z])/", "\$1 \$2", $baseName);
             $baseName = preg_replace("/([a-zA-Z])([\d])/", "\$1 \$2", $baseName);
             $baseName = preg_replace("/([\d])([a-zA-Z])/", "\$1 \$2", $baseName);
             $baseName = str_replace("_", " ", $baseName);
             $baseName = preg_replace("/([A-Z:])([A-Z][^A-Z])/", "\$1 \$2", 
$baseName);
             $baseName = ucwords($baseName);
             print "<i>$baseName</i>";
             $baseName = str_replace(' ', '_', $baseName);


     $hash = md5( $baseName );
	$oldumask = umask(0);
     $wgUploadDirectory = 
'C:/apachefriends/xampp/htdocs/wikis/mediawiki-1.3.9/images';
	$dest = $wgUploadDirectory . '/' . $hash{0};
	if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
	$dest .= '/' . substr( $hash, 0, 2 );
	if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }

$newfile= "$dest/$baseName";
$myfile= "$file";
copy($myfile, $newfile);
echo "$newfile<br />";

$filename2 = preg_replace("/([a-z])([A-Z])/", "\$1 \$2", $filename);
$filename2 = str_replace("_", " ", $filename2);
$filename2 = preg_replace("/([A-Z:])([A-Z][^A-Z])/", "\$1 \$2", $filename2);

if (($target == "db")|($target == "both")){
     // process form
     $db = mysql_connect("localhost", "root", "xxxxx");
     $creator = "admin";
     mysql_select_db("wikidb",$db);
     if ($replace){
     $sql = "DELETE FROM tiki_pages WHERE pageName = '$baseName'";
     $result = mysql_query($sql);
     $err = mysql_error();
     printf("Erased: $baseName -- $err<br />\n");
     }
     $sql = "INSERT INTO cur (cur_namespace, cur_title,cur_text, cur_timestamp) 
VALUES('6','$baseName','$filename2', now()+0)";
     //echo = "Query:
     $result = mysql_query($sql);
     //$result = mysql_query($sql)or die(mysql_error());
     $err = mysql_error();
     printf("$count: $baseName...$err<br />\n");
     }

     elseif(($target == "file")|($target == "both")){
     $outputDir = "/out2/";
     $outputFile = ($outputDir . $baseName.".txt");
     $out = fopen($outputFile, "w");
     fwrite($out,$strFile);
     printf("$count: $baseName ...$err<br />\n");
     }


     }



}
}

}
?>
</body>
</html>




More information about the MediaWiki-l mailing list