tips:clean_media_directory
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tips:clean_media_directory [2008-11-11 09:48] – 193.29.186.1 | tips:clean_media_directory [2009-06-09 15:18] (current) – 212.204.78.42 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Clean up Media Directory ====== | ||
+ | This is a little perl script for your shell to find Files in the Media-Directory which have no link to it. You can only print out a little bit statistic about these orphaned files, move them to a " | ||
+ | \\ | ||
+ | **Hint:** Its always better to have a Backup because there are no guarantee ;) And maybe its better for the first run to use the " | ||
+ | \\ | ||
+ | Feel free to send me your opinion, bug report or feature request. Thanks :) < | ||
+ | \\ | ||
+ | |||
+ | Source: | ||
+ | ---- | ||
+ | <code perl># | ||
+ | # Abstract | ||
+ | # ~~~~~~~~ | ||
+ | # Find all files in the Media Directory from dokuwiki which have no Link to itself and move or delete them. | ||
+ | # Also can print out a little bit statistics about this orphaned files. | ||
+ | # | ||
+ | # | ||
+ | # Author | ||
+ | # ~~~~~~~ | ||
+ | # Pascal Bendeich < | ||
+ | # | ||
+ | # Changes | ||
+ | # ~~~~~~~ | ||
+ | # | ||
+ | # 2008-11-10, bendeich: use of Getopt and added counter for statistics | ||
+ | # 2008-10-31, bendeich: initial release | ||
+ | # | ||
+ | # Todo | ||
+ | # ~~~~ | ||
+ | # - wating for bug reports ;) | ||
+ | |||
+ | |||
+ | |||
+ | ############## | ||
+ | # Modules | ||
+ | |||
+ | use strict; | ||
+ | use File:: | ||
+ | use File::Copy; | ||
+ | use Getopt:: | ||
+ | |||
+ | |||
+ | |||
+ | ############## | ||
+ | # Debug | ||
+ | |||
+ | #use Data:: | ||
+ | #use warnings; | ||
+ | |||
+ | |||
+ | |||
+ | ############## | ||
+ | # Variables | ||
+ | |||
+ | my $dataDir = undef; | ||
+ | my $moveDir = "/ | ||
+ | my $mediaFiles = []; | ||
+ | my $pageFiles = []; | ||
+ | my $orphaned =[] ; | ||
+ | my $help = 0; | ||
+ | my $unlink = 0; | ||
+ | my $statistics = 0; | ||
+ | my $verbose = 0; | ||
+ | my $counter = 0; | ||
+ | |||
+ | GetOptions ( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | |||
+ | |||
+ | |||
+ | ############## | ||
+ | # Functions | ||
+ | |||
+ | # Search recurse for existing files under a Directory | ||
+ | sub searchFilesRecurse { | ||
+ | |||
+ | my $dir = shift; | ||
+ | my $data = shift; | ||
+ | my $files = undef; | ||
+ | |||
+ | opendir(DIR, | ||
+ | foreach my $file (sort readdir DIR) { | ||
+ | |||
+ | # ignore . and .. | ||
+ | next if $file =~ /^\./; | ||
+ | |||
+ | # recurse | ||
+ | if (-d " | ||
+ | searchFilesRecurse(" | ||
+ | } else { | ||
+ | push @$data, " | ||
+ | } | ||
+ | } | ||
+ | # be nice | ||
+ | closedir(DIR); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | # Print Usage text | ||
+ | sub usage { | ||
+ | my $usage = << | ||
+ | Usage: cleanup_dokuwiki_mediadir.pl -datadir .../data [OPTIONS...] | ||
+ | |||
+ | Clean up orphaned files in the Media Directory of DokuWiki | ||
+ | |||
+ | Options: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | -?, -h, --help Print this help | ||
+ | |||
+ | USAGE | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | ############## | ||
+ | # Main | ||
+ | |||
+ | # Test for needed parameter or help-Flag and exit | ||
+ | if($help or !$dataDir) { | ||
+ | print usage; | ||
+ | exit 0; | ||
+ | } | ||
+ | # remove optional last slash at data-dir | ||
+ | chop $dataDir if $dataDir =~ m/ | ||
+ | |||
+ | # build Hashes with media-/ | ||
+ | searchFilesRecurse(" | ||
+ | searchFilesRecurse(" | ||
+ | |||
+ | # DEBUG | ||
+ | #print Dumper($mediaFiles); | ||
+ | #print scalar @$mediaFiles ." | ||
+ | #print Dumper($pageFiles); | ||
+ | #print scalar @$pageFiles ." | ||
+ | |||
+ | # search in all pages for a link to the mediafile | ||
+ | foreach my $file (@$mediaFiles) { | ||
+ | my $i = 0; | ||
+ | my $length = length " | ||
+ | my $paddern = substr($file, | ||
+ | $paddern =~ s/\//:/g; | ||
+ | $paddern = ":?" | ||
+ | foreach my $page (@$pageFiles) { | ||
+ | open FILE, "< | ||
+ | foreach (< | ||
+ | $i++ if (lc($_) =~ / | ||
+ | } | ||
+ | # be nice | ||
+ | close(FILE); | ||
+ | } | ||
+ | # print only statistics and ignore unlink-Flag | ||
+ | if($statistics == 1) { | ||
+ | print "Links to $file: | ||
+ | next; | ||
+ | } | ||
+ | # delete or move file if no link is found | ||
+ | if($i == 0 ) { | ||
+ | $counter++; | ||
+ | if ($unlink == 0) { # move | ||
+ | my $result = move(" | ||
+ | if ($result) { | ||
+ | print "Moved $file to $moveDir\n" | ||
+ | } else { | ||
+ | print "Move of file $file to $moveDir failed: $!\n"; | ||
+ | } | ||
+ | } elsif ($unlink == 1) { # delete | ||
+ | my $result = unlink(" | ||
+ | if ($result) { | ||
+ | print " | ||
+ | } else { | ||
+ | print " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | print "\nSum of existing mediafiles: " . scalar @$mediaFiles if $statistics == 1; | ||
+ | print " | ||
+ | |||
+ | # thats it | ||
+ | exit 0; | ||
+ | |||
+ | |||
+ | |||
+ | ############## | ||
+ | # eof | ||
+ | </ |