3 // Upgrade YOURLS and DB schema
\r
4 function yourls_upgrade( $step, $oldver, $newver, $oldsql, $newsql ) {
\r
5 /* Code / DB version table:
\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
16 // other upgrades which are done in a single pass
\r
22 yourls_upgrade_to_141();
\r
25 yourls_upgrade_to_143();
\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
39 /************************** 1.4.1 -> 1.4.3 **************************/
\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
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
52 echo "<p>Structure of existing tables updated. Please wait...</p>";
\r
55 /************************** 1.4 -> 1.4.1 **************************/
\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
63 yourls_alter_url_table_to_141();
\r
64 // recreate the htaccess file if needed
\r
65 yourls_create_htaccess();
\r
68 // Alter table URL to 1.4.1
\r
69 function yourls_alter_url_table_to_141() {
\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
78 /************************** 1.3 -> 1.4 **************************/
\r
80 // Main func for upgrade from 1.3-RC1 to 1.4
\r
81 function yourls_upgrade_to_14( $step ) {
\r
85 // create table log & table options
\r
86 // update table url structure
\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
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
98 // convert each link in table url
\r
99 yourls_update_table_to_14();
\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
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
119 if( defined('YOURLS_DB_TABLE_NEXTDEC') ) {
\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
126 yourls_update_option( 'next_id', 1 ); // In case someone mistakenly deleted the next_id constant or table too early
\r
130 // Create new tables for YOURLS 1.4: options & log
\r
131 function yourls_create_tables_for_14() {
\r
134 $queries = array();
\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
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
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
162 echo "<p>New tables created. Please wait...</p>";
\r
166 // Alter table structure, part 1 (change schema, drop index)
\r
167 function yourls_alter_url_table_to_14() {
\r
169 $table = YOURLS_DB_TABLE_URL;
\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
177 foreach ( $alters as $query ) {
\r
178 $ydb->query( $query );
\r
181 echo "<p>Structure of existing tables updated. Please wait...</p>";
\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
187 $table = YOURLS_DB_TABLE_URL;
\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
194 foreach ( $alters as $query ) {
\r
195 $ydb->query( $query );
\r
198 echo "<p>New table index created</p>";
\r
201 // Convert each link from 1.3 (id) to 1.4 (keyword) structure
\r
202 function yourls_update_table_to_14() {
\r
204 $table = YOURLS_DB_TABLE_URL;
\r
206 // Modify each link to reflect new structure
\r
208 $from = isset($_GET['from']) ? intval( $_GET['from'] ) : 0 ;
\r
209 $total = yourls_get_db_stats();
\r
210 $total = $total['total_links'];
\r
212 $sql = "SELECT `keyword`,`url` FROM `$table` WHERE 1=1 ORDER BY `url` ASC LIMIT $from, $chunk ;";
\r
214 $rows = $ydb->get_results($sql);
\r
218 foreach( $rows as $row ) {
\r
219 $keyword = $row->keyword;
\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
226 echo "<p>Huho... Could not update rown with url='$url', from keyword '$keyword' to keyword '$newkeyword'</p>"; // Find what went wrong :/
\r
231 // All done for this chunk of queries, did it all go as expected?
\r
233 if( $count != $queries ) {
\r
235 $num = $count - $queries;
\r
236 echo "<p>$num error(s) occured while updating the URL table :(</p>";
\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
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
253 // Clean .htaccess as it existed before 1.4. Returns boolean
\r
254 function yourls_clean_htaccess_for_14() {
\r
255 $filename = dirname(dirname(__FILE__)).'/.htaccess';
\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
266 $contents = str_replace( $find, $replace, $contents );
\r
268 // Write cleaned file
\r
269 $f = fopen( $filename, 'w' );
\r
270 fwrite( $f, $contents );
\r