rdbms: inject more dependencies into DatabaseOracle and remove $wgContLang use
[lhc/web/wiklou.git] / includes / db / PatchFileLocation.php
1 <?php
2 /**
3 * Trait for finding SQL patch files.
4 *
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 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\DB;
24
25 use RuntimeException;
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29 * Trait for finding SQL patch files.
30 *
31 * @since 1.32
32 */
33 trait PatchFileLocation {
34
35 /**
36 * Utility function for finding the appropriate SQL patch file for the currently
37 * used database type.
38 *
39 * The file will be searched for in the following locations, in order of preference:
40 * "$patchDir/$name.$dbType.sql",
41 * "$patchDir/$dbType/$name.sql",
42 * "$patchDir/$dbType/archives/$name.sql",
43 * "$patchDir/$name.sql",
44 * "$patchDir/archives/$name.sql"
45 *
46 * @param IDatabase $db
47 * @param string $name The script name (relative to $patchDir, without the '.sql' suffix)
48 * @param string|null $patchDir The directory to find the script in. Use __DIR__ to search in the
49 * directory the calling code is located in. If omitted, the "maintenance"
50 * directory will be used, where the scripts used by the updater are located.
51 *
52 * @return string
53 * @throws RuntimeException if no matching patch file could be found.
54 */
55 protected function getSqlPatchPath( IDatabase $db, $name, $patchDir = null ) {
56 $dbType = $db->getType();
57
58 if ( $patchDir === null ) {
59 $patchDir = $GLOBALS['IP'] . '/maintenance';
60 }
61
62 $paths = [
63
64 // For a small number of patch files, closely associated with code,
65 // e.g. for unit tests:
66 "$patchDir/$name.$dbType.sql",
67
68 // For a large number of patch files, e.g. for schema updates of extensions:
69 "$patchDir/$dbType/$name.sql",
70
71 // For MediaWiki core schema update patches:
72 "$patchDir/$dbType/archives/$name.sql",
73
74 // Database-agnostic fallback:
75 "$patchDir/$name.sql",
76
77 // Database-agnostic fallback for MediaWiki core schema update patches:
78 "$patchDir/archives/$name.sql"
79 ];
80
81 foreach ( $paths as $p ) {
82 if ( file_exists( $p ) ) {
83 return $p;
84 }
85 }
86
87 throw new RuntimeException( "No SQL script matching $name could be found in $patchDir" );
88 }
89
90 }