Wiki source for UpdateCategoryAction


Show raw source

=====Category Update Action=====
>>==See also:==
Documentation: UpdateCategoryActionInfo.>>This is the development page for the category update action.::c::

%%(php)
<?php
/**
* Converts from original category system to the system used in PageAndCategoryDivisionInACategory.
*
* @version 0.1
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
*
* @author {@link http://wikkawiki.org/MasinAlDujaili 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]);
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').
'<input name="submit" type="submit" value="'.
UPDATE_LABEL.'" />'.
$this->FormClose();
}
?>
%%
::c::----
CategoryDevelopmentActions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki