Completed
Pull Request — master (#1432)
by Sam
02:38
created

SiteTreeLinkTracking::augmentSyncLinkTracking()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 13
nc 7
nop 0
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;
0 ignored issues
show
Documentation introduced by
The property HasBrokenLink does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
144
					} else {
145
						$linkedPages[] = $link['Target'];
146
					}
147
					break;
148
149
				case 'file':
150
				case 'image':
151
					if ($link['Broken']) {
152
						$record->HasBrokenFile = true;
0 ignored issues
show
Documentation introduced by
The property HasBrokenFile does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
153
					} else {
154
						$linkedFiles[] = $link['Target'];
155
					}
156
					break;
157
158
				default:
159
					if ($link['Broken']) {
160
						$record->HasBrokenLink = true;
0 ignored issues
show
Documentation introduced by
The property HasBrokenLink does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
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())) {
0 ignored issues
show
Bug introduced by
The method LinkTracking() does not exist on SiteTree. Did you maybe mean syncLinkTracking()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $linkedPages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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())) {
0 ignored issues
show
Documentation Bug introduced by
The method ImageTracking does not exist on object<SiteTree>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $linkedFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The method get_stage() does not seem to exist on object<Versioned>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
			return;
200
		}
201
202
		// Reset boolean broken flags
203
		$this->owner->HasBrokenLink = false;
0 ignored issues
show
Documentation introduced by
The property HasBrokenLink does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
204
		$this->owner->HasBrokenFile = false;
0 ignored issues
show
Documentation introduced by
The property HasBrokenFile does not exist on object<SiteTree>. Since you implemented __set, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
205
206
		// Build a list of HTMLText fields
207
		$allFields = $this->owner->db();
208
		$htmlFields = array();
209
		foreach($allFields as $field => $fieldSpec) {
0 ignored issues
show
Bug introduced by
The expression $allFields of type array|string|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
210
			$fieldObj = $this->owner->dbObject($field);
211
			if($fieldObj instanceof DBHTMLText) {
0 ignored issues
show
Bug introduced by
The class SilverStripe\Model\FieldType\DBHTMLText does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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