The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

CGI::Wiki - A toolkit for building Wikis.

DESCRIPTION

Helps you develop Wikis quickly by taking care of the boring bits for you. The aim is to allow different types of backend storage and search without you having to worry about the details.

NOTE FOR PEOPLE USING THE Search::InvertedIndex BACKEND

Version 0.33 adds the capability for fuzzy title matching to CGI::Wiki::Search::SII, but you will need to re-index all existing nodes in your wiki in order to take advantage of this (see the Changes file for how).

NOTE FOR PEOPLE USING POSTGRES

I added an index to the metadata table in version 0.31 - this really speeds up RecentChanges on larger wikis. See the Changes file for details on applying the index to existing databases. I've not done any benchmarks on MySQL and SQLite yet, so I'm leaving those alone for now.

SYNOPSIS

  # Set up a wiki object with an SQLite storage backend, and an
  # inverted index/DB_File search backend.  This store/search
  # combination can be used on systems with no access to an actual
  # database server.

  my $store     = CGI::Wiki::Store::SQLite->new(
      dbname => "/home/wiki/store.db" );
  my $indexdb   = Search::InvertedIndex::DB::DB_File_SplitHash->new(
      -map_name  => "/home/wiki/indexes.db",
      -lock_mode => "EX" );
  my $search    = CGI::Wiki::Search::SII->new(
      indexdb => $indexdb );

  my $wiki      = CGI::Wiki->new( store     => $store,
                                  search    => $search );

  # Do all the CGI stuff.
  my $q      = CGI->new;
  my $action = $q->param("action");
  my $node   = $q->param("node");

  if ($action eq 'display') {
      my $raw    = $wiki->retrieve_node($node);
      my $cooked = $wiki->format($raw);
      print_page(node    => $node,
                 content => $cooked);
  } elsif ($action eq 'preview') {
      my $submitted_content = $q->param("content");
      my $preview_html      = $wiki->format($submitted_content);
      print_editform(node    => $node,
                     content => $submitted_content,
                     preview => $preview_html);
  } elsif ($action eq 'commit') {
      my $submitted_content = $q->param("content");
      my $cksum = $q->param("checksum");
      my $written = $wiki->write_node($node, $submitted_content, $cksum);
      if ($written) {
          print_success($node);
      } else {
          handle_conflict($node, $submitted_content);
      }
  }

METHODS

new
  # Set up store, search and formatter objects.
  my $store     = CGI::Wiki::Store::SQLite->new(
      dbname => "/home/wiki/store.db" );
  my $indexdb   = Search::InvertedIndex::DB::DB_File_SplitHash->new(
      -map_name  => "/home/wiki/indexes.db",
      -lock_mode => "EX" );
  my $search    = CGI::Wiki::Search::SII->new(
      indexdb => $indexdb );
  my $formatter = My::HomeMade::Formatter->new;

  my $wiki = CGI::Wiki->new(
      store     => $store,     # mandatory
      search    => $search,    # defaults to undef
      formatter => $formatter  # defaults to something suitable
  );

