xref: /webtrees/app/Schema/Migration22.php (revision 3cf92ae205660ec36316541b9e23f2ecbf0af8bb)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2015 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Schema;
17
18use Fisharebest\Webtrees\Database;
19use Fisharebest\Webtrees\File;
20
21/**
22 * Upgrade the database schema from version 22 to version 23.
23 */
24class Migration22 implements MigrationInterface {
25	/**
26	 * Upgrade to to the next version
27	 */
28	public function upgrade() {
29		// - data update for 1.4.0 media changes
30		$_cfgs = Database::prepare(
31			"SELECT gs1.gedcom_id AS gedcom_id, gs1.setting_value AS media_directory, gs2.setting_value AS use_media_firewall, gs3.setting_value AS media_firewall_thumbs, gs4.setting_value AS media_firewall_rootdir" .
32			" FROM `##gedcom_setting` gs1" .
33			" LEFT JOIN `##gedcom_setting` gs2 ON (gs1.gedcom_id = gs2.gedcom_id AND gs2.setting_name='USE_MEDIA_FIREWALL')" .
34			" LEFT JOIN `##gedcom_setting` gs3 ON (gs1.gedcom_id = gs3.gedcom_id AND gs3.setting_name='MEDIA_FIREWALL_THUMBS')" .
35			" LEFT JOIN `##gedcom_setting` gs4 ON (gs1.gedcom_id = gs4.gedcom_id AND gs4.setting_name='MEDIA_FIREWALL_ROOTDIR')" .
36			" WHERE gs1.setting_name = 'MEDIA_DIRECTORY'"
37		)->fetchAll();
38
39		// The constant WT_DATA_DIR is not defined yet (although it was when this script was originally written).
40		$WT_DATA_DIR = realpath('data');
41
42		// Check the config for each tree
43		foreach ($_cfgs as $_cfg) {
44			if ($_cfg->use_media_firewall) {
45				// We’re using the media firewall.
46				$_mf_dir = realpath($_cfg->media_firewall_rootdir) . DIRECTORY_SEPARATOR;
47				if ($_mf_dir == $WT_DATA_DIR) {
48					// We’re already storing our media in the data folder - nothing to do.
49				} else {
50					// We’ve chosen a custom location for our media folder - need to update our media-folder to point to it.
51					// We have, for example,
52					// $_mf_dir = /home/fisharebest/my_pictures/
53					// $WT_DATA_DIR = /home/fisharebest/public_html/webtrees/data/
54					// Therefore we need to calculate ../../../my_pictures/
55					$_media_dir = '';
56					$_tmp_dir   = $WT_DATA_DIR;
57					while (strpos($_mf_dir, $_tmp_dir) !== 0) {
58						$_media_dir .= '../';
59						$_tmp_dir = preg_replace('~[^/\\\\]+[/\\\\]$~', '', $_tmp_dir);
60						if ($_tmp_dir == '') {
61							// Shouldn't get here - but this script is not allowed to fail...
62							continue 2;
63						}
64					}
65					$_media_dir .= $_cfg->media_directory;
66					Database::prepare(
67						"UPDATE `##gedcom_setting`" .
68						" SET setting_value=?" .
69						" WHERE gedcom_id=? AND setting_name='MEDIA_DIRECTORY'"
70					)->execute(array($_media_dir, $_cfg->gedcom_id));
71				}
72			} else {
73				// Not using the media firewall - just move the public folder to the new location (if we can).
74				if (
75					file_exists(WT_ROOT . $_cfg->media_directory) &&
76					is_dir(WT_ROOT . $_cfg->media_directory) &&
77					!file_exists($WT_DATA_DIR . $_cfg->media_directory)
78				) {
79					try {
80						rename(WT_ROOT . $_cfg->media_directory, $WT_DATA_DIR . $_cfg->media_directory);
81					} catch (\ErrorException $ex) {
82						// Cannot move the folder?
83					}
84					File::delete($WT_DATA_DIR . $_cfg->media_directory . '.htaccess');
85					File::delete($WT_DATA_DIR . $_cfg->media_directory . 'index.php');
86					File::delete($WT_DATA_DIR . $_cfg->media_directory . 'Mediainfo.txt');
87					File::delete($WT_DATA_DIR . $_cfg->media_directory . 'thumbs/Thumbsinfo.txt');
88				}
89			}
90		}
91
92		// Delete old settings
93		Database::exec("DELETE FROM `##gedcom_setting` WHERE setting_name IN ('USE_MEDIA_FIREWALL', 'MEDIA_FIREWALL_THUMBS', 'MEDIA_FIREWALL_ROOTDIR')");
94	}
95}
96