| CGI-Wiki-Plugin-Categoriser documentation | Contained in the CGI-Wiki-Plugin-Categoriser distribution. |
CGI::Wiki::Plugin::Categoriser - Category management for CGI::Wiki.
Uses node metadata to build a model of how nodes are related to each other in terms of categories.
use CGI::Wiki;
use CGI::Wiki::Plugin::Categoriser;
my $wiki = CGI::Wiki->new( ... );
$wiki->write_node( "Red Lion", "nice beer", $checksum,
{ category => [ "Pubs", "Pub Food" ] } );
$wiki->write_node( "Holborn Station", "busy at peak times", $checksum,
{ category => "Tube Station" } );
my $categoriser = CGI::Wiki::Plugin::Categoriser->new;
$wiki->register_plugin( plugin => $categoriser );
my $isa_pub = $categoriser->in_category( category => "Pubs",
node => "Red Lion" );
my @categories = $categoriser->categories( node => "Holborn Station" );
my $categoriser = CGI::Wiki::Plugin::Categoriser->new; $wiki->register_plugin( plugin => $categoriser );
my $isa_pub = $categoriser->in_category( category => "Pubs",
node => "Red Lion" );
Returns true if the node is in the category, and false otherwise. Note
that this is case-insensitive, so Pubs is the same category as
pubs. I might do something to make it plural-insensitive at some
point too.
$wiki->write_node( "Category Pub Food",
"pubs that serve food",
$checksum,
{ category => [ "Pubs", "Food", "Category" ] } );
my @subcategories = $categoriser->subcategories( category => "Pubs" );
# will return ( "Pub Food" )
# Or if you prefer CamelCase node names:
$wiki->write_node( "CategoryPubFood",
"pubs that serve food",
$checksum,
{ category => [ "Pubs", "Food", "Category" ] } );
my @subcategories = $categoriser->subcategories( category => "Pubs" );
# will return ( "PubFood" )
To add a subcategory Foo to a given category Bar, write a node
called any one of Foo, Category Foo, or CategoryFoo with
metadata indicating that it's in categories Bar and Category.
Yes, this pays specific attention to the Wiki convention of defining
categories by prefacing the category name with Category and
creating a node by that name. If different behaviour is required we
should probably implement it using an optional argument in the
constructor.
my @categories = $categoriser->categories( node => "Holborn Station" );
Returns an array of category names in no particular order.
Kake Pugh (kake@earth.li).
Copyright (C) 2003 Kake Pugh. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Improve subcategories, do category hierarchy.
| CGI-Wiki-Plugin-Categoriser documentation | Contained in the CGI-Wiki-Plugin-Categoriser distribution. |
package CGI::Wiki::Plugin::Categoriser; use strict; use CGI::Wiki::Plugin; use vars qw( $VERSION @ISA ); $VERSION = '0.02'; @ISA = qw( CGI::Wiki::Plugin );
sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; }
sub in_category { my ($self, %args) = @_; my @catarr = $self->categories( node => $args{node} ); my %categories = map { lc($_) => 1 } @catarr; return $categories{lc($args{category})}; }
sub subcategories { my ($self, %args) = @_; return () unless $args{category}; my $datastore = $self->datastore; my %cats = map { $_ => 1 } $datastore->list_nodes_by_metadata( metadata_type => "category", metadata_value => "Category" ); my @in_cat = $datastore->list_nodes_by_metadata( metadata_type => "category", metadata_value => $args{category} ); return map { s/^Category\s+//; $_ } grep { $cats{$_} } @in_cat; }
sub categories { my ($self, %args) = @_; my $dbh = $self->datastore->dbh; my $sth = $dbh->prepare( " SELECT metadata.metadata_value FROM metadata, node WHERE node.name = ? AND metadata.node = node.name AND metadata.version = node.version AND metadata.metadata_type = 'category' " ); $sth->execute( $args{node} ); my @categories; while ( my ($cat) = $sth->fetchrow_array ) { push @categories, $cat; } return @categories; }
1;