|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class AutomatedLinkReportTask extends Controller { |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
private $GlobalSettings; |
|
6
|
|
|
private $Links; |
|
7
|
|
|
|
|
8
|
|
|
private static $exclude_classes = array('RedirectorPage', 'VirtualPage'); |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
public function index() { |
|
11
|
|
|
if (!Director::is_cli()) return 'Please run this controller in CLI'; |
|
12
|
|
|
|
|
13
|
|
|
libxml_use_internal_errors(true); |
|
14
|
|
|
set_time_limit(600); |
|
15
|
|
|
|
|
16
|
|
|
$this->checkLinks(); |
|
17
|
|
|
|
|
18
|
|
|
return array(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Check each page on the site and add the data of the links |
|
23
|
|
|
* found in AutomatedLinkPageResult Objects |
|
24
|
|
|
* |
|
25
|
|
|
* @see AutomatedLinkPageResult |
|
26
|
|
|
* @return ArrayList |
|
27
|
|
|
*/ |
|
28
|
|
|
public function checkLinks() { |
|
29
|
|
|
$data = ArrayList::create(); |
|
30
|
|
|
|
|
31
|
|
|
$run_in_realtime = Config::inst()->get('AutomatedLinkReport', 'run_in_realtime'); |
|
32
|
|
|
|
|
33
|
|
|
// Enable this since we will need to render the pages for the report |
|
34
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', true); |
|
35
|
|
|
|
|
36
|
|
|
$this->GlobalSettings = GlobalAutoLinkSettings::get_current(); |
|
37
|
|
|
$this->Links = AutomatedLink::get()->sort('Priority'); |
|
38
|
|
|
$includeInFields = $this->GlobalSettings->IncludeInFields(); |
|
39
|
|
|
if (!$this->GlobalSettings) { |
|
40
|
|
|
user_error('Run dev/build before starting to use SEOToolbox'); |
|
41
|
|
|
return $data; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$exclude = Config::inst()->get($this->class, 'exclude_classes'); |
|
45
|
|
|
$exclude = ($exclude) ? "'".implode("','", $exclude)."'" : ''; |
|
46
|
|
|
foreach (SiteTree::get()->where("ClassName NOT IN($exclude)") as $page) { |
|
47
|
|
|
if (!$this->checkForPossibleLinks($page, $includeInFields)) continue; |
|
48
|
|
|
$page = $this->getLinkData($page, $includeInFields); |
|
49
|
|
|
if (!$page) continue; |
|
50
|
|
|
|
|
51
|
|
|
if (!$run_in_realtime) AutomatedLinkPageResult::add_or_update($page); |
|
52
|
|
|
$data->push($page); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!$run_in_realtime) AutomatedLinkPageResult::remove_old_data(); |
|
56
|
|
|
|
|
57
|
|
|
return $data; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns all the data on how the provided $page was |
|
62
|
|
|
* affected by automated links |
|
63
|
|
|
* |
|
64
|
|
|
* @param SiteTree $page |
|
65
|
|
|
* @param array $includeIn |
|
66
|
|
|
* |
|
67
|
|
|
* @return SiteTree|false $page |
|
68
|
|
|
*/ |
|
69
|
|
|
private function getLinkData(SiteTree $page, array $includeIn) { |
|
70
|
|
|
// Set a list of all fields that can have autolinks created in them |
|
71
|
|
|
$page->AutomateableFields = ArrayList::create(); |
|
72
|
|
|
|
|
73
|
|
|
foreach ($this->getAllDatabaseFields($page->class) as $field => $type) |
|
74
|
|
|
if (in_array($field, $includeIn) && |
|
75
|
|
|
!$page->AutomateableFields->find('DataField', $field) && |
|
76
|
|
|
AutomatedLink::isFieldParsable($page, $field) |
|
77
|
|
|
) $page->AutomateableFields->push(DataObject::create(array('DataField' => $field))); |
|
78
|
|
|
|
|
79
|
|
|
// Get data Pre-Automated Links creation |
|
80
|
|
|
$withLinks = $this->getPageDOM($page, true); |
|
|
|
|
|
|
81
|
|
|
if (!$withLinks) return false; |
|
82
|
|
|
|
|
83
|
|
|
$links = $withLinks->getElementsByTagName('a'); |
|
84
|
|
|
|
|
85
|
|
|
$page->TotalLinks = $links->length; |
|
86
|
|
|
$page->OriginalLinkCount = $page->TotalLinks; |
|
|
|
|
|
|
87
|
|
|
$page->LinkCount = 0; |
|
88
|
|
|
|
|
89
|
|
|
// List all automated links that were created in this $page |
|
90
|
|
|
$linksUsed = array(); |
|
91
|
|
|
foreach ($this->Links as $autolink) |
|
92
|
|
|
foreach ($links as $link) { |
|
93
|
|
|
if ($link->getAttribute('data-id') == $autolink->ID) { |
|
94
|
|
|
$linksUsed[$autolink->ID] = $autolink->Phrase; |
|
95
|
|
|
$page->OriginalLinkCount--; |
|
|
|
|
|
|
96
|
|
|
$page->LinkCount++; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$page->Links = implode(', ', $linksUsed); |
|
101
|
|
|
|
|
102
|
|
|
if ($page->LinkCount < 1) return false; |
|
103
|
|
|
|
|
104
|
|
|
return $page; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return all possible database fields for the |
|
109
|
|
|
* $class provided |
|
110
|
|
|
* |
|
111
|
|
|
* @param String $class |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
private function getAllDatabaseFields($class) { |
|
115
|
|
|
$fields = array(); |
|
116
|
|
View Code Duplication |
foreach (ClassInfo::ancestry($class, true) as $cls) |
|
|
|
|
|
|
117
|
|
|
$fields = array_merge($fields, (array) DataObject::database_fields($cls)); |
|
118
|
|
|
|
|
119
|
|
|
return $fields; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns a rendered version of the page supplied |
|
124
|
|
|
* creating automated links according inside a DOMDocument |
|
125
|
|
|
* object or false if anything fails. |
|
126
|
|
|
* |
|
127
|
|
|
* @param SiteTree $page |
|
128
|
|
|
* @return DOMDocument |
|
129
|
|
|
*/ |
|
130
|
|
|
private function getPageDOM(SiteTree $page) { |
|
131
|
|
|
$controllerClass = $page->class.'_Controller'; |
|
132
|
|
|
if (!class_exists($controllerClass)) $controller = $page->class.'Controller'; |
|
|
|
|
|
|
133
|
|
|
if (!class_exists($controllerClass)) return false; |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$controller = $controllerClass::create($page); |
|
136
|
|
|
$controller->invokeWithExtensions('addAutomatedLinks'); |
|
137
|
|
|
|
|
138
|
|
|
// Set the fields with possible links into a single variable that |
|
139
|
|
|
// will be dumped in the link checker template |
|
140
|
|
|
$page->AutomateableText = ''; |
|
141
|
|
|
foreach ($page->AutomateableFields as $field) { |
|
142
|
|
|
$field = $field->DataField; |
|
143
|
|
|
$page->AutomateableText .= $page->$field; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$content = mb_convert_encoding( |
|
147
|
|
|
$controller->renderWith('LinkCheckerTemplate'), |
|
148
|
|
|
'html-entities', |
|
149
|
|
|
GlobalAutoLinkSettings::$encoding |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
return (!$content) ? false : AutomatedLink::constructDOMDocument($content); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Checks if the page could have the possibility of automated links |
|
157
|
|
|
* |
|
158
|
|
|
* @param SiteTree $page |
|
159
|
|
|
* @param array $includeIn |
|
160
|
|
|
* |
|
161
|
|
|
* @return Boolean |
|
162
|
|
|
*/ |
|
163
|
|
|
private function checkForPossibleLinks(SiteTree $page, array $includeIn) { |
|
164
|
|
|
foreach ($this->Links as $link) |
|
165
|
|
|
foreach ($includeIn as $possibleField) |
|
166
|
|
|
if (isset($page->$possibleField) && preg_match("/\b{$link->Phrase}\b/i", $page->$possibleField)) return true; |
|
167
|
|
|
|
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.