This file is indexed.

/usr/share/perl5/Socialtext/Resting/LocalCopy.pm is in libsocialtext-resting-utils-perl 0.21-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
150
package Socialtext::Resting::LocalCopy;
use strict;
use warnings;
use JSON::XS;

=head1 NAME

Socialtext::Resting::LocalCopy - Maintain a copy on disk of a workspace

=head1 SYNOPSIS

Socialtext::Resting::LocalCopy allows one to copy a workspace into files
on the local disk, and to update a workspace from files on disk.

=cut

our $VERSION = '0.01';

=head1 METHODS

=head2 new

Create a new LocalCopy object.  Requires a C<rester> parameter, which should
be a Socialtext::Rester-like object.

=cut

sub new {
    my $class = shift;
    my $self = { @_ };

    die 'rester is mandatory' unless $self->{rester};
    bless $self, $class;
    return $self;
}

=head2 pull

Reads a workspace and pulls all of the pages into files in the specified
directory.  Options are passed in as a list of named options:

=over 4

=item dir - The directory the files should be saved to.

=item tag - an optional tag.  If specified, only tagged files will be pulled.

=back

=cut

sub pull {
    my $self = shift;
    my %opts = @_;
    my $dir  = $opts{dir};
    my $tag  = $opts{tag};
    my $r    = $self->{rester};

    $r->accept('text/plain');
    my @pages = $tag ? $r->get_taggedpages($tag) : $r->get_pages();
    $r->accept('application/json');
    $r->json_verbose(1);
    for my $p (@pages) {
        print "Saving $p ...\n";
        my $obj = decode_json($r->get_page($p));

        # Trim the content
        my %to_keep = map { $_ => 1 } $self->_keys_to_keep;
        for my $k (keys %$obj) {
            delete $obj->{$k} unless $to_keep{$k};
        }

        my $wikitext_file = "$dir/$obj->{page_id}";
        open(my $fh, ">$wikitext_file") or die "Can't open $wikitext_file: $!";
        binmode $fh, ':utf8';
        print $fh delete $obj->{wikitext};
        close $fh or die "Can't write $wikitext_file: $!";

        my $json_file = "$wikitext_file.json";
        open(my $jfh, ">$json_file") or die "Can't open $json_file: $!";
        print $jfh encode_json($obj);
        close $jfh or die "Can't write $json_file: $!";
    }
}

sub _keys_to_keep { qw/page_id name wikitext tags/ }

=head2 push

Reads a directory and pushes all the files in that directory up to
the specified workspace.  Options are passed in as a list of named options:

=over 4

=item dir - The directory the files should be saved to.

=item tag - an optional tag.  If specified, only tagged files will be pushed.

Note - tag is not yet implemented.

=back

=cut

sub push {
    my $self = shift;
    my %opts = @_;
    my $dir  = $opts{dir};
    my $tag  = $opts{tag};
    my $r    = $self->{rester};

    die "Sorry - push by tag is not yet implemented!" if $tag;

    my @files = glob("$dir/*.json");
    for my $f (@files) {
        open(my $fh, $f) or die "Can't open $f: $!";
        local $/ = undef;
        my $obj = decode_json(<$fh>);
        close $fh;

        (my $wikitext_file = $f) =~ s/\.json$//;
        open(my $wtfh, $wikitext_file) or die "Can't open $wikitext_file: $!";
        $obj->{wikitext} = <$wtfh>;
        close $wtfh;

        print "Putting $obj->{page_id} ...\n";
        $r->put_page($obj->{name}, $obj->{wikitext});
        $r->put_pagetag($obj->{name}, $_) for @{ $obj->{tags} };
    }
}

=head1 BUGS

Attachments are not yet supported.
Push by tag is not yet supported.

=head1 AUTHOR

Luke Closs, C<< <luke.closs at socialtext.com> >>

=head1 COPYRIGHT & LICENSE

Copyright 2007 Luke Closs, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1;