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/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\retour\models; |
13
|
|
|
|
14
|
|
|
use nystudio107\retour\validators\DbStringValidator; |
15
|
|
|
use nystudio107\retour\validators\ParsedUriValidator; |
16
|
|
|
use nystudio107\retour\validators\UriValidator; |
17
|
|
|
use yii\behaviors\AttributeTypecastBehavior; |
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @author nystudio107 |
|
|
|
|
21
|
|
|
* @package Retour |
|
|
|
|
22
|
|
|
* @since 3.0.0 |
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
class StaticRedirects extends DbModel |
25
|
|
|
{ |
26
|
|
|
// Public Properties |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var int The id of the redirect. |
31
|
|
|
*/ |
32
|
|
|
public int $id = 0; |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @var null|int The siteId of the redirect (0 or null for all sites). |
36
|
|
|
*/ |
37
|
|
|
public ?int $siteId = null; |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @var int The id of the Element associated with this redirect (unused/vestigial). |
41
|
|
|
*/ |
42
|
|
|
public int $associatedElementId = 0; |
43
|
|
|
|
44
|
|
|
/** |
|
|
|
|
45
|
|
|
* @var bool Whether the redirect is enabled or not. |
46
|
|
|
*/ |
47
|
|
|
public bool $enabled = true; |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @var string The unparsed URL pattern that Retour should match |
51
|
|
|
*/ |
52
|
|
|
public string $redirectSrcUrl = ''; |
53
|
|
|
|
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* @var string The parsed URL pattern that Retour should match |
56
|
|
|
*/ |
57
|
|
|
public string $redirectSrcUrlParsed = ''; |
58
|
|
|
|
59
|
|
|
/** |
|
|
|
|
60
|
|
|
* @var string Should the legacy URL be matched by path or by full URL? |
61
|
|
|
*/ |
62
|
|
|
public string $redirectSrcMatch = ''; |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @var string Whether an `exactmatch` or `regexmatch` should be used when matching the URL. |
66
|
|
|
*/ |
67
|
|
|
public string $redirectMatchType = ''; |
68
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @var string The URL that should be redirected to. |
71
|
|
|
*/ |
72
|
|
|
public string $redirectDestUrl = ''; |
73
|
|
|
|
74
|
|
|
/** |
|
|
|
|
75
|
|
|
* @var int The http status code that should be used for the redirect. |
76
|
|
|
*/ |
77
|
|
|
public int $redirectHttpCode = 301; |
78
|
|
|
|
79
|
|
|
/** |
|
|
|
|
80
|
|
|
* @var int The priority that determines the selected redirect in case of multiple matches |
81
|
|
|
*/ |
82
|
|
|
public int $priority = 5; |
83
|
|
|
|
84
|
|
|
/** |
|
|
|
|
85
|
|
|
* @var int The number of times this redirect has been hit. |
86
|
|
|
*/ |
87
|
|
|
public int $hitCount = 0; |
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* @var string A datetime string of when this redirect was last hit |
91
|
|
|
*/ |
92
|
|
|
public string $hitLastTime = ''; |
93
|
|
|
|
94
|
|
|
// Public Methods |
95
|
|
|
// ========================================================================= |
96
|
|
|
|
97
|
|
|
/** |
|
|
|
|
98
|
|
|
* @inheritDoc |
99
|
|
|
*/ |
|
|
|
|
100
|
|
|
public function init(): void |
101
|
|
|
{ |
102
|
|
|
parent::init(); |
103
|
|
|
// If the redirectSrcUrl starts with `http`, default the match type to `fullurl` |
104
|
|
|
if (strpos($this->redirectSrcUrl, 'http') === 0) { |
105
|
|
|
$this->redirectSrcMatch = 'fullurl'; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
|
|
|
|
110
|
|
|
* @inheritdoc |
111
|
|
|
*/ |
|
|
|
|
112
|
|
|
public function rules(): array |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
['id', 'integer'], |
116
|
|
|
['siteId', 'integer'], |
117
|
|
|
['siteId', 'default', 'value' => null], |
118
|
|
|
['associatedElementId', 'default', 'value' => 0], |
119
|
|
|
['associatedElementId', 'integer'], |
120
|
|
|
['enabled', 'boolean'], |
121
|
|
|
['redirectSrcMatch', 'string'], |
122
|
|
|
['redirectSrcMatch', 'in', 'range' => ['pathonly', 'fullurl']], |
123
|
|
|
['redirectSrcMatch', 'default', 'value' => 'pathonly'], |
124
|
|
|
['redirectSrcMatch', DbStringValidator::class, 'max' => 32], |
125
|
|
|
['redirectMatchType', 'string'], |
126
|
|
|
['redirectMatchType', 'in', 'range' => ['exactmatch', 'regexmatch']], |
127
|
|
|
['redirectMatchType', 'default', 'value' => 'exactmatch'], |
128
|
|
|
['redirectMatchType', DbStringValidator::class, 'max' => 32], |
129
|
|
|
[ |
130
|
|
|
[ |
131
|
|
|
'redirectSrcUrl', |
132
|
|
|
'redirectSrcUrlParsed', |
133
|
|
|
'redirectDestUrl', |
134
|
|
|
], |
135
|
|
|
'default', |
136
|
|
|
'value' => '', |
137
|
|
|
], |
138
|
|
|
['redirectSrcUrlParsed', ParsedUriValidator::class, 'source' => 'redirectSrcUrl'], |
139
|
|
|
[ |
140
|
|
|
[ |
141
|
|
|
'redirectSrcUrl', |
142
|
|
|
'redirectSrcUrlParsed', |
143
|
|
|
'redirectDestUrl', |
144
|
|
|
], |
145
|
|
|
UriValidator::class, |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
[ |
149
|
|
|
'redirectSrcUrl', |
150
|
|
|
'redirectSrcUrlParsed', |
151
|
|
|
'redirectDestUrl', |
152
|
|
|
], |
153
|
|
|
DbStringValidator::class, |
154
|
|
|
'max' => 255, |
155
|
|
|
], |
156
|
|
|
[ |
157
|
|
|
[ |
158
|
|
|
'redirectSrcUrl', |
159
|
|
|
'redirectSrcUrlParsed', |
160
|
|
|
'redirectDestUrl', |
161
|
|
|
], |
162
|
|
|
'string', |
163
|
|
|
], |
164
|
|
|
['redirectHttpCode', 'integer'], |
165
|
|
|
['redirectHttpCode', 'in', 'range' => [301, 302, 307, 308, 410]], |
166
|
|
|
['redirectHttpCode', 'default', 'value' => 301], |
167
|
|
|
['priority', 'default', 'value' => 5], |
168
|
|
|
['priority', 'integer'], |
169
|
|
|
['priority', 'in', 'range' => [1, 2, 3, 4, 5, 6, 7, 8, 9]], |
170
|
|
|
['hitCount', 'default', 'value' => 0], |
171
|
|
|
['hitCount', 'integer'], |
172
|
|
|
['hitLastTime', 'safe'], |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
|
|
|
|
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function behaviors(): array |
180
|
|
|
{ |
181
|
|
|
return [ |
182
|
|
|
'typecast' => [ |
183
|
|
|
'class' => AttributeTypecastBehavior::class, |
184
|
|
|
// 'attributeTypes' will be composed automatically according to `rules()` |
185
|
|
|
], |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|