-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented backend to create new files
- Loading branch information
Showing
9 changed files
with
378 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,7 @@ xapian/* | |
dbic.yaml | ||
root/static/js/ckeditor* | ||
specials/0blog0/* | ||
staging/* | ||
!staging/README.txt | ||
*~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
package AmuseWikiFarm::Archive::Edit; | ||
|
||
use strict; | ||
use warnings; | ||
use utf8; | ||
|
||
use Moose; | ||
use namespace::autoclean; | ||
|
||
use File::Spec; | ||
use File::Basename qw/basename/; | ||
use Cwd; | ||
use Data::Dumper; | ||
use Date::Parse; | ||
use DateTime; | ||
|
||
use Text::Amuse; | ||
use Text::Amuse::Preprocessor::HTML qw/html_to_muse/; | ||
use AmuseWikiFarm::Utils::Amuse qw/muse_naming_algo/; | ||
|
||
=head1 SYNOPSIS | ||
This class must B<not> be reusued. Create in one request and throw it | ||
away. | ||
=cut | ||
|
||
has site_schema => (is => 'ro', | ||
required => 1, | ||
isa => 'Object'); | ||
|
||
has basedir => (is => 'ro', | ||
required => 1, | ||
default => sub { getcwd }, | ||
isa => 'Str'); | ||
|
||
has error => (is => 'rw', | ||
isa => 'Str'); | ||
|
||
has redirect => (is => 'rw', | ||
isa => 'Str'); | ||
|
||
sub staging_dirname { | ||
return 'staging'; | ||
} | ||
|
||
sub staging_dir { | ||
my $self = shift; | ||
return File::Spec->catdir($self->basedir, $self->staging_dirname); | ||
} | ||
|
||
sub create_new { | ||
my ($self, $params) = @_; | ||
|
||
# assert that the directory where to put the files exists | ||
my $staging_dir = $self->staging_dir; | ||
unless (-d $staging_dir) { | ||
mkdir $self->staging_dir or die "Couldn't create $staging_dir $!"; | ||
} | ||
|
||
# URI generation | ||
my $author = $params->{author} || ""; | ||
my $title = $params->{title} || ""; | ||
my $uri; | ||
if ($params->{uri}) { | ||
$uri = muse_naming_algo($params->{uri}); | ||
# replace the params with our clean form | ||
} | ||
elsif ($title) { | ||
$uri = muse_naming_algo("$author $title"); | ||
} | ||
unless ($uri) { | ||
$self->error("Couldn't generate the uri!"); | ||
return; | ||
} | ||
# and store it in the params | ||
$params->{uri} = $uri; | ||
|
||
|
||
# check if the uri already exists. If so, throw an error and set | ||
# the redirection. | ||
|
||
my $exists = $self->site_schema->titles->find({ uri => $uri }); | ||
if ($exists) { | ||
$self->error("Such an uri already exists"); | ||
$self->redirect($exists->uri); | ||
return; | ||
} | ||
else { | ||
return $self->import_text_from_html_params($params); | ||
} | ||
} | ||
|
||
sub import_text_from_html_params { | ||
my ($self, $params) = @_; | ||
my $uri = $params->{uri}; | ||
die "uri not set!" unless $uri; | ||
|
||
# the first thing we do is to assing a path and create a revision in the db | ||
my $pubdate = str2time($params->{pubdate}) || time(); | ||
my $pubdt = DateTime->from_epoch(epoch => $pubdate); | ||
$params->{pubdate} = $pubdt->iso8601; | ||
|
||
# documented in Result::Title | ||
my $bogus = { | ||
uri => $uri, | ||
pubdate => $pubdate, | ||
f_suffix => '.muse', | ||
status => 'editing', | ||
}; | ||
|
||
foreach my $f (qw/f_path f_archive_rel_path f_timestamp | ||
f_full_path_name f_name/) { | ||
$bogus->{$f} = ''; | ||
} | ||
|
||
my $title = $self->site_schema->titles->create($bogus); | ||
|
||
|
||
# where to store the text. But we need to know which id we have in | ||
# advance, so first set f_path to the empty string. | ||
|
||
my $revision = $title->revisions->create({ | ||
updated => DateTime->now, | ||
}); | ||
|
||
$self->revision_set_file_path($revision); | ||
|
||
my $file = $revision->f_full_path_name; | ||
die "full path was not set!" unless $file; | ||
|
||
# populate the file with the parameters | ||
open (my $fh, '>:encoding(utf-8)', $file) or die "Couldn't open $file $!"; | ||
# TODO add support for uid and cat (ATR) | ||
foreach my $directive (qw/title subtitle author LISTtitle SORTauthors | ||
SORTtopics date | ||
source lang pubdate/) { | ||
|
||
$self->_add_directive($fh, $directive, $params->{$directive}); | ||
} | ||
# add the notes | ||
$self->_add_directive($fh, notes => html_to_muse($params->{notes})); | ||
|
||
# separator | ||
print $fh "\n"; | ||
|
||
my $body = html_to_muse($params->{textbody}); | ||
if (defined $body) { | ||
print $fh $body; | ||
} | ||
print $fh "\n\n"; | ||
close $fh or die $!; | ||
return $revision; | ||
} | ||
|
||
sub _add_directive { | ||
my ($self, $fh, $directive, $text) = @_; | ||
die unless $fh && $directive; | ||
return unless defined $text; | ||
# usual washing | ||
$text =~ s/\r*\n/ /gs; # it's a directive, no \n | ||
# leading and trailing spaces | ||
$text =~ s/^\s*//s; | ||
$text =~ s/\s+$//s; | ||
$text =~ s/ +/ /gs; # pack the whitespaces | ||
return unless length($text); | ||
print $fh '#' . $directive . ' ' . $text . "\n"; | ||
} | ||
|
||
sub revision_set_file_path { | ||
my ($self, $revision) = @_; | ||
die "Bad usage, missing argument (revision row)" unless $revision; | ||
|
||
my $uri = $revision->title->uri; | ||
die "Couldn't find uri for belonging title!" unless $uri; | ||
|
||
# the root | ||
my $target_dir = File::Spec->catdir($self->staging_dir, $revision->id); | ||
if (-d $target_dir) { | ||
# mm, some db backend is reusing the ids, so clean it up | ||
opendir(my $dh, $target_dir) or die "Can't open dir $target_dir $!"; | ||
my @cleanup = grep { | ||
-f File::Spec->catfile($target_dir, $_) | ||
} readdir($dh); | ||
closedir $dh; | ||
foreach my $clean (@cleanup) { | ||
warn "Removing $clean in $target_dir"; | ||
unlink File::Spec->catfile($target_dir, $clean) or warn $!; | ||
} | ||
} | ||
else { | ||
mkdir $target_dir or die "Couldn't create $target_dir $!"; | ||
} | ||
my $fullpath = File::Spec->catfile($target_dir, $uri . '.muse'); | ||
$revision->f_full_path_name($fullpath); | ||
} | ||
|
||
|
||
__PACKAGE__->meta->make_immutable; | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Here we store the ongoing revisions for the files. Directories use numbers, | ||
which point to the revision ID. Anyway, the DB knows where to find its files. | ||
|
||
|
Oops, something went wrong.