Revision history for UpdateCategoryAction

Revision [19247]

Last edited on 2008-01-28 00:14:44 by MasinAlDujaili [Modified links pointing to docs server]

No Differences

Revision [17051]

Edited on 2007-06-06 12:40:01 by MasinAlDujaili [working code]
* Converts from original category system to the system used in PageAndCategoryDivisionInACategory.
* @version 0.1
* @license GNU General Public License
* @author {@link Masin Al-Dujaili} (original code)
* @uses Wakka::IsAdmin()
* @uses Wakka::FormClose()
* @uses Wakka::FormOpen()
* @uses Wakka::LoadAllPages()
* @uses Wakka::SavePage()
* @todo cleaning up code;
* @todo use central regex library for validation;
// constants
if (!defined('UPDATE_LABEL')) define('UPDATE_LABEL', 'Update');
if (!defined('NO_ADMIN_MESSAGE')) define('NO_ADMIN_MESSAGE', '<p>Only administrators are allowed to use this function.</p>');
if (!defined('PAGE_LOAD_ERROR_MESSAGE')) define('PAGE_LOAD_ERROR_MESSAGE', "<p>An error occured while loading Wikka's pages.</p>");
if (!defined('UPDATE_ACTION_TEXT')) define('UPDATE_ACTION_TEXT', '<p>Update the old category system using <strong>Category</strong>Name to the new category system using [[(cat)Name]].</p>'
.'<p>This action uses some constants to determine what to replace. So it might easily be adapted to other uses.</p>'
.'<p><strong>Warning</strong>: This action touches <strong>any</strong> 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.</p>');
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]);
$page['body'] = updateCategory($page['body'], $page_cats[$tag][0]);
$this->SavePage($page['tag'], $page['body'], CATEGORY_UPDATE_NOTE);
echo $this->FormOpen('', '', 'post').
'<input name="submit" type="submit" value="'.

Revision [16645]

Edited on 2007-05-29 03:58:56 by MasinAlDujaili [still empty, will be filled in the next few hours (I hope)]
=====Category Update Action=====
Documentation: UpdateCategoryActionInfo.>>This is the development page for the category update action.::c::
=====xxxxx Action=====
Documentation: xxxxxActionInfo.>>This is the development page for the xxxxx action.::c::
//This page is a **template**. It belongs to CategoryTemplate (which contains more handy templates). To create an action development page, [[ clone this page]] to a page called **xxxxxAction** (where xxxxx is the (capitalized) name of the action), replace all occurrences of 'xxxxx' with the name of the action and replace this paragraph with the actual content.//

Revision [16644]

The oldest known version of this page was created on 2007-05-29 03:01:31 by MasinAlDujaili [still empty, will be filled in the next few hours (I hope)]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki