Index: trunk/admin/install/upgrades/inportal_upgrade_v1.1.4.php =================================================================== diff -u -r3244 -r3440 --- trunk/admin/install/upgrades/inportal_upgrade_v1.1.4.php (.../inportal_upgrade_v1.1.4.php) (revision 3244) +++ trunk/admin/install/upgrades/inportal_upgrade_v1.1.4.php (.../inportal_upgrade_v1.1.4.php) (revision 3440) @@ -7,7 +7,113 @@ $modules_list = GetModuleArray(); $path = $pathtoroot.$modules_list[$module_prefix].'admin/include/parser.php'; if ( file_exists($path) ) include_once($path); - + + + // !!! do not use variable $rs as record-set; + // rid - ResourceId + + $rid_modules=GetModuleArray(); + foreach($rid_modules as $rid_module_name) + { + $rid_path=$pathtoroot.$rid_module_name.'admin/include/parser.php'; + if(file_exists($rid_path)) include_once($rid_path); + } + + function dupe_ResourceIDs_check() + { + global $objModules, $ado; + + // 1. get required info about each module installed + $ret=Array(); + $table_info=$objModules->ExecuteFunction('GetModuleInfo', 'dupe_resourceids'); + + foreach($table_info as $module_name => $module_info) + { + foreach($module_info as $module_sub_info) + { + $module_sub_info['Module']=$module_name; + array_push($ret,$module_sub_info); + } + } + + // 2. build mega sql joining all tables in on and search resource id throught them + $ado->Execute('DROP TABLE IF EXISTS '.GetTablePrefix().'DupeRIDs'); + + $sql = Array(); + $sql_template = 'SELECT %1$s AS ID, \'%1$s\' AS IDField, ResourceId, \'%2$s\' AS TableName, \'%3$s\' AS Module FROM '.GetTablePrefix().'%2$s '; + foreach($ret as $sql_part) + { + $sql[]=sprintf($sql_template,$sql_part['IDField'],$sql_part['Table'],$sql_part['Module']); + } + + $module_table=GetTablePrefix().'DupeRIDs '; + + $sql='CREATE TABLE '.$module_table.' '.implode(' UNION ',$sql); + $ado->Execute($sql); + + $sql='SELECT ResourceId FROM '.$module_table.' GROUP BY ResourceId HAVING COUNT(*)>1'; + $duplicate_rids=$ado->GetCol($sql); + if(!$duplicate_rids) return; + + $sql='SELECT * FROM '.$module_table.' WHERE ResourceId IN ('.implode(',',$duplicate_rids).') ORDER BY TableName,ResourceId,ID'; + $rs=$ado->Execute($sql); + + $originals=Array(); // this are originals + $dupes=Array(); // this is dupes + + $update_tpl='UPDATE '.GetTablePrefix().'%s SET ResourceId=%s WHERE %s=%s'; + + $non_category_items = Array(); + while(!$rs->EOF) + { + $rec=&$rs->fields; + if( isset($originals[$rec['ResourceId']]) ) + { + // this is dupe + $next_id = GetNextResourceId(); + $update_sql = sprintf($update_tpl,$rec['TableName'],$next_id,$rec['IDField'],$rec['ID']); + $ado->Execute($update_sql); + $dupes[$next_id] = $rec['ID']; + if($rec['Module'] == 'In-Portal') $non_category_items[$next_id] = $rec['ID']; + } + else + { + // this is original + $originals[ $rec['ResourceId'] ]=$rec['ID']; + } + $rs->MoveNext(); + } + + if($dupes) + { + // find lost+found category + global $objCatList; + if( !(isset($objCatList) && is_object($objCatList)) ) $objCatList=new clsCatList(); + + $cat=$objCatList->GetItemByField('Name','Lost & Found'); + if( !is_object($cat) ) + { + $cat=&$objCatList->Add(0,'Lost & Found','Lost & Found Items',adodb_mktime(),0,0,2,2,2,0,'',''); + $cat->UpdateACL(); + } + + $cat_id=$cat->UniqueId(); + + // move to lost + found category + foreach($dupes as $dupe_rid => $dupe_id) + { + if( !isset($non_category_items[$dupe_rid]) ) + { + $ado->Execute('INSERT INTO '.GetTablePrefix().'CategoryItems VALUES ('.$cat_id.','.$dupe_rid.',1)'); + } + } + + } + $ado->Execute('DROP TABLE IF EXISTS '.$module_table); + } + + dupe_ResourceIDs_check(); + $module_info = m_GetModuleInfo('filenames'); $sql = 'SELECT '.$module_info['title_field'].', '.$module_info['id_field'].' FROM '.$table_prefix.$module_info['table']; $items_rs = $inst_ado->Execute($sql); @@ -21,5 +127,7 @@ $inst_ado->Execute($sql); $items_rs->MoveNext(); } - + + + ?> \ No newline at end of file