Skip to content

Instantly share code, notes, and snippets.

@Elsensee
Last active August 29, 2015 14:08
Show Gist options
  • Save Elsensee/8f904e6fb596c0925725 to your computer and use it in GitHub Desktop.
Save Elsensee/8f904e6fb596c0925725 to your computer and use it in GitHub Desktop.
Fix left and right IDs in Ascraeus (3.1) - Code is from STK for 3.0 but changed to do it's work in 3.1 (and to fit into a single file which can just be called)
<?php
/**
*
* @package Support Toolkit - Fix Left/Right ID's
* @version $Id$
* @copyright (c) 2009 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
$changes_made = false;
/**
* Fix Left/Right ID's for the modules table
*/
// We need to do each module class separately, so we need to get a list of each class available.
$result = $db->sql_query('SELECT DISTINCT(module_class) FROM ' . MODULES_TABLE);
while ($row = $db->sql_fetchrow($result))
{
// Now start fixing the modules for this class
$i = 1;
$where = array('module_class = \'' . $row['module_class'] .'\'');
$changes_made = ((fixem($i, 'module_id', MODULES_TABLE, 0, $where)) || $changes_made) ? true : false;
}
$db->sql_freeresult($result);
/**
* Fix the Left/Right ID's for the forums table
*/
$i = 1;
$changes_made = ((fixem($i, 'forum_id', FORUMS_TABLE)) || $changes_made) ? true : false;
// Purge the cache so the next time a page with modules is viewed it is not getting an old version from the cache
$cache->purge();
if ($changes_made)
{
echo 'Successfully repaired left/right-ids.';
}
else
{
echo 'No changes with left/right-ids necessary.';
}
garbage_collection();
exit;
// I <3 recursion.
function fixem(&$i, $pkey, $table, $parent_id = 0, $where = array())
{
global $db;
$changes_made = false;
$sql = 'SELECT ' . $pkey . ', left_id, right_id FROM ' . $table . '
WHERE parent_id = ' . (int) $parent_id .
((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . '
ORDER BY left_id ASC';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
// First we update the left_id for this module
if ($row['left_id'] != $i)
{
$db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('left_id' => $i)) . " WHERE $pkey = {$row[$pkey]}");
$changes_made = true;
}
$i++;
// Then we go through any children and update their left/right id's
$changes_made = ((fixem($i, $pkey, $table, $row[$pkey], $where)) || $changes_made) ? true : false;
// Then we come back and update the right_id for this module
if ($row['right_id'] != $i)
{
$db->sql_query('UPDATE ' . $table . ' SET ' . $db->sql_build_array('UPDATE', array('right_id' => $i)) . " WHERE $pkey = {$row[$pkey]}");
$changes_made = true;
}
$i++;
}
$db->sql_freeresult($result);
return $changes_made;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment