00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 $installer = $this;
00028
00029
00030 $installer->startSetup();
00031 $conn = $installer->getConnection();
00032 $conn->addColumn($installer->getTable('core_url_rewrite'), 'category_id', 'int unsigned NULL AFTER `store_id`');
00033 $conn->addColumn($installer->getTable('core_url_rewrite'), 'product_id', 'int unsigned NULL AFTER `category_id`');
00034 $installer->run("
00035 UPDATE `{$installer->getTable('core_url_rewrite')}`
00036 SET `category_id`=SUBSTRING_INDEX(SUBSTR(`id_path` FROM 10),'/',1)
00037 WHERE `id_path` LIKE 'category/%';
00038 UPDATE `{$installer->getTable('core_url_rewrite')}`
00039 SET `product_id`=SUBSTRING_INDEX(SUBSTR(`id_path` FROM 9),'/',1)
00040 WHERE `id_path` RLIKE 'product/[0-9]+$';
00041 UPDATE `{$installer->getTable('core_url_rewrite')}`
00042 SET `category_id`=SUBSTRING_INDEX(SUBSTR(`id_path` FROM 9),'/',-1),
00043 `product_id`=SUBSTRING_INDEX(SUBSTR(`id_path` FROM 9),'/',1)
00044 WHERE `id_path` LIKE 'product/%/%';
00045
00046 DROP TABLE IF EXISTS `{$installer->getTable('core_url_rewrite_temporary')}`;
00047 CREATE TABLE `{$installer->getTable('core_url_rewrite_temporary')}` (
00048 `url_rewrite_id` int unsigned not null,
00049 PRIMARY KEY(`url_rewrite_id`)
00050 ) ENGINE=MyISAM;
00051
00052 REPLACE INTO `{$installer->getTable('core_url_rewrite_temporary')}` (`url_rewrite_id`)
00053 SELECT `ur`.`url_rewrite_id` FROM `{$installer->getTable('core_url_rewrite')}` as `ur`
00054 LEFT JOIN `{$installer->getTable('catalog_category_entity')}` as `cc` ON `ur`.`category_id`=`cc`.`entity_id`
00055 WHERE `ur`.`category_id` IS NOT NULL
00056 AND `cc`.`entity_id` IS NULL;
00057 REPLACE INTO `{$installer->getTable('core_url_rewrite_temporary')}` (`url_rewrite_id`)
00058 SELECT `ur`.`url_rewrite_id` FROM `{$installer->getTable('core_url_rewrite')}` as `ur`
00059 LEFT JOIN `{$installer->getTable('catalog_product_entity')}` as `cp` ON `ur`.`product_id`=`cp`.`entity_id`
00060 WHERE `ur`.`product_id` IS NOT NULL
00061 AND `cp`.`entity_id` IS NULL;
00062 DELETE FROM `{$installer->getTable('core_url_rewrite')}` WHERE `url_rewrite_id` IN(
00063 SELECT `url_rewrite_id` FROM `{$installer->getTable('core_url_rewrite_temporary')}`
00064 );
00065 DROP TABLE IF EXISTS `{$installer->getTable('core_url_rewrite_temporary')}`;
00066 ");
00067 $conn->addConstraint(
00068 'FK_CORE_URL_REWRITE_CATEGORY', $installer->getTable('core_url_rewrite'), 'category_id',
00069 $installer->getTable('catalog_category_entity'), 'entity_id'
00070 );
00071 $conn->addConstraint(
00072 'FK_CORE_URL_REWRITE_PRODUCT', $installer->getTable('core_url_rewrite'), 'product_id',
00073 $installer->getTable('catalog_product_entity'), 'entity_id'
00074 );
00075 $installer->endSetup();