This file is indexed.

/usr/share/perl5/Socialtext/EditPage.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package Socialtext::EditPage;
use warnings;
use strict;
use Carp qw/croak/;
use File::Temp;
use Socialtext::Resting::DefaultRester;
use Socialtext::Resting;
use JSON::XS;

=head1 NAME

Socialtext::EditPage - Edit a wiki page using your favourite EDITOR.

=cut

our $VERSION = '0.04';

=head1 SYNOPSIS

Fetch a page, edit it, and then post it.

    use Socialtext::EditPage;

    # The rester is set with the server and workspace
    my $rester = Socialtext::Resting->new(%opts);

    my $s = Socialtext::EditPage->new(rester => $rester);
    $s->edit_page('Snakes on a Plane');

=head1 FUNCTIONS

=head2 new( %opts )

Arguments:

=over 4

=item rester

Users must provide a Socialtext::Resting object setup to use the desired 
workspace and server.

=item pull_includes

If true, C<include> wafls will be inlined into the page as extraclude
blocks.

=back

=cut

sub new {
    my ($class, %opts) = @_;
    $opts{rester} ||= Socialtext::Resting::DefaultRester->new(%opts);
    my $self = { %opts };
    bless $self, $class;
    return $self;
}

=head2 C<edit_page( %opts )>

This method will fetch the page content, and then run $EDITOR on the file.
After the file has been edited, it will be put back on the wiki server.

Arguments:

=over 4

=item page

The name of the page you wish to edit.

=item callback

If supplied, callback will be called after the page has been edited.  This
function will be passed the edited content, and should return the content to
be put onto the server.

=item summary_callback

If supplied, callback will be called after the page has been edit.  This 
function should return the edit summary text for this edit, if desired.

=item tags 

If supplied, these tags will be applied to the page after it is updated.

=item output

If supplied, the page will be saved to the given file instead of edited. 
The page will not be uploaded to the server.

=item template

If specified, this page will be used as the template for a new page.

=back

=cut

sub edit_page {
    my $self = shift;
    my %args = @_;
    my $page = $self->{page} = delete $args{page};
    croak "page is mandatory" unless $page;

    my $rester = $self->{rester};
    my $content = $self->_get_page($page);

    my $tags = delete $args{tags} || [];
    if ($args{template}) {
        if ($rester->response->code eq '404') {
            $content = $self->_get_page($args{template});
        }
        else {
            print "Not using template '$args{template}' - page already "
                 . "exists.\n";
        }
        $rester->accept('text/plain');
        my @tmpl_tags = grep { !/^template$/ } $rester->get_pagetags($args{template});
        push @$tags, @tmpl_tags;
    }

    if ($args{output}) {
        _write_file($args{output}, $content);
        print "Wrote $page content to $args{output}\n";
        return;
    }

    my $orig_content = $content;
    my $edit_summary;
    while (1) {
        my $new_content = $content;
        $new_content = $self->_pre_process_special_wafls($new_content);
        $new_content = $self->_edit_content($new_content);

        if ($orig_content eq $new_content) {
            print "$page did not change.\n";
            return;
        }

        $new_content = $args{callback}->($new_content) if $args{callback};

        $new_content = $self->_process_special_wafls($new_content);

        $edit_summary ||= $args{summary_callback}->() if $args{summary_callback};

        eval { 
            $page =~ s#/#-#g; # cannot have /'s in the page name
            $rester->put_page($page, {
                    content => $new_content,
                    date => scalar(gmtime),
                    ($edit_summary ? (edit_summary => $edit_summary) : ()),
                }
            );
        };
        last unless $@;
        if ($@ =~ /412/) { # collision detected!
            print "A collision was detected.  I will merge the changes and "
                  . "re-open your editor.\nHit enter.\n";
            sleep 2;
            print "Merging...\n";
            $orig_content = $self->_get_page($page);
            my $updated_file = _write_file(undef, $orig_content);
            my $orig_file = _write_file(undef, $content);
            my $our_file  = _write_file(undef, $new_content);

            # merge the content and re-edit
            # XXX: STDERR is not redirected.  Should use IPC::Run.  However,
            # it's nice to be able to create pages w/ quotes and other shell
            # characters in their name.
            system(qw(merge -L yours -L original -L), "new edit", $our_file,
                   $orig_file, $updated_file);

            $content = _read_file($our_file);
        }
        else {
            $self->_handle_error($@, $page, $new_content);
        }
    }

    if ($tags) {
        $tags = [$tags] unless ref($tags) eq 'ARRAY';
        for my $tag (@$tags) {
            print "Putting page tag $tag on $page\n";
            $rester->put_pagetag($page, $tag);
        }
    }

    print "Updated page $page\n";
}

=head2 C<edit_last_page( %opts )>

This method will retrieve a last of all pages tagged with the supplied
tag, and then open the latest one for edit.

Arguments are passed through to edit_page(), accept for:

=over 4

=item tag

The name of the tag you wish to edit.

=back

=cut

