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