#!/usr/bin/perl use DBI; my $output_path="/var/www/dotclear/images/"; my $www_path="/images"; my $dbh = DBI->connect('DBI:mysql:dotclear:localhost', 'mysql-user','mysq-pass') or die $DBI::errstr; my $SQL = 'select post_id, post_content, post_content_wiki from dc_post where post_content_wiki like "%((http://%"'; $cursor = $dbh->prepare($SQL); $cursor->execute; while ( @columns = $cursor->fetchrow ) { my $id = $columns[0]; my $content = $columns[1]; my $wiki = $columns[2]; my $modified_wiki; $modified_wiki = $dbh->quote(&get_urls($wiki,$id)); $SQL2 = "update dc_post set post_content_wiki=$modified_wiki where post_id=$id"; $cursor2 = $dbh->prepare($SQL2); $cursor2->execute; $cursor2->finish; } $cursor->finish; $dbh->disconnect; sub get_urls { local $text=shift; local $id=shift; local $localname; $checktext=$text; # 1 detecter les URLS des images while ( $checktext =~ /(.*?\(\()(http:.*?)(\)\).*)/is ) { $beforeurl=$1; $url=$2; $afterurl=$3; $checktext=$afterurl; # 2 faire un GET et les sauver sur le disque $localname = &get_file($url,$id); $text =~ s/(.*?\(\()(http:.*?)(\)\).*)/\1$localname\3/is; } # 3 modifier le post original pour utiliser l'image locale return $text; } sub get_file { local $url=shift; local $id=shift; local $postfix; if ( $url =~ /(.*?)(\|.*)/ ) { $url=$1; $postfix=$2; } mkdir("$output_path/$id"); local @args=("/usr/bin/wget", "-q", "-P$output_path/$id/", "$url"); system(@args); local $basename; $basename = $url; $basename =~ s/.*\///; local $wwwname; $wwwname = "$www_path/$id/$basename"; return "$wwwname"."$postfix"; }