Polyreviki: A Key-Value Based Wiki

Polyreviki is wiki system which holds content in key-value pairs rather than in one large chunk. It then manages each revision as a collection of these pairs. By managing the key-value pairs of an entry individually subsequent commits store only the changed values thus eliminating data duplication. If you’re just looking for the source code, skip to the end of this post.

A lot of smart programmers already use the basis of the technique that inspired Polyreviki in their own databases. A common example is a user table which would have a schema like this:

Column Name Column Type
user_id UINT(11) auto_increment
user_name VARCHAR(32)
user_password VARCHAR(32)
etc.

Data that is frequently accessed in the application or used by the database engine to filter rows is commonly stored as a table column. Additional data which is accessed less frequently but still needs to be associated with a user is stored as key-value pairs in another table.

Column Name Column Type
user_id UINT(12)
data_key VARCHAR(32)
data_value VARCHAR(128)

When a user (with unique ID 1234) has a successful login to the site their row is pulled from the main table as well as all rows where user_id = 1234. Despite being over-simplified, this example gives a good representation of how the Polyreviki storage system works. Storing user data in this format is beneficial since only the user can theoretically modify the values of any key. The advantages of turning this storage into a wiki-based system is that a user can make edits to their key-value pairs knowing they can always revert back to previous entries. It also allows administrators and moderators to make changes that are highly visible to the user. Instead of wondering why a value was changed the user can view a diff of the changes as well as read the comment which explains them.

The user storage mechanism is just one example of how the Polyreviki class could be used to extend functionality of an already existing system. Simplifying the Polyreviki class through extension to create a standard wiki clone is also possible. The following code shows how trivial it is to achieve functionality that most wiki users are accustomed to.

class StandardWiki(Polyreviki):
    prefix = "StandardWiki__"
 
    keys = {'title'  : {'regex': '^[a-zA-Z0-9_-]+$'},
            'content': {}
           }
    required = ['title', 'content']

A standard wiki only stores two pieces of data for each page, the title and content. Since the Polyreviki references entries directly only by a unique ID this presents a problem for referencing content via title. Through python’s keyword arguments you can initialize an instance without an ID but with search parameters. For the standard wiki you need only parse the title out of the URL and initialize as follows:

s = StandardWiki(title=parsed_url_title)

where parsed_url_title is the variable you stored the parsed URL title and title is the key name used to reference the wiki entry’s title. Since the nature of this storage mechanism makes it extremely difficult and wildly inefficient to enforce restraints like value uniqueness it is possible that two entries could have the same title. If this is the case a MultipleIDResultError is thrown with the matches contained in the ids variable.

While this example only scratches the surface of what is possible with the Polyreviki class it provides a good idea of how simple yet powerful it is to extend its functionality. If you’re looking for more detailed examples keep checking the repository under the examples folder.

As with all of my projects, the source code as well as issue tracking, documentation, and the repository is available in the mine. Here are some links to get you quickly started with the Polyreviki class:

Written on: 12-26-08 · No Comments » · Permalink

Leave a Reply