Author: nick Date: 2006-08-10 17:22:53 +0100 (Thu, 10 Aug 2006) New Revision: 833
Added: trunk/t/72_node_moderation.t Modified: trunk/MANIFEST trunk/lib/OpenGuides.pm Log: Finish moderation support
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2006-08-10 15:02:47 UTC (rev 832) +++ trunk/MANIFEST 2006-08-10 16:22:53 UTC (rev 833) @@ -104,6 +104,7 @@ t/68_bug_website_displayed.t t/70_admin_home.t t/71_missing_metadata.t +t/72_node_moderation.t t/73_toggle_moderation.t t/templates/15_test.tt wiki.cgi
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2006-08-10 15:02:47 UTC (rev 832) +++ trunk/lib/OpenGuides.pm 2006-08-10 16:22:53 UTC (rev 833) @@ -988,29 +988,13 @@ if $metadata{longitude_unmunged};
# Check to make sure all the indexable nodes are created - # TODO: Split this off into another function - # TODO: Don't run this if the node requires moderation (only do it after someone moderates) - foreach my $type (qw(Category Locale)) { - my $lctype = lc($type); - foreach my $index (@{$metadata{$lctype}}) { - $index =~ s/(.*)/\u$1/; - my $node = $type . " " . $index; - # Uppercase the node name before checking for existence - $node =~ s/ (\S+)/ \u$1/g; - unless ( $wiki->node_exists($node) ) { - my $category = $type eq "Category" ? "Category" : "Locales"; - $wiki->write_node( - $node, - "@INDEX_LINK [[$node]]", - undef, - { - username => "Auto Create", - comment => "Auto created $lctype stub page", - category => $category - } - ); - } - } + # Skip this for nodes needing moderation - this occurs for them once + # they've been moderated + unless($wiki->node_required_moderation($node)) { + $self->_autoCreateCategoryLocale( + id => $node, + metadata => %metadata + ); }
foreach my $var ( qw( summary username comment edit_type ) ) { @@ -1060,7 +1044,52 @@ } }
+=item B<_autoCreateCategoryLocale>
+ $guide->_autoCreateCategoryLocale( + id => "FAQ", + metadata => %metadata, + ); + +When a new node is added, or a previously un-moderated node is moderated, +identifies if any of its Categories or Locales are missing, and creates them. + +For nodes not requiring moderation, should be called on writing the node +For nodes requiring moderation, should only be called on moderation +=cut +sub _autoCreateCategoryLocale { + my ($self, %args) = @_; + + my $wiki = $self->wiki; + my $id = $args{'id'}; + my %metadata = %{$args{'metadata'}}; + + # Check to make sure all the indexable nodes are created + foreach my $type (qw(Category Locale)) { + my $lctype = lc($type); + foreach my $index (@{$metadata{$lctype}}) { + $index =~ s/(.*)/\u$1/; + my $node = $type . " " . $index; + # Uppercase the node name before checking for existence + $node =~ s/ (\S+)/ \u$1/g; + unless ( $wiki->node_exists($node) ) { + my $category = $type eq "Category" ? "Category" : "Locales"; + $wiki->write_node( + $node, + "@INDEX_LINK [[$node]]", + undef, + { + username => "Auto Create", + comment => "Auto created $lctype stub page", + category => $category + } + ); + } + } + } +} + + =item B<delete_node>
$guide->delete_node( @@ -1211,6 +1240,85 @@ } }
+=item B<moderate_node> + + $guide->moderate_node( + id => "FAQ", + version => 12, + password => "beer", + ); + +Marks a version of a node as moderated. Will also auto-create and Locales +and Categories for the newly moderated version. + +If C<password> is not supplied then a form for entering the password +will be displayed. +=cut +sub moderate_node { + my ($self, %args) = @_; + my $node = $args{id} or croak "No node ID supplied for node moderation"; + my $version = $args{version} or croak "No node version supplied for node moderation"; + my $return_tt_vars = $args{return_tt_vars} || 0; + my $return_output = $args{return_output} || 0; + + # Set up the TT variables + my %tt_vars = ( + not_editable => 1, + not_deletable => 1, + deter_robots => 1, + version => $version, + moderation_action => 'moderate_node', + moderation_url_args => 'action=moderate_node&version='.$version + ); + + my $password = $args{password}; + + if ($password) { + if ($password ne $self->config->admin_pass) { + return %tt_vars if $return_tt_vars; + my $output = $self->process_template( + id => $node, + template => "moderate_password_wrong.tt", + tt_vars => %tt_vars, + ); + return $output if $return_output; + print $output; + } else { + $self->wiki->moderate_node( + name => $node, + version => $version + ); + + # Create any categories or locales for it + my %details = $self->wiki->retrieve_node( + name => $node, + version => $version + ); + $self->_autoCreateCategoryLocale( + id => $node, + metadata => $details{'metadata'} + ); + + # Send back to the admin interface + my $script_url = $self->config->script_url; + my $script_name = $self->config->script_name; + my $q = CGI->new; + my $output = $q->redirect( $script_url.$script_name."?action=admin&moderation=moderated" ); + return $output if $return_output; + print $output; + } + } else { + return %tt_vars if $return_tt_vars; + my $output = $self->process_template( + id => $node, + template => "moderate_confirm.tt", + tt_vars => %tt_vars, + ); + return $output if $return_output; + print $output; + } +} + =item B<show_missing_metadata> Search for nodes which don't have a certain kind of metadata. Optionally also excludes Locales and Categories
Added: trunk/t/72_node_moderation.t =================================================================== --- trunk/t/72_node_moderation.t 2006-08-10 15:02:47 UTC (rev 832) +++ trunk/t/72_node_moderation.t 2006-08-10 16:22:53 UTC (rev 833) @@ -0,0 +1,117 @@ +use strict; +use Wiki::Toolkit::Setup::SQLite; +use OpenGuides; +use OpenGuides::Test; +use Test::More tests => 19; + +eval { require DBD::SQLite; }; +my $have_sqlite = $@ ? 0 : 1; + +SKIP: { + skip "DBD::SQLite not installed - no database to test with", 19 + unless $have_sqlite; + + Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); + my $config = OpenGuides::Test->make_basic_config; + $config->script_name( "wiki.cgi" ); + $config->script_url( "http://example.com/" ); + my $guide = OpenGuides->new( config => $config ); + isa_ok( $guide, "OpenGuides" ); + my $wiki = $guide->wiki; + isa_ok( $wiki, "Wiki::Toolkit" ); + + # Clear out the database from any previous runs. + foreach my $del_node ( $wiki->list_all_nodes ) { + print "# Deleting node $del_node\n"; + $wiki->delete_node( $del_node ) or die "Can't delete $del_node"; + } + + + # Add a page + my $q = CGI->new; + $q->param( -name => "content", -value => "foo" ); + $q->param( -name => "categories", -value => "Alpha" ); + $q->param( -name => "locales", -value => "" ); + $q->param( -name => "phone", -value => "" ); + $q->param( -name => "fax", -value => "" ); + $q->param( -name => "website", -value => "" ); + $q->param( -name => "hours_text", -value => "" ); + $q->param( -name => "address", -value => "" ); + $q->param( -name => "postcode", -value => "" ); + $q->param( -name => "map_link", -value => "" ); + $q->param( -name => "os_x", -value => "" ); + $q->param( -name => "os_y", -value => "" ); + $q->param( -name => "username", -value => "bob" ); + $q->param( -name => "comment", -value => "foo" ); + $q->param( -name => "edit_type", -value => "Minor tidying" ); + $ENV{REMOTE_ADDR} = "127.0.0.1"; + + my $output = $guide->commit_node( + return_output => 1, + id => "Wombats", + cgi_obj => $q, + ); + + # Check it's moderated + my %details = $wiki->retrieve_node("Wombats"); + is($details{'moderated'}, 1, "Moderated"); + is($wiki->node_required_moderation("Wombats"), 0, "No moderation"); + + # Turn on moderation + $wiki->set_node_moderation( + name => "Wombats", + required => 1, + ); + is($wiki->node_required_moderation("Wombats"), 1, "Moderation"); + + + # Now add a new one, with new categories and locales + $q->param( -name => "categories", -value => "Alpha\r\nBeta" ); + $q->param( -name => "locales", -value => "Hello" ); + $q->param( -name => "edit_type", -value => "Normal edit" ); + $q->param( -name => "checksum", -value => $details{checksum} ); + my $output = $guide->commit_node( + return_output => 1, + id => "Wombats", + cgi_obj => $q, + ); + + # Check that the current version is still 1 + %details = $wiki->retrieve_node("Wombats"); + is($details{'version'}, 1, "Still on v1"); + is($details{'moderated'}, 1, "v1 Moderated"); + + # Check that version 2 isn't moderated + my %v2 = $wiki->retrieve_node(name=>"Wombats",version=>2); + is($v2{'version'}, 2, "Is v2"); + is($v2{'moderated'}, 0, "Not moderated"); + + # Check that the new categories and locales aren't there + is(1, $wiki->node_exists("Category Alpha"), "Right Categories"); + is(0, $wiki->node_exists("Category Beta"), "Right Categories"); + is(0, $wiki->node_exists("Locale Hello"), "Right Locales"); + + + # Moderate + $guide->moderate_node( + id => "Wombats", + version => 2, + password => $guide->config->admin_pass + ); + + + # Check that the current version is 2 + %details = $wiki->retrieve_node(name=>"Wombats"); + is($details{'version'}, 2, "Is v2"); + is($details{'moderated'}, 1, "Moderated"); + + # Check that version 2 is moderated + %v2 = $wiki->retrieve_node(name=>"Wombats",version=>2); + is($v2{'version'}, 2, "Is v2"); + is($v2{'moderated'}, 1, "Moderated"); + + # Check that the new categories and locales exist + is(1, $wiki->node_exists("Category Alpha"), "Right Categories"); + is(1, $wiki->node_exists("Category Beta"), "Right Categories"); + is(1, $wiki->node_exists("Locale Hello"), "Right Locales"); +}