nystudio107 /
craft-retour
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Retour plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * Retour allows you to intelligently redirect legacy URLs, so that you don't |
||
| 6 | * lose SEO value when rebuilding & restructuring a website |
||
| 7 | * |
||
| 8 | * @link https://nystudio107.com/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 9 | * @copyright Copyright (c) 2018 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 10 | */ |
||
|
0 ignored issues
–
show
|
|||
| 11 | |||
| 12 | namespace nystudio107\retour\migrations; |
||
| 13 | |||
| 14 | use Craft; |
||
| 15 | use craft\db\Migration; |
||
| 16 | use nystudio107\retour\widgets\RetourWidget; |
||
| 17 | |||
| 18 | /** |
||
|
0 ignored issues
–
show
|
|||
| 19 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 20 | * @package Retour |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @since 3.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 22 | */ |
||
|
0 ignored issues
–
show
|
|||
| 23 | class Install extends Migration |
||
| 24 | { |
||
| 25 | // Public Properties |
||
| 26 | // ========================================================================= |
||
| 27 | |||
| 28 | /** |
||
|
0 ignored issues
–
show
|
|||
| 29 | * @var ?string The database driver to use |
||
| 30 | */ |
||
| 31 | public ?string $driver = null; |
||
| 32 | |||
| 33 | // Public Methods |
||
| 34 | // ========================================================================= |
||
| 35 | |||
| 36 | /** |
||
|
0 ignored issues
–
show
|
|||
| 37 | * @inheritdoc |
||
| 38 | */ |
||
|
0 ignored issues
–
show
|
|||
| 39 | public function safeUp(): bool |
||
| 40 | { |
||
| 41 | $this->driver = Craft::$app->getConfig()->getDb()->driver; |
||
| 42 | if ($this->createTables()) { |
||
| 43 | $this->createIndexes(); |
||
| 44 | $this->addForeignKeys(); |
||
| 45 | // Refresh the db schema caches |
||
| 46 | Craft::$app->db->schema->refresh(); |
||
| 47 | $this->insertDefaultData(); |
||
| 48 | } |
||
| 49 | |||
| 50 | // Update retour widget type |
||
| 51 | $this->update('{{%widgets}}', [ |
||
|
0 ignored issues
–
show
|
|||
| 52 | 'type' => RetourWidget::class, |
||
| 53 | ], ['type' => 'Retour']); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 54 | |||
| 55 | return true; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
|
0 ignored issues
–
show
|
|||
| 59 | * @inheritdoc |
||
| 60 | */ |
||
|
0 ignored issues
–
show
|
|||
| 61 | public function safeDown(): bool |
||
| 62 | { |
||
| 63 | $this->driver = Craft::$app->getConfig()->getDb()->driver; |
||
| 64 | $this->removeTables(); |
||
| 65 | |||
| 66 | return true; |
||
| 67 | } |
||
| 68 | |||
| 69 | // Protected Methods |
||
| 70 | // ========================================================================= |
||
| 71 | |||
| 72 | /** |
||
|
0 ignored issues
–
show
|
|||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | protected function createTables(): bool |
||
| 76 | { |
||
| 77 | $tablesCreated = false; |
||
| 78 | |||
| 79 | $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_redirects}}'); |
||
| 80 | if ($tableSchema === null) { |
||
| 81 | $tablesCreated = true; |
||
| 82 | $this->createTable( |
||
| 83 | '{{%retour_redirects}}', |
||
| 84 | [ |
||
| 85 | 'id' => $this->primaryKey(), |
||
| 86 | 'dateCreated' => $this->dateTime()->notNull(), |
||
| 87 | 'dateUpdated' => $this->dateTime()->notNull(), |
||
| 88 | 'uid' => $this->uid(), |
||
| 89 | |||
| 90 | 'siteId' => $this->integer()->null()->defaultValue(null), |
||
| 91 | 'associatedElementId' => $this->integer()->notNull(), |
||
| 92 | 'enabled' => $this->boolean()->defaultValue(true), |
||
| 93 | 'redirectSrcUrl' => $this->string(255)->defaultValue(''), |
||
| 94 | 'redirectSrcUrlParsed' => $this->string(255)->defaultValue(''), |
||
| 95 | 'redirectSrcMatch' => $this->string(32)->defaultValue('pathonly'), |
||
| 96 | 'redirectMatchType' => $this->string(32)->defaultValue('exactmatch'), |
||
| 97 | 'redirectDestUrl' => $this->string(255)->defaultValue(''), |
||
| 98 | 'redirectHttpCode' => $this->integer()->defaultValue(301), |
||
| 99 | 'priority' => $this->integer()->null()->defaultValue(5), |
||
| 100 | 'hitCount' => $this->integer()->defaultValue(1), |
||
| 101 | 'hitLastTime' => $this->dateTime(), |
||
| 102 | ] |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_static_redirects}}'); |
||
| 107 | if ($tableSchema === null) { |
||
| 108 | $tablesCreated = true; |
||
| 109 | $this->createTable( |
||
| 110 | '{{%retour_static_redirects}}', |
||
| 111 | [ |
||
| 112 | 'id' => $this->primaryKey(), |
||
| 113 | 'dateCreated' => $this->dateTime()->notNull(), |
||
| 114 | 'dateUpdated' => $this->dateTime()->notNull(), |
||
| 115 | 'uid' => $this->uid(), |
||
| 116 | |||
| 117 | 'siteId' => $this->integer()->null()->defaultValue(null), |
||
| 118 | 'associatedElementId' => $this->integer()->notNull(), |
||
| 119 | 'enabled' => $this->boolean()->defaultValue(true), |
||
| 120 | 'redirectSrcUrl' => $this->string(255)->defaultValue(''), |
||
| 121 | 'redirectSrcUrlParsed' => $this->string(255)->defaultValue(''), |
||
| 122 | 'redirectSrcMatch' => $this->string(32)->defaultValue('pathonly'), |
||
| 123 | 'redirectMatchType' => $this->string(32)->defaultValue('exactmatch'), |
||
| 124 | 'redirectDestUrl' => $this->string(255)->defaultValue(''), |
||
| 125 | 'redirectHttpCode' => $this->integer()->defaultValue(301), |
||
| 126 | 'priority' => $this->integer()->null()->defaultValue(5), |
||
| 127 | 'hitCount' => $this->integer()->defaultValue(1), |
||
| 128 | 'hitLastTime' => $this->dateTime(), |
||
| 129 | ] |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 133 | $tableSchema = Craft::$app->db->schema->getTableSchema('{{%retour_stats}}'); |
||
| 134 | if ($tableSchema === null) { |
||
| 135 | $tablesCreated = true; |
||
| 136 | $this->createTable( |
||
| 137 | '{{%retour_stats}}', |
||
| 138 | [ |
||
| 139 | 'id' => $this->primaryKey(), |
||
| 140 | 'dateCreated' => $this->dateTime()->notNull(), |
||
| 141 | 'dateUpdated' => $this->dateTime()->notNull(), |
||
| 142 | 'uid' => $this->uid(), |
||
| 143 | |||
| 144 | 'siteId' => $this->integer()->null()->defaultValue(null), |
||
| 145 | 'redirectSrcUrl' => $this->string(255)->defaultValue(''), |
||
| 146 | 'referrerUrl' => $this->string(2000)->defaultValue(''), |
||
| 147 | 'remoteIp' => $this->string(45)->defaultValue(''), |
||
| 148 | 'userAgent' => $this->string(255)->defaultValue(''), |
||
| 149 | 'exceptionMessage' => $this->string(255)->defaultValue(''), |
||
| 150 | 'exceptionFilePath' => $this->string(255)->defaultValue(''), |
||
| 151 | 'exceptionFileLine' => $this->integer()->defaultValue(0), |
||
| 152 | 'hitCount' => $this->integer()->defaultValue(1), |
||
| 153 | 'hitLastTime' => $this->dateTime(), |
||
| 154 | 'handledByRetour' => $this->boolean()->defaultValue(false), |
||
| 155 | ] |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $tablesCreated; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
|
0 ignored issues
–
show
|
|||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | protected function createIndexes(): void |
||
| 166 | { |
||
| 167 | $this->createIndex( |
||
| 168 | $this->db->getIndexName(), |
||
| 169 | '{{%retour_static_redirects}}', |
||
| 170 | 'redirectSrcUrlParsed', |
||
| 171 | false |
||
| 172 | ); |
||
| 173 | |||
| 174 | $this->createIndex( |
||
| 175 | $this->db->getIndexName(), |
||
| 176 | '{{%retour_redirects}}', |
||
| 177 | 'redirectSrcUrlParsed', |
||
| 178 | false |
||
| 179 | ); |
||
| 180 | |||
| 181 | $this->createIndex( |
||
| 182 | $this->db->getIndexName(), |
||
| 183 | '{{%retour_static_redirects}}', |
||
| 184 | 'redirectSrcUrl', |
||
| 185 | false |
||
| 186 | ); |
||
| 187 | |||
| 188 | $this->createIndex( |
||
| 189 | $this->db->getIndexName(), |
||
| 190 | '{{%retour_redirects}}', |
||
| 191 | 'redirectSrcUrl', |
||
| 192 | false |
||
| 193 | ); |
||
| 194 | |||
| 195 | $this->createIndex( |
||
| 196 | $this->db->getIndexName(), |
||
| 197 | '{{%retour_stats}}', |
||
| 198 | 'redirectSrcUrl', |
||
| 199 | false |
||
| 200 | ); |
||
| 201 | |||
| 202 | $this->createIndex( |
||
| 203 | $this->db->getIndexName(), |
||
| 204 | '{{%retour_static_redirects}}', |
||
| 205 | 'redirectMatchType', |
||
| 206 | false |
||
| 207 | ); |
||
| 208 | |||
| 209 | $this->createIndex( |
||
| 210 | $this->db->getIndexName(), |
||
| 211 | '{{%retour_redirects}}', |
||
| 212 | 'redirectMatchType', |
||
| 213 | false |
||
| 214 | ); |
||
| 215 | |||
| 216 | $this->createIndex( |
||
| 217 | $this->db->getIndexName(), |
||
| 218 | '{{%retour_redirects}}', |
||
| 219 | 'siteId', |
||
| 220 | false |
||
| 221 | ); |
||
| 222 | |||
| 223 | $this->createIndex( |
||
| 224 | $this->db->getIndexName(), |
||
| 225 | '{{%retour_static_redirects}}', |
||
| 226 | 'siteId', |
||
| 227 | false |
||
| 228 | ); |
||
| 229 | |||
| 230 | $this->createIndex( |
||
| 231 | $this->db->getIndexName(), |
||
| 232 | '{{%retour_stats}}', |
||
| 233 | 'siteId', |
||
| 234 | false |
||
| 235 | ); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
|
0 ignored issues
–
show
|
|||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | protected function addForeignKeys(): void |
||
| 242 | { |
||
| 243 | $this->addForeignKey( |
||
| 244 | $this->db->getForeignKeyName(), |
||
| 245 | '{{%retour_redirects}}', |
||
| 246 | 'associatedElementId', |
||
| 247 | '{{%elements}}', |
||
| 248 | 'id', |
||
| 249 | 'CASCADE', |
||
| 250 | 'CASCADE' |
||
| 251 | ); |
||
| 252 | |||
| 253 | $this->addForeignKey( |
||
| 254 | $this->db->getForeignKeyName(), |
||
| 255 | '{{%retour_static_redirects}}', |
||
| 256 | 'siteId', |
||
| 257 | '{{%sites}}', |
||
| 258 | 'id', |
||
| 259 | 'CASCADE', |
||
| 260 | 'CASCADE' |
||
| 261 | ); |
||
| 262 | |||
| 263 | $this->addForeignKey( |
||
| 264 | $this->db->getForeignKeyName(), |
||
| 265 | '{{%retour_stats}}', |
||
| 266 | 'siteId', |
||
| 267 | '{{%sites}}', |
||
| 268 | 'id', |
||
| 269 | 'CASCADE', |
||
| 270 | 'CASCADE' |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
|
0 ignored issues
–
show
|
|||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | protected function insertDefaultData(): void |
||
| 278 | { |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
|
0 ignored issues
–
show
|
|||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | protected function removeTables(): void |
||
| 285 | { |
||
| 286 | $this->dropTableIfExists('{{%retour_redirects}}'); |
||
| 287 | $this->dropTableIfExists('{{%retour_static_redirects}}'); |
||
| 288 | $this->dropTableIfExists('{{%retour_stats}}'); |
||
| 289 | } |
||
| 290 | } |
||
| 291 |