-
Notifications
You must be signed in to change notification settings - Fork 47
/
papercite_db.php
108 lines (82 loc) · 3.67 KB
/
papercite_db.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
<?php
/* Copyright 2012 Benjamin Piwowarski (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Database management
* See http://codex.wordpress.org/Creating_Tables_with_Plugins
*/
global $wpdb;
global $papercite_db_version;
global $papercite_table_name;
global $papercite_table_name_url;
$papercite_table_name = $GLOBALS["wpdb"]->prefix . "plugin_papercite";
$papercite_table_name_url = $papercite_table_name . "_url";
$papercite_db_version = "1.9";
function papercite_msg_upgraded() {
print "<p style='text-align: center; color: blue'>Papercite plugin: database updated to version $GLOBALS[papercite_db_version]</p>";
}
function papercite_install($force = false) {
global $wpdb, $papercite_db_version, $papercite_table_name, $papercite_table_name_url;
$installed_ver = get_option( "papercite_db_version" );
if ($force || $installed_ver != $papercite_db_version) {
if (!empty($installed_ver)) {
if (version_compare($installed_ver, "1.4") < 0) {
$wpdb->query("DROP TABLE $papercite_table_name");
} elseif (version_compare($installed_ver, "1.9") < 0) {
// Remove all entries (change in PHP class names)
$wpdb->query("TRUNCATE TABLE $papercite_table_name");
$wpdb->query("TRUNCATE TABLE $papercite_table_name_url");
}
}
require(ABSPATH . 'wp-admin/includes/upgrade.php');
$sql = "CREATE TABLE $papercite_table_name (
urlid INT UNSIGNED NOT NULL,
bibtexid VARCHAR(200) CHARSET ASCII NOT NULL,
entrytype VARCHAR(80) CHARSET ASCII NOT NULL,
year SMALLINT,
data TEXT NOT NULL,
PRIMARY KEY id (urlid, bibtexid),
INDEX year (year),
INDEX entrytype (entrytype)
) DEFAULT CHARACTER SET $wpdb->charset";
ob_start();
$wpdb->show_errors(true);
dbDelta($sql, true);
$output = ob_get_contents();
ob_end_clean();
if ($wpdb->last_result !== FALSE) {
$sql = "CREATE TABLE $papercite_table_name_url (
urlid INT UNSIGNED NOT NULL AUTO_INCREMENT,
url VARCHAR(300) CHARSET ASCII NOT NULL,
ts BIGINT UNSIGNED NOT NULL,
PRIMARY KEY id (urlid),
UNIQUE KEY url (url)
) DEFAULT CHARACTER SET $wpdb->charset";
ob_start();
$wpdb->show_errors(true);
dbDelta($sql, true);
$output = ob_get_contents();
ob_end_clean();
}
if ($wpdb->last_result !== FALSE) {
// Set the current version
update_option("papercite_db_version", $papercite_db_version);
add_action('admin_notices', 'papercite_msg_upgraded');
}
return array($wpdb->last_result !== FALSE, $output);
}
return true;
}
papercite_install();
?>