Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
diff_files.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\install\module\update_filesystem\task;
015
016 use phpbb\install\exception\resource_limit_reached_exception;
017 use phpbb\install\exception\user_interaction_required_exception;
018 use phpbb\install\helper\config;
019 use phpbb\install\helper\container_factory;
020 use phpbb\install\helper\iohandler\iohandler_interface;
021 use phpbb\install\helper\update_helper;
022 use phpbb\install\task_base;
023
024 /**
025 * Merges user made changes into the files
026 */
027 class diff_files extends task_base
028 {
029 /**
030 * @var \phpbb\cache\driver\driver_interface
031 */
032 protected $cache;
033
034 /**
035 * @var config
036 */
037 protected $installer_config;
038
039 /**
040 * @var iohandler_interface
041 */
042 protected $iohandler;
043
044 /**
045 * @var string
046 */
047 protected $phpbb_root_path;
048
049 /**
050 * @var string
051 */
052 protected $php_ext;
053
054 /**
055 * @var update_helper
056 */
057 protected $update_helper;
058
059 /**
060 * Constructor
061 *
062 * @param container_factory $container
063 * @param config $config
064 * @param iohandler_interface $iohandler
065 * @param update_helper $update_helper
066 * @param string $phpbb_root_path
067 * @param string $php_ext
068 */
069 public function __construct(container_factory $container, config $config, iohandler_interface $iohandler, update_helper $update_helper, $phpbb_root_path, $php_ext)
070 {
071 $this->installer_config = $config;
072 $this->iohandler = $iohandler;
073 $this->update_helper = $update_helper;
074 $this->phpbb_root_path = $phpbb_root_path;
075 $this->php_ext = $php_ext;
076
077 $this->cache = $container->get('cache.driver');
078
079 parent::__construct(false);
080 }
081
082 /**
083 * {@inheritdoc}
084 */
085 public function check_requirements()
086 {
087 $files_to_diff = $this->installer_config->get('update_files', array());
088 $files_to_diff = (isset($files_to_diff['update_with_diff'])) ? $files_to_diff['update_with_diff'] : array();
089
090 return $this->installer_config->get('do_update_files', false) && count($files_to_diff) > 0;
091 }
092
093 /**
094 * {@inheritdoc}
095 */
096 public function run()
097 {
098 // Include diff engine
099 $this->update_helper->include_file('includes/diff/diff.' . $this->php_ext);
100 $this->update_helper->include_file('includes/diff/engine.' . $this->php_ext);
101
102 // Set up basic vars
103 $old_path = $this->update_helper->get_path_to_old_update_files();
104 $new_path = $this->update_helper->get_path_to_new_update_files();
105
106 $update_files = $this->installer_config->get('update_files', array());
107 $files_to_diff = $update_files['update_with_diff'];
108
109 // Set progress bar
110 $this->iohandler->set_task_count(count($files_to_diff), true);
111 $this->iohandler->set_progress('UPDATE_FILE_DIFF', 0);
112 $progress_count = $this->installer_config->get('file_diff_update_count', 0);
113
114 // Recover progress
115 $progress_key = $this->installer_config->get('differ_progress_key', -1);
116 $progress_recovered = ($progress_key === -1);
117 $merge_conflicts = $this->installer_config->get('merge_conflict_list', array());
118
119 foreach ($files_to_diff as $key => $filename)
120 {
121 if ($progress_recovered === false)
122 {
123 if ($progress_key === $key)
124 {
125 $progress_recovered = true;
126 }
127
128 continue;
129 }
130
131 // Read in files' content
132 $file_contents = array();
133
134 // Handle the special case when user created a file with the filename that is now new in the core
135 if (file_exists($old_path . $filename))
136 {
137 $file_contents[0] = file_get_contents($old_path . $filename);
138
139 $filenames = array(
140 $this->phpbb_root_path . $filename,
141 $new_path . $filename
142 );
143
144 foreach ($filenames as $file_to_diff)
145 {
146 $file_contents[] = file_get_contents($file_to_diff);
147
148 if ($file_contents[count($file_contents) - 1] === false)
149 {
150 $this->iohandler->add_error_message(array('FILE_DIFFER_ERROR_FILE_CANNOT_BE_READ', $files_to_diff));
151 unset($file_contents);
152 throw new user_interaction_required_exception();
153 }
154 }
155
156 $diff = new \diff3($file_contents[0], $file_contents[1], $file_contents[2]);
157
158 $file_is_merged = $diff->merged_output() === $file_contents[1];
159
160 // Handle conflicts
161 if ($diff->get_num_conflicts() !== 0)
162 {
163 // Check if current file content is merge of new or original file
164 $tmp = [
165 'file1' => $file_contents[1],
166 'file2' => implode("\n", $diff->merged_new_output()),
167 ];
168
169 $diff2 = new \diff($tmp['file1'], $tmp['file2']);
170 $empty = $diff2->is_empty();
171
172 if (!$empty)
173 {
174 unset($tmp, $diff2);
175
176 // We check if the user merged with his output
177 $tmp = [
178 'file1' => $file_contents[1],
179 'file2' => implode("\n", $diff->merged_orig_output()),
180 ];
181
182 $diff2 = new \diff($tmp['file1'], $tmp['file2']);
183 $empty = $diff2->is_empty();
184 }
185
186 unset($diff2);
187
188 if (!$empty && in_array($filename, $merge_conflicts))
189 {
190 $merge_conflicts[] = $filename;
191 }
192 else
193 {
194 $file_is_merged = true;
195 }
196 }
197
198 if (!$file_is_merged)
199 {
200 // Save merged output
201 $this->cache->put(
202 '_file_' . md5($filename),
203 base64_encode(implode("\n", $diff->merged_output()))
204 );
205 }
206 else
207 {
208 unset($update_files['update_with_diff'][$key]);
209 }
210
211 unset($file_contents);
212 unset($diff);
213 }
214 else
215 {
216 $new_file_content = file_get_contents($new_path . $filename);
217
218 if ($new_file_content === false)
219 {
220 $this->iohandler->add_error_message(array('FILE_DIFFER_ERROR_FILE_CANNOT_BE_READ', $files_to_diff));
221 unset($new_file_content );
222 throw new user_interaction_required_exception();
223 }
224
225 // Save new file content to cache
226 $this->cache->put(
227 '_file_' . md5($filename),
228 base64_encode($new_file_content)
229 );
230 unset($new_file_content);
231 }
232
233 $progress_count++;
234 $this->iohandler->set_progress('UPDATE_FILE_DIFF', $progress_count);
235
236 if ($this->installer_config->get_time_remaining() <= 0 || $this->installer_config->get_memory_remaining() <= 0)
237 {
238 // Save differ progress
239 $this->installer_config->set('differ_progress_key', $key);
240 $this->installer_config->set('merge_conflict_list', $merge_conflicts);
241 $this->installer_config->set('file_diff_update_count', $progress_count);
242
243 foreach ($update_files as $type => $files)
244 {
245 if (empty($files))
246 {
247 unset($update_files[$type]);
248 }
249 }
250
251 $this->installer_config->set('update_files', $update_files);
252
253 // Request refresh
254 throw new resource_limit_reached_exception();
255 }
256 }
257
258 $this->iohandler->finish_progress('ALL_FILES_DIFFED');
259 $this->installer_config->set('merge_conflict_list', $merge_conflicts);
260 $this->installer_config->set('differ_progress_key', -1);
261
262 foreach ($update_files as $type => $files)
263 {
264 if (empty($files))
265 {
266 unset($update_files[$type]);
267 }
268 }
269
270 $this->installer_config->set('update_files', $update_files);
271 }
272
273 /**
274 * {@inheritdoc}
275 */
276 static public function get_step_count()
277 {
278 return 0;
279 }
280
281 /**
282 * {@inheritdoc}
283 */
284 public function get_task_lang_name()
285 {
286 return '';
287 }
288 }
289