]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/UpgradeWizard/SugarMerge/SugarMerge.php
Release 6.4.0
[Github/sugarcrm.git] / modules / UpgradeWizard / SugarMerge / SugarMerge.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  * Description:  Defines the English language pack for the base application.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46 require_once('modules/UpgradeWizard/SugarMerge/EditViewMerge.php');
47 require_once('modules/UpgradeWizard/SugarMerge/DetailViewMerge.php');
48 require_once('modules/UpgradeWizard/SugarMerge/SearchMerge.php');
49 require_once('modules/UpgradeWizard/SugarMerge/ListViewMerge.php');
50 require_once('modules/UpgradeWizard/SugarMerge/QuickCreateMerge.php');
51 require_once('modules/ModuleBuilder/parsers/views/History.php');
52
53 /**
54  * SugarMerge wraps around all the merge functionality of Sugar given a module name and the path to an unzipped patch
55  *
56  */
57 class SugarMerge {
58         private $mergeMapping = array();
59         private $new_path = '';
60         private $custom_path = 'custom';
61         private $original_path = '';
62         private $merged = array();
63         private $fp = NULL;
64         
65         function SugarMerge($new_path='', $original_path='', $custom_path='custom') {
66                 
67                 $this->new_path = empty($new_path) || preg_match('/[\/]$/', $new_path) ? $new_path : $new_path . '/';
68                 $this->original_path = empty($original_path) || preg_match('/[\/]$/', $original_path) ? $original_path : $original_path . '/';
69                 $this->custom_path = empty($custom_path) || preg_match('/[\/]$/', $custom_path) ? $custom_path : $custom_path . '/';
70
71                 $this->mergeMapping = array(
72                         'editviewdefs.php'=> new EditViewMerge(),
73                         'detailviewdefs.php'=>new DetailViewMerge(),
74                         'listviewdefs.php'=>new ListViewMerge(),
75                         'searchdefs.php'=>new SearchMerge(),
76                         'quickcreatedefs.php'=>new QuickCreateMerge(),
77                 );              
78         }
79
80         function setLogFilePointer($fp){
81                 $this->fp = $fp;
82         }
83
84
85
86         /**
87          * This will run through all the modules that may need merging and determine if there is anything to merge
88          * if $merge is set to true it will perform the merge
89          * if $merge and $save are set to true it will perform the merge and save the results in the custom directory
90          *
91          * @param BOOLEAN|ARRAY $merge - do we wish to perform the merge if false it will just return a list of files that can be merged.  If an array is passed, only those modules present in the array will be merged.
92          * @param BOOLEAN $save - do we wish to save the merged files to true - $merge must be true for this to apply - otherwise it will simulate merging so you can view the log files of the merge
93          * @param BOOLEAN $logHistory - do we wish to create history entries for any of the merges
94          * @return ARRAY - an associative array of module names to files that were either merged or have the potential to be merged depeneding if $merge and $save  are set to true
95          */
96         function mergeAll($merge=true, $save=true, $logHistory=true){
97                 $this->merged = array();
98                 $searchDirectory = $this->custom_path;
99                 if(!preg_match('/[\/]modules$/si', $searchDirectory)) {
100                    $searchDirectory .= preg_match('/[\/]$/', $this->custom_path) ? 'modules' : '/modules';
101                 }
102
103                 if(file_exists($searchDirectory)){
104                         $dir = dir($searchDirectory);
105                         while($e = $dir->read()){
106                                 if(substr($e , 0, 1) != '.') {
107                                         if(is_dir("{$searchDirectory}/{$e}/metadata")){
108
109                         //lets make sure that the directory matches the case of the module before we pass it in
110                         global $moduleList;
111                         //lets populate an array with the available modules, and make the key's lowercase
112                         $checkModList =  array_combine ($moduleList,$moduleList);
113                         $checkModList = array_change_key_case($checkModList);
114
115                                                 //now lets compare with the current directory.  This accounts for cases in which the directory was created in lowercase
116                         if(!empty($checkModList[strtolower($e)])){
117                             //directory was lowercase, let's use the right module value
118                                                         $e = $checkModList[strtolower($e)];
119                         }
120
121                                             if( is_array($merge) )
122                                             {
123                                                 if ( in_array($e,$merge) ) 
124                                                         $this->merged[$e] = $this->mergeModule($e, TRUE, $save,$logHistory );
125                                                 else
126                                                 {
127                                                     $GLOBALS['log']->debug("SugarMerge is skipping $e module as filter array passed in but module not specified for merge.");
128                                                     continue;
129                                                 }
130                                             }
131                                             else
132                                                   $this->merged[$e] = $this->mergeModule($e, $merge, $save,$logHistory );
133                                         }
134                                 }
135                         }
136                 }
137                 return $this->merged;
138         }
139
140         
141         
142
143         /**
144          * This will merge any files that need merging for a given module
145          * if $merge is set to true it will perform the merge
146          * if $merge and $save are set to true it will perform the merge and save the results in the custom directory
147          *
148          * @param STRING $module - the name of the module to merge files for
149          * @param BOOLEAN $merge - do we wish to perform the merge if false it will just return a list of files that can be merged
150          * @param BOOLEAN $save - do we wish to save the merged files to true - $merge must be true for this to apply - otherwise it will simulate merging so you can view the log files of the merge
151          * @param BOOLEAN $logHistory - do we wish to create history entries for any of the merges
152          * @return ARRAY - an associative array of files that were either merged or have the potential to be merged depeneding if $merge and $save  are set to true
153          */
154         function mergeModule($module, $merge = true, $save=true,$logHistory=true){
155                 $merged = array();
156                 $path = $this->original_path . 'modules/' . $module . '/metadata/';
157                 $custom_path = $this->custom_path . 'modules/' . $module . '/metadata/';
158                 $new_path = $this->new_path . 'modules/' . $module . '/metadata/';
159                 foreach($this->mergeMapping as $file=>&$object){
160                         if(file_exists("{$custom_path}{$file}") && file_exists("{$new_path}{$file}")){  
161                                 if($merge){
162                                         $merged[$file] = $this->mergeFile($module, $file, $save, $logHistory);
163                                 }else{
164                                         $merged[$file] = true;
165                                 }
166                         }
167                 }
168
169                 return $merged;
170
171         }
172
173         /**
174          * This function will merge a single file for a module
175          *
176          * @param STRING $module - name of the module
177          * @param STRING $file - name of the file
178          * @param STRING $save - should the merged file be saved to the custom directory
179          * @return BOOLEAN - success or failure of the merge
180          */
181         function mergeFile($module, $file, $save=true,$logHistory=true){
182                 $path = $this->original_path . 'modules/' . $module . '/metadata/';
183                 $custom_path = $this->custom_path . 'modules/' . $module . '/metadata/';
184                 $new_path = $this->new_path . 'modules/' . $module . '/metadata/';
185                 if($this->fp) $this->mergeMapping[$file]->setLogFilePointer($this->fp);
186                 if(isset($this->mergeMapping[$file]) && file_exists("{$path}{$file}") && file_exists("{$custom_path}{$file}") && file_exists("{$new_path}{$file}")){
187                     //Create a log entry of the custom file before it is merged
188                     if($logHistory && $save)
189                           $this->createHistoryLog($module, "{$custom_path}{$file}",$file);
190                     return $this->mergeMapping[$file]->merge($module, "{$path}{$file}", "{$new_path}{$file}", "{$custom_path}{$file}", $save);
191                 }
192                 return false;
193
194         }
195         
196     /**
197          * Create a history copy of the custom file that will be merged so that it can be access through
198          * studio if admins wish to revert at a later date.
199          *
200          * @param STRING $module - name of the module
201          * @param STRING $file - name of the file
202          * @param STRING $customFile - Path to the custom file that will be merged
203          */
204         protected function createHistoryLog($module,$customFile,$file)
205         {
206             $historyPath = 'custom/' . MB_HISTORYMETADATALOCATION . "/modules/$module/metadata/$file";
207             $history = new History($historyPath);
208             $timeStamp = $history->append($customFile);     
209             $GLOBALS['log']->debug("Created history file after merge with new file: " . $historyPath .'_'.$timeStamp);
210         }
211         
212         /**
213          * Return the custom modules path
214          *
215          * @return STRING directory where custom module files are located
216          */
217         function getCustomPath() {
218                 return $this->custom_path;
219         }
220         
221         
222         /**
223          * Return the new upgrade modules path
224          *
225          * @return STRING directory where new module files are located
226          */
227         function getNewPath() {
228                 return $this->new_path;
229         }       
230
231
232         /**
233          * Return the original modules path
234          *
235          * @return STRING directory where new module files are located
236          */
237         function getOriginalPath() {
238                 return $this->original_path;
239         }               
240         
241 }
242 ?>