2013-08-07 01:03:15 +02:00
|
|
|
<?php
|
2015-08-24 06:14:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
|
|
|
|
2013-08-07 01:03:15 +02:00
|
|
|
/**
|
2014-12-01 12:22:48 +01:00
|
|
|
* Adds tracking of links in any HTMLText fields which reference SiteTree or File items.
|
2014-08-12 06:39:11 +02:00
|
|
|
*
|
2013-08-07 01:03:15 +02:00
|
|
|
* Attaching this to any DataObject will add four fields which contain all links to SiteTree and File items
|
2014-12-01 12:22:48 +01:00
|
|
|
* referenced in any HTMLText fields, and two booleans to indicate if there are any broken links. Call
|
|
|
|
* augmentSyncLinkTracking to update those fields with any changes to those fields.
|
2013-08-07 01:03:15 +02:00
|
|
|
*
|
2016-01-26 06:38:42 +01:00
|
|
|
* Note that since both SiteTree and File are versioned, LinkTracking and ImageTracking will
|
|
|
|
* only be enabled for the Stage record.
|
|
|
|
*
|
|
|
|
* {@see SiteTreeFileExtension} for the extension applied to {@see File}
|
|
|
|
*
|
2015-10-15 00:08:52 +02:00
|
|
|
* @property SiteTree $owner
|
2013-08-07 01:03:15 +02:00
|
|
|
*
|
2015-10-15 00:08:52 +02:00
|
|
|
* @property bool $HasBrokenFile
|
|
|
|
* @property bool $HasBrokenLink
|
2014-12-01 12:22:48 +01:00
|
|
|
*
|
2015-10-15 00:08:52 +02:00
|
|
|
* @method ManyManyList LinkTracking() List of site pages linked on this page.
|
|
|
|
* @method ManyManyList ImageTracking() List of Images linked on this page.
|
2016-01-26 06:38:42 +01:00
|
|
|
* @method ManyManyList BackLinkTracking List of site pages that link to this page.
|
2013-08-07 01:03:15 +02:00
|
|
|
*/
|
|
|
|
class SiteTreeLinkTracking extends DataExtension {
|
|
|
|
|
2016-03-17 01:02:50 +01:00
|
|
|
/**
|
|
|
|
* @var SiteTreeLinkTracking_Parser
|
|
|
|
*/
|
|
|
|
protected $parser;
|
2014-08-12 06:39:11 +02:00
|
|
|
|
2016-03-17 01:02:50 +01:00
|
|
|
/**
|
|
|
|
* Inject parser for each page
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
2014-08-12 06:39:11 +02:00
|
|
|
private static $dependencies = array(
|
2016-03-17 01:02:50 +01:00
|
|
|
'Parser' => '%$SiteTreeLinkTracking_Parser'
|
2014-08-12 06:39:11 +02:00
|
|
|
);
|
|
|
|
|
2016-03-17 01:02:50 +01:00
|
|
|
/**
|
|
|
|
* Parser for link tracking
|
|
|
|
*
|
|
|
|
* @return SiteTreeLinkTracking_Parser
|
|
|
|
*/
|
|
|
|
public function getParser() {
|
|
|
|
return $this->parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SiteTreeLinkTracking_Parser $parser
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setParser($parser) {
|
|
|
|
$this->parser = $parser;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 01:03:15 +02:00
|
|
|
private static $db = array(
|
|
|
|
"HasBrokenFile" => "Boolean",
|
|
|
|
"HasBrokenLink" => "Boolean"
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
|
|
|
"LinkTracking" => "SiteTree",
|
2016-02-21 23:44:24 +01:00
|
|
|
"ImageTracking" => "File" // {@see SiteTreeFileExtension}
|
2013-08-07 01:03:15 +02:00
|
|
|
);
|
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
private static $belongs_many_many = array(
|
|
|
|
"BackLinkTracking" => "SiteTree.LinkTracking"
|
|
|
|
);
|
|
|
|
|
2016-02-21 23:44:24 +01:00
|
|
|
/**
|
|
|
|
* Tracked images are considered owned by this page
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $owns = array(
|
|
|
|
"ImageTracking"
|
|
|
|
);
|
|
|
|
|
2013-08-07 01:03:15 +02:00
|
|
|
private static $many_many_extraFields = array(
|
|
|
|
"LinkTracking" => array("FieldName" => "Varchar"),
|
|
|
|
"ImageTracking" => array("FieldName" => "Varchar")
|
|
|
|
);
|
|
|
|
|
2014-12-01 12:22:48 +01:00
|
|
|
/**
|
|
|
|
* Scrape the content of a field to detect anly links to local SiteTree pages or files
|
|
|
|
*
|
2015-06-02 09:23:48 +02:00
|
|
|
* @param string $fieldName The name of the field on {@link @owner} to scrape
|
2014-12-01 12:22:48 +01:00
|
|
|
*/
|
2014-08-15 02:04:48 +02:00
|
|
|
public function trackLinksInField($fieldName) {
|
2013-08-07 01:03:15 +02:00
|
|
|
$record = $this->owner;
|
|
|
|
|
|
|
|
$linkedPages = array();
|
|
|
|
$linkedFiles = array();
|
|
|
|
|
2014-08-15 02:04:48 +02:00
|
|
|
$htmlValue = Injector::inst()->create('HTMLValue', $record->$fieldName);
|
2014-08-12 06:39:11 +02:00
|
|
|
$links = $this->parser->process($htmlValue);
|
2013-08-07 01:03:15 +02:00
|
|
|
|
2014-08-15 02:04:48 +02:00
|
|
|
// Highlight broken links in the content.
|
|
|
|
foreach ($links as $link) {
|
2016-03-09 23:57:39 +01:00
|
|
|
// Skip links without domelements
|
|
|
|
if(!isset($link['DOMReference'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:04:48 +02:00
|
|
|
$classStr = trim($link['DOMReference']->getAttribute('class'));
|
|
|
|
if (!$classStr) {
|
|
|
|
$classes = array();
|
|
|
|
} else {
|
|
|
|
$classes = explode(' ', $classStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add or remove the broken class from the link, depending on the link status.
|
|
|
|
if ($link['Broken']) {
|
|
|
|
$classes = array_unique(array_merge($classes, array('ss-broken')));
|
|
|
|
} else {
|
|
|
|
$classes = array_diff($classes, array('ss-broken'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($classes)) {
|
|
|
|
$link['DOMReference']->setAttribute('class', implode(' ', $classes));
|
|
|
|
} else {
|
|
|
|
$link['DOMReference']->removeAttribute('class');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$record->$fieldName = $htmlValue->getContent();
|
|
|
|
|
2013-08-07 01:03:15 +02:00
|
|
|
// Populate link tracking for internal links & links to asset files.
|
2014-08-12 06:39:11 +02:00
|
|
|
foreach ($links as $link) {
|
|
|
|
switch ($link['Type']) {
|
|
|
|
case 'sitetree':
|
|
|
|
if ($link['Broken']) {
|
|
|
|
$record->HasBrokenLink = true;
|
|
|
|
} else {
|
|
|
|
$linkedPages[] = $link['Target'];
|
|
|
|
}
|
|
|
|
break;
|
2013-08-07 01:03:15 +02:00
|
|
|
|
2014-08-12 06:39:11 +02:00
|
|
|
case 'file':
|
2016-03-09 23:57:39 +01:00
|
|
|
case 'image':
|
2014-08-12 06:39:11 +02:00
|
|
|
if ($link['Broken']) {
|
|
|
|
$record->HasBrokenFile = true;
|
|
|
|
} else {
|
|
|
|
$linkedFiles[] = $link['Target'];
|
2013-08-07 01:03:15 +02:00
|
|
|
}
|
2014-08-12 06:39:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if ($link['Broken']) {
|
2013-08-23 04:33:36 +02:00
|
|
|
$record->HasBrokenLink = true;
|
|
|
|
}
|
2014-08-12 06:39:11 +02:00
|
|
|
break;
|
2013-08-07 01:03:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the "LinkTracking" many_many
|
2015-10-15 00:08:52 +02:00
|
|
|
if($record->ID && $record->manyManyComponent('LinkTracking') && ($tracker = $record->LinkTracking())) {
|
|
|
|
$tracker->removeByFilter(array(
|
|
|
|
sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey())
|
|
|
|
=> array($fieldName, $record->ID)
|
2013-08-07 01:03:15 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
if($linkedPages) foreach($linkedPages as $item) {
|
2014-08-15 02:04:48 +02:00
|
|
|
$tracker->add($item, array('FieldName' => $fieldName));
|
2013-08-07 01:03:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the "ImageTracking" many_many
|
2015-10-15 00:08:52 +02:00
|
|
|
if($record->ID && $record->manyManyComponent('ImageTracking') && ($tracker = $record->ImageTracking())) {
|
|
|
|
$tracker->removeByFilter(array(
|
|
|
|
sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey())
|
|
|
|
=> array($fieldName, $record->ID)
|
2013-08-07 01:03:15 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
if($linkedFiles) foreach($linkedFiles as $item) {
|
2014-08-15 02:04:48 +02:00
|
|
|
$tracker->add($item, array('FieldName' => $fieldName));
|
2013-08-07 01:03:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 12:22:48 +01:00
|
|
|
/**
|
|
|
|
* Find HTMLText fields on {@link owner} to scrape for links that need tracking
|
2016-01-26 06:38:42 +01:00
|
|
|
*
|
|
|
|
* @todo Support versioned many_many for per-stage page link tracking
|
2014-12-01 12:22:48 +01:00
|
|
|
*/
|
2014-08-15 02:04:48 +02:00
|
|
|
public function augmentSyncLinkTracking() {
|
2016-01-26 06:38:42 +01:00
|
|
|
// Skip live tracking
|
2016-03-17 01:02:50 +01:00
|
|
|
if(\Versioned::get_stage() == \Versioned::LIVE) {
|
2016-01-26 06:38:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-07 01:03:15 +02:00
|
|
|
// Reset boolean broken flags
|
|
|
|
$this->owner->HasBrokenLink = false;
|
|
|
|
$this->owner->HasBrokenFile = false;
|
|
|
|
|
|
|
|
// Build a list of HTMLText fields
|
|
|
|
$allFields = $this->owner->db();
|
|
|
|
$htmlFields = array();
|
|
|
|
foreach($allFields as $field => $fieldSpec) {
|
|
|
|
if(preg_match('/([^(]+)/', $fieldSpec, $matches)) {
|
|
|
|
$class = $matches[0];
|
|
|
|
if(class_exists($class)){
|
|
|
|
if($class == 'HTMLText' || is_subclass_of($class, 'HTMLText')) $htmlFields[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
foreach($htmlFields as $field) {
|
|
|
|
$this->trackLinksInField($field);
|
|
|
|
}
|
2013-08-07 01:03:15 +02:00
|
|
|
}
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper object for extracting information about links.
|
|
|
|
*/
|
|
|
|
class SiteTreeLinkTracking_Parser {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the links that are of interest for the link tracking automation. Checks for brokenness and attaches
|
|
|
|
* extracted metadata so consumers can decide what to do with the DOM element (provided as DOMReference).
|
|
|
|
*
|
|
|
|
* @param SS_HTMLValue $htmlValue Object to parse the links from.
|
|
|
|
* @return array Associative array containing found links with the following field layout:
|
|
|
|
* Type: string, name of the link type
|
|
|
|
* Target: any, a reference to the target object, depends on the Type
|
|
|
|
* Anchor: string, anchor part of the link
|
|
|
|
* DOMReference: DOMElement, reference to the link to apply changes.
|
|
|
|
* Broken: boolean, a flag highlighting whether the link should be treated as broken.
|
|
|
|
*/
|
|
|
|
public function process(SS_HTMLValue $htmlValue) {
|
|
|
|
$results = array();
|
|
|
|
|
|
|
|
$links = $htmlValue->getElementsByTagName('a');
|
|
|
|
if(!$links) return $results;
|
|
|
|
|
|
|
|
foreach($links as $link) {
|
|
|
|
if (!$link->hasAttribute('href')) continue;
|
|
|
|
|
|
|
|
$href = Director::makeRelative($link->getAttribute('href'));
|
|
|
|
|
|
|
|
// Definitely broken links.
|
|
|
|
if($href == '' || $href[0] == '/') {
|
|
|
|
$results[] = array(
|
|
|
|
'Type' => 'broken',
|
|
|
|
'Target' => null,
|
|
|
|
'Anchor' => null,
|
|
|
|
'DOMReference' => $link,
|
|
|
|
'Broken' => true
|
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link to a page on this site.
|
|
|
|
$matches = array();
|
2016-03-09 23:57:39 +01:00
|
|
|
if(preg_match('/\[sitetree_link(?:\s*|%20|,)?id=(?<id>[0-9]+)\](#(?<anchor>.*))?/i', $href, $matches)) {
|
|
|
|
$page = DataObject::get_by_id('SiteTree', $matches['id']);
|
2016-01-11 15:52:31 +01:00
|
|
|
$broken = false;
|
|
|
|
|
2014-08-12 06:39:11 +02:00
|
|
|
if (!$page) {
|
|
|
|
// Page doesn't exist.
|
|
|
|
$broken = true;
|
2016-03-09 23:57:39 +01:00
|
|
|
} else if (!empty($matches['anchor'])) {
|
|
|
|
$anchor = preg_quote($matches['anchor'], '/');
|
2016-01-11 15:52:31 +01:00
|
|
|
|
|
|
|
if (!preg_match("/(name|id)=\"{$anchor}\"/", $page->Content)) {
|
|
|
|
// Broken anchor on the target page.
|
|
|
|
$broken = true;
|
|
|
|
}
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$results[] = array(
|
|
|
|
'Type' => 'sitetree',
|
2016-03-09 23:57:39 +01:00
|
|
|
'Target' => $matches['id'],
|
|
|
|
'Anchor' => empty($matches['anchor']) ? null : $matches['anchor'],
|
2014-08-12 06:39:11 +02:00
|
|
|
'DOMReference' => $link,
|
|
|
|
'Broken' => $broken
|
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link to a file on this site.
|
|
|
|
$matches = array();
|
2016-03-09 23:57:39 +01:00
|
|
|
if(preg_match('/\[file_link(?:\s*|%20|,)?id=(?<id>[0-9]+)/i', $href, $matches)) {
|
2014-08-12 06:39:11 +02:00
|
|
|
$results[] = array(
|
|
|
|
'Type' => 'file',
|
2016-03-09 23:57:39 +01:00
|
|
|
'Target' => $matches['id'],
|
2014-08-12 06:39:11 +02:00
|
|
|
'Anchor' => null,
|
|
|
|
'DOMReference' => $link,
|
2016-03-09 23:57:39 +01:00
|
|
|
'Broken' => !DataObject::get_by_id('File', $matches['id'])
|
2014-08-12 06:39:11 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local anchor.
|
|
|
|
$matches = array();
|
|
|
|
if(preg_match('/^#(.*)/i', $href, $matches)) {
|
|
|
|
$results[] = array(
|
|
|
|
'Type' => 'localanchor',
|
|
|
|
'Target' => null,
|
|
|
|
'Anchor' => $matches[1],
|
|
|
|
'DOMReference' => $link,
|
|
|
|
'Broken' => !preg_match("#(name|id)=\"{$matches[1]}\"#", $htmlValue->getContent())
|
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:57:39 +01:00
|
|
|
// Find all [image ] shortcodes (will be inline, not inside attributes)
|
|
|
|
$content = $htmlValue->getContent();
|
|
|
|
if(preg_match_all('/\[image([^\]]+)\bid=(["])?(?<id>\d+)\D/i', $content, $matches)) {
|
|
|
|
foreach($matches['id'] as $id) {
|
|
|
|
$results[] = array(
|
|
|
|
'Type' => 'image',
|
|
|
|
'Target' => (int)$id,
|
|
|
|
'Anchor' => null,
|
|
|
|
'DOMReference' => null,
|
|
|
|
'Broken' => !DataObject::get_by_id('Image', (int)$id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-08-12 06:39:11 +02:00
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|