store must be an object of type CGI::Wiki::Store::* and search if supplied must be of type CGI::Wiki::Search::* (though this isn't checked yet - FIXME). If formatter isn't supplied, it defaults to an object of class CGI::Wiki::Formatter::Default.

You can get a searchable Wiki up and running on a system without an actual database server by using the SQLite storage backend with the SII/DB_File search backend - cut and paste the lines above for a quick start, and see CGI::Wiki::Store::SQLite, CGI::Wiki::Search::SII, and Search::InvertedIndex::DB::DB_File_SplitHash when you want to learn the details.

formatter can be any object that behaves in the right way; this essentially means that it needs to provide a format method which takes in raw text and returns the formatted version. See CGI::Wiki::Formatter::Default for an example. Note that you can create a suitable object from a sub very quickly by using Test::MockObject like so:

  my $formatter = Test::MockObject->new();
  $formatter->mock( 'format', sub { my ($self, $raw) = @_;
                                    return uc( $raw );
                                  } );

I'm not sure whether to put this in the module or not - it'd let you just supply a sub instead of an object as the formatter, but it feels wrong to be using a Test::* module in actual code.

register_plugin
  my $plugin = CGI::Wiki::Plugin::Foo->new;
  $wiki->register_plugin( plugin => $plugin );

Registers the plugin with the wiki as one that needs to be informed when we write a node. Calls the plugin class's on_register method, which should be used to check tables are set up etc.

If the plugin isa CGI::Wiki::Plugin, calls the methods set up by that parent class to let it know about the backend store, search and formatter objects.

get_registered_plugins
  my @plugins = $wiki->get_registered_plugins;

Returns an array of plugin objects.

write_node
  my $written = $wiki->write_node($node, $content, $checksum, \%metadata);
  if ($written) {
      display_node($node);
  } else {
      handle_conflict();
  }

Writes the specified content into the specified node in the backend storage; and indexes/reindexes the node in the search indexes (if a search is set up); calls post_write on any registered plugins.

Note that you can blank out a node without deleting it by passing the empty string as $content, if you want to.

If you expect the node to already exist, you must supply a checksum, and the node is write-locked until either your checksum has been proved old, or your checksum has been accepted and your change committed. If no checksum is supplied, and the node is found to already exist and be nonempty, a conflict will be raised.

The first two parameters are mandatory, the others optional. If you want to supply metadata but have no checksum (for a newly-created node), supply a checksum of undef.

Returns 1 on success, 0 on conflict, croaks on error.

Note on the metadata hashref: Any data in here that you wish to access directly later must be a key-value pair in which the value is either a scalar or a reference to an array of scalars. For example:

  $wiki->write_node( "Calthorpe Arms", "nice pub", $checksum,
                     { category => [ "Pubs", "Bloomsbury" ],
                       postcode => "WC1X 8JR" } );

  # and later

  my @nodes = $wiki->list_nodes_by_metadata(
      metadata_type  => "category",
      metadata_value => "Pubs"             );

For more advanced usage (passing data through to registered plugins) you may if you wish pass key-value pairs in which the value is a hashref or an array of hashrefs. The data in the hashrefs will not be stored as metadata; it will be checksummed and the checksum will be stored instead. Such data can only be accessed via plugins.

format
  my $cooked = $wiki->format($raw);

Passed straight through to your chosen formatter object.

store
  my $store  = $wiki->store;
  my $dbname = eval { $wiki->store->dbname; }
    or warn "Not a DB backend";

Returns the storage backend object.

search_obj
  my $search_obj = $wiki->search_obj;

Returns the search backend object.

formatter
  my $formatter = $wiki->formatter;

Returns the formatter backend object.

Methods provided by storage backend

See the docs for your chosen storage backend to see how these work.

  • delete_node (also calls the delete_node method in the search backend, if any)

  • list_all_nodes

  • list_backlinks

  • list_dangling_links

  • list_nodes_by_metadata

  • list_recent_changes

  • node_exists

  • retrieve_node

  • verify_checksum

Methods provided by search backend

See the docs for your chosen search backend to see how these work.

Methods provided by formatter backend

See the docs for your chosen formatter backend to see how these work.

  • format

SEE ALSO

For a very quick Wiki startup without any of that icky programming stuff, see Max Maischein's CGI::Wiki::Simple, which uses CGI::Wiki with CGI::Application to get you up and running in one or two minutes.

Or for the specialised application of a wiki about a city, see the OpenGuides distribution.

CGI::Wiki allows you to use different formatting modules. Text::WikiFormat might be useful for anyone wanting to write a custom formatter. Existing formatters include:

There's currently a choice of three storage backends - all database-backed.

A search backend is optional:

Standalone plugins can also be written - currently they should only read from the backend storage, but write access guidelines are coming soon. Plugins written so far and available from CPAN:

If writing a plugin you might want an easy way to run tests for it on all possible backends:

Other ways to implement Wikis in Perl include:

AUTHOR

Kake Pugh (kake@earth.li).

COPYRIGHT

     Copyright (C) 2002-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.

FEEDBACK

Please send me mail and tell me what you think of this. It's my first CPAN module, so stuff probably sucks. Tell me what sucks, send me patches, send me tests. Or if it doesn't suck, tell me that too. I love getting mail, even if all it says is "I used your thing and I like it", or "I didn't use your thing because of X".

blair christensen, Clint Moore and Max Maischein won the beer.

CREDITS

Various London.pm types helped out with code review, encouragement, JFDI, style advice, code snippets, module recommendations, and so on; far too many to name individually, but particularly Richard Clamp, Tony Fisher, Mark Fowler, and Chris Ball.

blair christensen sent patches and gave me some good ideas. chromatic continues to patiently apply my patches to Text::WikiFormat. Paul Makepeace helped me add support for connecting to non-local databases. The grubstreet team keep me well-supplied with encouragement and bug reports.

CGI::WIKI IN ACTION!

Max Maischein has set up a CGI::Wiki-based wiki describing various file formats, at http://www.corion.net/cgi-bin/wiki.cgi

I've set up a clone of grubstreet, a usemod wiki, at http://the.earth.li/~kake/cgi-bin/cgi-wiki/wiki.cgi -- it's not yet feature complete, but it's pure CGI::Wiki, using the new CGI::Wiki::Formatter::UseMod formatter. Code is at http://the.earth.li/~kake/code/cgi-wiki-usemod-emulator/

GRATUITOUS PLUG

I'm only obsessed with Wikis because of grubstreet, the Open Community Guide to London -- http://grault.net/grubstreet/