2010-02-04 05:52:34 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Reports;
|
|
|
|
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SS_Report;
|
|
|
|
use ClassInfo;
|
|
|
|
use Controller;
|
|
|
|
use FieldList;
|
|
|
|
use DropdownField;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2010-02-04 05:52:34 +01:00
|
|
|
/**
|
|
|
|
* Content side-report listing pages with broken links
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
2010-02-04 06:11:02 +01:00
|
|
|
class BrokenLinksReport extends SS_Report {
|
2010-02-16 06:20:00 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function title() {
|
2010-02-04 05:53:22 +01:00
|
|
|
return _t('BrokenLinksReport.BROKENLINKS',"Broken links report");
|
2010-02-04 05:52:34 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function sourceRecords($params, $sort, $limit) {
|
2010-02-04 05:55:03 +01:00
|
|
|
$join = '';
|
2010-02-04 05:55:28 +01:00
|
|
|
$sortBrokenReason = false;
|
2010-02-04 05:55:03 +01:00
|
|
|
if($sort) {
|
|
|
|
$parts = explode(' ', $sort);
|
|
|
|
$field = $parts[0];
|
|
|
|
$direction = $parts[1];
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2010-02-04 05:55:03 +01:00
|
|
|
if($field == 'AbsoluteLink') {
|
|
|
|
$sort = 'URLSegment ' . $direction;
|
2010-02-04 05:55:09 +01:00
|
|
|
} elseif($field == 'Subsite.Title') {
|
2010-02-04 05:55:03 +01:00
|
|
|
$join = 'LEFT JOIN "Subsite" ON "Subsite"."ID" = "SiteTree"."SubsiteID"';
|
2010-02-04 05:55:09 +01:00
|
|
|
} elseif($field == 'BrokenReason') {
|
2010-02-04 05:55:28 +01:00
|
|
|
$sortBrokenReason = true;
|
|
|
|
$sort = '';
|
2010-02-04 05:55:03 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-10 03:23:58 +01:00
|
|
|
$brokenFilter = array(
|
|
|
|
'"SiteTree"."HasBrokenLink" = ? OR "SiteTree"."HasBrokenFile" = ?' => array(true, true)
|
|
|
|
);
|
|
|
|
$isLive = !isset($params['CheckSite']) || $params['CheckSite'] == 'Published';
|
|
|
|
if ($isLive) {
|
2016-07-22 01:32:32 +02:00
|
|
|
$ret = Versioned::get_by_stage('SilverStripe\\CMS\\Model\\SiteTree', 'Live', $brokenFilter, $sort, $join, $limit);
|
2013-06-21 00:45:33 +02:00
|
|
|
} else {
|
2016-07-22 01:32:32 +02:00
|
|
|
$ret = DataObject::get('SilverStripe\\CMS\\Model\\SiteTree', $brokenFilter, $sort, $join, $limit);
|
2013-06-21 00:45:33 +02:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-05-05 12:40:26 +02:00
|
|
|
$returnSet = new ArrayList();
|
2010-02-04 05:52:34 +01:00
|
|
|
if ($ret) foreach($ret as $record) {
|
|
|
|
$reason = false;
|
2016-07-22 01:32:32 +02:00
|
|
|
$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
|
|
|
|
2010-02-04 05:52:34 +01:00
|
|
|
if ($isVirtualPage) {
|
|
|
|
if ($record->HasBrokenLink) {
|
2010-02-16 06:20:00 +01:00
|
|
|
$reason = _t('BrokenLinksReport.VirtualPageNonExistent', "virtual page pointing to non-existent page");
|
2010-02-04 05:52:34 +01:00
|
|
|
$reasonCodes = array("VPBROKENLINK");
|
|
|
|
}
|
|
|
|
} else if ($isRedirectorPage) {
|
|
|
|
if ($record->HasBrokenLink) {
|
2010-02-16 06:20:00 +01:00
|
|
|
$reason = _t('BrokenLinksReport.RedirectorNonExistent', "redirector page pointing to non-existent page");
|
2010-02-04 05:52:34 +01:00
|
|
|
$reasonCodes = array("RPBROKENLINK");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($record->HasBrokenLink && $record->HasBrokenFile) {
|
2010-02-16 06:20:00 +01:00
|
|
|
$reason = _t('BrokenLinksReport.HasBrokenLinkAndFile', "has broken link and file");
|
2010-02-04 05:52:34 +01:00
|
|
|
$reasonCodes = array("BROKENFILE", "BROKENLINK");
|
|
|
|
} else if ($record->HasBrokenLink && !$record->HasBrokenFile) {
|
2010-02-16 06:20:00 +01:00
|
|
|
$reason = _t('BrokenLinksReport.HasBrokenLink', "has broken link");
|
2010-02-04 05:52:34 +01:00
|
|
|
$reasonCodes = array("BROKENLINK");
|
|
|
|
} else if (!$record->HasBrokenLink && $record->HasBrokenFile) {
|
2010-02-16 06:20:00 +01:00
|
|
|
$reason = _t('BrokenLinksReport.HasBrokenFile', "has broken file");
|
2010-02-04 05:52:34 +01:00
|
|
|
$reasonCodes = array("BROKENFILE");
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2010-02-04 05:52:34 +01:00
|
|
|
if ($reason) {
|
2010-02-04 05:53:22 +01:00
|
|
|
if (isset($params['Reason']) && $params['Reason'] && !in_array($params['Reason'], $reasonCodes)) continue;
|
2010-02-04 05:52:34 +01:00
|
|
|
$record->BrokenReason = $reason;
|
|
|
|
$returnSet->push($record);
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-06-15 05:30:08 +02:00
|
|
|
if($sortBrokenReason) $returnSet = $returnSet->sort('BrokenReason', $direction);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2010-02-04 05:52:34 +01:00
|
|
|
return $returnSet;
|
|
|
|
}
|
2012-09-19 12:07:46 +02:00
|
|
|
public function columns() {
|
2015-06-10 03:37:27 +02:00
|
|
|
if(isset($_REQUEST['filters']['CheckSite']) && $_REQUEST['filters']['CheckSite'] == 'Draft') {
|
2010-02-16 06:20:00 +01:00
|
|
|
$dateTitle = _t('BrokenLinksReport.ColumnDateLastModified', 'Date last modified');
|
|
|
|
} else {
|
|
|
|
$dateTitle = _t('BrokenLinksReport.ColumnDateLastPublished', 'Date last published');
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$linkBase = singleton('SilverStripe\\CMS\\Controllers\\CMSPageEditController')->Link('show');
|
2010-02-04 05:52:34 +01:00
|
|
|
$fields = array(
|
|
|
|
"Title" => array(
|
2010-05-17 04:44:25 +02:00
|
|
|
"title" => _t('BrokenLinksReport.PageName', 'Page name'),
|
2014-10-20 03:22:20 +02:00
|
|
|
'formatting' => function($value, $item) use ($linkBase) {
|
2015-07-02 02:42:26 +02:00
|
|
|
return sprintf('<a href="%s" title="%s">%s</a>',
|
2014-10-20 03:22:20 +02:00
|
|
|
Controller::join_links($linkBase, $item->ID),
|
|
|
|
_t('BrokenLinksReport.HoverTitleEditPage', 'Edit page'),
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
}
|
2010-02-04 05:52:34 +01:00
|
|
|
),
|
2010-02-04 05:53:22 +01:00
|
|
|
"LastEdited" => array(
|
2010-02-16 06:20:00 +01:00
|
|
|
"title" => $dateTitle,
|
2016-06-16 06:57:19 +02:00
|
|
|
'casting' => 'DBDatetime->Full'
|
2010-02-04 05:53:22 +01:00
|
|
|
),
|
2010-02-04 05:52:34 +01:00
|
|
|
"BrokenReason" => array(
|
2010-02-16 06:20:00 +01:00
|
|
|
"title" => _t('BrokenLinksReport.ColumnProblemType', "Problem type")
|
2010-02-04 05:52:34 +01:00
|
|
|
),
|
|
|
|
'AbsoluteLink' => array(
|
2010-02-16 06:20:00 +01:00
|
|
|
'title' => _t('BrokenLinksReport.ColumnURL', 'URL'),
|
2012-07-12 16:29:58 +02:00
|
|
|
'formatting' => function($value, $item) {
|
|
|
|
$liveLink = $item->AbsoluteLiveLink;
|
|
|
|
$stageLink = $item->AbsoluteLink();
|
|
|
|
return sprintf('%s <a href="%s">%s</a>',
|
|
|
|
$stageLink,
|
2016-03-10 17:38:42 +01:00
|
|
|
$liveLink ? $liveLink : Controller::join_links($stageLink, '?stage=Stage'),
|
2012-07-12 16:29:58 +02:00
|
|
|
$liveLink ? '(live)' : '(draft)'
|
|
|
|
);
|
|
|
|
}
|
2010-02-04 05:52:34 +01:00
|
|
|
)
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2010-02-04 05:52:34 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
2012-09-19 12:07:46 +02:00
|
|
|
public function parameterFields() {
|
2011-10-26 07:35:51 +02:00
|
|
|
return new FieldList(
|
2010-05-17 04:44:25 +02:00
|
|
|
new DropdownField('CheckSite', _t('BrokenLinksReport.CheckSite','Check site'), array(
|
2010-02-16 06:20:00 +01:00
|
|
|
'Published' => _t('BrokenLinksReport.CheckSiteDropdownPublished', 'Published Site'),
|
|
|
|
'Draft' => _t('BrokenLinksReport.CheckSiteDropdownDraft', 'Draft Site')
|
2010-02-04 05:53:28 +01:00
|
|
|
)),
|
2010-02-16 06:20:00 +01:00
|
|
|
new DropdownField(
|
2016-06-16 06:57:19 +02:00
|
|
|
'Reason',
|
|
|
|
_t('BrokenLinksReport.ReasonDropdown', 'Problem to check'),
|
2010-02-16 06:20:00 +01:00
|
|
|
array(
|
2010-05-17 04:44:25 +02:00
|
|
|
'' => _t('BrokenLinksReport.Any', 'Any'),
|
2010-02-16 06:20:00 +01:00
|
|
|
'BROKENFILE' => _t('BrokenLinksReport.ReasonDropdownBROKENFILE', 'Broken file'),
|
|
|
|
'BROKENLINK' => _t('BrokenLinksReport.ReasonDropdownBROKENLINK', 'Broken link'),
|
|
|
|
'VPBROKENLINK' => _t('BrokenLinksReport.ReasonDropdownVPBROKENLINK', 'Virtual page pointing to non-existent page'),
|
|
|
|
'RPBROKENLINK' => _t('BrokenLinksReport.ReasonDropdownRPBROKENLINK', 'Redirector page pointing to non-existent page'),
|
|
|
|
)
|
|
|
|
)
|
2010-02-04 05:52:34 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|