silverstripe-cms/code/Reports/BrokenLinksReport.php

169 lines
7.6 KiB
PHP
Raw Normal View History

<?php
2016-07-22 01:32:32 +02:00
namespace SilverStripe\CMS\Reports;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Versioned\Versioned;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ArrayList;
2016-09-09 01:26:24 +02:00
use SilverStripe\Reports\Report;
/**
* Content side-report listing pages with broken links
*/
2017-01-25 21:59:25 +01:00
class BrokenLinksReport extends Report
{
2017-01-25 21:59:25 +01:00
public function title()
{
2017-04-20 03:15:29 +02:00
return _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.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)
{
$join = '';
$sortBrokenReason = false;
if ($sort) {
$parts = explode(' ', $sort);
$field = $parts[0];
$direction = $parts[1];
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
if ($field == 'AbsoluteLink') {
$sort = 'URLSegment ' . $direction;
} elseif ($field == 'Subsite.Title') {
$join = 'LEFT JOIN "Subsite" ON "Subsite"."ID" = "SiteTree"."SubsiteID"';
} elseif ($field == 'BrokenReason') {
$sortBrokenReason = true;
$sort = '';
}
}
$brokenFilter = array(
'"SiteTree"."HasBrokenLink" = ? OR "SiteTree"."HasBrokenFile" = ?' => array(true, true)
);
$isLive = !isset($params['CheckSite']) || $params['CheckSite'] == 'Published';
if ($isLive) {
$ret = Versioned::get_by_stage('SilverStripe\\CMS\\Model\\SiteTree', 'Live', $brokenFilter, $sort, $join, $limit);
} else {
$ret = DataObject::get('SilverStripe\\CMS\\Model\\SiteTree', $brokenFilter, $sort, $join, $limit);
}
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
$returnSet = new ArrayList();
if ($ret) {
foreach ($ret as $record) {
$reason = false;
$isRedirectorPage = in_array($record->ClassName, ClassInfo::subclassesFor('SilverStripe\\CMS\\Model\\RedirectorPage'));
$isVirtualPage = in_array($record->ClassName, ClassInfo::subclassesFor('SilverStripe\\CMS\\Model\\VirtualPage'));
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
$reasonCodes = [];
if ($isVirtualPage) {
if ($record->HasBrokenLink) {
2017-04-20 03:15:29 +02:00
$reason = _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.VirtualPageNonExistent', "virtual page pointing to non-existent page");
2017-01-25 21:59:25 +01:00
$reasonCodes = array("VPBROKENLINK");
}
} elseif ($isRedirectorPage) {
if ($record->HasBrokenLink) {
2017-04-20 03:15:29 +02:00
$reason = _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.RedirectorNonExistent', "redirector page pointing to non-existent page");
2017-01-25 21:59:25 +01:00
$reasonCodes = array("RPBROKENLINK");
}
} else {
if ($record->HasBrokenLink && $record->HasBrokenFile) {
2017-04-20 03:15:29 +02:00
$reason = _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.HasBrokenLinkAndFile', "has broken link and file");
2017-01-25 21:59:25 +01:00
$reasonCodes = array("BROKENFILE", "BROKENLINK");
} elseif ($record->HasBrokenLink && !$record->HasBrokenFile) {
2017-04-20 03:15:29 +02:00
$reason = _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.HasBrokenLink', "has broken link");
2017-01-25 21:59:25 +01:00
$reasonCodes = array("BROKENLINK");
} elseif (!$record->HasBrokenLink && $record->HasBrokenFile) {
2017-04-20 03:15:29 +02:00
$reason = _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.HasBrokenFile', "has broken file");
2017-01-25 21:59:25 +01:00
$reasonCodes = array("BROKENFILE");
}
}
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
if ($reason) {
if (isset($params['Reason']) && $params['Reason'] && !in_array($params['Reason'], $reasonCodes)) {
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-04-20 03:15:29 +02:00
$dateTitle = _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.ColumnDateLastModified', 'Date last modified');
2017-01-25 21:59:25 +01:00
} else {
2017-04-20 03:15:29 +02:00
$dateTitle = _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.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');
$fields = array(
"Title" => array(
2017-04-20 03:15:29 +02:00
"title" => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.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-04-20 03:15:29 +02:00
_t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.HoverTitleEditPage', 'Edit page'),
2017-01-25 21:59:25 +01:00
$value
);
}
),
"LastEdited" => array(
"title" => $dateTitle,
'casting' => 'DBDatetime->Full'
),
"BrokenReason" => array(
2017-04-20 03:15:29 +02:00
"title" => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.ColumnProblemType', "Problem type")
2017-01-25 21:59:25 +01:00
),
'AbsoluteLink' => array(
2017-04-20 03:15:29 +02:00
'title' => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.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)'
);
}
)
);
2016-03-08 21:50:55 +01:00
2017-01-25 21:59:25 +01:00
return $fields;
}
public function parameterFields()
{
return new FieldList(
2017-04-20 03:15:29 +02:00
new DropdownField('CheckSite', _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.CheckSite', 'Check site'), array(
'Published' => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.CheckSiteDropdownPublished', 'Published Site'),
'Draft' => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.CheckSiteDropdownDraft', 'Draft Site')
2017-01-25 21:59:25 +01:00
)),
new DropdownField(
'Reason',
2017-04-20 03:15:29 +02:00
_t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.ReasonDropdown', 'Problem to check'),
2017-01-25 21:59:25 +01:00
array(
2017-04-20 03:15:29 +02:00
'' => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.Any', 'Any'),
'BROKENFILE' => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.ReasonDropdownBROKENFILE', 'Broken file'),
'BROKENLINK' => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.ReasonDropdownBROKENLINK', 'Broken link'),
'VPBROKENLINK' => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.ReasonDropdownVPBROKENLINK', 'Virtual page pointing to non-existent page'),
'RPBROKENLINK' => _t('SilverStripe\\CMS\\Reports\\BrokenLinksReport.ReasonDropdownRPBROKENLINK', 'Redirector page pointing to non-existent page'),
2017-01-25 21:59:25 +01:00
)
)
);
}
}