1 <?php
2
3 4 5 6 7
8
9 class BrokenLinksReport extends SS_Report {
10
11 function title() {
12 return _t('BrokenLinksReport.BROKENLINKS',"Broken links report");
13 }
14
15 function sourceRecords($params, $sort, $limit) {
16 $join = '';
17 $sortBrokenReason = false;
18 if($sort) {
19 $parts = explode(' ', $sort);
20 $field = $parts[0];
21 $direction = $parts[1];
22
23 if($field == 'AbsoluteLink') {
24 $sort = 'URLSegment ' . $direction;
25 } elseif($field == 'Subsite.Title') {
26 $join = 'LEFT JOIN "Subsite" ON "Subsite"."ID" = "SiteTree"."SubsiteID"';
27 } elseif($field == 'BrokenReason') {
28 $sortBrokenReason = true;
29 $sort = '';
30 }
31 }
32 $q = DB::USE_ANSI_SQL ? '"' : '`';
33 if (!isset($_REQUEST['CheckSite']) || $params['CheckSite'] == 'Published') $ret = Versioned::get_by_stage('SiteTree', 'Live', "({$q}SiteTree{$q}.{$q}HasBrokenLink{$q} = 1 OR {$q}SiteTree{$q}.{$q}HasBrokenFile{$q} = 1)", $sort, $join, $limit);
34 else $ret = DataObject::get('SiteTree', "({$q}SiteTree{$q}.{$q}HasBrokenFile{$q} = 1 OR {$q}HasBrokenLink{$q} = 1)", $sort, $join, $limit);
35
36 $returnSet = new DataObjectSet();
37 if ($ret) foreach($ret as $record) {
38 $reason = false;
39 $isRedirectorPage = in_array($record->ClassName, ClassInfo::subclassesFor('RedirectorPage'));
40 $isVirtualPage = in_array($record->ClassName, ClassInfo::subclassesFor('VirtualPage'));
41
42 if ($isVirtualPage) {
43 if ($record->HasBrokenLink) {
44 $reason = _t('BrokenLinksReport.VirtualPageNonExistent', "virtual page pointing to non-existent page");
45 $reasonCodes = array("VPBROKENLINK");
46 }
47 } else if ($isRedirectorPage) {
48 if ($record->HasBrokenLink) {
49 $reason = _t('BrokenLinksReport.RedirectorNonExistent', "redirector page pointing to non-existent page");
50 $reasonCodes = array("RPBROKENLINK");
51 }
52 } else {
53 if ($record->HasBrokenLink && $record->HasBrokenFile) {
54 $reason = _t('BrokenLinksReport.HasBrokenLinkAndFile', "has broken link and file");
55 $reasonCodes = array("BROKENFILE", "BROKENLINK");
56 } else if ($record->HasBrokenLink && !$record->HasBrokenFile) {
57 $reason = _t('BrokenLinksReport.HasBrokenLink', "has broken link");
58 $reasonCodes = array("BROKENLINK");
59 } else if (!$record->HasBrokenLink && $record->HasBrokenFile) {
60 $reason = _t('BrokenLinksReport.HasBrokenFile', "has broken file");
61 $reasonCodes = array("BROKENFILE");
62 }
63 }
64
65 if ($reason) {
66 if (isset($params['Reason']) && $params['Reason'] && !in_array($params['Reason'], $reasonCodes)) continue;
67 $record->BrokenReason = $reason;
68 $returnSet->push($record);
69 }
70 }
71
72 if($sortBrokenReason) $returnSet->sort('BrokenReason', $direction);
73
74 return $returnSet;
75 }
76 function columns() {
77 if(isset($_REQUEST['CheckSite']) && $_REQUEST['CheckSite'] == 'Draft') {
78 $dateTitle = _t('BrokenLinksReport.ColumnDateLastModified', 'Date last modified');
79 } else {
80 $dateTitle = _t('BrokenLinksReport.ColumnDateLastPublished', 'Date last published');
81 }
82
83 $fields = array(
84 "Title" => array(
85 "title" => _t('BrokenLinksReport.ColumnPageName', 'Page name'),
86 'formatting' => sprintf(
87 '<a href=\"admin/show/$ID\" title=\"%s\">$value</a>',
88 _t('BrokenLinksReport.HoverTitleEditPage', 'Edit page')
89 )
90 ),
91 "LastEdited" => array(
92 "title" => $dateTitle,
93 'casting' => 'SSDatetime->Full'
94 ),
95 "BrokenReason" => array(
96 "title" => _t('BrokenLinksReport.ColumnProblemType', "Problem type")
97 ),
98 'AbsoluteLink' => array(
99 'title' => _t('BrokenLinksReport.ColumnURL', 'URL'),
100 'formatting' => '$value " . ($AbsoluteLiveLink ? "<a target=\"_blank\" href=\"$AbsoluteLiveLink\">(live)</a>" : "") . " <a target=\"_blank\" href=\"$value?stage=Stage\">(draft)</a>'
101 )
102 );
103
104 return $fields;
105 }
106 function parameterFields() {
107 return new FieldSet(
108 new DropdownField('CheckSite', _t('BrokenLinksReport.CheckSiteDropdown', 'Check site'), array(
109 'Published' => _t('BrokenLinksReport.CheckSiteDropdownPublished', 'Published Site'),
110 'Draft' => _t('BrokenLinksReport.CheckSiteDropdownDraft', 'Draft Site')
111 )),
112 new DropdownField(
113 'Reason',
114 _t('BrokenLinksReport.ReasonDropdown', 'Problem to check'),
115 array(
116 '' => _t('BrokenLinksReport.ReasonDropdownANY', 'Any'),
117 'BROKENFILE' => _t('BrokenLinksReport.ReasonDropdownBROKENFILE', 'Broken file'),
118 'BROKENLINK' => _t('BrokenLinksReport.ReasonDropdownBROKENLINK', 'Broken link'),
119 'VPBROKENLINK' => _t('BrokenLinksReport.ReasonDropdownVPBROKENLINK', 'Virtual page pointing to non-existent page'),
120 'RPBROKENLINK' => _t('BrokenLinksReport.ReasonDropdownRPBROKENLINK', 'Redirector page pointing to non-existent page'),
121 )
122 )
123 );
124 }
125 }
126
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-