root/tags/rel0_62/wiki.cgi

Revision 1163, 14.7 kB (checked in by dom, 6 months ago)

update version numbers and copyright years

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