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