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
10 // other upgrades which are done in a single pass
\r
16 yourls_upgrade_to_141();
\r
19 yourls_upgrade_to_143();
\r
22 yourls_upgrade_to_15();
\r
25 yourls_upgrade_482();
\r
27 yourls_redirect_javascript( yourls_admin_url( "upgrade.php?step=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
40 function yourls_upgrade_482() {
\r
41 // Change URL title charset to UTF8
\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
49 /************************** 1.4.3 -> 1.5 **************************/
\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
58 // Alter URL table to store titles
\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
66 yourls_create_htaccess();
\r
67 echo "<p>Updating .htaccess file. Please wait...</p>";
\r
70 /************************** 1.4.1 -> 1.4.3 **************************/
\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
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
83 echo "<p>Structure of existing tables updated. Please wait...</p>";
\r
86 /************************** 1.4 -> 1.4.1 **************************/
\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
94 yourls_alter_url_table_to_141();
\r
95 // recreate the htaccess file if needed
\r
96 yourls_create_htaccess();
\r
99 // Alter table URL to 1.4.1
\r
100 function yourls_alter_url_table_to_141() {
\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
109 /************************** 1.3 -> 1.4 **************************/
\r
111 // Main func for upgrade from 1.3-RC1 to 1.4
\r
112 function yourls_upgrade_to_14( $step ) {
\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
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
129 // convert each link in table url
\r
130 yourls_update_table_to_14();
\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
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
150 if( defined('YOURLS_DB_TABLE_NEXTDEC') ) {
\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
157 yourls_update_option( 'next_id', 1 ); // In case someone mistakenly deleted the next_id constant or table too early
\r
161 // Create new tables for YOURLS 1.4: options & log
\r
162 function yourls_create_tables_for_14() {
\r
165 $queries = array();
\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
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
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
193 echo "<p>New tables created. Please wait...</p>";
\r
197 // Alter table structure, part 1 (change schema, drop index)
\r
198 function yourls_alter_url_table_to_14() {
\r
200 $table = YOURLS_DB_TABLE_URL;
\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
208 foreach ( $alters as $query ) {
\r
209 $ydb->query( $query );
\r
212 echo "<p>Structure of existing tables updated. Please wait...</p>";
\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
218 $table = YOURLS_DB_TABLE_URL;
\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
225 foreach ( $alters as $query ) {
\r
226 $ydb->query( $query );
\r
229 echo "<p>New table index created</p>";
\r
232 // Convert each link from 1.3 (id) to 1.4 (keyword) structure
\r
233 function yourls_update_table_to_14() {
\r
235 $table = YOURLS_DB_TABLE_URL;
\r
237 // Modify each link to reflect new structure
\r
239 $from = isset($_GET['from']) ? intval( $_GET['from'] ) : 0 ;
\r
240 $total = yourls_get_db_stats();
\r
241 $total = $total['total_links'];
\r
243 $sql = "SELECT `keyword`,`url` FROM `$table` WHERE 1=1 ORDER BY `url` ASC LIMIT $from, $chunk ;";
\r
245 $rows = $ydb->get_results($sql);
\r
249 foreach( $rows as $row ) {
\r
250 $keyword = $row->keyword;
\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
257 echo "<p>Huho... Could not update rown with url='$url', from keyword '$keyword' to keyword '$newkeyword'</p>"; // Find what went wrong :/
\r
262 // All done for this chunk of queries, did it all go as expected?
\r
264 if( $count != $queries ) {
\r
266 $num = $count - $queries;
\r
267 echo "<p>$num error(s) occured while updating the URL table :(</p>";
\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
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
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
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
297 $contents = str_replace( $find, $replace, $contents );
\r
299 // Write cleaned file
\r
300 $f = fopen( $filename, 'w' );
\r
301 fwrite( $f, $contents );
\r