Creating namespaces

Creating a new namespace is essential if your plugin adds a new type of content to the site. Namespace creation takes just a little more work than adding a special page.

Register the namespace

First, register the namespace during the session_started hook.

$plugins->attachHook('session_started', 'myplugin_setup();');
 
function myplugin_setup()
{
  global $paths;
  // First parameter is the namespace ID ([A-z0-9_]), second is URL prefix (almost any character)
  $paths->create_namespace('MyNamespace', 'MyNamespace:');
}

Create a new Namespace class

Second, you need to create a Namespace class that extends Namespace_Default. It should have, at a minimum, a constructor (the __construct() method) and the methods build_cdata() and send(). The constructor takes three parameters: $page_id, $namespace, and $revision_id.

The constructor

The constructor should set $this->page_id to the first parameter, $this->namespace to the second, and $this->revision_id to the third. It can also perform any validation or setup that you need.

You don't need to use the same page_id or even namespace that is passed here. For example, the Newsboy plugin changes the page ID to allow for more search engine friendly URLs, without the user even noticing anything on the frontend.

build_cdata()

The build_cdata() method should build a cdata array and set it to $this->cdata. In Enano, the cdata structure is based on the contents of the pages table, but the three most important keys are name, urlname, and namespace. The name can be whatever name you want, but urlname and namespace should be the same as what you set in the constructor.

This is also a good place to set $this->exists, a Boolean value that should be true if your page exists, and false otherwise.

Also use this method to set the value $this->title, which can be a reference to $this->cdata['name']. This is where the title of the page is retrieved for internal links.

Be sure to call Namespace_Default::bake_cdata() on your cdata array when you are done, to fill in any missing values and calculate final values for everything.

send()

The send() method should simply echo out your content. It needs to check $this->exists itself - Enano does not do this for you.

It is important that you properly set $this->exists because this is how Enano determines whether an internal link should be a "redlink", or labeled as a link to a page that does not yet exist.

Example

The following example namespace contains two pages: Test1 and Test2. All other pages are reported as nonexistent.

class Namespace_Example extends Namespace_Default
{
  function __construct($page_id, $namespace, $rev = 0)
  {
    $this->page_id = $page_id;
    $this->namespace = $namespace;
    $this->revision_id = $rev;
 
    $this->build_cdata();
  }
 
  function build_cdata()
  {
    $this->exists = in_array($this->page_id, array('Test1', 'Test2'));
 
    $this->cdata = array(
      'name' => str_replace('_', ' ', dirtify_page_id($this->page_id)),
      'urlname' => $this->page_id,
      'namespace' => $this->namespace,
    );
 
    $this->cdata = Namespace_Default::bake_cdata($this->cdata);
    $this->title =& $this->cdata['name'];
  }
 
  function send()
  {
    global $output;
 
    $output->header();
    if ( $this->exists )
    {
      $output->set_title('Testing namespaces [' . $this->page_id . ']');
      echo '<p>It does exist!</p>';
    }
    else
    {
      $this->error_404();
    }
    $output->footer();
  }
}

Other methods

You can create other methods to be used in your namespace.

error_404()

Called when the page doesn't exist, just echo out a nice looking "Not Found" page.

get_redirect()

If this method exists, you can return an associative array with the keys page_id and namespace to redirect the user Enano-wiki-style to another page. The namespace doesn't have to be your own - in fact, when you return something from here, your current instance will be destroyed and a new Namespace object will be instanciated.

Example

class Namespace_Example extends Namespace_Default
{
  // First three methods are just the same as the above
 
  function __construct($page_id, $namespace, $rev = 0)
  {
    $this->page_id = $page_id;
    $this->namespace = $namespace;
    $this->revision_id = $rev;
 
    $this->build_cdata();
  }
 
  function build_cdata()
  {
    $this->exists = in_array($this->page_id, array('Test1', 'Test2'));
 
    $this->cdata = array(
      'name' => str_replace('_', ' ', dirtify_page_id($this->page_id)),
      'urlname' => $this->page_id,
      'namespace' => $this->namespace,
    );
 
    $this->cdata = Namespace_Default::bake_cdata($this->cdata);
    $this->title =& $this->cdata['name'];
  }
 
  function send()
  {
    global $output;
 
    $output->header();
    if ( $this->exists )
    {
      $output->set_title('Testing namespaces [' . $this->page_id . ']');
      echo '<p>It does exist!</p>';
    }
    else
    {
      $this->error_404();
    }
    $output->footer();
  }
 
  // Our redirect method
  function get_redirect()
  {
    if ( $this->page_id == 'Test3' )
    {
      return array(
          'page_id' => 'Test1',
          'namespace' => $this->namespace
        );
    }
  }
}

Categories: (Uncategorized)