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