[dev] Re: [cvs] commit: horde/lib/Category sql.php

Chuck Hagenbuch chuck@horde.org
Tue, 2 Jul 2002 23:29:25 -0400


Quoting Chuck Hagenbuch <chuck@horde.org>:

>   Log:
>   - revamp how Category_sql stores data. only one table is used now, and
>   we avoid fetching the entire group tree when it's not necessary now.

After adding the new category_parents field to your horde_categories table, 
the following code will update your DB to the new format. Afterwards, you 
can drop the horde_categories_categories table.

<?php

define('HORDE_BASE', dirname(__FILE__));
require_once HORDE_BASE . '/lib/base.php';
require_once HORDE_BASE . '/lib/Menu.php';
require_once HORDE_BASE . '/lib/Perms.php';
require_once HORDE_BASE . '/lib/CategoryTree.php';

migrate('horde.perms');
migrate('horde.groups');

function migrate($group)
{
    global $conf;
    $cat = &Category::singleton($conf['category']['driver'],
                                array_merge($conf['category']['params'],
                                            array('group' => $group)));
    global $db;
    $db = $cat->_db;

    global $parents;
    $parents = array();
    foo($cat->export(CATEGORY_FORMAT_TREE), array(''));

    foreach ($parents as $id => $parent) {
        $db->query('update horde_categories set category_parents = ' . $db-
>quote($parent) . ' where category_id = ' . $id);
    }
}

function foo($vals, $curparents)
{
    global $db, $parents;
    foreach ($vals as $key => $val) {
        $id = $db->getOne('select category_id from horde_categories where 
category_name = ' . $db->quote($key));
        $parents[$id] = implode(':', $curparents);
        if (is_array($val)) {
            if (!empty($id)) {
                array_push($curparents, $id);
            }
            foo($val, $curparents);
        }
    }
}
?>


-chuck

--
Charles Hagenbuch, <chuck@horde.org>
"After a few minutes the most aromatic and nice smelling Italian coffee 
 will come out of the exhaustpipe." - Our stove-top espresso pot