| Conditions | 27 |
| Paths | 1768 |
| Total Lines | 189 |
| Code Lines | 91 |
| Lines | 15 |
| Ratio | 7.94 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 21 | public static function upgrade() |
||
| 22 | { |
||
| 23 | // 2.3dev |
||
| 24 | if (version_compare(self::$existing_version, '2.3dev', '<=')) { |
||
| 25 | Symphony::Configuration()->set('version', '2.3dev', 'symphony'); |
||
| 26 | Symphony::Configuration()->set('useragent', 'Symphony/2.3dev', 'general'); |
||
| 27 | |||
| 28 | // Add Publish Label to `tbl_fields` |
||
| 29 | if (!Symphony::Database()->tableContainsField('tbl_fields', 'publish_label')) { |
||
| 30 | Symphony::Database()->query('ALTER TABLE `tbl_fields` ADD `publish_label` VARCHAR(255) DEFAULT NULL'); |
||
| 31 | } |
||
| 32 | |||
| 33 | // Migrate any Checkbox's Long Description to Publish Label |
||
| 34 | try { |
||
| 35 | $checkboxes = Symphony::Database()->fetch("SELECT `field_id`, `description` FROM `tbl_fields_checkbox`"); |
||
| 36 | |||
| 37 | foreach ($checkboxes as $field) { |
||
| 38 | if (!isset($field['description'])) { |
||
| 39 | continue; |
||
| 40 | } |
||
| 41 | |||
| 42 | Symphony::Database()->query(sprintf( |
||
| 43 | " |
||
| 44 | UPDATE `tbl_fields` |
||
| 45 | SET `publish_label` = '%s' |
||
| 46 | WHERE `id` = %d |
||
| 47 | LIMIT 1; |
||
| 48 | ", |
||
| 49 | $field['description'], |
||
| 50 | $field['field_id'] |
||
| 51 | )); |
||
| 52 | } |
||
| 53 | |||
| 54 | Symphony::Database()->query("ALTER TABLE `tbl_fields_checkbox` DROP `description`"); |
||
| 55 | } catch (Exception $ex) { |
||
| 56 | } |
||
| 57 | |||
| 58 | // Removing unused settings |
||
| 59 | Symphony::Configuration()->remove('allow_page_subscription', 'symphony'); |
||
| 60 | Symphony::Configuration()->remove('strict_error_handling', 'symphony'); |
||
| 61 | Symphony::Configuration()->remove('character_set', 'database'); |
||
| 62 | Symphony::Configuration()->remove('character_encoding', 'database'); |
||
| 63 | Symphony::Configuration()->remove('runtime_character_set_alter', 'database'); |
||
| 64 | |||
| 65 | if (Symphony::Configuration()->get('pagination_maximum_rows', 'symphony') === '17') { |
||
| 66 | Symphony::Configuration()->set('pagination_maximum_rows', '20', 'symphony'); |
||
| 67 | } |
||
| 68 | |||
| 69 | Symphony::Configuration()->write(); |
||
| 70 | } |
||
| 71 | |||
| 72 | // 2.3 Beta 1 |
||
| 73 | if (version_compare(self::$existing_version, '2.3beta1', '<=')) { |
||
| 74 | Symphony::Configuration()->set('version', '2.3beta1', 'symphony'); |
||
| 75 | Symphony::Configuration()->set('useragent', 'Symphony/2.3 Beta 1', 'general'); |
||
| 76 | |||
| 77 | Symphony::Configuration()->write(); |
||
| 78 | } |
||
| 79 | |||
| 80 | // 2.3 Beta 2 |
||
| 81 | if (version_compare(self::$existing_version, '2.3beta2', '<=')) { |
||
| 82 | // Migrate Publish Labels (if created) to the Label field |
||
| 83 | // Then drop Publish Label, we're going to use element_name and label |
||
| 84 | // to take care of the same functionality! |
||
| 85 | try { |
||
| 86 | if (Symphony::Database()->tableContainsField('tbl_fields', 'publish_label')) { |
||
| 87 | $fields = Symphony::Database()->fetch('SELECT `publish_label`, `label`, `id` FROM `tbl_fields`'); |
||
| 88 | |||
| 89 | foreach ($fields as $field) { |
||
| 90 | if (!$field['publish_label']) { |
||
| 91 | continue; |
||
| 92 | } |
||
| 93 | |||
| 94 | Symphony::Database()->query(sprintf( |
||
| 95 | " |
||
| 96 | UPDATE `tbl_fields` |
||
| 97 | SET `label` = '%s' |
||
| 98 | WHERE `id` = %d |
||
| 99 | LIMIT 1; |
||
| 100 | ", |
||
| 101 | $field['publish_label'], |
||
| 102 | $field['id'] |
||
| 103 | )); |
||
| 104 | } |
||
| 105 | |||
| 106 | Symphony::Database()->query("ALTER TABLE `tbl_fields` DROP `publish_label`"); |
||
| 107 | } |
||
| 108 | } catch (Exception $ex) { |
||
| 109 | Symphony::Log()->notice($ex->getMessage()); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Add uniqueness constraint for the Authors table. #937 |
||
| 113 | try { |
||
| 114 | Symphony::Database()->query("ALTER TABLE `tbl_authors` ADD UNIQUE KEY `email` (`email`)"); |
||
| 115 | } catch (DatabaseException $ex) { |
||
| 116 | // 1061 will be 'duplicate key', which is fine (means key was added earlier) |
||
| 117 | // 1062 means the key failed to apply, which is bad. |
||
| 118 | // @see http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html |
||
| 119 | if ($ex->getDatabaseErrorCode() === 1062) { |
||
| 120 | Symphony::Log()->error( |
||
| 121 | __("You have multiple Authors with the same email address, which can cause issues with password retrieval. Please ensure all Authors have unique email addresses before updating. " . $ex->getMessage()) |
||
| 122 | ); |
||
| 123 | |||
| 124 | return false; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | // Update the version information |
||
| 129 | Symphony::Configuration()->set('version', '2.3beta2', 'symphony'); |
||
| 130 | Symphony::Configuration()->set('useragent', 'Symphony/2.3 Beta 2', 'general'); |
||
| 131 | |||
| 132 | Symphony::Configuration()->write(); |
||
| 133 | } |
||
| 134 | |||
| 135 | // 2.3 Beta 3 |
||
| 136 | if (version_compare(self::$existing_version, '2.3beta3', '<=')) { |
||
| 137 | // Refresh indexes on existing Author field tables |
||
| 138 | $author_fields = Symphony::Database()->fetchCol( |
||
| 139 | "field_id", |
||
| 140 | "SELECT `field_id` FROM `tbl_fields_author`" |
||
| 141 | ); |
||
| 142 | |||
| 143 | View Code Duplication | foreach ($author_fields as $id) { |
|
| 144 | $table = 'tbl_entries_data_' . $id; |
||
| 145 | |||
| 146 | // MySQL doesn't support DROP IF EXISTS, so we'll try and catch. |
||
| 147 | try { |
||
| 148 | Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP INDEX `entry_id`"); |
||
| 149 | } catch (Exception $ex) { |
||
| 150 | } |
||
| 151 | |||
| 152 | try { |
||
| 153 | Symphony::Database()->query("CREATE UNIQUE INDEX `author` ON `" . $table . "` (`entry_id`, `author_id`)"); |
||
| 154 | Symphony::Database()->query("OPTIMIZE TABLE " . $table); |
||
| 155 | } catch (Exception $ex) { |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | // Move section sorting data from the database to the filesystem. #977 |
||
| 160 | if (Symphony::Database()->tableContainsField('tbl_sections', 'entry_order')) { |
||
| 161 | $sections = Symphony::Database()->fetch("SELECT `handle`, `entry_order`, `entry_order_direction` FROM `tbl_sections`"); |
||
| 162 | foreach ($sections as $s) { |
||
| 163 | Symphony::Configuration()->set( |
||
| 164 | 'section_' . $s['handle'] . '_sortby', |
||
| 165 | $s['entry_order'], |
||
| 166 | 'sorting' |
||
| 167 | ); |
||
| 168 | Symphony::Configuration()->set( |
||
| 169 | 'section_' . $s['handle'] . '_order', |
||
| 170 | $s['entry_order_direction'], |
||
| 171 | 'sorting' |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | // Drop `local`/`gmt` from Date fields, add `date` column. #693 |
||
| 177 | $date_fields = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_date`"); |
||
| 178 | |||
| 179 | foreach ($date_fields as $id) { |
||
| 180 | $table = 'tbl_entries_data_' . $id; |
||
| 181 | |||
| 182 | // Don't catch an Exception, we should halt updating if something goes wrong here |
||
| 183 | // Add the new `date` column for Date fields |
||
| 184 | if (!Symphony::Database()->tableContainsField($table, 'date')) { |
||
| 185 | Symphony::Database()->query("ALTER TABLE `" . $table . "` ADD `date` DATETIME DEFAULT NULL"); |
||
| 186 | Symphony::Database()->query("CREATE INDEX `date` ON `" . $table . "` (`date`)"); |
||
| 187 | } |
||
| 188 | |||
| 189 | if (Symphony::Database()->tableContainsField($table, 'date')) { |
||
| 190 | // Populate new Date column |
||
| 191 | if (Symphony::Database()->query("UPDATE `" . $table . "` SET date = CONVERT_TZ(SUBSTRING(value, 1, 19), SUBSTRING(value, -6), '+00:00')")) { |
||
| 192 | // Drop the `local`/`gmt` columns from Date fields |
||
| 193 | if (Symphony::Database()->tableContainsField($table, 'local')) { |
||
| 194 | Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `local`;"); |
||
| 195 | } |
||
| 196 | |||
| 197 | if (Symphony::Database()->tableContainsField($table, 'gmt')) { |
||
| 198 | Symphony::Database()->query("ALTER TABLE `" . $table . "` DROP `gmt`;"); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | Symphony::Database()->query("OPTIMIZE TABLE " . $table); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | // Update the version information |
||
| 208 | return parent::upgrade(); |
||
| 209 | } |
||
| 210 | |||
| 219 |