2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
39 ///////////////////////////////////////////////////////////////////////////////
41 function compare($table_name, $db_indexes, $var_indexes) {
42 global $add_index, $drop_index, $change_index;
43 if(empty($change_index))$change_index = array();
44 foreach ($var_indexes as $var_i_name=>$var_i_def) {
45 //find corresponding db index with same name
46 //else by columns in the index.
48 $var_fields_string ='';
49 if(count($var_i_def['fields'])>0)
50 $var_fields_string = implode('',$var_i_def['fields']);
51 $field_list_match = false;
52 if(isset($db_indexes[$var_i_name])) {
53 $sel_db_index = $db_indexes[$var_i_name];
54 $db_fields_string = implode('', $db_indexes[$var_i_name]['fields']);
55 if(strcasecmp($var_fields_string, $db_fields_string)==0) {
56 $field_list_match=true;
59 //search by column list.
60 foreach ($db_indexes as $db_i_name=>$db_i_def) {
61 $db_fields_string=implode('',$db_i_def['fields']);
62 if(strcasecmp($var_fields_string , $db_fields_string)==0) {
63 $sel_db_index=$db_indexes[$db_i_name];
64 $field_list_match=true;
70 //no matching index in database.
71 if(empty($sel_db_index)) {
72 $add_index[]=$GLOBALS['db']->helper->add_drop_constraint($table_name,$var_i_def);
75 if(!$field_list_match) {
76 //drop the db index and create new index based on vardef
77 $drop_index[]=$GLOBALS['db']->helper->add_drop_constraint($table_name,$sel_db_index,true);
78 $add_index[]=$GLOBALS['db']->helper->add_drop_constraint($table_name,$var_i_def);
81 //check for name match.
82 //it should not occur for indexes of type primary or unique.
83 if( $var_i_def['type'] != 'primary' and $var_i_def['type'] != 'unique' and $var_i_def['name'] != $sel_db_index['name']) {
85 $rename=$GLOBALS['db']->helper->rename_index($sel_db_index,$var_i_def,$table_name);
86 if(is_array($rename)) {
87 $change_index=array_merge($change_index,$rename);
89 $change_index[]=$rename;
95 //// END LOCAL UTILITY
96 ///////////////////////////////////////////////////////////////////////////////
99 ///////////////////////////////////////////////////////////////////////////////
101 if(!is_admin($current_user)) sugar_die("Unauthorized access to administration.");
102 set_time_limit(3600);
104 * Note: $_REQUEST['silent'] is set from ModuleInstaller::repair_indices();
106 if(!isset($_REQUEST['silent'])) $_REQUEST['silent'] = false;
110 $change_index=array();
112 global $current_user, $beanFiles, $dictionary, $sugar_config, $mod_strings;;
113 include_once ('include/database/DBManager.php');
115 $db = &DBManagerFactory::getInstance();
116 $processed_tables=array();
118 ///////////////////////////////////////////////////////////////////////////////
119 //// PROCESS MODULE BEANS
120 (function_exists('logThis')) ? logThis("found ".count($beanFiles)." Beans to process") : "";
121 (function_exists('logThis')) ? logThis("found ".count($dictionary)." Dictionary entries to process") : "";
123 foreach ($beanFiles as $beanname=>$beanpath) {
124 require_once($beanpath);
125 $focus= new $beanname();
127 //skips beans based on same tables. user, employee and group are an example.
128 if(empty($focus->table_name) || isset($processed_tables[$focus->table_name])) {
131 $processed_tables[$focus->table_name]=$focus->table_name;
134 if(!empty($dictionary[$focus->object_name]['indices'])) {
135 $indices=$dictionary[$focus->object_name]['indices'];
140 //clean vardef defintions.. removed indexes not value for this dbtype.
141 //set index name as the key.
142 $var_indices=array();
143 foreach ($indices as $definition) {
144 //database helpers do not know how to handle full text indices
145 if ($definition['type']=='fulltext') {
149 if(empty($definition['db']) or $definition['db'] == $focus->db->dbType) {
150 $var_indices[$definition['name']] = $definition;
154 $db_indices=$focus->db->helper->get_indices($focus->table_name);
155 compare($focus->table_name,$db_indices,$var_indices);
157 //// END PROCESS MODULE BEANS
158 ///////////////////////////////////////////////////////////////////////////////
161 ///////////////////////////////////////////////////////////////////////////////
162 //// PROCESS RELATIONSHIP METADATA - run thru many to many relationship files too...
163 include('modules/TableDictionary.php');
164 foreach ($dictionary as $rel=>$rel_def) {
165 if(!empty($rel_def['indices'])) {
166 $indices=$rel_def['indices'];
171 //clean vardef defintions.. removed indexes not value for this dbtype.
172 //set index name as the key.
173 $var_indices=array();
174 foreach ($indices as $definition) {
175 if(empty($definition['db']) or $definition['db'] == $focus->db->dbType) {
176 $var_indices[$definition['name']] = $definition;
180 $db_indices=$focus->db->helper->get_indices($rel_def['table']);
182 compare($rel_def['table'],$db_indices,$var_indices);
184 //// END PROCESS RELATIONSHIP METADATA
185 ///////////////////////////////////////////////////////////////////////////////
188 (function_exists('logThis')) ? logThis("RepairIndex: we have ".count($drop_index)." indices to DROP.") : "";
189 (function_exists('logThis')) ? logThis("RepairIndex: we have ".count($add_index)." indices to ADD.") : "";
190 (function_exists('logThis')) ? logThis("RepairIndex: we have ".count($change_index)." indices to CHANGE.") : "";
192 if((count($drop_index) > 0 or count($add_index) > 0 or count($change_index) > 0)) {
193 if(!isset($_REQUEST['mode']) or $_REQUEST['mode'] != 'execute') {
194 echo ($_REQUEST['silent']) ? "" : "<BR><BR><BR>";
195 echo ($_REQUEST['silent']) ? "" : "<a href='index.php?module=Administration&action=RepairIndex&mode=execute'>Execute Script</a>";
198 $focus = new Account();
199 if(count($drop_index) > 0) {
200 if(isset($_REQUEST['mode']) and $_REQUEST['mode']=='execute') {
201 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_DROPPING'];
202 foreach ($drop_index as $statement) {
203 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_EXECUTING'].$statement;
204 (function_exists('logThis')) ? logThis("RepairIndex: {$statement}") : "";
205 $focus->db->query($statement);
208 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_DROP'];
209 foreach ($drop_index as $statement) {
210 echo ($_REQUEST['silent']) ? "" : "<BR>".$statement.";";
215 if(count($add_index) > 0) {
216 if(isset($_REQUEST['mode']) and $_REQUEST['mode']=='execute') {
217 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_ADDING'];
218 foreach ($add_index as $statement) {
219 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_EXECUTING'].$statement;
220 (function_exists('logThis')) ? logThis("RepairIndex: {$statement}") : "";
221 $focus->db->query($statement);
224 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_ADD'];
225 foreach ($add_index as $statement) {
226 echo ($_REQUEST['silent']) ? "" : "<BR>".$statement.";";
230 if(count($change_index) > 0) {
231 if(isset($_REQUEST['mode']) and $_REQUEST['mode']=='execute') {
232 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_ALTERING'];
233 foreach ($change_index as $statement) {
234 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_EXECUTING'].$statement;
235 (function_exists('logThis')) ? logThis("RepairIndex: {$statement}") : "";
236 $focus->db->query($statement);
239 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_ALTER'];
240 foreach ($change_index as $statement) {
241 echo ($_REQUEST['silent']) ? "" : "<BR>".$statement.";";
246 if(!isset($_REQUEST['mode']) or $_REQUEST['mode'] != 'execute') {
247 echo ($_REQUEST['silent']) ? "" : "<BR><BR><BR>";
248 echo ($_REQUEST['silent']) ? "" : "<a href='index.php?module=Administration&action=RepairIndex&mode=execute'>Execute Script</a>";
251 (function_exists('logThis')) ? logThis("RepairIndex: Index definitions are in sync.") : "";
252 echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_SYNC'];