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