]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/RepairIndex.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Administration / RepairIndex.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39 ///////////////////////////////////////////////////////////////////////////////
40 ////    LOCAL UTILITY
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.
47                 $sel_db_index = null;
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;
57                         }
58                 } else {
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;
65                                         break;
66                                 }
67                         }
68                 }
69
70                 //no matching index in database.
71                 if(empty($sel_db_index)) {
72                         $add_index[]=$GLOBALS['db']->add_drop_constraint($table_name,$var_i_def);
73                         continue;
74                 }
75                 if(!$field_list_match) {
76                         //drop the db index and create new index based on vardef
77                         $drop_index[]=$GLOBALS['db']->add_drop_constraint($table_name,$sel_db_index,true);
78                         $add_index[]=$GLOBALS['db']->add_drop_constraint($table_name,$var_i_def);
79                         continue;
80                 }
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']) {
84                         //rename index.
85                         $rename=$GLOBALS['db']->renameIndexDefs($sel_db_index,$var_i_def,$table_name);
86                         if(is_array($rename)) {
87                                 $change_index=array_merge($change_index,$rename);
88                         } else {
89                                 $change_index[]=$rename;
90                         }
91                         continue;
92                 }
93         }
94 }
95 ////    END LOCAL UTILITY
96 ///////////////////////////////////////////////////////////////////////////////
97
98
99 ///////////////////////////////////////////////////////////////////////////////
100 ////    PROCESS
101 if(!is_admin($current_user)) sugar_die("Unauthorized access to administration.");
102 set_time_limit(3600);
103 /**
104  * Note: $_REQUEST['silent'] is set from ModuleInstaller::repair_indices();
105  */
106 if(!isset($_REQUEST['silent'])) $_REQUEST['silent'] = false;
107
108 $add_index=array();
109 $drop_index=array();
110 $change_index=array();
111
112 global $current_user, $beanFiles, $dictionary, $sugar_config, $mod_strings;;
113 include_once ('include/database/DBManager.php');
114
115 $db = &DBManagerFactory::getInstance();
116 $processed_tables=array();
117
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") : "";
122
123 foreach ($beanFiles as $beanname=>$beanpath) {
124         require_once($beanpath);
125         $focus= new $beanname();
126
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])) {
129                 continue;
130         } else {
131                 $processed_tables[$focus->table_name]=$focus->table_name;
132         }
133
134         if(!empty($dictionary[$focus->object_name]['indices'])) {
135                 $indices=$dictionary[$focus->object_name]['indices'];
136         } else {
137                 $indices=array();
138         }
139
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') {
146                         continue;
147                 }
148
149                 if(empty($definition['db']) or $definition['db'] == $focus->db->dbType) {
150                         $var_indices[$definition['name']] = $definition;
151                 }
152         }
153
154         $db_indices=$focus->db->get_indices($focus->table_name);
155         compare($focus->table_name,$db_indices,$var_indices);
156 }
157 ////    END PROCESS MODULE BEANS
158 ///////////////////////////////////////////////////////////////////////////////
159
160
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'];
167         } else {
168                 $indices=array();
169         }
170
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;
177                 }
178         }
179
180         $db_indices=$focus->db->get_indices($rel_def['table']);
181
182         compare($rel_def['table'],$db_indices,$var_indices);
183 }
184 ////    END PROCESS RELATIONSHIP METADATA
185 ///////////////////////////////////////////////////////////////////////////////
186
187
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.") : "";
191
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>";
196         }
197
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);
206                         }
207                 } else {
208                         echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_DROP'];
209                         foreach ($drop_index as $statement) {
210                                 echo ($_REQUEST['silent']) ? "" : "<BR>".$statement.";";
211                         }
212                 }
213         }
214
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);
222                         }
223                 } else {
224                         echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_ADD'];
225                         foreach ($add_index as $statement) {
226                                 echo ($_REQUEST['silent']) ? "" : "<BR>".$statement.";";
227                         }
228                 }
229         }
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);
237                         }
238                 } else {
239                         echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_ALTER'];
240                         foreach ($change_index as $statement) {
241                                 echo ($_REQUEST['silent']) ? "" : "<BR>".$statement.";";
242                         }
243                 }
244         }
245
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>";
249         }
250 } else {
251         (function_exists('logThis')) ? logThis("RepairIndex: Index definitions are in sync.") : "";
252         echo ($_REQUEST['silent']) ? "" : $mod_strings['LBL_REPAIR_INDEX_SYNC'];
253 }