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