root/tags/rel0_59/wiki.cgi

Revision 991, 13.9 kB (checked in by nick, 20 months ago)

Initial code to revert a user's edits, still needs tests

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/local/bin/perl
2
3use strict;
4use warnings;
5
6use vars qw( $VERSION );
7$VERSION = '0.59';
8
9use CGI qw/:standard/;
10use CGI::Carp qw(croak);
11use Wiki::Toolkit;
12use Geography::NationalGrid;
13use Geography::NationalGrid::GB;
14use OpenGuides;
15use OpenGuides::CGI;
16use OpenGuides::Config;
17use OpenGuides::RDF;
18use OpenGuides::Utils;
19use OpenGuides::Template;
20use Time::Piece;
21use URI::Escape;
22
23my $config_file = $ENV{OPENGUIDES_CONFIG_FILE} || "wiki.conf";
24my $config = OpenGuides::Config->new( file => $config_file );
25
26my $script_name = $config->script_name;
27my $script_url  = $config->script_url;
28
29my ($guide, $wiki, $formatter, $q);
30eval {
31    $guide = OpenGuides->new( config => $config );
32    $wiki = $guide->wiki;
33    $formatter = $wiki->formatter;
34
35    # Get CGI object, find out what to do.
36    $q = CGI->new;
37
38    # Note $q->param('keywords') gives you the entire param string.
39    # We need this to do URLs like foo.com/wiki.cgi?This_Page
40    my $node = $q->param('id') || $q->param('title') || $q->param('keywords') || '';
41    $node = $formatter->node_param_to_node_name( $node );
42
43    # If we did a post, then CGI->param probably hasn't fully de-escaped,
44    #  in the same way as a get would've done
45    my $request_method = $q->request_method() || '';
46    if($request_method eq 'POST') {
47        $node = uri_unescape($node);
48    }
49
50    # Grab our common parameters
51    my $action       = $q->param('action')  || 'display';
52    my $commit       = $q->param('Save')    || 0;
53    my $preview      = $q->param('preview') || 0;
54    my $search_terms = $q->param('terms')   || $q->param('search') || '';
55    my $format       = $q->param('format')  || '';
56    my $oldid        = $q->param('oldid')   || '';
57
58    # Alternative method of calling search, supported by usemod.
59    $action = 'search' if $q->param("search");
60
61    if ($commit) {
62        $guide->commit_node(
63                             id      => $node,
64                             cgi_obj => $q,
65                           );
66    } elsif ($preview) {
67        $guide->preview_edit(
68                              id      => $node,
69                              cgi_obj => $q,
70                            );
71    } elsif ($action eq 'edit') {
72        $guide->display_edit_form( id => $node );
73    } elsif ($action eq 'search') {
74        do_search($search_terms);
75    } elsif ($action eq 'show_backlinks') {
76        $guide->show_backlinks( id => $node );
77    } elsif ($action eq 'show_wanted_pages') {
78        show_wanted_pages();
79    } elsif ($action eq 'show_needing_moderation') {
80        show_needing_moderation();
81    } elsif ($action eq 'index') {
82        $guide->show_index(
83                            type   => $q->param("index_type") || "Full",
84                            value  => $q->param("index_value") || "",
85                            format => $format,
86                          );
87    } elsif ($action eq 'random') {
88        my @nodes = $wiki->list_all_nodes();
89        $node = $nodes[int(rand(scalar(@nodes) + 1)) + 1];
90        print $guide->redirect_to_node($node);
91    } elsif ($action eq 'find_within_distance') {
92        $guide->find_within_distance(
93                                      id => $node,
94                                      metres => $q->param("distance_in_metres")
95                                    );
96    } elsif ( $action eq 'admin' ) {
97        $guide->display_admin_interface(
98                             moderation_completed => $q->param("moderation"),
99        );
100    } elsif ( $action eq 'revert_user' ) {
101        $guide->revert_user_interface(
102                        username => $q->param("username") || "",
103                        host     => $q->param("host") || "",
104                        password => $q->param("password") || "",
105        );
106    } elsif ( $action eq 'show_missing_metadata' ) {
107        $guide->show_missing_metadata(
108                   metadata_type  => $q->param("metadata_type") || "",
109                   metadata_value => $q->param("metadata_value") || "",
110                   exclude_locales => $q->param("exclude_locales") || "",
111                   exclude_categories => $q->param("exclude_categories") || ""
112        );
113    } elsif ( $action eq 'set_moderation' ) {
114        $guide->set_node_moderation(
115                             id       => $node,
116                             password => $q->param("password") || "",
117                             moderation_flag => $q->param("moderation_flag") || "",
118                           );
119    } elsif ( $action eq 'moderate' ) {
120        $guide->moderate_node(
121                             id       => $node,
122                             version  => $q->param("version") || "",
123                             password => $q->param("password") || "",
124                           );
125    } elsif ( $action eq 'delete'
126              and ( lc($config->enable_page_deletion) eq "y"
127                    or $config->enable_page_deletion eq "1" )
128            ) {
129        $guide->delete_node(
130                             id       => $node,
131                             version  => $q->param("version") || "",
132                             password => $q->param("password") || "",
133                           );
134    } elsif ($action eq 'userstats') {
135        show_userstats(
136                        username => $q->param("username") || "",
137                        host     => $q->param("host") || "",
138                      );
139    } elsif ($action eq 'list_all_versions') {
140        if($format && ($format eq "rss" || $format eq "atom")) {
141            my %args = (
142                            feed_type    => $format,
143                            feed_listing => 'node_all_versions',
144                            name         => $node
145            );
146            $guide->display_feed( %args );
147        } else {
148            $guide->list_all_versions( id => $node );
149        }
150    } elsif ($action eq 'rc') {
151        if ($format && $format eq 'rss') {
152            my $feed = $q->param("feed");
153            if ( !defined $feed or $feed eq "recent_changes" ) {
154                my %args = map { $_ => ( $q->param($_) || "" ) }
155                           qw( feed items days ignore_minor_edits username
156                               category locale );
157                $args{feed_type} = 'rss';
158                $args{feed_listing} = 'recent_changes';
159                $guide->display_feed( %args );
160            } elsif ( $feed eq "chef_dan" ) {
161                display_node_rdf( node => $node );
162            } else {
163                croak "Unknown RSS feed type '$feed'";
164            }
165        } elsif ($format && $format eq 'atom') {
166            my %args = map { $_ => ( $q->param($_) || "" ) }
167                       qw( feed items days ignore_minor_edits username
168                           category locale );
169            $args{feed_type} = 'atom';
170            $args{feed_listing} = 'recent_changes';
171            $guide->display_feed( %args );
172        } else {
173            $guide->display_node( id => 'RecentChanges' );
174        }
175    } elsif ($action eq 'rss') {
176        my $redir_target = $script_url . $script_name . '?action=rc;format=rss';
177        my %args = map { $_ => ( $q->param($_) || "" ) }
178            qw( feed items days ignore_minor_edits username
179                category locale );
180        foreach my $arg (sort keys %args) {
181            if ($args{$arg} ne "") {
182                $redir_target .= ";$arg=$args{$arg}";
183            }
184        }
185        print $q->redirect( $redir_target );
186    } elsif ($action eq 'about') {
187        $guide->display_about(format => $format);
188    } else { # Default is to display a node.
189        if ( $format and $format eq "rdf" ) {
190            display_node_rdf( node => $node );
191        } elsif ( $format and $format eq 'raw' ) {
192            $guide->display_node(
193                                  id       => $node,
194                                  format   => 'raw',
195                                );
196        } else {
197            my $version = $q->param("version");
198            my $other_ver = $q->param("diffversion");
199            if ( $other_ver ) {
200                $guide->display_diffs(
201                                       id            => $node,
202                                       version       => $version,
203                                       other_version => $other_ver,
204                                     );
205            } else {
206                my $redirect;
207               
208                if ((defined $q->param("redirect")) && ($q->param("redirect") == 0)) {
209                  $redirect = 0;
210                } else {
211                  $redirect = 1;               
212                }
213               
214                $guide->display_node(
215                                      id       => $node,
216                                      version  => $version,
217                                      oldid    => $oldid,
218                                      redirect => $redirect,
219                                    );
220            }
221        }
222    }
223};
224
225if ($@) {
226    my $error = $@;
227    warn $error;
228    print $q->header;
229    my $contact_email = $config->contact_email;
230    print qq(<html><head><title>ERROR</title></head><body>
231             <p>Sorry!  Something went wrong.  Please contact the
232             Wiki administrator at
233             <a href="mailto:$contact_email">$contact_email</a> and quote
234             the following error message:</p><blockquote>)
235      . $q->escapeHTML($error)
236      . qq(</blockquote><p><a href="$script_name">Return to the Wiki home page</a>
237           </body></html>);
238}
239
240############################ subroutines ###################################
241
242sub show_userstats {
243    my %args = @_;
244    my ($username, $host) = @args{ qw( username host ) };
245    croak "No username or host supplied to show_userstats"
246        unless $username or $host;
247    my %criteria = ( last_n_changes => 5 );
248    $criteria{metadata_was} = $username ? { username => $username }
249                                        : { host     => $host };
250    my @nodes = $wiki->list_recent_changes( %criteria );
251    @nodes = map { {name          => $q->escapeHTML($_->{name}),
252            last_modified => $q->escapeHTML($_->{last_modified}),
253            comment       => $q->escapeHTML($_->{metadata}{comment}[0]),
254            url           => "$script_name?"
255          . $q->escape($formatter->node_name_to_node_param($_->{name})) }
256                       } @nodes;
257    my %tt_vars = ( last_five_nodes => \@nodes,
258            username        => $username,
259            username_param  => $wiki->formatter->node_name_to_node_param($username),
260                    host            => $host,
261                  );
262    process_template("userstats.tt", "", \%tt_vars);
263}
264
265sub get_cookie {
266    my $pref_name = shift or return "";
267    my %cookie_data = OpenGuides::CGI->get_prefs_from_cookie(config=>$config);
268    return $cookie_data{$pref_name};
269}
270
271sub display_node_rdf {
272    my %args = @_;
273    my $rdf_writer = OpenGuides::RDF->new( wiki      => $wiki,
274                       config => $config );
275    print "Content-type: application/rdf+xml\n\n";
276    print $rdf_writer->emit_rdfxml( node => $args{node} );
277}
278
279sub process_template {
280    my ($template, $node, $vars, $conf, $omit_header) = @_;
281
282    my %output_conf = ( wiki     => $wiki,
283            config   => $config,
284                        node     => $node,
285            template => $template,
286            vars     => $vars
287    );
288    $output_conf{content_type} = "" if $omit_header; # defaults otherwise
289    print OpenGuides::Template->output( %output_conf );
290}
291
292
293sub do_search {
294    my $terms = shift;
295    my %finds = $wiki->search_nodes($terms);
296#    my @sorted = sort { $finds{$a} cmp $finds{$b} } keys %finds;
297    my @sorted = sort keys %finds;
298    my @results = map {
299        { url   => $q->escape($formatter->node_name_to_node_param($_)),
300      title => $q->escapeHTML($_)
301        }             } @sorted;
302    my %tt_vars = ( results      => \@results,
303                    num_results  => scalar @results,
304                    not_editable => 1,
305                    search_terms => $q->escapeHTML($terms) );
306    process_template("search_results.tt", "", \%tt_vars);
307}
308
309sub show_wanted_pages {
310    my @dangling = $wiki->list_dangling_links;
311    my @wanted;
312    my %backlinks_count;
313    foreach my $node_name (@dangling) {
314        $backlinks_count{$node_name} = scalar($wiki->list_backlinks( node => $node_name ));
315    }
316    foreach my $node_name (sort { $backlinks_count{$b} <=> $backlinks_count{$a} } @dangling) {
317        my $node_param =
318         uri_escape($formatter->node_name_to_node_param($node_name));
319        push @wanted, {
320            name          => $q->escapeHTML($node_name),
321            edit_link     => $script_url . uri_escape($script_name)
322                           . "?action=edit;id=$node_param",
323            backlink_link => $script_url . uri_escape($script_name)
324                    . "?action=show_backlinks;id=$node_param",
325            backlinks_count => $backlinks_count{$node_name}
326        };
327    }
328    process_template( "wanted_pages.tt",
329                      "",
330                      { not_editable  => 1,
331                        not_deletable => 1,
332                        deter_robots  => 1,
333                        wanted        => \@wanted } );
334}
335
336sub show_needing_moderation {
337    my @nodes = $wiki->list_unmoderated_nodes;
338
339    # Build the moderate links
340    foreach my $node (@nodes) {
341        my $node_param =
342            uri_escape($formatter->node_name_to_node_param($node->{'name'}));
343        $node->{'moderate_url'} = $script_name . "?action=moderate;id=".$node_param.";version=".$node->{'version'};
344        $node->{'view_url'} = $script_name . "?id=".$node_param.";version=".$node->{'version'};
345        $node->{'diff_url'} = $script_name . "?id=".$node_param.";version=".$node->{'moderated_version'}.";diffversion=".$node->{'version'};
346        $node->{'delete_url'} = $script_name . "?action=delete;version=".$node->{'version'}.";id=".$node_param;
347    }
348
349    process_template( "needing_moderation.tt",
350                      "",
351                      { not_editable  => 1,
352                        not_deletable => 1,
353                        deter_robots  => 1,
354                        nodes        => \@nodes } );
355}
Note: See TracBrowser for help on using the browser.