Changeset 340


Ignore:
Timestamp:
Jul 27, 2006, 6:04:00 PM (13 years ago)
Author:
nick
Message:

Make it possible to get more details out of list_all_nodes

Location:
wiki-toolkit/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Store/Database.pm

    r338 r340  
    14161416
    14171417  my @nodes = $store->list_all_nodes();
     1418  print "First node is $nodes[0]\n";
     1419
     1420  my @nodes = $store->list_all_nodes( with_details=> 1 );
     1421  print "First node is ".$nodes[0]->{'name'}." at version ".$nodes[0]->{'version'}."\n";
    14181422
    14191423Returns a list containing the name of every existing node.  The list
    14201424won't be in any kind of order; do any sorting in your calling script.
    14211425
     1426Optionally also returns the id, version and moderation flag.
     1427
    14221428=cut
    14231429
    14241430sub list_all_nodes {
    1425     my $self = shift;
     1431    my ($self,%args) = @_;
    14261432    my $dbh = $self->dbh;
    1427     my $sql = "SELECT name FROM node;";
    1428     my $nodes = $dbh->selectall_arrayref($sql);
    1429     return ( map { $self->charset_decode( $_->[0] ) } (@$nodes) );
     1433        my @nodes;
     1434
     1435        if($args{with_details}) {
     1436                my $sql = "SELECT id, name, version, moderate FROM node;";
     1437                my $sth = $dbh->prepare( $sql );
     1438                $sth->execute();
     1439
     1440                while(my @results = $sth->fetchrow_array) {
     1441                        my %data;
     1442                        @data{ qw( node_id name version moderate ) } = @results;
     1443                        push @nodes, \%data;
     1444                }
     1445        } else {
     1446                my $sql = "SELECT name FROM node;";
     1447                my $raw_nodes = $dbh->selectall_arrayref($sql);
     1448                @nodes = ( map { $self->charset_decode( $_->[0] ) } (@$raw_nodes) );
     1449        }
     1450        return @nodes;
    14301451}
    14311452
  • wiki-toolkit/trunk/t/006_list_all.t

    r209 r340  
    66    plan skip_all => "no backends configured";
    77} else {
    8     plan tests => ( 2 * scalar @Wiki::Toolkit::TestLib::wiki_info );
     8    plan tests => ( 6 * scalar @Wiki::Toolkit::TestLib::wiki_info );
    99}
    1010
     
    1515        $wiki->write_node( $name, "content" ) or die "Can't write node";
    1616    }
     17
    1718    my @all_nodes = $wiki->list_all_nodes;
    1819    is( scalar @all_nodes, 3,
     
    2021    is_deeply( [sort @all_nodes], [ qw( Carrots Cheese Handbags ) ],
    2122               "...and the right ones, too" );
     23
     24    @all_nodes = $wiki->list_all_nodes(with_details=>1);
     25    is( scalar @all_nodes, 3,
     26        "list_all_nodes returns the right number of nodes" );
     27
     28        @all_nodes = sort { $a->{'name'} cmp $b->{'name'} } @all_nodes;
     29    is_deeply( $all_nodes[0],
     30                                { name=>'Carrots',  version=>'1', node_id=>'1', moderate=>'0' },
     31                        "...and the right ones, too" );
     32    is_deeply( $all_nodes[1],
     33                                { name=>'Cheese',   version=>'1', node_id=>'3', moderate=>'0' },
     34                        "...and the right ones, too" );
     35    is_deeply( $all_nodes[2],
     36                                { name=>'Handbags', version=>'1', node_id=>'2', moderate=>'0' },
     37                        "...and the right ones, too" );
    2238}
    2339
Note: See TracChangeset for help on using the changeset viewer.