/code/reports/BrokenLinksReport.php

https://github.com/markjames/silverstripe-cms · PHP · 125 lines · 108 code · 12 blank · 5 comment · 33 complexity · d063315ccab1743482449bf04a251e40 MD5 · raw file

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