-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathamber-install.php
202 lines (173 loc) · 6.45 KB
/
amber-install.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
global $amber_db_version;
$amber_db_version = '1.4';
class AmberInstall {
private static function get_tables() {
$tables = array();
$tables['amber_check'] = "(
id VARCHAR(32) NOT NULL,
url VARCHAR(2000) DEFAULT '' NOT NULL,
status int,
last_checked int,
next_check int,
message VARCHAR(2000),
PRIMARY KEY (id)
)";
$tables['amber_cache'] = "(
id VARCHAR(32) NOT NULL,
url VARCHAR(2000) DEFAULT '' NOT NULL,
location VARCHAR(2000) DEFAULT '' NOT NULL,
date int,
type VARCHAR(200) DEFAULT '' NOT NULL,
size int,
provider int,
provider_id VARCHAR(2000) DEFAULT '' NOT NULL,
PRIMARY KEY (id,provider)
)";
$tables['amber_activity'] = "(
id VARCHAR(32) NOT NULL,
date int,
views int DEFAULT 0 NOT NULL,
PRIMARY KEY (id)
)";
$tables['amber_queue'] = "(
id VARCHAR(32) NOT NULL,
url VARCHAR(2000) NOT NULL,
created int,
locked int,
PRIMARY KEY (id)
)";
return $tables;
}
private static function install_tables($tables) {
global $wpdb;
$charset_collate = '';
if ( ! empty( $wpdb->charset ) ) {
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
}
if ( ! empty( $wpdb->collate ) ) {
$charset_collate .= " COLLATE {$wpdb->collate}";
}
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
/* Delete existing index on amber_cache table if it exists */
$cache_table_name = $wpdb->prefix . "amber_cache";
if ($wpdb->get_var("SHOW TABLES LIKE '$cache_table_name'") == $cache_table_name) {
$wpdb->query("DROP INDEX `PRIMARY` ON $cache_table_name");
}
foreach ($tables as $name => $table) {
$table_name = $wpdb->prefix . $name;
$sql = "CREATE TABLE $table_name $table $charset_collate";
dbDelta( $sql );
}
}
public static function activate($networkwide) {
AmberInstall::iterate_over_sites("activate_site");
}
public static function activate_site() {
global $amber_db_version;
AmberInstall::install_tables( AmberInstall::get_tables() );
add_option( 'amber_db_version', $amber_db_version );
$options = get_option('amber_options');
if (empty($options)) {
/* Set default options */
$options = array(
'amber_post_types' => 'post,page',
'amber_max_file' => 5000,
'amber_max_disk' => 1000,
'amber_available_action' => AMBER_ACTION_NONE,
'amber_unavailable_action' => AMBER_ACTION_HOVER,
'amber_available_action_hover' => 2,
'amber_unavailable_action_hover' => 2,
'amber_storage_location' => 'amber',
'amber_update_strategy' => 0,
'amber_timegate' => 'http://timetravel.mementoweb.org/timegate/',
'amber_country_id' => '',
'amber_excluded_sites' => parse_url(home_url(), PHP_URL_HOST),
'amber_backend' => AMBER_BACKEND_LOCAL,
'amber_perma_server_url' => 'http://perma.cc',
'amber_perma_api_server_url' => 'https://api.perma.cc',
'amber_aws_region' => 'us-east-1',
);
update_option('amber_options', $options);
}
/* The hook name needs to be a string, it can't be a reference to a class function */
if ( ! wp_next_scheduled( 'amber_cron_event_hook' ) ) {
wp_schedule_event( time(), 'fiveminutes', 'amber_cron_event_hook' );
}
add_rewrite_rule('^.*amber/cache/([a-f0-9]+)/?$', '/index.php?amber_cache=$1', "top");
add_rewrite_rule('^.*amber/cacheframe/([a-f0-9]+)/?$', '/index.php?amber_cacheframe=$1', "top");
add_rewrite_rule('^.*amber/cacheframe/([a-f0-9]+)/assets/(.*)/?$', '/index.php?amber_cacheframe=$1&amber_asset=$2', "top");
add_rewrite_rule('^.*amber/logcacheview?(.*)/?$', '/wp-admin/admin-ajax.php?action=amber_logcacheview&$1', "top");
add_rewrite_rule('^.*amber/status?(.*)/?$', '/wp-admin/admin-ajax.php?action=amber_status&$1', "top");
add_rewrite_rule('^.*amber/memento?(.*)/?$', '/wp-admin/admin-ajax.php?action=amber_memento&$1', "top");
flush_rewrite_rules();
}
public static function upgrade() {
AmberInstall::iterate_over_sites("upgrade_site");
}
public static function upgrade_site() {
global $amber_db_version;
global $wpdb;
$installed_db_version = get_option( "amber_db_version" );
if ( $installed_db_version != $amber_db_version ) {
/* Upgrade from 1.0-1.3 => 1.4 */
/* Delete existing index on amber_cache table if it exists */
$cache_table_name = $wpdb->prefix . "amber_cache";
if ($wpdb->get_var("SHOW TABLES LIKE '$cache_table_name'") == $cache_table_name) {
$wpdb->query("DROP INDEX `PRIMARY` ON $cache_table_name");
}
$tables = array('amber_cache' => "(
id VARCHAR(32) NOT NULL,
url VARCHAR(2000) DEFAULT '' NOT NULL,
location VARCHAR(2000) DEFAULT '' NOT NULL,
date int,
type VARCHAR(200) DEFAULT '' NOT NULL,
size int,
provider int,
provider_id VARCHAR(2000) DEFAULT '' NOT NULL,
PRIMARY KEY (id,provider)
)");
AmberInstall::install_tables( $tables );
$options = get_option( 'amber_options' );
$options['amber_backend'] = AMBER_BACKEND_LOCAL;
$options['amber_perma_server_url'] = "http://perma.cc";
$options['amber_perma_api_server_url'] = "https://api.perma.cc";
$options['amber_aws_region'] = "us-east-1";
update_option( 'amber_options', $options );
update_option( 'amber_db_version', $amber_db_version );
}
}
public static function deactivate() {
AmberInstall::iterate_over_sites("deactivate_site");
}
public static function deactivate_site() {
wp_clear_scheduled_hook( 'amber_cron_event_hook' );
}
public static function iterate_over_sites($function_name) {
global $wpdb;
if (function_exists('is_multisite') && is_multisite()) {
// check if it is a network activation - if so, run the activation function for each blog id
$old_blog = $wpdb->blogid;
// Get all blog ids
$blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
foreach ($blogids as $blog_id) {
switch_to_blog($blog_id);
call_user_func("AmberInstall::" . $function_name);
}
switch_to_blog($old_blog);
} else {
call_user_func("AmberInstall::" . $function_name);
}
}
public static function uninstall() {
AmberInstall::iterate_over_sites("uninstall_site");
}
public static function uninstall_site() {
global $wpdb;
$tables = AmberInstall::get_tables();
foreach ($tables as $name => $table) {
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}${name}" );
}
}
}
?>