3 // Upgrade YOURLS and DB schema
\r
4 function yourls_upgrade( $step, $oldver, $newver, $oldsql, $newsql ) {
\r
5 // As of now, there's no other upgrading possibility.
\r
6 // In the future this function may contain tests to upgrade
\r
7 // from any release to the latest
\r
8 yourls_upgrade_to_14( $step );
\r
12 // Upgrade DB Schema from 1.3-RC1 or prior to 1.4
\r
13 function yourls_upgrade_to_14( $step ) {
\r
17 // create table log & table options
\r
18 // update table url structure
\r
20 yourls_create_tables_for_14(); // no value returned, assuming it went OK
\r
21 yourls_alter_url_table_to_14(); // no value returned, assuming it went OK
\r
22 $clean = yourls_clean_htaccess_for_14(); // returns bool
\r
23 $create = yourls_create_htaccess(); // returns bool
\r
25 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
26 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=2&oldver=1.3&newver=1.4&oldsql=100&newsql=200", $create );
\r
30 // convert each link in table url
\r
31 yourls_update_table_to_14();
\r
35 // update table url structure part 2: recreate indexes
\r
36 yourls_alter_url_table_to_14_part_two();
\r
37 // update version & db_version & next_id in the option table
\r
38 // attempt to drop YOURLS_DB_TABLE_NEXTDEC
\r
39 yourls_update_options_to_14();
\r
45 // Update options to reflect new version
\r
46 function yourls_update_options_to_14() {
\r
47 yourls_update_option( 'version', YOURLS_VERSION );
\r
48 yourls_update_option( 'db_version', YOURLS_DB_VERSION );
\r
50 if( defined('YOURLS_DB_TABLE_NEXTDEC') ) {
\r
52 $table = YOURLS_DB_TABLE_NEXTDEC;
\r
53 $next_id = $ydb->get_var("SELECT `next_id` FROM `$table`");
\r
54 yourls_update_option( 'next_id', $next_id );
\r
55 @$ydb->query( "DROP TABLE `$table`" );
\r
57 yourls_update_option( 'next_id', 1 ); // In case someone mistakenly deleted the next_id constant or table too early
\r
61 // Create new tables for YOURLS 1.4: options & log
\r
62 function yourls_create_tables_for_14() {
\r
67 $queries[YOURLS_DB_TABLE_OPTIONS] =
\r
68 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.
\r
69 '`option_id` int(11) unsigned NOT NULL auto_increment,'.
\r
70 '`option_name` varchar(64) NOT NULL default "",'.
\r
71 '`option_value` longtext NOT NULL,'.
\r
72 'PRIMARY KEY (`option_id`,`option_name`),'.
\r
73 'KEY `option_name` (`option_name`)'.
\r
76 $queries[YOURLS_DB_TABLE_LOG] =
\r
77 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.
\r
78 '`click_id` int(11) NOT NULL auto_increment,'.
\r
79 '`click_time` datetime NOT NULL,'.
\r
80 '`shorturl` varchar(200) NOT NULL,'.
\r
81 '`referrer` varchar(200) NOT NULL,'.
\r
82 '`user_agent` varchar(255) NOT NULL,'.
\r
83 '`ip_address` varchar(41) NOT NULL,'.
\r
84 '`country_code` char(2) NOT NULL,'.
\r
85 'PRIMARY KEY (`click_id`),'.
\r
86 'KEY `shorturl` (`shorturl`)'.
\r
89 foreach( $queries as $query ) {
\r
90 $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
93 echo "<p>New tables created. Please wait...</p>";
\r
97 // Alter table structure, part 1 (change schema, drop index)
\r
98 function yourls_alter_url_table_to_14() {
\r
100 $table = YOURLS_DB_TABLE_URL;
\r
103 $results = array();
\r
104 $alters[] = "ALTER TABLE `$table` CHANGE `id` `keyword` VARCHAR( 200 ) NOT NULL";
\r
105 $alters[] = "ALTER TABLE `$table` CHANGE `url` `url` TEXT NOT NULL";
\r
106 $alters[] = "ALTER TABLE `$table` DROP PRIMARY KEY";
\r
108 foreach ( $alters as $query ) {
\r
109 $ydb->query( $query );
\r
112 echo "<p>Structure of existing tables updated. Please wait...</p>";
\r
115 // Alter table structure, part 2 (recreate indexes after the table is up to date)
\r
116 function yourls_alter_url_table_to_14_part_two() {
\r
118 $table = YOURLS_DB_TABLE_URL;
\r
121 $alters[] = "ALTER TABLE `$table` ADD PRIMARY KEY ( `keyword` )";
\r
122 $alters[] = "ALTER TABLE `$table` ADD INDEX ( `ip` )";
\r
123 $alters[] = "ALTER TABLE `$table` ADD INDEX ( `timestamp` )";
\r
125 foreach ( $alters as $query ) {
\r
126 $ydb->query( $query );
\r
129 echo "<p>New table index created</p>";
\r
132 // Convert each link from 1.3 (id) to 1.4 (keyword) structure
\r
133 function yourls_update_table_to_14() {
\r
135 $table = YOURLS_DB_TABLE_URL;
\r
137 // Modify each link to reflect new structure
\r
139 $from = isset($_GET['from']) ? intval( $_GET['from'] ) : 0 ;
\r
140 $total = yourls_get_db_stats();
\r
141 $total = $total['total_links'];
\r
143 $sql = "SELECT `keyword`,`url` FROM `$table` WHERE 1=1 ORDER BY `url` ASC LIMIT $from, $chunk ;";
\r
145 $rows = $ydb->get_results($sql);
\r
149 foreach( $rows as $row ) {
\r
150 $keyword = $row->keyword;
\r
152 $newkeyword = yourls_int2string( $keyword );
\r
153 $ydb->query("UPDATE `$table` SET `keyword` = '$newkeyword' WHERE `url` = '$url';");
\r
154 if( $ydb->result === true ) {
\r
157 echo "<p>Huho... Could not update rown with url='$url', from keyword '$keyword' to keyword '$newkeyword'</p>"; // Find what went wrong :/
\r
162 // All done for this chunk of queries, did it all go as expected?
\r
164 if( $count != $queries ) {
\r
166 $num = $count - $queries;
\r
167 echo "<p>$num error(s) occured while updating the URL table :(</p>";
\r
170 if ( $count == $chunk ) {
\r
171 // there are probably other rows to convert
\r
172 $from = $from + $chunk;
\r
173 $remain = $total - $from;
\r
174 echo "<p>Converted $chunk database rows ($remain remaining). Continuing... Please do not close this window until it's finished!</p>";
\r
175 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=2&oldver=1.3&newver=1.4&oldsql=100&newsql=200&from=$from", $success );
\r
178 echo '<p>All rows converted! Please wait...</p>';
\r
179 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=3&oldver=1.3&newver=1.4&oldsql=100&newsql=200", $success );
\r
184 // Clean .htaccess as it existed before 1.4. Returns boolean
\r
185 function yourls_clean_htaccess_for_14() {
\r
186 $filename = dirname(dirname(__FILE__)).'/.htaccess';
\r
189 if( is_writeable( $filename ) ) {
\r
190 $contents = implode( '', file( $filename ) );
\r
191 // remove "ShortURL" block
\r
192 $contents = preg_replace( '/# BEGIN ShortURL.*# END ShortURL/s', '', $contents );
\r
193 // comment out deprecated RewriteRule
\r
194 $find = 'RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]';
\r
195 $replace = "# You can safely remove this 5 lines block -- it's no longer used in YOURLS\n".
\r
197 $contents = str_replace( $find, $replace, $contents );
\r
199 // Write cleaned file
\r
200 $f = fopen( $filename, 'w' );
\r
201 fwrite( $f, $contents );
\r