]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-upgrade.php
Typo. Fixes Issue 87.
[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(); // 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
24                 $result = ( $clean && $create );\r
25                 if ( !$result )\r
26                         echo "<p>Manually check your <tt>.htaccess</tt> file. Please refer to <a href='http://yourls.org/htaccess'>http://yourls.org/htaccess</a>.";\r
27                 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=2&oldver=1.3&newver=1.4&oldsql=100&newsql=200", $result );\r
28                 break;\r
29                 \r
30         case 2:\r
31                 // convert each link in table url\r
32                 yourls_update_table_to_14();\r
33                 break;\r
34         \r
35         case 3:\r
36                 // update table url structure part 2: recreate indexes\r
37                 yourls_alter_url_table_to_14_part_two();\r
38                 // update version & db_version & next_id in the option table\r
39                 // attempt to drop YOURLS_DB_TABLE_NEXTDEC\r
40                 yourls_update_options_to_14();\r
41                 break;\r
42         \r
43         }\r
44 }\r
45 \r
46 // Update options to reflect new version\r
47 function yourls_update_options_to_14() {\r
48         yourls_update_option( 'version', YOURLS_VERSION );\r
49         yourls_update_option( 'db_version', YOURLS_DB_VERSION );\r
50         \r
51         if( defined('YOURLS_DB_TABLE_NEXTDEC') ) {\r
52                 global $ydb;\r
53                 $table = YOURLS_DB_TABLE_NEXTDEC;\r
54                 $next_id = $ydb->get_var("SELECT `next_id` FROM `$table`");\r
55                 yourls_update_option( 'next_id', $next_id );\r
56                 @$ydb->query( "DROP TABLE `$table`" );\r
57         } else {\r
58                 yourls_update_option( 'next_id', 1 ); // In case someone mistakenly deleted the next_id constant or table too early\r
59         }\r
60 }\r
61 \r
62 // Create new tables for YOURLS 1.4: options & log\r
63 function yourls_create_tables_for_14() {\r
64         global $ydb;\r
65 \r
66         $queries = array();\r
67 \r
68         $queries[YOURLS_DB_TABLE_OPTIONS] = \r
69                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.\r
70                 '`option_id` int(11) unsigned NOT NULL auto_increment,'.\r
71                 '`option_name` varchar(64) NOT NULL default "",'.\r
72                 '`option_value` longtext NOT NULL,'.\r
73                 'PRIMARY KEY (`option_id`,`option_name`),'.\r
74                 'KEY `option_name` (`option_name`)'.\r
75                 ');';\r
76                 \r
77         $queries[YOURLS_DB_TABLE_LOG] = \r
78                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.\r
79                 '`click_id` int(11) NOT NULL auto_increment,'.\r
80                 '`click_time` datetime NOT NULL,'.\r
81                 '`shorturl` varchar(200) NOT NULL,'.\r
82                 '`referrer` varchar(200) NOT NULL,'.\r
83                 '`user_agent` varchar(255) NOT NULL,'.\r
84                 '`ip_address` varchar(41) NOT NULL,'.\r
85                 '`country_code` char(2) NOT NULL,'.\r
86                 'PRIMARY KEY (`click_id`),'.\r
87                 'KEY `shorturl` (`shorturl`)'.\r
88                 ');';\r
89         \r
90         foreach( $queries as $query ) {\r
91                 $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
92         }\r
93         \r
94         echo "<p>New tables created. Please wait...</p>";\r
95 \r
96 }\r
97 \r
98 // Alter table structure, part 1 (change schema, drop index)\r
99 function yourls_alter_url_table_to_14() {\r
100         global $ydb;\r
101         $table = YOURLS_DB_TABLE_URL;\r
102 \r
103         $alters = array();\r
104         $results = array();\r
105         $alters[] = "ALTER TABLE `$table` DROP PRIMARY KEY";\r
106         $alters[] = "ALTER TABLE `$table` CHANGE `id` `keyword` VARCHAR( 200 ) NOT NULL";\r
107         $alters[] = "ALTER TABLE `$table` CHANGE `url` `url` TEXT NOT NULL";\r
108         \r
109         foreach ( $alters as $query ) {\r
110                 $ydb->query( $query );\r
111         }\r
112         \r
113         echo "<p>Structure of existing tables updated. Please wait...</p>";\r
114 }\r
115 \r
116 // Alter table structure, part 2 (recreate indexes after the table is up to date)\r
117 function yourls_alter_url_table_to_14_part_two() {\r
118         global $ydb;\r
119         $table = YOURLS_DB_TABLE_URL;\r
120         \r
121         $alters = array();\r
122         $alters[] = "ALTER TABLE `$table` ADD PRIMARY KEY ( `keyword` )";\r
123         $alters[] = "ALTER TABLE `$table` ADD INDEX ( `ip` )";\r
124         $alters[] = "ALTER TABLE `$table` ADD INDEX ( `timestamp` )";\r
125         \r
126         foreach ( $alters as $query ) {\r
127                 $ydb->query( $query );\r
128         }\r
129 \r
130         echo "<p>New table index created</p>";\r
131 }\r
132 \r
133 // Convert each link from 1.3 (id) to 1.4 (keyword) structure\r
134 function yourls_update_table_to_14() {\r
135         global $ydb;\r
136         $table = YOURLS_DB_TABLE_URL;\r
137 \r
138         // Modify each link to reflect new structure\r
139         $chunk = 15;\r
140         $from = isset($_GET['from']) ? intval( $_GET['from'] ) : 0 ;\r
141         $total = yourls_get_db_stats();\r
142         $total = $total['total_links'];\r
143         \r
144         $sql = "SELECT `keyword`,`url` FROM `$table` WHERE 1=1 ORDER BY `url` ASC LIMIT $from, $chunk ;";\r
145         \r
146         $rows = $ydb->get_results($sql);\r
147         \r
148         $count = 0;\r
149         $queries = 0;\r
150         foreach( $rows as $row ) {\r
151                 $keyword = $row->keyword;\r
152                 $url = $row->url;\r
153                 $newkeyword = yourls_int2string( $keyword );\r
154                 $ydb->query("UPDATE `$table` SET `keyword` = '$newkeyword' WHERE `url` = '$url';");\r
155                 if( $ydb->result === true ) {\r
156                         $queries++;\r
157                 } else {\r
158                         echo "<p>Huho... Could not update rown with url='$url', from keyword '$keyword' to keyword '$newkeyword'</p>"; // Find what went wrong :/\r
159                 }\r
160                 $count++;\r
161         }\r
162         \r
163         // All done for this chunk of queries, did it all go as expected?\r
164         $success = true;\r
165         if( $count != $queries ) {\r
166                 $success = false;\r
167                 $num = $count - $queries;\r
168                 echo "<p>$num error(s) occured while updating the URL table :(</p>";\r
169         }\r
170         \r
171         if ( $count == $chunk ) {\r
172                 // there are probably other rows to convert\r
173                 $from = $from + $chunk;\r
174                 $remain = $total - $from;\r
175                 echo "<p>Converted $chunk database rows ($remain remaining). Continuing... Please do not close this window until it's finished!</p>";\r
176                 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=2&oldver=1.3&newver=1.4&oldsql=100&newsql=200&from=$from", $success );\r
177         } else {\r
178                 // All done\r
179                 echo '<p>All rows converted! Please wait...</p>';\r
180                 yourls_redirect_javascript( YOURLS_SITE."/admin/upgrade.php?step=3&oldver=1.3&newver=1.4&oldsql=100&newsql=200", $success );\r
181         }\r
182         \r
183 }\r
184 \r
185 // Clean .htaccess as it existed before 1.4. Returns boolean\r
186 function yourls_clean_htaccess_for_14() {\r
187         $filename = dirname(dirname(__FILE__)).'/.htaccess';\r
188         \r
189         $result = false;\r
190         if( is_writeable( $filename ) ) {\r
191                 $contents = implode( '', file( $filename ) );\r
192                 // remove "ShortURL" block\r
193                 $contents = preg_replace( '/# BEGIN ShortURL.*# END ShortURL/s', '', $contents );\r
194                 // comment out deprecated RewriteRule\r
195                 $find = 'RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]';\r
196                 $replace = "# You can safely remove this 5 lines block -- it's no longer used in YOURLS\n".\r
197                                 "# $find";\r
198                 $contents = str_replace( $find, $replace, $contents );\r
199                 \r
200                 // Write cleaned file\r
201                 $f = fopen( $filename, 'w' );\r
202                 fwrite( $f, $contents );\r
203                 fclose( $f );\r
204                 \r
205                 $result = true;\r
206         }\r
207 \r
208         if ( $result ) {\r
209                 echo "<p>Old .htaccess file cleaned up</p>";\r
210         } else {\r
211                 echo "<p>Could not update <tt>.htaccess</tt>.</p>";\r
212         }\r
213         \r
214         return $result;\r
215 }\r
216 \r