Tutorial: Sahana Wiki Module

(Creating the module)
Line 5: Line 5:
*A working copy of Sahana
*A working copy of Sahana
*An IDE such as Eclipse
*An IDE such as Eclipse
-
*A MySQL browser such as [http://www.phpmyadmin.net/ phpMyAdmin] (optional, but helpful)
+
*A MySQL browser such as [http://www.phpmyadmin.net/ phpMyAdmin] (optional, but helpful), set up to work with your database
=Set up the database=
=Set up the database=
We'll be storing the articles for our wiki in a database. For our example we need just one table, which we'll call <code>wiki_article</code>. We will be storing past revisions of articles, so our database needs to take this into account.
We'll be storing the articles for our wiki in a database. For our example we need just one table, which we'll call <code>wiki_article</code>. We will be storing past revisions of articles, so our database needs to take this into account.
-
Run the following SQL on your database to create the table:
+
Run the following SQL on your database to create the table. If you have phpMyAdmin installed, click the SQL tab at the top once you're in your database.
 +
 
  create table wiki_article (
  create table wiki_article (
   id mediumint(9) not null auto_increment,
   id mediumint(9) not null auto_increment,

Revision as of 19:50, 6 June 2008

As an exercise to familiarize ourselves with the Sahana framework, we wrote a wiki module.

Contents

Preparing

To follow along, you'll need:

  • A working copy of Sahana
  • An IDE such as Eclipse
  • A MySQL browser such as phpMyAdmin (optional, but helpful), set up to work with your database

Set up the database

We'll be storing the articles for our wiki in a database. For our example we need just one table, which we'll call . We will be storing past revisions of articles, so our database needs to take this into account.

Run the following SQL on your database to create the table. If you have phpMyAdmin installed, click the SQL tab at the top once you're in your database.

create table wiki_article (
 id mediumint(9) not null auto_increment,
 `name` varchar(64) not null,
 rev_date datetime default null,
 author varchar(60) default null,
 content text,
 primary key (id),
 key `name` (`name`),
 key author (author),
 fulltext key content (content)
);

You can view this in the phpMyAdmin browser and see the structure of the table it created:

Image:wiki_tablestructure.png

  • id is a unique identifier for a revision of an article.
  • rev_date is the date the revision was submitted.
  • author is the ID of the Sahana user who submitted the revision.
  • content stores the text in the article.

Creating the module

First, make the three files needed: main.inc, menu.inc, and conf.inc.

conf.inc is the simplest file, so do that first:

$conf['mod_wiki_name'] = _("Swikana");
$conf['mod_wiki_menuorder'] = 0;

These two lines are required for every module. Feel free to rename the module. Next, turn to main.inc:

include ($global ['approot']."inc/lib_menu.inc");
function shn_wiki_mainmenu(){
 global $global;
 require_once $global['approot'].'/mod/wiki/menu.inc';
}
function shn_wiki_default(){
 shn_wiki_showPage("Main Page");
}
The function delegates to menu.inc for organizational purposes. is the function used to display any wiki page. Make the function:
function shn_wiki_showPage($name = null, $revision = null){
 global $global;
}
When we go to a wiki page, the URL will have the form . When this happens, the showPage function will be called with no parameters. We capture it with:
if(!$name)
 $name = $_REQUEST['name'];

To ensure that the name is in the wiki-style format with underlines instead of spaces, encode it:

$eName = shn_wiki_encodeName($name);
We will have to define the function eventually. Next, we check the rev

Build the Wiki