AutoCreatePages

Summary:Automatically create pages based on the name of the current page.
Version: 2008-08-24
Prerequisites:
Status: Alpha
Maintainer: Peter Bowers
Users: (View? / Edit)
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 be sure a new page is created whenever I create such-and-such of a type of page

Description

Automatically create pages when the pagename of the page being posted matches certain patterns.

Notes

Probably basing this just on patterns on the pagename being posted is insufficient... Not quite sure what other conditions would be good... Maybe "newpage" or "newgroup" or stuff like that...?

This is only for CREATING pages. If the page already exists there will be no modification of any attributes. (Ultimately I can see where some kind of "create", "append", etc as an action might be a good idea -- i.e., updating a wikitrail for the group, etc.)

Installation

Put this in your config.php, editing the $AutoCreatePages as needed:

$EditFunctions[] = 'AutoCreatePages';
$AutoCreatePages = 
   array(    
             # Create GROUP.GroupConfig for all pages if not exist
             array(   '/.*/i', 
                      '{$Group}.GroupConfig', 
                      array('ctime' => $Now, 'passwdedit' => '@admins')),
             # Create Group.NAME-Sub for any newly created pages in Test.* group
             array(   '/^Test\./i', 
                      '{$FullName}-Sub',
                      array('ctime' => $Now, 'text'=>'Test of SUB page')),
             # Create GROUP.Foo-Sub for any newly created page named *.Foo
             array(   '/\.Foo$/i',
                      '{$Group}.Foo-Sub',
                      array('ctime' => $Now))
         );
# AutoCreatePages()
# Should be placed after PostPage() in $EditFunctions (at the end usually)
# $AutoCreatePages is an array of n elements, each consisting of a page which
# might need to be created automatically.  
# The value of each element is a 3-element array:
#    1st element: The pattern which the page being posted must match
#    2nd element: The pagename format for the page which will be created
#    3rd element: An array of page attributes
# In code it looks like this:
# $AutoCreatePages = 
#    array(    array(   '/.*/i',
#                       '{$Group}.GroupConfig', 
#                       array('ctime' => $Now, 'passwdedit' => '@admins')),
#              array(   '/\.Foo$/i',
#                       '{$Group}.Foo-Sub',
#                       array('ctime' => $Now)));
function AutoCreatePages($pagename, &$page, &$new) {
    global $IsPagePosted, $AutoCreatePages;
    if (!$IsPagePosted) return;
    foreach((array)@$AutoCreatePages as $x) {
        list($pat, $pnfmt, $init) = $x;
        if (preg_match($pat, $pagename)) {
            if (is_null($init) || !$pnfmt) continue;
            $pn = MakePageName($pagename, FmtPagename($pnfmt, $pagename));
            if (!$pn || PageExists($pn)) continue;
            if (!CondAuth($pn, 'edit')) continue;
            WritePage($pn, $init);
        }
    }
}

Release Notes

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".

  • 2008-08-24: Initial release

See Also

Contributors

Function based on AutoCreateTargets() in pmwiki.php, so credit to PM.

$EditFunctions[] = 'AutoCreatePages';
$AutoCreatePages = 
   array(    array(   'pat' => '/.*/i',
                      'name' => '{$Group}.GroupConfig', 
                      'page' => array('ctime' => $Now, 'passwdedit' => '@admins')),
             array(   'pat' => '/^Test\./i',
                      'name' => '{$FullName}-Sub',
                      'page' => array('ctime' => $Now, 'text'=>'Test of SUB page')),
             array(   'pat' => '/\.Foo$/i',
                      'name' => '{$Group}.Foo-Sub',
                      'page' => array('ctime' => $Now))
         );
# AutoCreatePages()
# Should be placed after PostPage() in $EditFunctions (at the end usually)
# $AutoCreatePages is an array of n elements, each consisting of a page which
# might need to be created automatically.  
# The value of each element is a 3-element array:
#    1st element: The pattern which the page being posted must match
#    2nd element: The pagename format for the page which will be created
#    3rd element: An array of page attributes
# In code it looks like this:
# $AutoCreatePages = 
#    array(    array(   'pat' => '/.*/i',
#                       'name' => '{$Group}.GroupConfig', 
#                       'init' => array('ctime' => $Now, 'passwdedit' => '@admins')),
#              array(   'pat' => '/\.Foo$/i',
#                       'name' => '{$Group}.Foo-Sub',
#                       'init' => array('ctime' => $Now)));
function AutoCreatePages($pagename, &$page, &$new) {
    global $IsPagePosted, $AutoCreatePages;
    if (!$IsPagePosted) return;
    foreach((array)@$AutoCreatePages as $x) {
        $pat = @$x['pat'];  // match anything if not specified
        $cond = @$x['cond']; $pnfmt = $x['name']; $init = $x['init']; 
        $cmd = (@$x['cmd'] ? $x['cmd'] : 'create');
        if ($pat && !preg_match($pat, $pagename)) continue;
        if ($cond == 'newpage') {
            if ($new['ctime'] != $Now) continue;
        } elseif ($cond == 'newgroup') {
            if ($new['ctime'] != $Now) continue;
            $gn = FmtPagename("{$Group}.RecentChanges", $pagename);
            if (PageExists($gn)) continue;
        }
        if (!$init || !$pnfmt) continue;
        if (!($pn = MakePageName($pagename, FmtPagename($pnfmt, $pagename))) return;
        if (!CondAuth($pn, 'edit')) continue;
        if ($cmd == 'create') {
            if (!$pn || PageExists($pn)) continue;
            $npage = $init;
        } else {
            if (PageExists($pn))
                $npage = ReadPage($pn, READCURRENT); //abc
            else
                $npage = array('text'=>'');
            $tmp = $init; unset($tmp['text']);
            $npage = $npage + $tmp;
            if ($cmd == 'append') {
                $npage['text'] .= "\n" . $init['text'];
            } elseif ($cmd == 'prepend') {
                $npage['text'] = $init['text'] . "\n" . $npage['text'];
            }
        }
        WritePage($pn, $init);
    }
}

Comments

See discussion at AutoCreatePages-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.