madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r19473: LJSV-1406: Content dumps creation added;...

Committer: sbelyaev
LJSV-1406: Content dumps creation added;
U   trunk/src/jbackup/jbackup.pl
Modified: trunk/src/jbackup/jbackup.pl
===================================================================
--- trunk/src/jbackup/jbackup.pl	2011-07-14 03:00:30 UTC (rev 19472)
+++ trunk/src/jbackup/jbackup.pl	2011-07-14 07:58:13 UTC (rev 19473)
@@ -70,6 +70,7 @@
 use XML::Parser;
 use Digest::MD5 qw(md5_hex);
 use Term::ReadKey;
+use POSIX;
 
 # get options
 my %opts;
@@ -89,7 +90,8 @@
                "md5pass=s" => \$opts{md5password},
                "alter-security=s" => \$opts{alter_security},
                "confirm-alter" => \$opts{confirm_alter},
-               "no-comments" => \$opts{no_comments},);
+               "no-comments" => \$opts{no_comments},
+               "content-dump-dir=s" => \$opts{content_dump},);
 
 # hit up .jbackup for other options
 if (-e "$ENV{HOME}/.jbackup") {
@@ -143,6 +145,11 @@
     --dump=X        Dump data in the specified format: html, xml, raw.
     --publiconly    When dumping, only spit out public entries.
     --file=X        Dump to specified file instead of the screen.
+    
+  Debug options:
+    --content-dump-dir=X Specify the directory to use for content dumps.
+                         Content dump creates if a problem occurred during
+                         journal dumping.
 
 Usage examples:
 
@@ -345,7 +352,15 @@
 
         # now we want to XML parse this
         my $parser = new XML::Parser(Handlers => { Start => $meta_handler, Char => $meta_content, End => $meta_closer });
-        $parser->parse($content);
+        eval {
+            $parser->parse($content);
+        };
+
+        if ($@) {
+            my $error = $@;
+            print STDERR  "Dump journal failed. Reason is: $@\n";
+            create_content_dump($error, $content, 1)
+        }
     }
     $bak{"comment:ids"} = join ',', keys %meta;
     $bak{"usermap:userids"} = join ',', @userids;
@@ -391,8 +406,16 @@
 
         # now we want to XML parse this
         my $parser = new XML::Parser(Handlers => { Start => $body_handler, Char => $body_content, End => $body_closer });
-        $parser->parse($content);
+        eval {
+            $parser->parse($content);
+        };
 
+        if ($@) {
+            my $error = $@;
+            print STDERR  "Dump journal failed. Reason is: $@\n";
+            create_content_dump($error, $content, 2)
+        }
+
         # now at this point what we have to decide whether we should loop again for more metadata
         $lastid += $COMMENTS_FETCH_BODY;
         last unless $lastid < $server_max_id;
@@ -412,6 +435,27 @@
     $bak{"comment:lastid"} = $lastid if $count;
 }
 
+
+# save a content to dump file
+sub create_content_dump {
+    my ($error, $content, $id) = @_;
+    
+    if ($opts{content_dump}) {
+        my $dump_file = $opts{content_dump} . "/lj_" . $opts{user} . "_".
+                        strftime('%d_%b_%Y__%H_%M', localtime) .
+                        ".$id.xml";
+        open(my $fh, ">$dump_file")
+            or die "error: failed to create $dump_file $dump_file";
+
+        print $fh $content;
+        close $fh;
+
+        print "-------------------------------------------------\n";
+        print "content dump created: $dump_file\n";
+        print "-------------------------------------------------\n";
+    }
+}
+
 # save an event that we get
 sub save_event {
     my $data = shift;

Tags: livejournal, madeon, pl
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments