2013-08-07 01:03:15 +02:00
|
|
|
<?php
|
2015-08-24 06:14:33 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Model;
|
|
|
|
|
2017-06-21 06:29:40 +02:00
|
|
|
use DOMElement;
|
2017-02-28 03:46:19 +01:00
|
|
|
use SilverStripe\Assets\File;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\ORM\DataExtension;
|
2016-10-06 06:54:35 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\ManyManyList;
|
2017-03-21 05:26:46 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2017-04-18 00:52:51 +02:00
|
|
|
use SilverStripe\View\Parsers\HTMLValue;
|
2016-03-22 22:00:16 +01:00
|
|
|
|
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
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class SiteTreeLinkTracking extends DataExtension
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SiteTreeLinkTracking_Parser
|
|
|
|
*/
|
|
|
|
protected $parser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inject parser for each page
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
2017-05-11 07:37:54 +02:00
|
|
|
private static $dependencies = [
|
|
|
|
'Parser' => '%$' . SiteTreeLinkTracking_Parser::class
|
|
|
|
];
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parser for link tracking
|
|
|
|
*
|
|
|
|
* @return SiteTreeLinkTracking_Parser
|
|
|
|
*/
|
|
|
|
public function getParser()
|
|
|
|
{
|
|
|
|
return $this->parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SiteTreeLinkTracking_Parser $parser
|
|
|
|
* @return $this
|
|
|
|
*/
|
2017-05-11 07:37:54 +02:00
|
|
|
public function setParser(SiteTreeLinkTracking_Parser $parser = null)
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
|
|
|
$this->parser = $parser;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
"HasBrokenFile" => "Boolean",
|
|
|
|
"HasBrokenLink" => "Boolean"
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
2017-02-28 03:46:19 +01:00
|
|
|
"LinkTracking" => SiteTree::class,
|
|
|
|
"ImageTracking" => File::class, // {@see SiteTreeFileExtension}
|
2017-01-25 21:59:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $belongs_many_many = array(
|
2017-05-11 07:37:54 +02:00
|
|
|
"BackLinkTracking" => SiteTree::class . '.LinkTracking',
|
2017-01-25 21:59:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracked images are considered owned by this page
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $owns = array(
|
|
|
|
"ImageTracking"
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many_extraFields = array(
|
|
|
|
"LinkTracking" => array("FieldName" => "Varchar"),
|
|
|
|
"ImageTracking" => array("FieldName" => "Varchar")
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrape the content of a field to detect anly links to local SiteTree pages or files
|
|
|
|
*
|
|
|
|
* @param string $fieldName The name of the field on {@link @owner} to scrape
|
|
|
|
*/
|
|
|
|
public function trackLinksInField($fieldName)
|
|
|
|
{
|
|
|
|
$record = $this->owner;
|
|
|
|
|
2018-03-14 04:34:46 +01:00
|
|
|
$linkedPages = [];
|
|
|
|
$linkedFiles = [];
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2017-04-18 00:52:51 +02:00
|
|
|
$htmlValue = HTMLValue::create($record->$fieldName);
|
2017-01-25 21:59:25 +01:00
|
|
|
$links = $this->parser->process($htmlValue);
|
|
|
|
|
|
|
|
// Highlight broken links in the content.
|
|
|
|
foreach ($links as $link) {
|
|
|
|
// Skip links without domelements
|
|
|
|
if (!isset($link['DOMReference'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var DOMElement $domReference */
|
|
|
|
$domReference = $link['DOMReference'];
|
|
|
|
$classStr = trim($domReference->getAttribute('class'));
|
|
|
|
if (!$classStr) {
|
2018-03-14 04:34:46 +01:00
|
|
|
$classes = [];
|
2017-01-25 21:59:25 +01:00
|
|
|
} 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)) {
|
|
|
|
$domReference->setAttribute('class', implode(' ', $classes));
|
|
|
|
} else {
|
|
|
|
$domReference->removeAttribute('class');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$record->$fieldName = $htmlValue->getContent();
|
|
|
|
|
|
|
|
// Populate link tracking for internal links & links to asset files.
|
|
|
|
foreach ($links as $link) {
|
|
|
|
switch ($link['Type']) {
|
|
|
|
case 'sitetree':
|
|
|
|
if ($link['Broken']) {
|
|
|
|
$record->HasBrokenLink = true;
|
|
|
|
} else {
|
|
|
|
$linkedPages[] = $link['Target'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'file':
|
|
|
|
case 'image':
|
|
|
|
if ($link['Broken']) {
|
|
|
|
$record->HasBrokenFile = true;
|
|
|
|
} else {
|
|
|
|
$linkedFiles[] = $link['Target'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if ($link['Broken']) {
|
|
|
|
$record->HasBrokenLink = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the "LinkTracking" many_many
|
2018-03-14 04:34:46 +01:00
|
|
|
if ($record->getSchema()->manyManyComponent(get_class($record), 'LinkTracking')
|
2017-01-25 21:59:25 +01:00
|
|
|
&& ($tracker = $record->LinkTracking())
|
|
|
|
) {
|
2018-03-14 04:34:46 +01:00
|
|
|
// If already saved, clear existing records
|
|
|
|
if ($record->isInDB()) {
|
|
|
|
$tracker->removeByFilter(array(
|
|
|
|
sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey())
|
2017-01-25 21:59:25 +01:00
|
|
|
=> array($fieldName, $record->ID)
|
2018-03-14 04:34:46 +01:00
|
|
|
));
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-03-14 04:34:46 +01:00
|
|
|
foreach ($linkedPages as $item) {
|
|
|
|
$tracker->add($item, array('FieldName' => $fieldName));
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the "ImageTracking" many_many
|
2018-03-14 04:34:46 +01:00
|
|
|
if ($record->getSchema()->manyManyComponent(get_class($record), 'ImageTracking')
|
2017-01-25 21:59:25 +01:00
|
|
|
&& ($tracker = $record->ImageTracking())
|
|
|
|
) {
|
2018-03-14 04:34:46 +01:00
|
|
|
// If already saved, clear existing records
|
|
|
|
if ($record->isInDB()) {
|
|
|
|
$tracker->removeByFilter(array(
|
|
|
|
sprintf('"FieldName" = ? AND "%s" = ?', $tracker->getForeignKey())
|
2017-01-25 21:59:25 +01:00
|
|
|
=> array($fieldName, $record->ID)
|
2018-03-14 04:34:46 +01:00
|
|
|
));
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-03-14 04:34:46 +01:00
|
|
|
foreach ($linkedFiles as $item) {
|
|
|
|
$tracker->add($item, array('FieldName' => $fieldName));
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find HTMLText fields on {@link owner} to scrape for links that need tracking
|
|
|
|
*
|
|
|
|
* @todo Support versioned many_many for per-stage page link tracking
|
|
|
|
*/
|
|
|
|
public function augmentSyncLinkTracking()
|
|
|
|
{
|
|
|
|
// Skip live tracking
|
2018-03-14 04:34:46 +01:00
|
|
|
if (Versioned::get_stage() === Versioned::LIVE) {
|
2017-01-25 21:59:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset boolean broken flags
|
|
|
|
$this->owner->HasBrokenLink = false;
|
|
|
|
$this->owner->HasBrokenFile = false;
|
|
|
|
|
|
|
|
// Build a list of HTMLText fields
|
|
|
|
$allFields = DataObject::getSchema()->fieldSpecs($this->owner);
|
|
|
|
$htmlFields = array();
|
|
|
|
foreach ($allFields as $field => $fieldSpec) {
|
|
|
|
$fieldObj = $this->owner->dbObject($field);
|
|
|
|
if ($fieldObj instanceof DBHTMLText) {
|
|
|
|
$htmlFields[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($htmlFields as $field) {
|
|
|
|
$this->trackLinksInField($field);
|
|
|
|
}
|
|
|
|
}
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|