-
Notifications
You must be signed in to change notification settings - Fork 3
/
redirect.entity.inc
240 lines (205 loc) · 5.46 KB
/
redirect.entity.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Defines the Redirect entity class.
*/
class Redirect extends Entity {
/**
* The redirect ID.
*
* @var integer
*/
public $rid;
/**
* The type of the redirect entity. May be:
*
* - redirect: A redirect that is saved and loaded from the database.
* - manual: A single-use redirect that was manually created on-the-fly.
*
* @var string
*/
public $type = 'redirect';
/**
* The comment language code.
*
* @var string
*/
public $langcode = LANGUAGE_NONE;
/**
* The from which the redirect will start.
*
* @var string
*/
public $source = '';
/**
* An array of options for the source of the redirect.
*
* @var array
*/
public $source_options = array();
/**
* The path to which the source will be redirected.
*
* @var string
*/
public $redirect;
/**
* An array of options for the redirect.
*
* @var array
*/
public $redirect_options = array();
/**
* The function callback that will execute the redirect.
*
* @var string
*/
public $callback = 'redirect_goto';
/**
* The redirect creator ID.
*
* @var int
*/
public $uid = 0;
/**
* The redirect status code.
*
* @var int
*/
public $status_code = 0;
/**
* The total number of times this redirect has been followed.
*
* @var int
*/
public $count = 0;
/**
* Hashes are an alternative ID by which a redirect may be loaded.
*
* @var string
*/
public $hash = '';
/**
* Implements EntityInterface::id().
*/
public function id() {
return $this->rid;
}
/**
* Implements EntityInterface::entityType().
*/
public function entityType() {
return 'redirect';
}
/**
* Implements EntityInterface::label().
*/
public function label() {
return $this->subject;
}
/**
* Implements EntityInterface::uri().
*/
public function uri() {
return array(
'path' => $this->redirect,
'options' => $this->redirect_options,
);
}
/**
* Constructor for creating a new redirect.
*
* @param array $values
* An array of values to populate the redirect object.
*/
public function __construct(array $values = array()) {
parent::__construct($values);
if (empty($this->status_code)) {
$this->status_code = config_get('redirect.settings', 'default_status_code');
}
// Unserialize the URL option fields.
if (is_string($this->source_options)) {
$this->source_options = unserialize($this->source_options);
}
if (is_string($this->redirect_options)) {
$this->redirect_options = unserialize($this->redirect_options);
}
module_invoke_all('redirect_prepare', $this);
}
}
/**
* Controller class for redirects.
*
* This extends the DefaultEntityController class, adding required
* special handling for redirect objects.
*/
class RedirectController extends DefaultEntityController {
public function save(EntityInterface $redirect) {
$transaction = db_transaction();
try {
if (!empty($redirect->rid) && !isset($redirect->original)) {
$redirect->original = entity_load_unchanged('redirect', $redirect->rid);
}
// Determine if we will be inserting a new node.
if (!isset($redirect->is_new)) {
$redirect->is_new = empty($redirect->rid);
}
// The changed timestamp is always updated for bookkeeping purposes.
//$redirect->changed = time();
redirect_hash($redirect);
if ($redirect->is_new || $redirect->hash != $redirect->original->hash) {
// Only new or changed redirects reset the last used value.
$redirect->count = 0;
$redirect->access = 0;
}
// Allow other modules to alter the redirect before saving.
module_invoke_all('redirect_presave', $redirect);
module_invoke_all('entity_presave', $redirect, 'redirect');
// Save the redirect to the database and invoke the post-save hooks.
if ($redirect->is_new) {
backdrop_write_record('redirect', $redirect);
module_invoke_all('redirect_insert', $redirect);
module_invoke_all('entity_insert', $redirect, 'redirect');
}
else {
backdrop_write_record('redirect', $redirect, array('rid'));
module_invoke_all('redirect_update', $redirect);
module_invoke_all('entity_update', $redirect, 'redirect');
}
// Clear internal properties.
unset($redirect->is_new);
unset($redirect->original);
// Clear the static loading cache.
$this->resetCache(array($redirect->rid));
// Ignore slave server temporarily to give time for the
// saved node to be propagated to the slave.
db_ignore_slave();
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('redirect', $e);
throw $e;
}
}
public function delete($rids) {
$transaction = db_transaction();
if (!empty($rids)) {
$redirects = redirect_load_multiple($rids);
try {
// Let modules react to the individual redirects being deleted.
foreach ($redirects as $rid => $redirect) {
module_invoke_all('redirect_delete', $redirect);
module_invoke_all('entity_delete', $redirect, 'redirect');
}
db_delete('redirect')
->condition('rid', $rids, 'IN')
->execute();
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('redirect', $e);
throw $e;
}
$this->resetCache();
}
}
}