1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package cms |
5
|
|
|
* @subpackage model |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use SilverStripe\Model\FieldType\DBHTMLText; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Adds tracking of links in any HTMLText fields which reference SiteTree or File items. |
12
|
|
|
* |
13
|
|
|
* Attaching this to any DataObject will add four fields which contain all links to SiteTree and File items |
14
|
|
|
* referenced in any HTMLText fields, and two booleans to indicate if there are any broken links. Call |
15
|
|
|
* augmentSyncLinkTracking to update those fields with any changes to those fields. |
16
|
|
|
* |
17
|
|
|
* Note that since both SiteTree and File are versioned, LinkTracking and ImageTracking will |
18
|
|
|
* only be enabled for the Stage record. |
19
|
|
|
* |
20
|
|
|
* {@see SiteTreeFileExtension} for the extension applied to {@see File} |
21
|
|
|
* |
22
|
|
|
* @property SiteTree $owner |
23
|
|
|
* |
24
|
|
|
* @property bool $HasBrokenFile |
25
|
|
|
* @property bool $HasBrokenLink |
26
|
|
|
* |
27
|
|
|
* @method ManyManyList LinkTracking() List of site pages linked on this page. |
28
|
|
|
* @method ManyManyList ImageTracking() List of Images linked on this page. |
29
|
|
|
* @method ManyManyList BackLinkTracking List of site pages that link to this page. |
30
|
|
|
*/ |
31
|
|
|
class SiteTreeLinkTracking extends DataExtension { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SiteTreeLinkTracking_Parser |
35
|
|
|
*/ |
36
|
|
|
protected $parser; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Inject parser for each page |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
* @config |
43
|
|
|
*/ |
44
|
|
|
private static $dependencies = array( |
45
|
|
|
'Parser' => '%$SiteTreeLinkTracking_Parser' |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Parser for link tracking |
50
|
|
|
* |
51
|
|
|
* @return SiteTreeLinkTracking_Parser |
52
|
|
|
*/ |
53
|
|
|
public function getParser() { |
54
|
|
|
return $this->parser; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param SiteTreeLinkTracking_Parser $parser |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setParser($parser) { |
62
|
|
|
$this->parser = $parser; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private static $db = array( |
67
|
|
|
"HasBrokenFile" => "Boolean", |
68
|
|
|
"HasBrokenLink" => "Boolean" |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
private static $many_many = array( |
72
|
|
|
"LinkTracking" => "SiteTree", |
73
|
|
|
"ImageTracking" => "File" // {@see SiteTreeFileExtension} |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
private static $belongs_many_many = array( |
77
|
|
|
"BackLinkTracking" => "SiteTree.LinkTracking" |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Tracked images are considered owned by this page |
82
|
|
|
* |
83
|
|
|
* @config |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
private static $owns = array( |
87
|
|
|
"ImageTracking" |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
private static $many_many_extraFields = array( |
91
|
|
|
"LinkTracking" => array("FieldName" => "Varchar"), |
92
|
|
|
"ImageTracking" => array("FieldName" => "Varchar") |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Scrape the content of a field to detect anly links to local SiteTree pages or files |
97
|
|
|
* |
98
|
|
|
* @param string $fieldName The name of the field on {@link @owner} to scrape |
99
|
|
|
*/ |
100
|
|
|
public function trackLinksInField($fieldName) { |
101
|
|
|
$record = $this->owner; |
102
|
|
|
|
103
|
|
|
$linkedPages = array(); |
104
|
|
|
$linkedFiles = array(); |
105
|
|
|
|
106
|
|
|
$htmlValue = Injector::inst()->create('HTMLValue', $record->$fieldName); |
107
|
|
|
$links = $this->parser->process($htmlValue); |
108
|
|
|
|
109
|
|
|
// Highlight broken links in the content. |
110
|
|
|
foreach ($links as $link) { |
111
|
|
|
// Skip links without domelements |
112
|
|
|
if(!isset($link['DOMReference'])) { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$classStr = trim($link['DOMReference']->getAttribute('class')); |
117
|
|
|
if (!$classStr) { |
118
|
|
|
$classes = array(); |
119
|
|
|
} else { |
120
|
|
|
$classes = explode(' ', $classStr); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Add or remove the broken class from the link, depending on the link status. |
124
|
|
|
if ($link['Broken']) { |
125
|
|
|
$classes = array_unique(array_merge($classes, array('ss-broken'))); |
126
|
|
|
} else { |
127
|
|
|
$classes = array_diff($classes, array('ss-broken')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (!empty($classes)) { |
131
|
|
|
$link['DOMReference']->setAttribute('class', implode(' ', $classes)); |
132
|
|
|
} else { |
133
|
|
|
$link['DOMReference']->removeAttribute('class'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
$record->$fieldName = $htmlValue->getContent(); |
137
|
|
|
|
138
|
|
|
// Populate link tracking for internal links & links to asset files. |
139
|
|
|
foreach ($links as $link) { |
140
|
|
|
switch ($link['Type']) { |
141
|
|
|
case 'sitetree': |
142
|
|
|
if ($link['Broken']) { |
143
|
|
|
$record->HasBrokenLink = true; |
|
|
|
|
144
|
|
|
} else { |
145
|
|
|
$linkedPages[] = $link['Target']; |
146
|
|
|
} |
147
|
|
|
break; |
148
|
|
|
|
149
|
|
|
case 'file': |
150
|
|
|
case 'image': |
151
|
|
|
if ($link['Broken']) { |
152
|
|
|
$record->HasBrokenFile = true; |
|
|
|
|
153
|
|
|
} else { |
154
|
|
|
$linkedFiles[] = $link['Target']; |
155
|
|
|
} |
156
|
|
|
break; |
157
|
|
|
|
158
|
|
|
default: |
159
|
|
|
if ($link['Broken']) { |
160
|
|
|
$record->HasBrokenLink = true; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
break; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Update the "LinkTracking" many_many |
167
|
|
View Code Duplication |
if($record->ID && $record->manyManyComponent('LinkTracking') && ($tracker = $record->LinkTracking())) { |
|
|
|
|
168
|
|
|
$tracker->removeByFilter(array( |
169
|
|
|
sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey()) |
170
|
|
|
=> array($fieldName, $record->ID) |
171
|
|
|
)); |
172
|
|
|
|
173
|
|
|
if($linkedPages) foreach($linkedPages as $item) { |
|
|
|
|
174
|
|
|
$tracker->add($item, array('FieldName' => $fieldName)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// Update the "ImageTracking" many_many |
179
|
|
View Code Duplication |
if($record->ID && $record->manyManyComponent('ImageTracking') && ($tracker = $record->ImageTracking())) { |
|
|
|
|
180
|
|
|
$tracker->removeByFilter(array( |
181
|
|
|
sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey()) |
182
|
|
|
=> array($fieldName, $record->ID) |
183
|
|
|
)); |
184
|
|
|
|
185
|
|
|
if($linkedFiles) foreach($linkedFiles as $item) { |
|
|
|
|
186
|
|
|
$tracker->add($item, array('FieldName' => $fieldName)); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Find HTMLText fields on {@link owner} to scrape for links that need tracking |
193
|
|
|
* |
194
|
|
|
* @todo Support versioned many_many for per-stage page link tracking |
195
|
|
|
*/ |
196
|
|
|
public function augmentSyncLinkTracking() { |
197
|
|
|
// Skip live tracking |
198
|
|
|
if(\Versioned::get_stage() == \Versioned::LIVE) { |
|
|
|
|
199
|
|
|
return; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// Reset boolean broken flags |
203
|
|
|
$this->owner->HasBrokenLink = false; |
|
|
|
|
204
|
|
|
$this->owner->HasBrokenFile = false; |
|
|
|
|
205
|
|
|
|
206
|
|
|
// Build a list of HTMLText fields |
207
|
|
|
$allFields = $this->owner->db(); |
208
|
|
|
$htmlFields = array(); |
209
|
|
|
foreach($allFields as $field => $fieldSpec) { |
|
|
|
|
210
|
|
|
$fieldObj = $this->owner->dbObject($field); |
211
|
|
|
if($fieldObj instanceof DBHTMLText) { |
|
|
|
|
212
|
|
|
$htmlFields[] = $field; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
foreach($htmlFields as $field) { |
217
|
|
|
$this->trackLinksInField($field); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* A helper object for extracting information about links. |
224
|
|
|
*/ |
225
|
|
|
class SiteTreeLinkTracking_Parser { |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Finds the links that are of interest for the link tracking automation. Checks for brokenness and attaches |
229
|
|
|
* extracted metadata so consumers can decide what to do with the DOM element (provided as DOMReference). |
230
|
|
|
* |
231
|
|
|
* @param SS_HTMLValue $htmlValue Object to parse the links from. |
232
|
|
|
* @return array Associative array containing found links with the following field layout: |
233
|
|
|
* Type: string, name of the link type |
234
|
|
|
* Target: any, a reference to the target object, depends on the Type |
235
|
|
|
* Anchor: string, anchor part of the link |
236
|
|
|
* DOMReference: DOMElement, reference to the link to apply changes. |
237
|
|
|
* Broken: boolean, a flag highlighting whether the link should be treated as broken. |
238
|
|
|
*/ |
239
|
|
|
public function process(SS_HTMLValue $htmlValue) { |
240
|
|
|
$results = array(); |
241
|
|
|
|
242
|
|
|
$links = $htmlValue->getElementsByTagName('a'); |
243
|
|
|
if(!$links) return $results; |
244
|
|
|
|
245
|
|
|
foreach($links as $link) { |
246
|
|
|
if (!$link->hasAttribute('href')) continue; |
247
|
|
|
|
248
|
|
|
$href = Director::makeRelative($link->getAttribute('href')); |
249
|
|
|
|
250
|
|
|
// Definitely broken links. |
251
|
|
|
if($href == '' || $href[0] == '/') { |
252
|
|
|
$results[] = array( |
253
|
|
|
'Type' => 'broken', |
254
|
|
|
'Target' => null, |
255
|
|
|
'Anchor' => null, |
256
|
|
|
'DOMReference' => $link, |
257
|
|
|
'Broken' => true |
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
continue; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
// Link to a page on this site. |
264
|
|
|
$matches = array(); |
265
|
|
|
if(preg_match('/\[sitetree_link(?:\s*|%20|,)?id=(?<id>[0-9]+)\](#(?<anchor>.*))?/i', $href, $matches)) { |
266
|
|
|
$page = DataObject::get_by_id('SiteTree', $matches['id']); |
267
|
|
|
$broken = false; |
268
|
|
|
|
269
|
|
|
if (!$page) { |
270
|
|
|
// Page doesn't exist. |
271
|
|
|
$broken = true; |
272
|
|
|
} else if (!empty($matches['anchor'])) { |
273
|
|
|
$anchor = preg_quote($matches['anchor'], '/'); |
274
|
|
|
|
275
|
|
|
if (!preg_match("/(name|id)=\"{$anchor}\"/", $page->Content)) { |
276
|
|
|
// Broken anchor on the target page. |
277
|
|
|
$broken = true; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$results[] = array( |
282
|
|
|
'Type' => 'sitetree', |
283
|
|
|
'Target' => $matches['id'], |
284
|
|
|
'Anchor' => empty($matches['anchor']) ? null : $matches['anchor'], |
285
|
|
|
'DOMReference' => $link, |
286
|
|
|
'Broken' => $broken |
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
continue; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
// Link to a file on this site. |
293
|
|
|
$matches = array(); |
294
|
|
View Code Duplication |
if(preg_match('/\[file_link(?:\s*|%20|,)?id=(?<id>[0-9]+)/i', $href, $matches)) { |
|
|
|
|
295
|
|
|
$results[] = array( |
296
|
|
|
'Type' => 'file', |
297
|
|
|
'Target' => $matches['id'], |
298
|
|
|
'Anchor' => null, |
299
|
|
|
'DOMReference' => $link, |
300
|
|
|
'Broken' => !DataObject::get_by_id('File', $matches['id']) |
301
|
|
|
); |
302
|
|
|
|
303
|
|
|
continue; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
// Local anchor. |
307
|
|
|
$matches = array(); |
308
|
|
View Code Duplication |
if(preg_match('/^#(.*)/i', $href, $matches)) { |
|
|
|
|
309
|
|
|
$results[] = array( |
310
|
|
|
'Type' => 'localanchor', |
311
|
|
|
'Target' => null, |
312
|
|
|
'Anchor' => $matches[1], |
313
|
|
|
'DOMReference' => $link, |
314
|
|
|
'Broken' => !preg_match("#(name|id)=\"{$matches[1]}\"#", $htmlValue->getContent()) |
315
|
|
|
); |
316
|
|
|
|
317
|
|
|
continue; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
// Find all [image ] shortcodes (will be inline, not inside attributes) |
323
|
|
|
$content = $htmlValue->getContent(); |
324
|
|
|
if(preg_match_all('/\[image([^\]]+)\bid=(["])?(?<id>\d+)\D/i', $content, $matches)) { |
325
|
|
|
foreach($matches['id'] as $id) { |
326
|
|
|
$results[] = array( |
327
|
|
|
'Type' => 'image', |
328
|
|
|
'Target' => (int)$id, |
329
|
|
|
'Anchor' => null, |
330
|
|
|
'DOMReference' => null, |
331
|
|
|
'Broken' => !DataObject::get_by_id('Image', (int)$id) |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
return $results; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
} |
339
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.