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;
|
2018-04-06 05:53:57 +02:00
|
|
|
use SilverStripe\Assets\Shortcodes\FileLinkTracking;
|
2018-08-31 04:57:17 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\FormScaffolder;
|
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;
|
2018-04-06 05:53:57 +02:00
|
|
|
use SilverStripe\ORM\ManyManyThroughList;
|
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
|
|
|
*
|
2018-04-06 05:53:57 +02:00
|
|
|
* Note that since both SiteTree and File are versioned, LinkTracking and FileTracking will
|
2016-01-26 06:38:42 +01:00
|
|
|
* only be enabled for the Stage record.
|
|
|
|
*
|
2018-04-06 05:53:57 +02:00
|
|
|
* Note: To support `HasBrokenLink` for non-SiteTree classes, add a boolean `HasBrokenLink`
|
|
|
|
* field to your `db` config and this extension will ensure it's flagged appropriately.
|
2016-01-26 06:38:42 +01:00
|
|
|
*
|
2018-04-06 05:53:57 +02:00
|
|
|
* @property DataObject|SiteTreeLinkTracking $owner
|
|
|
|
* @method ManyManyThroughList LinkTracking() List of site pages linked on this dataobject
|
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
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
private static $many_many = [
|
|
|
|
"LinkTracking" => [
|
|
|
|
'through' => SiteTreeLink::class,
|
|
|
|
'from' => 'Parent',
|
|
|
|
'to' => 'Linked',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2018-08-31 04:57:17 +02:00
|
|
|
/**
|
|
|
|
* Controls visibility of the Link Tracking tab
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @see linktracking.yml
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $show_sitetree_link_tracking = false;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
// Trigger link tracking (unless this would also be triggered by FileLinkTracking)
|
|
|
|
if (!$this->owner->hasExtension(FileLinkTracking::class)) {
|
|
|
|
$this->owner->syncLinkTracking();
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-06 05:53:57 +02:00
|
|
|
* Public method to call when triggering symlink extension. Can be called externally,
|
|
|
|
* or overridden by class implementations.
|
2017-01-25 21:59:25 +01:00
|
|
|
*
|
2018-04-06 05:53:57 +02:00
|
|
|
* {@see SiteTreeLinkTracking::augmentSyncLinkTracking}
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
2018-04-06 05:53:57 +02:00
|
|
|
public function syncLinkTracking()
|
|
|
|
{
|
|
|
|
$this->owner->extend('augmentSyncLinkTracking');
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-06 05:53:57 +02:00
|
|
|
* Find HTMLText fields on {@link owner} to scrape for links that need tracking
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
2018-04-06 05:53:57 +02:00
|
|
|
public function augmentSyncLinkTracking()
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
2018-04-06 05:53:57 +02:00
|
|
|
// If owner is versioned, skip tracking on live
|
|
|
|
if (Versioned::get_stage() == Versioned::LIVE && $this->owner->hasExtension(Versioned::class)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Build a list of HTMLText fields, merging all linked pages together.
|
|
|
|
$allFields = DataObject::getSchema()->fieldSpecs($this->owner);
|
2018-03-14 04:34:46 +01:00
|
|
|
$linkedPages = [];
|
2018-04-06 05:53:57 +02:00
|
|
|
$anyBroken = false;
|
|
|
|
foreach ($allFields as $field => $fieldSpec) {
|
|
|
|
$fieldObj = $this->owner->dbObject($field);
|
|
|
|
if ($fieldObj instanceof DBHTMLText) {
|
|
|
|
// Merge links in this field with global list.
|
|
|
|
$linksInField = $this->trackLinksInField($field, $anyBroken);
|
|
|
|
$linkedPages = array_merge($linkedPages, $linksInField);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2018-04-06 05:53:57 +02:00
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Soft support for HasBrokenLink db field (e.g. SiteTree)
|
|
|
|
if ($this->owner->hasField('HasBrokenLink')) {
|
|
|
|
$this->owner->HasBrokenLink = $anyBroken;
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Update the "LinkTracking" many_many.
|
|
|
|
$this->owner->LinkTracking()->setByIDList($linkedPages);
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
public function onAfterDelete()
|
|
|
|
{
|
|
|
|
// If owner is versioned, skip tracking on live
|
|
|
|
if (Versioned::get_stage() == Versioned::LIVE && $this->owner->hasExtension(Versioned::class)) {
|
|
|
|
return;
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
$this->owner->LinkTracking()->removeAll();
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param bool &$anyBroken Will be flagged to true (by reference) if a link is broken.
|
|
|
|
* @return int[] Array of page IDs found (associative array)
|
|
|
|
*/
|
|
|
|
public function trackLinksInField($fieldName, &$anyBroken = false)
|
|
|
|
{
|
|
|
|
// Pull down current field content
|
|
|
|
$htmlValue = HTMLValue::create($this->owner->$fieldName);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Process all links
|
|
|
|
$linkedPages = [];
|
|
|
|
$links = $this->parser->process($htmlValue);
|
|
|
|
foreach ($links as $link) {
|
|
|
|
// Toggle highlight class to element
|
|
|
|
$this->toggleElementClass($link['DOMReference'], 'ss-broken', $link['Broken']);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Flag broken
|
|
|
|
if ($link['Broken']) {
|
|
|
|
$anyBroken = true;
|
2018-03-14 04:34:46 +01:00
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Collect page ids
|
|
|
|
if ($link['Type'] === 'sitetree' && $link['Target']) {
|
|
|
|
$pageID = (int)$link['Target'];
|
|
|
|
$linkedPages[$pageID] = $pageID;
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-06 05:53:57 +02:00
|
|
|
|
|
|
|
// Update any changed content
|
|
|
|
$this->owner->$fieldName = $htmlValue->getContent();
|
|
|
|
return $linkedPages;
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-06 05:53:57 +02:00
|
|
|
* Add the given css class to the DOM element.
|
2017-01-25 21:59:25 +01:00
|
|
|
*
|
2018-04-06 05:53:57 +02:00
|
|
|
* @param DOMElement $domReference Element to modify.
|
|
|
|
* @param string $class Class name to toggle.
|
|
|
|
* @param bool $toggle On or off.
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
2018-04-06 05:53:57 +02:00
|
|
|
protected function toggleElementClass(DOMElement $domReference, $class, $toggle)
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
2018-04-06 05:53:57 +02:00
|
|
|
// Get all existing classes.
|
|
|
|
$classes = array_filter(explode(' ', trim($domReference->getAttribute('class'))));
|
|
|
|
|
|
|
|
// Add or remove the broken class from the link, depending on the link status.
|
|
|
|
if ($toggle) {
|
|
|
|
$classes = array_unique(array_merge($classes, [$class]));
|
|
|
|
} else {
|
|
|
|
$classes = array_diff($classes, [$class]);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
if (!empty($classes)) {
|
|
|
|
$domReference->setAttribute('class', implode(' ', $classes));
|
|
|
|
} else {
|
|
|
|
$domReference->removeAttribute('class');
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 04:57:17 +02:00
|
|
|
|
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
if (!$this->owner->config()->get('show_sitetree_link_tracking')) {
|
|
|
|
$fields->removeByName('LinkTracking');
|
|
|
|
} elseif ($this->owner->ID && !$this->owner->getField('LinkTracking')) {
|
|
|
|
FormScaffolder::addManyManyRelationshipFields($fields, 'LinkTracking', null, true, $this->owner);
|
|
|
|
}
|
|
|
|
}
|
2014-08-12 06:39:11 +02:00
|
|
|
}
|