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