/
import_content.pl
executable file
·149 lines (113 loc) · 4.1 KB
/
import_content.pl
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env perl
=head1 NAME
import_content.pl - import content from a file into a MojoMojo page
=head1 SYNOPSIS
script/util/import_content.pl /path/to/page page.markdown
Since this operation is undoable, the script will prompt you to confirm that
you really want to replace the contents of the last version of /path/to/page
with what's in F<page.markdown>.
=head1 DESCRIPTION
Replace the contents of the last version of a page with the content from a
file. Useful if you want to fix a typo in a page without bumping the version
and creating yet another revision in the database. Of course, can be used for
evil, but then so could be a series of SQL commands.
=head1 AUTHORS
Dan Dascalescu (dandv), http://dandascalescu.com
=head1 LICENSE
You may distribute this code under the same terms as Perl itself.
=head1 COPYRIGHT
Copyright (C) 2010, Dan Dascalescu.
=cut
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../../lib";
use MojoMojo::Schema;
=head2 preview
Return the middle of a string. Examples:
preview('abcdefghijk', 10)
'ab [...] k'
preview('abcdefghijkl', 10), "\n";
'ab [...] l'
preview('abcdefghijkl', 11), "\n";
'ab [...] kl'
preview('abcdefghijklm', 10), "\n";
'ab [...] m'
preview('abcdef0000000000ghijklm', 10), "\n";
'ab [...] m'
=cut
sub preview {
my ($string, $limit) = @_;
my $length = length $string;
return $string if $length <= $limit;
my $middle = ' [...] ';
return
substr( $string, 0, ($limit+1 - length $middle)/2 )
. $middle
. substr( $string, $length - ($limit-1 - length $middle)/2 )
;
}
my ($page_path, $filename_content, $dsn, $user, $pass) = @ARGV;
if (!$page_path) {
die "USAGE: $0 /path/to/page filename [dsn user pass]
Replace the contents of the last version of a page with the content from a file.
\n";
}
if (!$dsn) {
# no DSN passed via the command line; attempting to read one from the config file
require Config::ZOMG;
my $config = Config::ZOMG->new(name => "MojoMojo")->load;
die "Couldn't read config file" if not keys %{$config};
eval {
if (ref $config->{'Model::DBIC'}->{'connect_info'} eq 'HASH') {
$dsn = $config->{'Model::DBIC'}->{'connect_info'}->{dsn};
$user = $config->{'Model::DBIC'}->{'connect_info'}->{user};
$pass = $config->{'Model::DBIC'}->{'connect_info'}->{password};
} else {
($dsn, $user, $pass) = @{$config->{'Model::DBIC'}->{connect_info}};
}
};
die "Your DSN settings in mojomojo.conf seem invalid: $@\n" if $@;
}
die "Couldn't find a 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 ( $path_pages, $proto_pages ) = $schema->resultset('Page')->path_pages( $page_path )
or die "Can't find page $page_path\n";
if (scalar @$proto_pages) {
die "One or more pages at the end do(es) not exist: ",
(join ", ", map { $_->{name_orig} } @$proto_pages),
"\n";
}
# Get the lastest content version of the page
my $page = $path_pages->[-1];
my $page_content_rs = $schema->resultset('Content')->search(
{
page => $page->id,
version => $page->content_version,
}
);
die "More than one 'last version' for page <$page_path>. The database may be corrupt.\n"
if $page_content_rs->count > 1;
my $page_content = $page_content_rs->first;
open my $file_content, '<:utf8', $filename_content or die $!;
my $content; {local $/; $content = <$file_content>};
print "Are you sure you want to replace\n",
preview($page_content->body, 300),
"\nwith\n",
preview($content, 300),
"\n? ('yes'/anything else): ";
my $answer = <STDIN>; chomp $answer;
if ($answer eq 'yes') {
$page_content->update(
{
body => $content,
precompiled => '', # this needs to be blanked so that MojoMojo will re-compile it
}
);
print "Done.\n";
} else {
print "Aborted.\n";
exit 1;
}