]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-upgrade.php
Clean .htaccess file from 1.3 to 1.4
[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         // 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
9 }\r
10 \r
11 \r
12 // Upgrade DB Schema from 1.3-RC1 or prior to 1.4\r
13 function yourls_upgrade_to_14( $step ) {\r
14         \r
15         switch( $step ) {\r
16         case 1:\r
17                 // create table log & table options\r
18                 // update table url structure\r
19                 // update .htaccess\r
20                 yourls_create_tables_for_14();\r
21                 yourls_alter_url_table_to_14();\r
22                 yourls_clean_htaccess_for_14();\r
23                 yourls_create_htaccess();\r
24                 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=2&oldver=1.3&newver=1.4&oldsql=100&newsql=200" );\r
25                 break;\r
26                 \r
27         case 2:\r
28                 // convert each link in table url\r
29                 yourls_update_table_to_14();\r
30                 break;\r
31         \r
32         case 3:\r
33                 // update table url structure part 2: recreate indexes\r
34                 yourls_alter_url_table_to_14_part_two();\r
35                 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=4&oldver=1.3&newver=1.4&oldsql=100&newsql=200" );\r
36                 break;\r
37         \r
38         case 4:\r
39                 // update version & db_version & next_id in the option table\r
40                 // attempt to drop YOURLS_DB_TABLE_NEXTDEC\r
41                 yourls_update_options_to_14();\r
42         }\r
43 }\r
44 \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
49         \r
50         global $ydb;\r
51         $table = YOURLS_DB_TABLE_NEXTDEC;\r
52         $next_id = $ydb->get_var("SELECT `next_id` FROM `$table`");\r
53         yourls_update_option( 'next_id', $next_id );\r
54         @$ydb->query( "DROP TABLE `$table`" );\r
55 }\r
56 \r
57 // Create new tables for YOURLS 1.4: options & log\r
58 function yourls_create_tables_for_14() {\r
59         global $ydb;\r
60 \r
61         $queries = array();\r
62 \r
63         $queries[YOURLS_DB_TABLE_OPTIONS] = \r
64                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.\r
65                 '`option_id` int(11) unsigned NOT NULL auto_increment,'.\r
66                 '`option_name` varchar(64) NOT NULL default "",'.\r
67                 '`option_value` longtext NOT NULL,'.\r
68                 'PRIMARY KEY (`option_id`,`option_name`),'.\r
69                 'KEY `option_name` (`option_name`)'.\r
70                 ');';\r
71                 \r
72         $queries[YOURLS_DB_TABLE_LOG] = \r
73                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.\r
74                 '`click_id` int(11) NOT NULL auto_increment,'.\r
75                 '`click_time` datetime NOT NULL,'.\r
76                 '`shorturl` varchar(200) NOT NULL,'.\r
77                 '`referrer` varchar(200) NOT NULL,'.\r
78                 '`user_agent` varchar(255) NOT NULL,'.\r
79                 '`ip_address` varchar(41) NOT NULL,'.\r
80                 '`country_code` char(2) NOT NULL,'.\r
81                 'PRIMARY KEY (`click_id`),'.\r
82                 'KEY `shorturl` (`shorturl`,`referrer`,`country_code`)'.\r
83                 ');';\r
84         \r
85         foreach( $queries as $query ) {\r
86                 $ydb->query( $query );\r
87         }\r
88         \r
89         echo "<p>New tables created. Please wait...</p>";\r
90 \r
91 }\r
92 \r
93 // Alter table structure, part 1 (change schema, drop index)\r
94 function yourls_alter_url_table_to_14() {\r
95         global $ydb;\r
96         $table = YOURLS_DB_TABLE_URL;\r
97 \r
98         $alters = array();\r
99         $results = array();\r
100         $alters[] = "ALTER TABLE `$table` CHANGE `id` `keyword` VARCHAR( 200 ) NOT NULL";\r
101         $alters[] = "ALTER TABLE `$table` CHANGE `url` `url` TEXT NOT NULL";\r
102         $alters[] = "ALTER TABLE `$table` DROP PRIMARY KEY";\r
103         \r
104         foreach ( $alters as $query ) {\r
105                 $ydb->query( $query );\r
106         }\r
107         \r
108         echo "<p>Structure of existing tables updated. Please wait...</p>";\r
109 }\r
110 \r
111 // Alter table structure, part 2 (recreate index after the table is up to date)\r
112 function yourls_alter_url_table_to_14_part_two() {\r
113         global $ydb;\r
114         $table = YOURLS_DB_TABLE_URL;\r
115         \r
116         $alters = array();\r
117         $alters[] = "ALTER TABLE `$table` ADD PRIMARY KEY ( `keyword` )";\r
118         \r
119         foreach ( $alters as $query ) {\r
120                 $ydb->query( $query );\r
121         }\r
122 \r
123         echo "<p>New table index created</p>";\r
124 }\r
125 \r
126 // Convert each link from 1.3 (id) to 1.4 (keyword) structure\r
127 function yourls_update_table_to_14() {\r
128         global $ydb;\r
129         $table = YOURLS_DB_TABLE_URL;\r
130 \r
131         // Modify each link to reflect new structure\r
132         $chunk = 30;\r
133         $from = isset($_GET['from']) ? intval( $_GET['from'] ) : 0 ;\r
134         $total = yourls_get_db_stats();\r
135         $total = $total['total_links'];\r
136         \r
137         $sql = "SELECT `keyword`,`url` FROM `$table` WHERE 1=1 ORDER BY `url` ASC LIMIT $from, $chunk ;";\r
138         \r
139         $rows = $ydb->get_results($sql);\r
140         \r
141         $count = 0;\r
142         foreach( $rows as $row ) {\r
143                 $keyword = $row->keyword;\r
144                 $url = $row->url;\r
145                 $newkeyword = yourls_int2string( $keyword );\r
146                 $result = $ydb->query("UPDATE `$table` SET `keyword` = '$newkeyword' WHERE `url` = '$url';");\r
147                 $count++;\r
148         }\r
149         \r
150         if ( $count == $chunk ) {\r
151                 // there are probably other rows to convert\r
152                 $from = $from + $chunk;\r
153                 $remain = $total - $from;\r
154                 echo "<p>Converted $chunk database rows ($remain remaining). Continuing... Please do not close this window until it's finished!</p>";\r
155                 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=2&oldver=1.3&newver=1.4&oldsql=100&newsql=200&from=$from" );\r
156         } else {\r
157                 // All done\r
158                 echo '<p>All rows converted! Please wait...</p>';\r
159                 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=3&oldver=1.3&newver=1.4&oldsql=100&newsql=200" );\r
160         }\r
161         \r
162 }\r
163 \r
164 // Clean .htaccess as it existed before 1.4\r
165 function yourls_clean_htaccess_for_14() {\r
166         $filename = dirname(dirname(__FILE__)).'/.htaccess';\r
167         \r
168         $contents = implode( '', file( $filename );\r
169         // remove "ShortURL" block\r
170         $contents = preg_replace( '/# BEGIN ShortURL.*# END ShortURL/', '', $contents );\r
171         // comment out deprecated RewriteRule\r
172         $find = 'RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]';\r
173         $replace = "# You can safely remove this 4 lines block -- it's no longer used in YOURLS\n".\r
174                         "# $find";\r
175         $contents = str_replace( $find, $replace, $contents );\r
176         \r
177         // Write cleaned file\r
178         $f = fopen( $filename, 'w' );\r
179         fwrite( $f, $contents );\r
180         fclose( $f );\r
181 \r
182         echo "<p>Old .htaccess file cleaned up</p>";\r
183         \r
184 }\r
185 \r