]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-upgrade.php
Replace deprecated function split()
[Github/YOURLS.git] / includes / functions-upgrade.php
1 <?php\r
2 \r
3 // Upgrade YOURLS and DB schema\r
4 function yourls_upgrade( $step, $oldver, $newver, $oldsql, $newsql ) {\r
5         // special case for 1.3: the upgrade is a multi step procedure\r
6         if( $oldsql == 100 ) {\r
7                 yourls_upgrade_to_14( $step );\r
8         }\r
9         \r
10         // other upgrades which are done in a single pass\r
11         switch( $step ) {\r
12         \r
13         case 1:\r
14         case 2:\r
15                 if( $oldsql < 210 )\r
16                         yourls_upgrade_to_141();\r
17                         \r
18                 if( $oldsql < 220 )\r
19                         yourls_upgrade_to_143();\r
20                 \r
21                 if( $oldsql < 250 )\r
22                         yourls_upgrade_to_15();\r
23                         \r
24                 if( $oldsql < 482 )\r
25                         yourls_upgrade_482();\r
26                 \r
27                 yourls_redirect_javascript( yourls_admin_url( "upgrade.php?step=3" ) );\r
28 \r
29                 break;\r
30                 \r
31         case 3:\r
32                 // Update options to reflect latest version\r
33                 yourls_update_option( 'version', YOURLS_VERSION );\r
34                 yourls_update_option( 'db_version', YOURLS_DB_VERSION );\r
35                 break;\r
36         }\r
37 }\r
38 \r
39 // Upgrade r482\r
40 function yourls_upgrade_482() {\r
41         // Change URL title charset to UTF8\r
42         global $ydb;\r
43         $table_url = YOURLS_DB_TABLE_URL;\r
44         $sql = "ALTER TABLE `$table_url` CHANGE `title` `title` TEXT CHARACTER SET utf8;";\r
45         $ydb->query( $sql );\r
46         echo "<p>Updating table structure. Please wait...</p>";\r
47 }\r
48 \r
49 /************************** 1.4.3 -> 1.5 **************************/\r
50 \r
51 // Main func for upgrade from 1.4.3 to 1.5\r
52 function yourls_upgrade_to_15( ) {\r
53         // Create empty 'active_plugins' entry in the option if needed\r
54         if( yourls_get_option( 'active_plugins' ) === false )\r
55                 yourls_add_option( 'active_plugins', array() );\r
56         echo "<p>Enabling the plugin API. Please wait...</p>";\r
57         \r
58         // Alter URL table to store titles\r
59         global $ydb;\r
60         $table_url = YOURLS_DB_TABLE_URL;\r
61         $sql = "ALTER TABLE `$table_url` ADD `title` TEXT AFTER `url`;";\r
62         $ydb->query( $sql );\r
63         echo "<p>Updating table structure. Please wait...</p>";\r
64         \r
65         // Update .htaccess\r
66         yourls_create_htaccess();\r
67         echo "<p>Updating .htaccess file. Please wait...</p>";\r
68 }\r
69 \r
70 /************************** 1.4.1 -> 1.4.3 **************************/\r
71 \r
72 // Main func for upgrade from 1.4.1 to 1.4.3\r
73 function yourls_upgrade_to_143( ) {\r
74         // Check if we have 'keyword' (borked install) or 'shorturl' (ok install)\r
75         global $ydb;\r
76         $table_log = YOURLS_DB_TABLE_LOG;\r
77         $sql = "SHOW COLUMNS FROM `$table_log`";\r
78         $cols = $ydb->get_results( $sql );\r
79         if ( $cols[2]->Field == 'keyword' ) {\r
80                 $sql = "ALTER TABLE `$table_log` CHANGE `keyword` `shorturl` VARCHAR( 200 ) BINARY;";\r
81                 $ydb->query( $sql );\r
82         }\r
83         echo "<p>Structure of existing tables updated. Please wait...</p>";\r
84 }\r
85 \r
86 /************************** 1.4 -> 1.4.1 **************************/\r
87 \r
88 // Main func for upgrade from 1.4 to 1.4.1\r
89 function yourls_upgrade_to_141( ) {\r
90         // Kill old cookies from 1.3 and prior\r
91         setcookie('yourls_username', null, time() - 3600 );\r
92         setcookie('yourls_password', null, time() - 3600 );\r
93         // alter table URL\r
94         yourls_alter_url_table_to_141();\r
95         // recreate the htaccess file if needed\r
96         yourls_create_htaccess();\r
97 }\r
98 \r
99 // Alter table URL to 1.4.1\r
100 function yourls_alter_url_table_to_141() {\r
101         global $ydb;\r
102         $table_url = YOURLS_DB_TABLE_URL;\r
103         $alter = "ALTER TABLE `$table_url` CHANGE `keyword` `keyword` VARCHAR( 200 ) BINARY, CHANGE `url` `url` TEXT BINARY ";\r
104         $ydb->query( $alter );\r
105         echo "<p>Structure of existing tables updated. Please wait...</p>";\r
106 }\r
107 \r
108 \r
109 /************************** 1.3 -> 1.4 **************************/\r
110 \r
111 // Main func for upgrade from 1.3-RC1 to 1.4\r
112 function yourls_upgrade_to_14( $step ) {\r
113         \r
114         switch( $step ) {\r
115         case 1:\r
116                 // create table log & table options\r
117                 // update table url structure\r
118                 // update .htaccess\r
119                 yourls_create_tables_for_14(); // no value returned, assuming it went OK\r
120                 yourls_alter_url_table_to_14(); // no value returned, assuming it went OK\r
121                 $clean = yourls_clean_htaccess_for_14(); // returns bool\r
122                 $create = yourls_create_htaccess(); // returns bool\r
123                 if ( !$create )\r
124                         echo "<p class='warning'>Please create your <tt>.htaccess</tt> file (I could not do it for you). Please refer to <a href='http://yourls.org/htaccess'>http://yourls.org/htaccess</a>.";\r
125                 yourls_redirect_javascript( yourls_admin_url( "upgrade.php?step=2&oldver=1.3&newver=1.4&oldsql=100&newsql=200" ), $create );\r
126                 break;\r
127                 \r
128         case 2:\r
129                 // convert each link in table url\r
130                 yourls_update_table_to_14();\r
131                 break;\r
132         \r
133         case 3:\r
134                 // update table url structure part 2: recreate indexes\r
135                 yourls_alter_url_table_to_14_part_two();\r
136                 // update version & db_version & next_id in the option table\r
137                 // attempt to drop YOURLS_DB_TABLE_NEXTDEC\r
138                 yourls_update_options_to_14();\r
139                 // Now upgrade to 1.4.1\r
140                 yourls_redirect_javascript( yourls_admin_url( "upgrade.php?step=1&oldver=1.4&newver=1.4.1&oldsql=200&newsql=210" ) );\r
141                 break;\r
142         }\r
143 }\r
144 \r
145 // Update options to reflect new version\r
146 function yourls_update_options_to_14() {\r
147         yourls_update_option( 'version', '1.4' );\r
148         yourls_update_option( 'db_version', '200' );\r
149         \r
150         if( defined('YOURLS_DB_TABLE_NEXTDEC') ) {\r
151                 global $ydb;\r
152                 $table = YOURLS_DB_TABLE_NEXTDEC;\r
153                 $next_id = $ydb->get_var("SELECT `next_id` FROM `$table`");\r
154                 yourls_update_option( 'next_id', $next_id );\r
155                 @$ydb->query( "DROP TABLE `$table`" );\r
156         } else {\r
157                 yourls_update_option( 'next_id', 1 ); // In case someone mistakenly deleted the next_id constant or table too early\r
158         }\r
159 }\r
160 \r
161 // Create new tables for YOURLS 1.4: options & log\r
162 function yourls_create_tables_for_14() {\r
163         global $ydb;\r
164 \r
165         $queries = array();\r
166 \r
167         $queries[YOURLS_DB_TABLE_OPTIONS] = \r
168                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.\r
169                 '`option_id` int(11) unsigned NOT NULL auto_increment,'.\r
170                 '`option_name` varchar(64) NOT NULL default "",'.\r
171                 '`option_value` longtext NOT NULL,'.\r
172                 'PRIMARY KEY (`option_id`,`option_name`),'.\r
173                 'KEY `option_name` (`option_name`)'.\r
174                 ');';\r
175                 \r
176         $queries[YOURLS_DB_TABLE_LOG] = \r
177                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.\r
178                 '`click_id` int(11) NOT NULL auto_increment,'.\r
179                 '`click_time` datetime NOT NULL,'.\r
180                 '`shorturl` varchar(200) NOT NULL,'.\r
181                 '`referrer` varchar(200) NOT NULL,'.\r
182                 '`user_agent` varchar(255) NOT NULL,'.\r
183                 '`ip_address` varchar(41) NOT NULL,'.\r
184                 '`country_code` char(2) NOT NULL,'.\r
185                 'PRIMARY KEY (`click_id`),'.\r
186                 'KEY `shorturl` (`shorturl`)'.\r
187                 ');';\r
188         \r
189         foreach( $queries as $query ) {\r
190                 $ydb->query( $query ); // There's no result to be returned to check if table was created (except making another query to check table existence, which we'll avoid)\r
191         }\r
192         \r
193         echo "<p>New tables created. Please wait...</p>";\r
194 \r
195 }\r
196 \r
197 // Alter table structure, part 1 (change schema, drop index)\r
198 function yourls_alter_url_table_to_14() {\r
199         global $ydb;\r
200         $table = YOURLS_DB_TABLE_URL;\r
201 \r
202         $alters = array();\r
203         $results = array();\r
204         $alters[] = "ALTER TABLE `$table` CHANGE `id` `keyword` VARCHAR( 200 ) NOT NULL";\r
205         $alters[] = "ALTER TABLE `$table` CHANGE `url` `url` TEXT NOT NULL";\r
206         $alters[] = "ALTER TABLE `$table` DROP PRIMARY KEY";\r
207         \r
208         foreach ( $alters as $query ) {\r
209                 $ydb->query( $query );\r
210         }\r
211         \r
212         echo "<p>Structure of existing tables updated. Please wait...</p>";\r
213 }\r
214 \r
215 // Alter table structure, part 2 (recreate indexes after the table is up to date)\r
216 function yourls_alter_url_table_to_14_part_two() {\r
217         global $ydb;\r
218         $table = YOURLS_DB_TABLE_URL;\r
219         \r
220         $alters = array();\r
221         $alters[] = "ALTER TABLE `$table` ADD PRIMARY KEY ( `keyword` )";\r
222         $alters[] = "ALTER TABLE `$table` ADD INDEX ( `ip` )";\r
223         $alters[] = "ALTER TABLE `$table` ADD INDEX ( `timestamp` )";\r
224         \r
225         foreach ( $alters as $query ) {\r
226                 $ydb->query( $query );\r
227         }\r
228 \r
229         echo "<p>New table index created</p>";\r
230 }\r
231 \r
232 // Convert each link from 1.3 (id) to 1.4 (keyword) structure\r
233 function yourls_update_table_to_14() {\r
234         global $ydb;\r
235         $table = YOURLS_DB_TABLE_URL;\r
236 \r
237         // Modify each link to reflect new structure\r
238         $chunk = 45;\r
239         $from = isset($_GET['from']) ? intval( $_GET['from'] ) : 0 ;\r
240         $total = yourls_get_db_stats();\r
241         $total = $total['total_links'];\r
242         \r
243         $sql = "SELECT `keyword`,`url` FROM `$table` WHERE 1=1 ORDER BY `url` ASC LIMIT $from, $chunk ;";\r
244         \r
245         $rows = $ydb->get_results($sql);\r
246         \r
247         $count = 0;\r
248         $queries = 0;\r
249         foreach( $rows as $row ) {\r
250                 $keyword = $row->keyword;\r
251                 $url = $row->url;\r
252                 $newkeyword = yourls_int2string( $keyword );\r
253                 $ydb->query("UPDATE `$table` SET `keyword` = '$newkeyword' WHERE `url` = '$url';");\r
254                 if( $ydb->result === true ) {\r
255                         $queries++;\r
256                 } else {\r
257                         echo "<p>Huho... Could not update rown with url='$url', from keyword '$keyword' to keyword '$newkeyword'</p>"; // Find what went wrong :/\r
258                 }\r
259                 $count++;\r
260         }\r
261         \r
262         // All done for this chunk of queries, did it all go as expected?\r
263         $success = true;\r
264         if( $count != $queries ) {\r
265                 $success = false;\r
266                 $num = $count - $queries;\r
267                 echo "<p>$num error(s) occured while updating the URL table :(</p>";\r
268         }\r
269         \r
270         if ( $count == $chunk ) {\r
271                 // there are probably other rows to convert\r
272                 $from = $from + $chunk;\r
273                 $remain = $total - $from;\r
274                 echo "<p>Converted $chunk database rows ($remain remaining). Continuing... Please do not close this window until it's finished!</p>";\r
275                 yourls_redirect_javascript( yourls_admin_url( "upgrade.php?step=2&oldver=1.3&newver=1.4&oldsql=100&newsql=200&from=$from" ), $success );\r
276         } else {\r
277                 // All done\r
278                 echo '<p>All rows converted! Please wait...</p>';\r
279                 yourls_redirect_javascript( yourls_admin_url( "upgrade.php?step=3&oldver=1.3&newver=1.4&oldsql=100&newsql=200" ), $success );\r
280         }\r
281         \r
282 }\r
283 \r
284 // Clean .htaccess as it existed before 1.4. Returns boolean\r
285 function yourls_clean_htaccess_for_14() {\r
286         $filename = YOURLS_ABSPATH.'/.htaccess';\r
287         \r
288         $result = false;\r
289         if( is_writeable( $filename ) ) {\r
290                 $contents = implode( '', file( $filename ) );\r
291                 // remove "ShortURL" block\r
292                 $contents = preg_replace( '/# BEGIN ShortURL.*# END ShortURL/s', '', $contents );\r
293                 // comment out deprecated RewriteRule\r
294                 $find = 'RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]';\r
295                 $replace = "# You can safely remove this 5 lines block -- it's no longer used in YOURLS\n".\r
296                                 "# $find";\r
297                 $contents = str_replace( $find, $replace, $contents );\r
298                 \r
299                 // Write cleaned file\r
300                 $f = fopen( $filename, 'w' );\r
301                 fwrite( $f, $contents );\r
302                 fclose( $f );\r
303                 \r
304                 $result = true;\r
305         }\r
306 \r
307         return $result;\r
308 }\r
309 \r