=====Category Update Action===== >>==See also:== Documentation: UpdateCategoryActionInfo.>>This is the development page for the category update action.::c:: %%(php) Only administrators are allowed to use this function.

'); if (!defined('PAGE_LOAD_ERROR_MESSAGE')) define('PAGE_LOAD_ERROR_MESSAGE', "

An error occured while loading Wikka's pages.

"); if (!defined('UPDATE_ACTION_TEXT')) define('UPDATE_ACTION_TEXT', '

Update the old category system using CategoryName to the new category system using [[(cat)Name]].

' .'

This action uses some constants to determine what to replace. So it might easily be adapted to other uses.

' .'

Warning: This action touches any page that contains categories in the ' .'original Wikka way of categorizing. It might create heavy server load. It might even happen, that the script ' .'times out -- as far as I know, this might be safe. But: Whenever manipulating the database it\'s a good idea to ' .'make a backup of it before doing anything.

'); if (!defined('OLD_CATEGORY_PATTERN')) define('OLD_CATEGORY_PATTERN', '%s'); if (!defined('NEW_CATEGORY_REPLACE_PATTERN')) define('NEW_CATEGORY_REPLACE_PATTERN', '[[(cat)%s]]'); if (!defined('NEW_CATEGORY_SEARCH_PATTERN')) define('NEW_CATEGORY_SEARCH_PATTERN', '\[\[\(cat\)%s\]\]'); if (!defined('CATEGORY_UPDATE_NOTE')) define('CATEGORY_UPDATE_NOTE', 'category update'); function isNewCategory($body, $categories) { $cats = array(); foreach($categories as $cat) $cats[] = sprintf(NEW_CATEGORY_SEARCH_PATTERN,$cat); $cats = '/'.implode('|', $cats).'/'; if(preg_match_all($cats, $body, $body_cats) > 0) return TRUE; return FALSE; // if we come here, the tested body is still using the old category system } function updateCategory($body, $categories) { $old_cats = array(); $new_cats = array(); foreach($categories as $cat) { $old_cats[] = sprintf('/'.OLD_CATEGORY_PATTERN.'/',$cat); $new_cats[] = sprintf(NEW_CATEGORY_REPLACE_PATTERN,$cat); } return preg_replace($old_cats, $new_cats, $body); } if(isset($_POST['submit']) && $_POST['submit'] == UPDATE_LABEL) { if($this->isAdmin()) // begin, check for access to all our pages { if ($pages = $this->LoadAllPages()) // load all pages { // get all categories and make one big regular expression out of them $categories = $this->LoadAll("select tag from ".$this->config["table_prefix"]."pages where latest='Y' AND body LIKE '".mysql_real_escape_string('%{{category%')."' order by time desc"); foreach($categories as $category) { $cat_array[] = $category['tag']; } $cat_regex = '('.implode('|', $cat_array).')'; // gather categories the pages belong to $page_cats = array(); foreach($pages as $page) { $tag = $page['tag']; preg_match_all($cat_regex, $page['body'], $page_cats[$tag]); if(sizeof($page_cats[$tag][0])) // only process those pages which have categories { $page_cats[$tag]['isNew'] = isNewCategory($page['body'], $page_cats[$tag][0]); if(!$page_cats[$tag]['isNew']) { $page['body'] = updateCategory($page['body'], $page_cats[$tag][0]); $this->SavePage($page['tag'], $page['body'], CATEGORY_UPDATE_NOTE); } } } } else { echo PAGE_LOAD_ERROR_MESSAGE; } } else { echo NO_ADMIN_MESSAGE; } } else { echo UPDATE_ACTION_TEXT; echo $this->FormOpen('', '', 'post'). ''. $this->FormClose(); } ?> %% ::c::---- CategoryDevelopmentActions