AutoCreateCategory

Summary: Automatically creates category pages for all tags in the page text
Version: 0.9 2008-11-27
Prerequisites:
Status: BETA
Maintainer: Michael Selbmann

Questions answered by this recipe

This section is optional; use it to indicate the types of questions (if any) this recipe is intended to answer. How can I create pages for all categories which are linked in my text?

Description

Automatically creates category pages for all categories which are linked in the page text.

Notes

I needed that for an intranet. Pages had to be linked to categories. These categories had to be listed in the navigation. pagelist didn't work.

This recipe is superseded in PmWiki 2.2.0 by using EditVariables#AutoCreate
Just add to config.php:

$AutoCreate['/^Category\./'] = array('ctime' => $Now, 'text' => $page['text']);

See also PmWiki.Categories: Show list of categories

Installation

Put this in your local/config.php.

function AutoCreateCategoryPage($pagename, &$page, &$new) 
	{
	if(isset($_POST['text']) && (strpos(@$new['text'], '[[!') == true) && (preg_match('/^Site\.|^PMWiki\.|WikiSandbox/', $pagename) == false)) 
		{
		$AutoCreateCategory = Array();
		// get text
		$txt = preg_replace('/[\n\a\t]/', '', @$new['text']);
		while(strpos($txt, '[[!'))
			{
			$curcat = preg_replace('/^.*?\[\[!(.*?)\]\].*$/', '$1', $txt);
			$txt = str_replace('[[!' . trim($curcat) . ']]', '', $txt);
			array_push($AutoCreateCategory, $curcat);
			}
		# CreateCategoryPages
		foreach((array)@$AutoCreateCategory as $x) 
			{
			$page = "Category.$x";
			if(!PageExists($page)) # Create it
				{
				$ntext['comment'] = 'Created by System';
				$ntext['ctime'] = $Now;
				}
			WritePage($page, $ntext);
			}
		}
	}
array_unshift($EditFunctions, 'AutoCreateCategoryPage');

Now you can show the category links with that code:

(:pagelist group=Category name=-Category fmt=#title list=normal :)

To build a category page, that shows all linked pages, I use a Category/GroupFooter like this:

!Pages in category: {$Name}
(:pagelist link=Category.{$Name} list=normal fmt=#group:)

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Contributor

Michael Selbmann

Comments

See discussion at AutoCreateCategory-Talk

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.