· 10 years ago · Sep 03, 2016, 09:18 PM
1<?php
2function icl_sitepress_activate(){
3
4 //if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'error_scrape'){
5 // return;
6 //}
7
8 global $wpdb;
9 global $EZSQL_ERROR;
10 require_once(ICL_PLUGIN_PATH . '/inc/lang-data.inc');
11 //defines $langs_names
12
13 $charset_collate = '';
14 if ( method_exists($wpdb, 'has_cap') && $wpdb->has_cap( 'collation' ) ) {
15 if ( ! empty($wpdb->charset) )
16 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
17 if ( ! empty($wpdb->collate) )
18 $charset_collate .= " COLLATE $wpdb->collate";
19 }
20
21 try{
22
23 // languages table
24 $table_name = $wpdb->prefix.'icl_languages';
25 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
26 $sql = "
27 CREATE TABLE `{$table_name}` (
28 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
29 `code` VARCHAR( 7 ) NOT NULL ,
30 `english_name` VARCHAR( 128 ) NOT NULL ,
31 `major` TINYINT NOT NULL DEFAULT '0',
32 `active` TINYINT NOT NULL ,
33 `default_locale` VARCHAR( 8 ),
34 UNIQUE KEY `code` (`code`),
35 UNIQUE KEY `english_name` (`english_name`)
36 ) ENGINE=MyISAM {$charset_collate}";
37 $wpdb->query($sql);
38 if($e = mysql_error()) throw new Exception($e);
39
40 //$langs_names is defined in ICL_PLUGIN_PATH . '/inc/lang-data.inc'
41 foreach($langs_names as $key=>$val){
42 if(strpos($key,'Norwegian Bokm')===0){ $key = 'Norwegian Bokmål'; $lang_codes[$key] = 'nb';} // exception for norwegian
43 $default_locale = isset($lang_locales[$lang_codes[$key]]) ? $lang_locales[$lang_codes[$key]] : '';
44 @$wpdb->insert($wpdb->prefix . 'icl_languages', array('english_name'=>$key, 'code'=>$lang_codes[$key], 'major'=>$val['major'], 'active'=>0, 'default_locale'=>$default_locale));
45 }
46 }
47
48 // languages translations table
49 $add_languages_translations = false;
50 $table_name = $wpdb->prefix.'icl_languages_translations';
51 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
52 $sql = "
53 CREATE TABLE `{$table_name}` (
54 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
55 `language_code` VARCHAR( 7 ) NOT NULL ,
56 `display_language_code` VARCHAR( 7 ) NOT NULL ,
57 `name` VARCHAR( 255 ) CHARACTER SET utf8 NOT NULL,
58 UNIQUE(`language_code`, `display_language_code`)
59 ) ENGINE=MyISAM {$charset_collate}";
60 $wpdb->query($sql);
61 if($e = mysql_error()) throw new Exception($e);
62 $add_languages_translations = true;
63 }
64 //else{
65 // this table will not be trucated on upgrade starting with WPML 1.7.3
66 // $add_languages_translations sticks to false;
67 //if(!defined('ICL_PRESERVE_LANGUAGES_TRANSLATIONS') || !ICL_PRESERVE_LANGUAGES_TRANSLATIONS){
68 // mysql_query("TRUNCATE TABLE `{$table_name}`");
69 // $add_languages_translations = true;
70 //}
71 //}
72
73 if($add_languages_translations){
74 foreach($langs_names as $lang=>$val){
75 if(strpos($lang,'Norwegian Bokm')===0){ $lang = 'Norwegian Bokmål'; $lang_codes[$lang] = 'nb';}
76 foreach($val['tr'] as $k=>$display){
77 if(strpos($k,'Norwegian Bokm')===0){ $k = 'Norwegian Bokmål';}
78 if(!trim($display)){
79 $display = $lang;
80 }
81 if(!($wpdb->get_var("SELECT id FROM {$table_name} WHERE language_code='{$lang_codes[$lang]}' AND display_language_code='{$lang_codes[$k]}'"))){
82 $wpdb->insert($wpdb->prefix . 'icl_languages_translations', array('language_code'=>$lang_codes[$lang], 'display_language_code'=>$lang_codes[$k], 'name'=>$display));
83 }
84 }
85 }
86 }
87
88
89 // translations
90 $table_name = $wpdb->prefix.'icl_translations';
91 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
92 $sql = "
93 CREATE TABLE `{$table_name}` (
94 `translation_id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
95 `element_type` VARCHAR( 32 ) NOT NULL DEFAULT 'post_post',
96 `element_id` BIGINT NULL DEFAULT NULL ,
97 `trid` BIGINT NOT NULL ,
98 `language_code` VARCHAR( 7 ) NOT NULL,
99 `source_language_code` VARCHAR( 7 ),
100 UNIQUE KEY `el_type_id` (`element_type`,`element_id`),
101 UNIQUE KEY `trid_lang` (`trid`,`language_code`)
102 ) ENGINE=MyISAM {$charset_collate}";
103 $wpdb->query($sql);
104 if($e = mysql_error()) throw new Exception($e);
105 }
106
107 // translation_status table
108 $table_name = $wpdb->prefix.'icl_translation_status';
109 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
110 $sql = "
111 CREATE TABLE `{$table_name}` (
112 `rid` bigint(20) NOT NULL AUTO_INCREMENT,
113 `translation_id` bigint(20) NOT NULL,
114 `status` tinyint(4) NOT NULL,
115 `translator_id` bigint(20) NOT NULL,
116 `needs_update` tinyint(4) NOT NULL,
117 `md5` varchar(32) NOT NULL,
118 `translation_service` varchar(16) NOT NULL,
119 `translation_package` text NOT NULL,
120 `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
121 `links_fixed` tinyint(4) NOT NULL DEFAULT 0,
122 `_prevstate` longtext,
123 PRIMARY KEY (`rid`),
124 UNIQUE KEY `translation_id` (`translation_id`)
125 ) ENGINE=MyISAM {$charset_collate}
126 ";
127 $wpdb->query($sql);
128 if($e = mysql_error()) throw new Exception($e);
129 }
130
131 // translation jobs
132 $table_name = $wpdb->prefix.'icl_translate_job';
133 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
134 $sql = "
135 CREATE TABLE `{$table_name}` (
136 `job_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
137 `rid` BIGINT UNSIGNED NOT NULL ,
138 `translator_id` INT UNSIGNED NOT NULL ,
139 `translated` TINYINT UNSIGNED NOT NULL DEFAULT 0,
140 `manager_id` INT UNSIGNED NOT NULL ,
141 `revision` INT UNSIGNED NULL,
142 INDEX ( `rid` , `translator_id` )
143 ) ENGINE = MYISAM ;
144 ";
145 $wpdb->query($sql);
146 if($e = mysql_error()) throw new Exception($e);
147 }
148
149 // translate table
150 $table_name = $wpdb->prefix.'icl_translate';
151 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
152 $sql = "
153 CREATE TABLE `{$table_name}` (
154 `tid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
155 `job_id` BIGINT UNSIGNED NOT NULL ,
156 `content_id` BIGINT UNSIGNED NOT NULL ,
157 `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
158 `field_type` VARCHAR( 128 ) NOT NULL ,
159 `field_format` VARCHAR( 16 ) NOT NULL ,
160 `field_translate` TINYINT NOT NULL ,
161 `field_data` TEXT NOT NULL ,
162 `field_data_translated` TEXT NOT NULL ,
163 `field_finished` TINYINT NOT NULL DEFAULT 0,
164 INDEX ( `job_id` )
165 ) ENGINE = MYISAM ;
166 ";
167 $wpdb->query($sql);
168 if($e = mysql_error()) throw new Exception($e);
169 }
170
171 // languages locale file names
172 $table_name = $wpdb->prefix.'icl_locale_map';
173 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
174 $sql = "
175 CREATE TABLE `{$table_name}` (
176 `code` VARCHAR( 8 ) NOT NULL ,
177 `locale` VARCHAR( 8 ) NOT NULL ,
178 UNIQUE (`code` ,`locale`)
179 ) ENGINE=MyISAM {$charset_collate}";
180 $wpdb->query($sql);
181 if($e = mysql_error()) throw new Exception($e);
182 }
183
184 // flags table
185 $table_name = $wpdb->prefix.'icl_flags';
186 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
187 $sql = "
188 CREATE TABLE `{$table_name}` (
189 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
190 `lang_code` VARCHAR( 10 ) NOT NULL ,
191 `flag` VARCHAR( 32 ) NOT NULL ,
192 `from_template` TINYINT NOT NULL DEFAULT '0',
193 UNIQUE (`lang_code`)
194 ) ENGINE=MyISAM {$charset_collate}";
195 $wpdb->query($sql);
196 if($e = mysql_error()) throw new Exception($e);
197 $codes = $wpdb->get_col("SELECT code FROM {$wpdb->prefix}icl_languages");
198 foreach($codes as $code){
199 if(!$code || $wpdb->get_var("SELECT lang_code FROM {$wpdb->prefix}icl_flags WHERE lang_code='{$code}'")) continue;
200 if(!file_exists(ICL_PLUGIN_PATH.'/res/flags/'.$code.'.png')){
201 $file = 'nil.png';
202 }else{
203 $file = $code.'.png';
204 }
205 $wpdb->insert($wpdb->prefix.'icl_flags', array('lang_code'=>$code, 'flag'=>$file, 'from_template'=>0));
206 }
207 }
208
209 /* general string translation */
210 $table_name = $wpdb->prefix.'icl_strings';
211 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
212 $sql = "
213 CREATE TABLE `{$table_name}` (
214 `id` bigint(20) unsigned NOT NULL auto_increment,
215 `language` varchar(10) NOT NULL,
216 `context` varchar(160) NOT NULL,
217 `name` varchar(160) NOT NULL,
218 `value` text NOT NULL,
219 `status` TINYINT NOT NULL,
220 PRIMARY KEY (`id`),
221 UNIQUE KEY `context_name` (`context`,`name`)
222 ) ENGINE=MyISAM {$charset_collate}";
223 $wpdb->query($sql);
224 if($e = mysql_error()) throw new Exception($e);
225 }
226
227 $table_name = $wpdb->prefix.'icl_string_translations';
228 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
229 $sql = "
230 CREATE TABLE `{$table_name}` (
231 `id` bigint(20) unsigned NOT NULL auto_increment,
232 `string_id` bigint(20) unsigned NOT NULL,
233 `language` varchar(10) NOT NULL,
234 `status` tinyint(4) NOT NULL,
235 `value` text NULL DEFAULT NULL,
236 `translator_id` bigint(20) unsigned DEFAULT NULL,
237 `translation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
238 PRIMARY KEY (`id`),
239 UNIQUE KEY `string_language` (`string_id`,`language`)
240 ) ENGINE=MyISAM {$charset_collate}";
241 $wpdb->query($sql);
242 if($e = mysql_error()) throw new Exception($e);
243 }
244
245 $table_name = $wpdb->prefix.'icl_string_status';
246 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
247 $sql = "
248 CREATE TABLE `{$table_name}` (
249 `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
250 `rid` BIGINT NOT NULL ,
251 `string_translation_id` BIGINT NOT NULL ,
252 `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
253 `md5` VARCHAR( 32 ) NOT NULL,
254 INDEX ( `string_translation_id` )
255 ) ENGINE=MyISAM {$charset_collate}";
256 $wpdb->query($sql);
257 if($e = mysql_error()) throw new Exception($e);
258 }
259
260 $table_name = $wpdb->prefix.'icl_string_positions';
261 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
262 $sql = "
263 CREATE TABLE `{$table_name}` (
264 `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
265 `string_id` BIGINT NOT NULL ,
266 `kind` TINYINT,
267 `position_in_page` VARCHAR( 255 ) NOT NULL,
268 INDEX ( `string_id` )
269 ) ENGINE=MyISAM {$charset_collate}";
270 $wpdb->query($sql);
271 if($e = mysql_error()) throw new Exception($e);
272 }
273
274 // message status table
275 $table_name = $wpdb->prefix.'icl_message_status';
276 if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
277 $sql = "
278 CREATE TABLE `{$table_name}` (
279 `id` bigint(20) unsigned NOT NULL auto_increment,
280 `rid` bigint(20) unsigned NOT NULL,
281 `object_id` bigint(20) unsigned NOT NULL,
282 `from_language` varchar(10) NOT NULL,
283 `to_language` varchar(10) NOT NULL,
284 `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
285 `md5` varchar(32) NOT NULL,
286 `object_type` varchar(64) NOT NULL,
287 `status` smallint(6) NOT NULL,
288 PRIMARY KEY (`id`),
289 UNIQUE KEY `rid` (`rid`),
290 KEY `object_id` (`object_id`)
291 ) ENGINE=MyISAM {$charset_collate}";
292 $wpdb->query($sql);
293 if($e = mysql_error()) throw new Exception($e);
294 }
295
296 /* string translation - start */
297 $icl_translation_sql = "
298 CREATE TABLE IF NOT EXISTS {$wpdb->prefix}icl_core_status (
299 `id` BIGINT NOT NULL auto_increment,
300 `rid` BIGINT NOT NULL,
301 `module` VARCHAR( 16 ) NOT NULL ,
302 `origin` VARCHAR( 64 ) NOT NULL ,
303 `target` VARCHAR( 64 ) NOT NULL ,
304 `status` SMALLINT NOT NULL,
305 PRIMARY KEY ( `id` ) ,
306 INDEX ( `rid` )
307 ) ENGINE=MyISAM {$charset_collate}
308 ";
309 $wpdb->query($icl_translation_sql);
310 if($e = mysql_error()) throw new Exception($e);
311
312 $icl_translation_sql = "
313 CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}icl_content_status` (
314 `rid` BIGINT NOT NULL ,
315 `nid` BIGINT NOT NULL ,
316 `timestamp` DATETIME NOT NULL ,
317 `md5` VARCHAR( 32 ) NOT NULL ,
318 PRIMARY KEY ( `rid` ) ,
319 INDEX ( `nid` )
320 ) ENGINE=MyISAM {$charset_collate}
321 ";
322 mysql_query($icl_translation_sql);
323 $wpdb->query($icl_translation_sql);
324 if($e = mysql_error()) throw new Exception($e);
325
326
327 $icl_translation_sql = "
328 CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}icl_node` (
329 `nid` BIGINT NOT NULL ,
330 `md5` VARCHAR( 32 ) NOT NULL ,
331 `links_fixed` TINYINT NOT NULL DEFAULT 0,
332 PRIMARY KEY ( `nid` )
333 ) ENGINE=MyISAM {$charset_collate}
334 ";
335 $wpdb->query($icl_translation_sql);
336 if($e = mysql_error()) throw new Exception($e);
337
338 $icl_translation_sql = "
339 CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}icl_reminders` (
340 `id` BIGINT NOT NULL ,
341 `message` TEXT NOT NULL ,
342 `url` TEXT NOT NULL ,
343 `can_delete` TINYINT NOT NULL ,
344 `show` TINYINT NOT NULL ,
345 PRIMARY KEY ( `id` )
346 ) ENGINE=MyISAM {$charset_collate}
347 ";
348 $wpdb->query($icl_translation_sql);
349 if($e = mysql_error()) throw new Exception($e);
350
351 } catch(Exception $e) {
352 trigger_error($e->getMessage(), E_USER_ERROR);
353 exit;
354 }
355
356 if(get_option('icl_sitepress_version')){
357 icl_plugin_upgrade();
358 }
359
360 // don't set the new version if a multi-step upgrade is in progress
361 if(!defined('ICL_MULTI_STEP_UPGRADE')){
362 delete_option('icl_sitepress_version');
363 add_option('icl_sitepress_version', ICL_SITEPRESS_VERSION, '', true);
364 }
365
366
367 $iclsettings = get_option('icl_sitepress_settings');
368 if($iclsettings === false ){
369 $short_v = implode('.', array_slice(explode('.', ICL_SITEPRESS_VERSION), 0, 3));
370 $settings = array(
371 'hide_upgrade_notice' => $short_v
372 );
373 add_option('icl_sitepress_settings', $settings, '', true);
374 }else{
375 // reset ajx_health_flag
376 $iclsettings['ajx_health_checked'] = 0;
377 update_option('icl_sitepress_settings',$iclsettings);
378 }
379
380}
381
382function icl_sitepress_deactivate(){
383}
384
385// Changed to use lowercased wpdb prefix. Some users have table name in uppercase.
386// http://bugs.mysql.com/bug.php?id=39894
387if(isset($_GET['activate'])){
388 if(!isset($wpdb)) global $wpdb;
389 $table_name = $wpdb->prefix.'icl_languages';
390 if(strtolower($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'")) != strtolower($table_name)){
391 add_action('admin_notices', 'icl_cant_create_table');
392 function icl_cant_create_table(){
393 echo '<div class="error"><ul><li><strong>';
394 echo __('WPML cannot create the database tables! Make sure that your mysql user has the CREATE privilege', 'sitepress');
395 echo '</strong></li></ul></div>';
396 $active_plugins = get_option('active_plugins');
397 $icl_sitepress_idx = array_search(ICL_PLUGIN_FOLDER . '/sitepress.php', $active_plugins);
398 if(false !== $icl_sitepress_idx){
399 unset($active_plugins[$icl_sitepress_idx]);
400 update_option('active_plugins', $active_plugins);
401 unset($_GET['activate']);
402 $recently_activated = get_option('recently_activated');
403 if(!isset($recently_activated[ICL_PLUGIN_FOLDER.'/sitepress.php'])){
404 $recently_activated[ICL_PLUGIN_FOLDER.'/sitepress.php'] = time();
405 update_option('recently_activated', $recently_activated);
406 }
407 }
408 }
409 }
410}
411
412/*
413function icl_cant_be_activated(){
414 echo '<div class="error"><ul><li><strong>';
415 echo sprintf(__('WPML cannot be acctivated because: %s', 'sitepress'), $GLOBALS['_icl_activation_error']);
416 echo '</strong></li></ul></div>';
417 $active_plugins = get_option('active_plugins');
418 $icl_sitepress_idx = array_search(ICL_PLUGIN_FOLDER . '/sitepress.php', $active_plugins);
419 if(false !== $icl_sitepress_idx){
420 unset($active_plugins[$icl_sitepress_idx]);
421 update_option('active_plugins', $active_plugins);
422 unset($_GET['activate']);
423 $recently_activated = get_option('recently_activated');
424 if(!isset($recently_activated[ICL_PLUGIN_FOLDER.'/sitepress.php'])){
425 $recently_activated[ICL_PLUGIN_FOLDER.'/sitepress.php'] = time();
426 update_option('recently_activated', $recently_activated);
427 }
428 }
429}
430*/
431
432?>