forked from bakoontz/WikkaWiki
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmigrateDeprecatedLinks.php
executable file
·141 lines (119 loc) · 5 KB
/
migrateDeprecatedLinks.php
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/*
* Quick and dirty script to update deprecated link format. That is, replace
* [[link label]] with [[link | label]].
*
* For more information, see https://github.com/wikkawik/WikkaWiki/issues/1207.
*
* USAGE
* php scripts/migrateDeprecatedLinks.php
*/
class LinkMigrator {
public function __construct($config) {
$this->pages_updated = 0;
$this->links_migrated = 0;
$this->updated = 0;
$this->config = $config;
$this->db = $this->connect_to_database();
}
public function run() {
$this->backup_database($this->config);
$pages = $this->load_pages();
foreach ( $pages as $page ) {
$page['body'] = $this->replace_old_style_links($page);
if($this->updated == 1) {
$this->updated = 0;
$this->update_page($page);
}
}
$this->report_results();
}
protected function backup_database($config) {
$db_archive_file = sprintf('%s-backup.%s.sql', $config['mysql_database'], date('Ymd'));
$mysqldump_f = 'mysqldump %s --user=%s --password=%s --single-transaction > /tmp/%s';
# Is exec the way to go here?
$result = exec(sprintf($mysqldump_f,
$config['mysql_database'],
$config['mysql_user'],
$config['mysql_password'],
$db_archive_file));
if ( ! $result ) {
printf("Database %s backed up to /tmp/%s\n", $config['mysql_database'], $db_archive_file);
}
else {
printf("Database backup failed: %s\n", $result);
}
}
protected function load_pages() {
$sql = sprintf('SELECT id, tag, body, owner, user FROM %spages where latest="Y"',
$this->config['table_prefix']);
$statement = $this->db->query($sql);
return $statement->fetchAll();
}
protected function replace_old_style_links($page) {
$page_body = $page['body'];
$links_found = preg_match_all("/\[\[[^\[]*?\]\]/msu", $page_body, $matches);
if ( $links_found ) {
$links = $matches[0];
foreach ( $links as $link ) {
# TODO: There is probably a better way to do this.
# For regex, see https://github.com/wikkawik/WikkaWiki/commit/3abc0d9935.
if ( preg_match("/^(.*?)\s+([^|]+)$/su", $link, $delink) ) {
$url = $delink[1];
$label = $delink[2];
# Skip if already has pipe
if ( strpos($url, '|') !== false ) {
$new_link = sprintf('%s %s', $url, $label);
}
else {
$new_link = sprintf('%s | %s', $url, $label);
$this->links_migrated += 1;
$this->updated = 1;
echo $delink[1]." ".$delink[2] ." -> ". $new_link." on page ".$page['tag']."<br/>\n";
}
#echo join(' -> ', array($link, $new_link)), PHP_EOL;
$page_body = str_replace($link, $new_link, $page_body);
}
}
}
return $page_body;
}
protected function update_page($page) {
# Save new page version.
$insert = 'INSERT INTO %spages (tag, body, owner, user, note, latest, time) ' .
'VALUES (?, ?, ?, ?, ?, "Y", NOW())';
$sql = sprintf($insert, $this->config['table_prefix']);
$note = 'Replaces old-style internal links with new pipe-split links.';
$params = array($page['tag'], $page['body'], $page['owner'], $page['user'], $note);
$query = $this->db->prepare($sql);
$query->execute($params);
# Update last page version.
$update = 'UPDATE %spages SET latest="N" WHERE id = ?';
$sql = sprintf($update, $this->config['table_prefix']);
$query = $this->db->prepare($sql);
$query->execute(array($page['id']));
$this->pages_updated += 1;
}
protected function report_results() {
printf("Migrated %d links in %d page records\n", $this->links_migrated, $this->pages_updated);
}
protected function connect_to_database() {
$dsn = sprintf('mysql:host=%s;dbname=%s;',
$this->config['mysql_host'],
$this->config['mysql_database']);
$db = new PDO($dsn, $this->config['mysql_user'], $this->config['mysql_password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
}
// This script is deprecated for PHP versions 7 and greater!
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
if(PHP_VERSION_ID >= 70000) {
die("This script is deprecated for PHP versions 7 and greater!");
}
include_once('wikka.config.php');
$migrator = new LinkMigrator($wakkaConfig);
$migrator->run();