2010-02-04 05:52:34 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Reports;
|
|
|
|
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\CMS\Controllers\CMSPageEditController;
|
2017-09-20 03:51:07 +02:00
|
|
|
use SilverStripe\CMS\Model\RedirectorPage;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2017-09-20 03:51:07 +02:00
|
|
|
use SilverStripe\CMS\Model\VirtualPage;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
2019-05-17 03:40:15 +02:00
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2019-02-22 10:11:15 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Reports\Report;
|
2019-02-22 10:11:15 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2010-02-04 05:52:34 +01:00
|
|
|
/**
|
|
|
|
* Content side-report listing pages with broken links
|
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class BrokenLinksReport extends Report
|
|
|
|
{
|
2010-02-16 06:20:00 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
public function title()
|
|
|
|
{
|
2019-02-22 10:11:15 +01:00
|
|
|
return _t(__CLASS__ . '.BROKENLINKS', 'Broken links report');
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
public function sourceRecords($params, $sort, $limit)
|
|
|
|
{
|
2019-05-17 03:40:15 +02:00
|
|
|
$sitetreeTbl = DataObject::singleton(SiteTree::class)->baseTable();
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$join = '';
|
|
|
|
$sortBrokenReason = false;
|
|
|
|
if ($sort) {
|
2022-04-13 07:07:59 +02:00
|
|
|
$parts = explode(' ', $sort ?? '');
|
2017-01-25 21:59:25 +01:00
|
|
|
$field = $parts[0];
|
|
|
|
$direction = $parts[1];
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2019-02-22 10:11:15 +01:00
|
|
|
if ($field === 'AbsoluteLink') {
|
2017-01-25 21:59:25 +01:00
|
|
|
$sort = 'URLSegment ' . $direction;
|
2019-05-17 03:40:15 +02:00
|
|
|
} elseif ($field == 'Subsite.Title') {
|
|
|
|
$subSiteTbl = DataObject::singleton(Subsite::class)->baseTable();
|
|
|
|
$join = sprintf(
|
|
|
|
'LEFT JOIN "%s" ON "%s"."ID" = "%s"."SubsiteID"',
|
|
|
|
$subSiteTbl,
|
|
|
|
$subSiteTbl,
|
|
|
|
$sitetreeTbl
|
|
|
|
);
|
|
|
|
} elseif ($field == 'BrokenReason') {
|
2017-01-25 21:59:25 +01:00
|
|
|
$sortBrokenReason = true;
|
|
|
|
$sort = '';
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 10:11:15 +01:00
|
|
|
$brokenFilter = [
|
2019-05-17 03:40:15 +02:00
|
|
|
sprintf('"%s"."HasBrokenLink" = ? OR "%s"."HasBrokenFile" = ?', $sitetreeTbl, $sitetreeTbl) => [true, true]
|
2019-02-22 10:11:15 +01:00
|
|
|
];
|
|
|
|
$isLive = !isset($params['CheckSite']) || $params['CheckSite'] === 'Published';
|
2017-01-25 21:59:25 +01:00
|
|
|
if ($isLive) {
|
2019-02-22 10:11:15 +01:00
|
|
|
$ret = Versioned::get_by_stage(SiteTree::class, Versioned::LIVE, $brokenFilter, $sort, $join, $limit);
|
2017-01-25 21:59:25 +01:00
|
|
|
} else {
|
2017-09-20 03:51:07 +02:00
|
|
|
$ret = DataObject::get(SiteTree::class, $brokenFilter, $sort, $join, $limit);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2019-02-22 10:11:15 +01:00
|
|
|
$returnSet = ArrayList::create();
|
2017-01-25 21:59:25 +01:00
|
|
|
if ($ret) {
|
|
|
|
foreach ($ret as $record) {
|
|
|
|
$reason = false;
|
2017-09-20 03:51:07 +02:00
|
|
|
$isRedirectorPage = $record instanceof RedirectorPage;
|
|
|
|
$isVirtualPage = $record instanceof VirtualPage;
|
2017-01-25 21:59:25 +01:00
|
|
|
$reasonCodes = [];
|
|
|
|
if ($isVirtualPage) {
|
|
|
|
if ($record->HasBrokenLink) {
|
2019-02-22 10:11:15 +01:00
|
|
|
$reason = _t(__CLASS__ . '.VirtualPageNonExistent', 'virtual page pointing to non-existent page');
|
|
|
|
$reasonCodes = ['VPBROKENLINK'];
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
} elseif ($isRedirectorPage) {
|
|
|
|
if ($record->HasBrokenLink) {
|
2019-02-22 10:11:15 +01:00
|
|
|
$reason = _t(__CLASS__ . '.RedirectorNonExistent', 'redirector page pointing to non-existent page');
|
|
|
|
$reasonCodes = ['RPBROKENLINK'];
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($record->HasBrokenLink && $record->HasBrokenFile) {
|
2019-02-22 10:11:15 +01:00
|
|
|
$reason = _t(__CLASS__ . '.HasBrokenLinkAndFile', 'has broken link and file');
|
|
|
|
$reasonCodes = ['BROKENFILE', 'BROKENLINK'];
|
2017-01-25 21:59:25 +01:00
|
|
|
} elseif ($record->HasBrokenLink && !$record->HasBrokenFile) {
|
2019-02-22 10:11:15 +01:00
|
|
|
$reason = _t(__CLASS__ . '.HasBrokenLink', 'has broken link');
|
|
|
|
$reasonCodes = ['BROKENLINK'];
|
2017-01-25 21:59:25 +01:00
|
|
|
} elseif (!$record->HasBrokenLink && $record->HasBrokenFile) {
|
2019-02-22 10:11:15 +01:00
|
|
|
$reason = _t(__CLASS__ . '.HasBrokenFile', 'has broken file');
|
|
|
|
$reasonCodes = ['BROKENFILE'];
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
if ($reason) {
|
2022-04-13 07:07:59 +02:00
|
|
|
if (isset($params['Reason']) && $params['Reason'] && !in_array($params['Reason'], $reasonCodes ?? [])) {
|
2017-01-25 21:59:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$record->BrokenReason = $reason;
|
|
|
|
$returnSet->push($record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
if ($sortBrokenReason) {
|
|
|
|
$returnSet = $returnSet->sort('BrokenReason', $direction);
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
return $returnSet;
|
|
|
|
}
|
|
|
|
public function columns()
|
|
|
|
{
|
|
|
|
if (isset($_REQUEST['filters']['CheckSite']) && $_REQUEST['filters']['CheckSite'] == 'Draft') {
|
2017-09-20 03:51:07 +02:00
|
|
|
$dateTitle = _t(__CLASS__ . '.ColumnDateLastModified', 'Date last modified');
|
2017-01-25 21:59:25 +01:00
|
|
|
} else {
|
2017-09-20 03:51:07 +02:00
|
|
|
$dateTitle = _t(__CLASS__ . '.ColumnDateLastPublished', 'Date last published');
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$linkBase = CMSPageEditController::singleton()->Link('show');
|
2019-02-22 10:11:15 +01:00
|
|
|
$fields = [
|
|
|
|
'Title' => [
|
|
|
|
'title' => _t(__CLASS__ . '.PageName', 'Page name'),
|
2017-01-25 21:59:25 +01:00
|
|
|
'formatting' => function ($value, $item) use ($linkBase) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s" title="%s">%s</a>',
|
|
|
|
Controller::join_links($linkBase, $item->ID),
|
2017-09-20 03:51:07 +02:00
|
|
|
_t(__CLASS__ . '.HoverTitleEditPage', 'Edit page'),
|
2017-01-25 21:59:25 +01:00
|
|
|
$value
|
|
|
|
);
|
|
|
|
}
|
2019-02-22 10:11:15 +01:00
|
|
|
],
|
|
|
|
'LastEdited' => [
|
|
|
|
'title' => $dateTitle,
|
2017-01-25 21:59:25 +01:00
|
|
|
'casting' => 'DBDatetime->Full'
|
2019-02-22 10:11:15 +01:00
|
|
|
],
|
|
|
|
'BrokenReason' => [
|
|
|
|
'title' => _t(__CLASS__ . '.ColumnProblemType', 'Problem type')
|
|
|
|
],
|
|
|
|
'AbsoluteLink' => [
|
2017-09-20 03:51:07 +02:00
|
|
|
'title' => _t(__CLASS__ . '.ColumnURL', 'URL'),
|
2017-01-25 21:59:25 +01:00
|
|
|
'formatting' => function ($value, $item) {
|
|
|
|
/** @var SiteTree $item */
|
|
|
|
$liveLink = $item->AbsoluteLiveLink;
|
|
|
|
$stageLink = $item->AbsoluteLink();
|
|
|
|
return sprintf(
|
|
|
|
'%s <a href="%s">%s</a>',
|
|
|
|
$stageLink,
|
|
|
|
$liveLink ? $liveLink : Controller::join_links($stageLink, '?stage=Stage'),
|
|
|
|
$liveLink ? '(live)' : '(draft)'
|
|
|
|
);
|
|
|
|
}
|
2019-02-22 10:11:15 +01:00
|
|
|
],
|
|
|
|
];
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
public function parameterFields()
|
|
|
|
{
|
2019-02-22 10:11:15 +01:00
|
|
|
return FieldList::create(
|
2020-04-19 06:18:01 +02:00
|
|
|
DropdownField::create('CheckSite', _t(__CLASS__ . '.CheckSite', 'Check site'), [
|
2017-09-20 03:51:07 +02:00
|
|
|
'Published' => _t(__CLASS__ . '.CheckSiteDropdownPublished', 'Published Site'),
|
|
|
|
'Draft' => _t(__CLASS__ . '.CheckSiteDropdownDraft', 'Draft Site')
|
2020-04-19 06:18:01 +02:00
|
|
|
]),
|
2019-02-22 10:11:15 +01:00
|
|
|
DropdownField::create(
|
2017-01-25 21:59:25 +01:00
|
|
|
'Reason',
|
2017-09-20 03:51:07 +02:00
|
|
|
_t(__CLASS__ . '.ReasonDropdown', 'Problem to check'),
|
2019-02-22 10:11:15 +01:00
|
|
|
[
|
2017-09-20 03:51:07 +02:00
|
|
|
'' => _t(__CLASS__ . '.Any', 'Any'),
|
|
|
|
'BROKENFILE' => _t(__CLASS__ . '.ReasonDropdownBROKENFILE', 'Broken file'),
|
|
|
|
'BROKENLINK' => _t(__CLASS__ . '.ReasonDropdownBROKENLINK', 'Broken link'),
|
|
|
|
'VPBROKENLINK' => _t(__CLASS__ . '.ReasonDropdownVPBROKENLINK', 'Virtual page pointing to non-existent page'),
|
|
|
|
'RPBROKENLINK' => _t(__CLASS__ . '.ReasonDropdownRPBROKENLINK', 'Redirector page pointing to non-existent page'),
|
2019-02-22 10:11:15 +01:00
|
|
|
]
|
2017-01-25 21:59:25 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2010-02-04 05:52:34 +01:00
|
|
|
}
|