1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package cms |
5
|
|
|
* @subpackage model |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Adds tracking of links in any HTMLText fields which reference SiteTree or File items. |
10
|
|
|
* |
11
|
|
|
* Attaching this to any DataObject will add four fields which contain all links to SiteTree and File items |
12
|
|
|
* referenced in any HTMLText fields, and two booleans to indicate if there are any broken links. Call |
13
|
|
|
* augmentSyncLinkTracking to update those fields with any changes to those fields. |
14
|
|
|
* |
15
|
|
|
* @property SiteTree owner |
16
|
|
|
* |
17
|
|
|
* @property bool HasBrokenFile |
18
|
|
|
* @property bool HasBrokenLink |
19
|
|
|
* |
20
|
|
|
* @method ManyManyList LinkTracking List of site pages linked on this page. |
21
|
|
|
* @method ManyManyList ImageTracking List of Images linked on this page. |
22
|
|
|
*/ |
23
|
|
|
class SiteTreeLinkTracking extends DataExtension { |
24
|
|
|
|
25
|
|
|
public $parser; |
26
|
|
|
|
27
|
|
|
private static $dependencies = array( |
28
|
|
|
'parser' => '%$SiteTreeLinkTracking_Parser' |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
private static $db = array( |
32
|
|
|
"HasBrokenFile" => "Boolean", |
33
|
|
|
"HasBrokenLink" => "Boolean" |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
private static $many_many = array( |
37
|
|
|
"LinkTracking" => "SiteTree", |
38
|
|
|
"ImageTracking" => "File" |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
private static $many_many_extraFields = array( |
42
|
|
|
"LinkTracking" => array("FieldName" => "Varchar"), |
43
|
|
|
"ImageTracking" => array("FieldName" => "Varchar") |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Scrape the content of a field to detect anly links to local SiteTree pages or files |
48
|
|
|
* |
49
|
|
|
* @param string $fieldName The name of the field on {@link @owner} to scrape |
50
|
|
|
*/ |
51
|
|
|
public function trackLinksInField($fieldName) { |
52
|
|
|
$record = $this->owner; |
53
|
|
|
|
54
|
|
|
$linkedPages = array(); |
55
|
|
|
$linkedFiles = array(); |
56
|
|
|
|
57
|
|
|
$htmlValue = Injector::inst()->create('HTMLValue', $record->$fieldName); |
58
|
|
|
$links = $this->parser->process($htmlValue); |
59
|
|
|
|
60
|
|
|
// Highlight broken links in the content. |
61
|
|
|
foreach ($links as $link) { |
62
|
|
|
$classStr = trim($link['DOMReference']->getAttribute('class')); |
63
|
|
|
if (!$classStr) { |
64
|
|
|
$classes = array(); |
65
|
|
|
} else { |
66
|
|
|
$classes = explode(' ', $classStr); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Add or remove the broken class from the link, depending on the link status. |
70
|
|
|
if ($link['Broken']) { |
71
|
|
|
$classes = array_unique(array_merge($classes, array('ss-broken'))); |
72
|
|
|
} else { |
73
|
|
|
$classes = array_diff($classes, array('ss-broken')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!empty($classes)) { |
77
|
|
|
$link['DOMReference']->setAttribute('class', implode(' ', $classes)); |
78
|
|
|
} else { |
79
|
|
|
$link['DOMReference']->removeAttribute('class'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
$record->$fieldName = $htmlValue->getContent(); |
83
|
|
|
|
84
|
|
|
// Populate link tracking for internal links & links to asset files. |
85
|
|
|
foreach ($links as $link) { |
86
|
|
|
switch ($link['Type']) { |
87
|
|
|
case 'sitetree': |
88
|
|
|
if ($link['Broken']) { |
89
|
|
|
$record->HasBrokenLink = true; |
|
|
|
|
90
|
|
|
} else { |
91
|
|
|
$linkedPages[] = $link['Target']; |
92
|
|
|
} |
93
|
|
|
break; |
94
|
|
|
|
95
|
|
|
case 'file': |
96
|
|
|
if ($link['Broken']) { |
97
|
|
|
$record->HasBrokenFile = true; |
|
|
|
|
98
|
|
|
} else { |
99
|
|
|
$linkedFiles[] = $link['Target']; |
100
|
|
|
} |
101
|
|
|
break; |
102
|
|
|
|
103
|
|
|
default: |
104
|
|
|
if ($link['Broken']) { |
105
|
|
|
$record->HasBrokenLink = true; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
break; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Add file tracking for image references |
112
|
|
|
if($images = $htmlValue->getElementsByTagName('img')) foreach($images as $img) { |
113
|
|
|
if($image = File::find($path = urldecode(Director::makeRelative($img->getAttribute('src'))))) { |
114
|
|
|
$linkedFiles[] = $image->ID; |
115
|
|
|
} else { |
116
|
|
|
if(substr($path, 0, strlen(ASSETS_DIR) + 1) == ASSETS_DIR . '/') { |
117
|
|
|
$record->HasBrokenFile = true; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Update the "LinkTracking" many_many |
123
|
|
View Code Duplication |
if($record->ID && $record->manyManyComponent('LinkTracking') && $tracker = $record->LinkTracking()) { |
|
|
|
|
124
|
|
|
$tracker->removeByFilter(sprintf( |
125
|
|
|
'"FieldName" = \'%s\' AND "%s" = %d', |
126
|
|
|
$fieldName, |
127
|
|
|
$tracker->getForeignKey(), |
128
|
|
|
$record->ID |
129
|
|
|
)); |
130
|
|
|
|
131
|
|
|
if($linkedPages) foreach($linkedPages as $item) { |
|
|
|
|
132
|
|
|
$tracker->add($item, array('FieldName' => $fieldName)); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// Update the "ImageTracking" many_many |
137
|
|
View Code Duplication |
if($record->ID && $record->manyManyComponent('ImageTracking') && $tracker = $record->ImageTracking()) { |
|
|
|
|
138
|
|
|
$tracker->removeByFilter(sprintf( |
139
|
|
|
'"FieldName" = \'%s\' AND "%s" = %d', |
140
|
|
|
$fieldName, |
141
|
|
|
$tracker->getForeignKey(), |
142
|
|
|
$record->ID |
143
|
|
|
)); |
144
|
|
|
|
145
|
|
|
if($linkedFiles) foreach($linkedFiles as $item) { |
|
|
|
|
146
|
|
|
$tracker->add($item, array('FieldName' => $fieldName)); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Find HTMLText fields on {@link owner} to scrape for links that need tracking |
153
|
|
|
*/ |
154
|
|
|
public function augmentSyncLinkTracking() { |
155
|
|
|
// Reset boolean broken flags |
156
|
|
|
$this->owner->HasBrokenLink = false; |
|
|
|
|
157
|
|
|
$this->owner->HasBrokenFile = false; |
|
|
|
|
158
|
|
|
|
159
|
|
|
// Build a list of HTMLText fields |
160
|
|
|
$allFields = $this->owner->db(); |
161
|
|
|
$htmlFields = array(); |
162
|
|
|
foreach($allFields as $field => $fieldSpec) { |
163
|
|
|
if(preg_match('/([^(]+)/', $fieldSpec, $matches)) { |
164
|
|
|
$class = $matches[0]; |
165
|
|
|
if(class_exists($class)){ |
166
|
|
|
if($class == 'HTMLText' || is_subclass_of($class, 'HTMLText')) $htmlFields[] = $field; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
foreach($htmlFields as $field) $this->trackLinksInField($field); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* A helper object for extracting information about links. |
177
|
|
|
*/ |
178
|
|
|
class SiteTreeLinkTracking_Parser { |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Finds the links that are of interest for the link tracking automation. Checks for brokenness and attaches |
182
|
|
|
* extracted metadata so consumers can decide what to do with the DOM element (provided as DOMReference). |
183
|
|
|
* |
184
|
|
|
* @param SS_HTMLValue $htmlValue Object to parse the links from. |
185
|
|
|
* @return array Associative array containing found links with the following field layout: |
186
|
|
|
* Type: string, name of the link type |
187
|
|
|
* Target: any, a reference to the target object, depends on the Type |
188
|
|
|
* Anchor: string, anchor part of the link |
189
|
|
|
* DOMReference: DOMElement, reference to the link to apply changes. |
190
|
|
|
* Broken: boolean, a flag highlighting whether the link should be treated as broken. |
191
|
|
|
*/ |
192
|
|
|
public function process(SS_HTMLValue $htmlValue) { |
193
|
|
|
$results = array(); |
194
|
|
|
|
195
|
|
|
$links = $htmlValue->getElementsByTagName('a'); |
196
|
|
|
if(!$links) return $results; |
197
|
|
|
|
198
|
|
|
foreach($links as $link) { |
199
|
|
|
if (!$link->hasAttribute('href')) continue; |
200
|
|
|
|
201
|
|
|
$href = Director::makeRelative($link->getAttribute('href')); |
202
|
|
|
|
203
|
|
|
// Definitely broken links. |
204
|
|
|
if($href == '' || $href[0] == '/') { |
205
|
|
|
$results[] = array( |
206
|
|
|
'Type' => 'broken', |
207
|
|
|
'Target' => null, |
208
|
|
|
'Anchor' => null, |
209
|
|
|
'DOMReference' => $link, |
210
|
|
|
'Broken' => true |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
continue; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Link to a page on this site. |
217
|
|
|
$matches = array(); |
218
|
|
|
if(preg_match('/\[sitetree_link(?:\s*|%20|,)?id=([0-9]+)\](#(.*))?/i', $href, $matches)) { |
219
|
|
|
$page = DataObject::get_by_id('SiteTree', $matches[1]); |
220
|
|
|
$broken = false; |
221
|
|
|
|
222
|
|
|
if (!$page) { |
223
|
|
|
// Page doesn't exist. |
224
|
|
|
$broken = true; |
225
|
|
|
} else if (!empty($matches[3])) { |
226
|
|
|
$anchor = preg_quote($matches[3], '/'); |
227
|
|
|
|
228
|
|
|
if (!preg_match("/(name|id)=\"{$anchor}\"/", $page->Content)) { |
229
|
|
|
// Broken anchor on the target page. |
230
|
|
|
$broken = true; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$results[] = array( |
235
|
|
|
'Type' => 'sitetree', |
236
|
|
|
'Target' => $matches[1], |
237
|
|
|
'Anchor' => empty($matches[3]) ? null : $matches[3], |
238
|
|
|
'DOMReference' => $link, |
239
|
|
|
'Broken' => $broken |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
continue; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// Link to a file on this site. |
246
|
|
|
$matches = array(); |
247
|
|
View Code Duplication |
if(preg_match('/\[file_link(?:\s*|%20|,)?id=([0-9]+)\]/i', $href, $matches)) { |
|
|
|
|
248
|
|
|
$results[] = array( |
249
|
|
|
'Type' => 'file', |
250
|
|
|
'Target' => $matches[1], |
251
|
|
|
'Anchor' => null, |
252
|
|
|
'DOMReference' => $link, |
253
|
|
|
'Broken' => !DataObject::get_by_id('File', $matches[1]) |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
continue; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
// Local anchor. |
260
|
|
|
$matches = array(); |
261
|
|
View Code Duplication |
if(preg_match('/^#(.*)/i', $href, $matches)) { |
|
|
|
|
262
|
|
|
$results[] = array( |
263
|
|
|
'Type' => 'localanchor', |
264
|
|
|
'Target' => null, |
265
|
|
|
'Anchor' => $matches[1], |
266
|
|
|
'DOMReference' => $link, |
267
|
|
|
'Broken' => !preg_match("#(name|id)=\"{$matches[1]}\"#", $htmlValue->getContent()) |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
continue; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $results; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
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.