3 // Upgrade YOURLS and DB schema
\r
4 function yourls_upgrade( $step, $oldver, $newver, $oldsql, $newsql ) {
\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
13 /************************** 1.4 -> 1.4.1 **************************/
\r
15 // Main func for upgrade from 1.4 to 1.4.1
\r
16 function yourls_upgrade_to_141( $step ) {
\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
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
30 yourls_update_options_to_141();
\r
34 // Alter table URL to 1.4.1
\r
35 function yourls_alter_url_table_to_141() {
\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
44 echo "<p>Structure of existing tables updated. Please wait...</p>";
\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
53 /************************** 1.3 -> 1.4 **************************/
\r
55 // Main func for upgrade from 1.3-RC1 to 1.4
\r
56 function yourls_upgrade_to_14( $step ) {
\r
60 // create table log & table options
\r
61 // update table url structure
\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
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
73 // convert each link in table url
\r
74 yourls_update_table_to_14();
\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
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
94 if( defined('YOURLS_DB_TABLE_NEXTDEC') ) {
\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
101 yourls_update_option( 'next_id', 1 ); // In case someone mistakenly deleted the next_id constant or table too early
\r
105 // Create new tables for YOURLS 1.4: options & log
\r
106 function yourls_create_tables_for_14() {
\r
109 $queries = array();
\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
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
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
137 echo "<p>New tables created. Please wait...</p>";
\r
141 // Alter table structure, part 1 (change schema, drop index)
\r
142 function yourls_alter_url_table_to_14() {
\r
144 $table = YOURLS_DB_TABLE_URL;
\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
152 foreach ( $alters as $query ) {
\r
153 $ydb->query( $query );
\r
156 echo "<p>Structure of existing tables updated. Please wait...</p>";
\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
162 $table = YOURLS_DB_TABLE_URL;
\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
169 foreach ( $alters as $query ) {
\r
170 $ydb->query( $query );
\r
173 echo "<p>New table index created</p>";
\r
176 // Convert each link from 1.3 (id) to 1.4 (keyword) structure
\r
177 function yourls_update_table_to_14() {
\r
179 $table = YOURLS_DB_TABLE_URL;
\r
181 // Modify each link to reflect new structure
\r
183 $from = isset($_GET['from']) ? intval( $_GET['from'] ) : 0 ;
\r
184 $total = yourls_get_db_stats();
\r
185 $total = $total['total_links'];
\r
187 $sql = "SELECT `keyword`,`url` FROM `$table` WHERE 1=1 ORDER BY `url` ASC LIMIT $from, $chunk ;";
\r
189 $rows = $ydb->get_results($sql);
\r
193 foreach( $rows as $row ) {
\r
194 $keyword = $row->keyword;
\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
201 echo "<p>Huho... Could not update rown with url='$url', from keyword '$keyword' to keyword '$newkeyword'</p>"; // Find what went wrong :/
\r
206 // All done for this chunk of queries, did it all go as expected?
\r
208 if( $count != $queries ) {
\r
210 $num = $count - $queries;
\r
211 echo "<p>$num error(s) occured while updating the URL table :(</p>";
\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
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
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
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
241 $contents = str_replace( $find, $replace, $contents );
\r
243 // Write cleaned file
\r
244 $f = fopen( $filename, 'w' );
\r
245 fwrite( $f, $contents );
\r