-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcleanSql.php
More file actions
33 lines (26 loc) · 1003 Bytes
/
cleanSql.php
File metadata and controls
33 lines (26 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
// This script takes a raw set of SQL queries from MediaWiki with regular table names
// and adds a placeholder for a table prefix.
// At table creation time this prefix can then be replaced with an appropriate prefix.
// TODO allow passing in file? or run for all files?
$placeholder = '<<prefix>>_';
$filename = __DIR__ . '/new/mw1.43-wbs1.sql';
// Get the file
$text = file_get_contents($filename);
// Make sure there are no excessive whitepsace gaps
$text = preg_replace('~\r\n?~', "\n", $text);
$text = preg_replace("/\n+/s", "\n", $text);
// Remove any placeholders previously added to the file
$text = str_replace($placeholder, '', $text);
// Add new placeholders and space out the statements
$replacementPrefixes = [
'CREATE TABLE `',
'INSERT INTO `',
'REPLACE INTO `',
];
foreach ($replacementPrefixes as $prefix) {
$text = str_replace($prefix, "\n" . $prefix . $placeholder, $text);
}
// Write the file
file_put_contents($filename, $text);
echo 'Done!' . PHP_EOL;