sub edit_last_page {
    my $self = shift;
    my %opts = @_;

    my $tag = delete $opts{tag} || croak "tag is mandatory";
    my $rester = $self->{rester};
    $rester->accept('application/json');
    my $pages = decode_json($rester->get_taggedpages($tag));
    unless (@$pages) {
        die "No pages found tagged '$tag'\n";
    }
    my @pages = sort { $b->{modified_time} <=> $a->{modified_time} }
                @$pages;
    my $newest_page = shift @pages;
    print "Editing '$newest_page->{name}'\n";
    $self->edit_page(page => $newest_page->{page_id}, %opts);
}

sub _get_page {
    my $self = shift;
    my $page_name = shift;
    my $rester = $self->{rester};
    $rester->accept('text/x.socialtext-wiki');

    my $page = $rester->get_page($page_name);

    if ($self->{pull_includes}) {
        while ($page =~ m/({include:?\s+\[([^\]]+)\]\s*}\n)/smg) {
            my $included_page = $2;
            my ($match_start, $match_size) = ($-[0], $+[0] - $-[0]);
            print "Pulling include in [$page_name] - [$included_page]\n";
            my $pulled_content = $self->_get_page($included_page);
            chomp $pulled_content;
            my $included_content = ".pulled-extraclude [$included_page]\n"
                                   . "$pulled_content\n"
                                   . ".pulled-extraclude\n";

            substr($page, $match_start, $match_size) = $included_content;
        }
    }

    return $page;
}

sub _edit_content {
    my $self = shift;
    my $content = shift;

    my $workspace = $self->{rester}->workspace || '';
    (my $page = $self->{page}) =~ s#/#_#g;
    my $filename = File::Temp->new( 
        TEMPLATE => "$workspace.$page.XXXX", 
        SUFFIX => '.wiki' 
    );
    _write_file($filename, $content);
    my $editor   = $ENV{EDITOR} || '/usr/bin/vim';

    system( $editor, $filename );

    return _read_file($filename);
}

{
    my @special_wafls = (
        [ '.extraclude' => '.e-x-t-r-a-c-l-u-d-e' ],
        [ '.pulled-extraclude' => '.extraclude', 'pre-only' ],
    );

    sub _pre_process_special_wafls {
        my $self = shift;
        my $text = shift;

        # Escape special wafls
        for my $w (@special_wafls) {
            my $wafl = $w->[0];
            my $expanded = $w->[1];
            $text =~ s/\Q$wafl\E\b/$expanded/g;
        }
        return $text;
    }

    sub _process_special_wafls {
        my $self = shift;
        my $text = shift;
        my $rester = $self->{rester};

        my $included_content = sub {
            my $type    = lc shift;
            my $name    = shift;
            my $newline = shift || '';

            if ($type eq 'clude') {
                return "{include: [$name]}\n";
            }
            elsif ($type eq 'link') {
                return "[$name]$newline";
            }
            die "Unknown extrathing: $type";
        };

        while ($text =~ s/\.extra(clude|link)\s     # $1 is title
                          \[([^\]]+)\]              # $2 is [name]
                          (\n?)                      # $3 is extra newline
                          (.+?)
                          \.extra(?:clude|link)\n
                         /$included_content->($1, $2, $3)/ismex) {
            my ($page, $new_content) = ($2, $4);
            print "Putting extraclude '$page'\n";
            eval {
                $rester->put_page($page, $new_content);
            };
            $self->_handle_error($@, $page, $new_content) if $@;
        }

        # Unescape special wafls
        for my $w (@special_wafls) {
            next if $w->[2];
            my $wafl = $w->[0];
            my $expanded = $w->[1];
            $text =~ s/\Q$expanded\E\b/$wafl/ig;
        }

        return $text;
    }

}

sub _handle_error {
    my ($self, $err, $page, $content) = @_;
    my $file = Socialtext::Resting::_name_to_id($page) . ".sav";
    my $i = 0;
    while (-f $file) {
        $i++;
        $file =~ s/\.sav(?:\.\d+)?$/\.sav\.$i/;
    }
    warn "Failed to write '$page', saving to $file\n";
    _write_file($file, $content);
    die "$err\n";
}

sub _write_file {
    my ($filename, $content) = @_;
    $filename ||= File::Temp->new( SUFFIX => '.wiki' );
    open(my $fh, ">$filename") or die "Can't open $filename: $!";
    print $fh $content || '';
    close $fh or die "Can't write $filename: $!";
    return $filename;
}

sub _read_file {
    my $filename = shift;
    open(my $fh, $filename) or die "unable to open $filename $!\n";
    my $new_content;
    {
        local $/;
        $new_content = <$fh>;
    }
    close $fh;
    return $new_content;
}

=head1 AUTHOR

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

=head1 BUGS

Please report any bugs or feature requests to
C<bug-socialtext-editpage at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Socialtext-Resting-Utils>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Socialtext::EditPage

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Socialtext-Resting-Utils>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Socialtext-Resting-Utils>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Socialtext-Resting-Utils>

=item * Search CPAN

L<http://search.cpan.org/dist/Socialtext-Resting-Utils>

=back

=head1 ACKNOWLEDGEMENTS

=head1 COPYRIGHT & LICENSE

Copyright 2006 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;