Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

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
Categories: Administration

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.

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

What do you think about my first recipe? Thank you. Michael Selbmann

Edit - History - Print - Recent Changes - Search
Page last modified on December 07, 2008, at 12:45 PM