File: dir2mojomojo.pl

package info (click to toggle)
libmojomojo-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,272 kB
  • ctags: 879
  • sloc: perl: 14,055; sh: 145; xml: 120; ruby: 6; makefile: 2
file content (130 lines) | stat: -rwxr-xr-x 3,191 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env perl

BEGIN { $ENV{CATALYST_DEBUG} = 0 }
use strict;
use warnings;
use FindBin '$Bin';
use lib "$Bin/../../lib";
use MojoMojo::Schema;
use Config::JFDI;
use MojoMojo::Formatter::File;
use Path::Class ();
use Getopt::Long;
use MojoMojo::Formatter::File::Image;
#use MojoMojo;
#use MojoMojo::Model::Search;


my($DIR, $URL_DIR, $EXCLUDE, $debug, $help);
GetOptions (  'dir=s'        => \$DIR,
              'urlbase=s'    => \$URL_DIR,
              'exclude=s'    => \$EXCLUDE,
              'debug'        => \$debug,
              'help'         => \$help ) or die &Usage;


$debug=0 if ( ! $debug);

# parametres fourni ?
if ( $help || ! $DIR || ! $URL_DIR ){
  &Usage;
  exit 1;
}

$DIR =~ s/\/$//;
$DIR =~ s/~/$ENV{HOME}/;

#-----------------------------------------------------------------------------#
# Connect to database
#-----------------------------------------------------------------------------#
my $jfdi = Config::JFDI->new(name => "MojoMojo");
my $config = $jfdi->get;

my ($dsn, $user, $pass) = @ARGV;
eval {
    if (!$dsn) {
        ($dsn, $user, $pass) =
          @{$config->{'Model::DBIC'}->{'connect_info'}};
    };
};
if($@){
    die "Your DSN line in mojomojo.conf doesn't look like a valid DSN.".
      "  Add one, or pass it on the command line.";
}
die "No valid Data Source Name (DSN).\n" if !$dsn;
$dsn =~ s/__HOME__/$FindBin::Bin\/\.\./g;

my $schema = MojoMojo::Schema->connect($dsn, $user, $pass) or 
  die "Failed to connect to database";

my $person = $schema->resultset('Person')->find( 1 );





# Walk in $DIR
my $rootdir = Path::Class::dir($DIR);
my @files;
my $body;
my $urlpage;
$rootdir->recurse(callback => sub {
            my ($entry) = @_;
            return if ( defined $EXCLUDE && grep(/$EXCLUDE/, $entry ));
            push @files, $entry unless ( $entry eq $DIR );
        });


my $exclude;
$exclude="exclude=$EXCLUDE" if $EXCLUDE;
createpage($URL_DIR, "{{dir $DIR $exclude}}", $person);

foreach my $f (@files){

  next if ( ! -r $f );

  $urlpage = $f;
  $urlpage =~ s/$DIR//;
  $f =~ /.*\.(.*)$/;
  $urlpage =~ s/\./_/
    if ( ! MojoMojo::Formatter::File::Image->can_format($1) );
  $urlpage = "${URL_DIR}${urlpage}";

  if ( ref $f eq 'Path::Class::Dir'){
    $body = "{{dir $f $exclude}}";
  }
  else{
    my $plugin   = MojoMojo::Formatter::File->plugin($f);

    if ( $plugin ){
      $body = "{{file $plugin $f}}";
    }
    else {
      print STDERR "Can't find plugin for $f !!!\n";
      $body = "{{ file UNKOWN_PLUGIN $f}}";
    }
  }

  $schema->resultset('Page')->create_page($urlpage,$body, $person);
}





#-----------------------------------------------------------------------------#
# Usage
#-----------------------------------------------------------------------------#
sub Usage{

  my $usage;
  $usage .= "$0 --dir=DIRECTORY --url=URLBASE [--exclude=\"dir1 dir2\"] [--debug] [--help]\n";
  $usage .= "Ex: $0 --dir=t/var/files --url=/myfiles --exclude='\.svn|\.git'\n";
  $usage .= "Add these lines to your mojomojo.conf:\n";
  $usage .= "<Formatter::Dir>\n";
  $usage .= "    prefix_url /myfiles\n";
  $usage .= "    whitelisting t/var/files\n";
  $usage .= "</Formatter::Dir>\n";

  print $usage;
}