@@ -43,9 +43,9 @@ discard block |
||
| 43 | 43 | 'Page' => 'SiteTree' |
| 44 | 44 | ); |
| 45 | 45 | |
| 46 | - private static $summary_fields = array( 'Phrase', 'PointsTo' ); |
|
| 47 | - private static $searchable_fields = array( 'Phrase' ); |
|
| 48 | - private static $singular_name = 'Automated Link'; |
|
| 46 | + private static $summary_fields = array('Phrase', 'PointsTo'); |
|
| 47 | + private static $searchable_fields = array('Phrase'); |
|
| 48 | + private static $singular_name = 'Automated Link'; |
|
| 49 | 49 | private static $plural_name = 'Automated Links'; |
| 50 | 50 | private static $parsableFields = array(); |
| 51 | 51 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | * |
| 55 | 55 | * @return string |
| 56 | 56 | */ |
| 57 | - public function PointsTo(){ |
|
| 57 | + public function PointsTo() { |
|
| 58 | 58 | return $this->Page()->Link(); |
| 59 | 59 | } |
| 60 | 60 | |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | * |
| 64 | 64 | * @return string |
| 65 | 65 | */ |
| 66 | - public function Title(){ |
|
| 66 | + public function Title() { |
|
| 67 | 67 | return $this->Phrase; |
| 68 | 68 | } |
| 69 | 69 | |
@@ -72,23 +72,23 @@ discard block |
||
| 72 | 72 | * |
| 73 | 73 | * @return String |
| 74 | 74 | */ |
| 75 | - public function forTemplate(){ |
|
| 75 | + public function forTemplate() { |
|
| 76 | 76 | return $this->getHTML(); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - public function canView( $member = false ){ |
|
| 79 | + public function canView($member = false) { |
|
| 80 | 80 | return Permission::check('AUTOMATEDLINK_VIEW'); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - public function canEdit( $member = false ){ |
|
| 83 | + public function canEdit($member = false) { |
|
| 84 | 84 | return Permission::check('AUTOMATEDLINK_EDIT'); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - public function canDelete( $member = false ){ |
|
| 87 | + public function canDelete($member = false) { |
|
| 88 | 88 | return Permission::check('AUTOMATEDLINK_DELETE'); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - public function canCreate( $member = false ){ |
|
| 91 | + public function canCreate($member = false) { |
|
| 92 | 92 | return Permission::check('AUTOMATEDLINK_CREATE'); |
| 93 | 93 | } |
| 94 | 94 | |
@@ -101,12 +101,12 @@ discard block |
||
| 101 | 101 | ); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - public function requireDefaultRecords(){ |
|
| 104 | + public function requireDefaultRecords() { |
|
| 105 | 105 | parent::requireDefaultRecords(); |
| 106 | 106 | |
| 107 | 107 | // Update all links to redirector pages during dev/build |
| 108 | - foreach( self::get() as $link ) { |
|
| 109 | - $link->CheckAndUpdateDestination( true ); |
|
| 108 | + foreach (self::get() as $link) { |
|
| 109 | + $link->CheckAndUpdateDestination(true); |
|
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | |
@@ -207,15 +207,15 @@ discard block |
||
| 207 | 207 | * @Boolean $write - Write the changes if any |
| 208 | 208 | * @return void |
| 209 | 209 | */ |
| 210 | - public function CheckAndUpdateDestination( $write = false ){ |
|
| 210 | + public function CheckAndUpdateDestination($write = false) { |
|
| 211 | 211 | $this->extend('beforeCheckAndUpdateDestination', $write); |
| 212 | 212 | |
| 213 | - if( $this->PageID && $this->Page() && |
|
| 213 | + if ($this->PageID && $this->Page() && |
|
| 214 | 214 | $this->Page()->ClassName == 'RedirectorPage' && |
| 215 | - $this->Page()->LinkToID && $this->Page()->RedirectionType == 'Internal' ) |
|
| 215 | + $this->Page()->LinkToID && $this->Page()->RedirectionType == 'Internal') |
|
| 216 | 216 | { |
| 217 | 217 | $this->PageID = $this->Page()->LinkToID; |
| 218 | - if( $write ) { |
|
| 218 | + if ($write) { |
|
| 219 | 219 | $this->write(); |
| 220 | 220 | } |
| 221 | 221 | } |
@@ -245,8 +245,8 @@ discard block |
||
| 245 | 245 | * @param ContentController $controller |
| 246 | 246 | * @return Boolean |
| 247 | 247 | */ |
| 248 | - public function canBeAdded( ContentController $controller ){ |
|
| 249 | - return ( $this->SelfLinking || $controller->ID != $this->PageID ); |
|
| 248 | + public function canBeAdded(ContentController $controller) { |
|
| 249 | + return ($this->SelfLinking || $controller->ID != $this->PageID); |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | /** |
@@ -255,20 +255,20 @@ discard block |
||
| 255 | 255 | * @param string $html |
| 256 | 256 | * @return DOMDocument |
| 257 | 257 | */ |
| 258 | - public static function constructDOMDocument($html){ |
|
| 259 | - if( class_exists( 'HTML5_Parser' ) ){ |
|
| 260 | - $html5 = HTML5_Parser::parse( $html ); |
|
| 261 | - if($html5 instanceof DOMNodeList){ |
|
| 258 | + public static function constructDOMDocument($html) { |
|
| 259 | + if (class_exists('HTML5_Parser')) { |
|
| 260 | + $html5 = HTML5_Parser::parse($html); |
|
| 261 | + if ($html5 instanceof DOMNodeList) { |
|
| 262 | 262 | $dom = new DOMDocument(); |
| 263 | - while($html5->length > 0) { |
|
| 263 | + while ($html5->length > 0) { |
|
| 264 | 264 | $dom->appendChild($html5->item(0)); |
| 265 | 265 | } |
| 266 | - }else{ |
|
| 266 | + }else { |
|
| 267 | 267 | $dom = $html5; |
| 268 | 268 | } |
| 269 | - } else{ |
|
| 269 | + }else { |
|
| 270 | 270 | $dom = new DOMDocument(); |
| 271 | - $dom->loadHTML( $html ); |
|
| 271 | + $dom->loadHTML($html); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | return $dom; |
@@ -280,9 +280,9 @@ discard block |
||
| 280 | 280 | * @param string $class |
| 281 | 281 | * @return array |
| 282 | 282 | */ |
| 283 | - public static function getAllDatabaseFields($class){ |
|
| 283 | + public static function getAllDatabaseFields($class) { |
|
| 284 | 284 | $fields = array(); |
| 285 | - foreach (ClassInfo::ancestry($class, true) as $ancestor){ |
|
| 285 | + foreach (ClassInfo::ancestry($class, true) as $ancestor) { |
|
| 286 | 286 | $fields = array_merge($fields, (array) DataObject::database_fields($ancestor)); |
| 287 | 287 | } |
| 288 | 288 | return $fields; |
@@ -263,7 +263,7 @@ |
||
| 263 | 263 | while($html5->length > 0) { |
| 264 | 264 | $dom->appendChild($html5->item(0)); |
| 265 | 265 | } |
| 266 | - }else{ |
|
| 266 | + } else{ |
|
| 267 | 267 | $dom = $html5; |
| 268 | 268 | } |
| 269 | 269 | } else{ |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | * Automated Link Report is a report that lists all automated |
| 8 | 8 | * links and how they affect the website |
| 9 | 9 | */ |
| 10 | -class AutomatedLinkReport extends SS_Report{ |
|
| 10 | +class AutomatedLinkReport extends SS_Report { |
|
| 11 | 11 | |
| 12 | 12 | protected $title = 'Automated Link Report'; |
| 13 | 13 | protected $description = 'Shows a list of all automated links and how they affect the site'; |
@@ -33,8 +33,8 @@ discard block |
||
| 33 | 33 | ); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - public function sourceRecords(){ |
|
| 37 | - if( !Config::inst()->get( $this->class, 'run_in_realtime' ) ) { |
|
| 36 | + public function sourceRecords() { |
|
| 37 | + if (!Config::inst()->get($this->class, 'run_in_realtime')) { |
|
| 38 | 38 | return AutomatedLinkPageResult::get(); |
| 39 | 39 | } |
| 40 | 40 | |
@@ -16,7 +16,9 @@ discard block |
||
| 16 | 16 | private static $exclude_classes = array('RedirectorPage', 'VirtualPage'); |
| 17 | 17 | |
| 18 | 18 | public function index() { |
| 19 | - if (!Director::is_cli()) return 'Please run this controller in CLI'; |
|
| 19 | + if (!Director::is_cli()) { |
|
| 20 | + return 'Please run this controller in CLI'; |
|
| 21 | + } |
|
| 20 | 22 | |
| 21 | 23 | libxml_use_internal_errors(true); |
| 22 | 24 | set_time_limit(600); |
@@ -52,15 +54,23 @@ discard block |
||
| 52 | 54 | $exclude = Config::inst()->get($this->class, 'exclude_classes'); |
| 53 | 55 | $exclude = ($exclude) ? "'".implode("','", $exclude)."'" : ''; |
| 54 | 56 | foreach (SiteTree::get()->where("ClassName NOT IN($exclude)") as $page) { |
| 55 | - if (!$this->checkForPossibleLinks($page, $includeInFields)) continue; |
|
| 57 | + if (!$this->checkForPossibleLinks($page, $includeInFields)) { |
|
| 58 | + continue; |
|
| 59 | + } |
|
| 56 | 60 | $page = $this->getLinkData($page, $includeInFields); |
| 57 | - if (!$page) continue; |
|
| 61 | + if (!$page) { |
|
| 62 | + continue; |
|
| 63 | + } |
|
| 58 | 64 | |
| 59 | - if (!$run_in_realtime) AutomatedLinkPageResult::add_or_update($page); |
|
| 65 | + if (!$run_in_realtime) { |
|
| 66 | + AutomatedLinkPageResult::add_or_update($page); |
|
| 67 | + } |
|
| 60 | 68 | $data->push($page); |
| 61 | 69 | } |
| 62 | 70 | |
| 63 | - if (!$run_in_realtime) AutomatedLinkPageResult::remove_old_data(); |
|
| 71 | + if (!$run_in_realtime) { |
|
| 72 | + AutomatedLinkPageResult::remove_old_data(); |
|
| 73 | + } |
|
| 64 | 74 | |
| 65 | 75 | return $data; |
| 66 | 76 | } |
@@ -78,15 +88,18 @@ discard block |
||
| 78 | 88 | // Set a list of all fields that can have autolinks created in them |
| 79 | 89 | $page->AutomateableFields = ArrayList::create(); |
| 80 | 90 | |
| 81 | - foreach (AutomatedLink::getAllDatabaseFields($page->class) as $field => $type) |
|
| 82 | - if (in_array($field, $includeIn) && |
|
| 91 | + foreach (AutomatedLink::getAllDatabaseFields($page->class) as $field => $type) { |
|
| 92 | + if (in_array($field, $includeIn) && |
|
| 83 | 93 | !$page->AutomateableFields->find('DataField', $field) && |
| 84 | 94 | AutomatedLink::isFieldParsable($page, $field) |
| 85 | 95 | ) $page->AutomateableFields->push(DataObject::create(array('DataField' => $field))); |
| 96 | + } |
|
| 86 | 97 | |
| 87 | 98 | // Get data Pre-Automated Links creation |
| 88 | 99 | $withLinks = $this->getPageDOM($page); |
| 89 | - if (!$withLinks) return false; |
|
| 100 | + if (!$withLinks) { |
|
| 101 | + return false; |
|
| 102 | + } |
|
| 90 | 103 | |
| 91 | 104 | $links = $withLinks->getElementsByTagName('a'); |
| 92 | 105 | |
@@ -96,10 +109,11 @@ discard block |
||
| 96 | 109 | |
| 97 | 110 | // List all automated links that were created in this $page |
| 98 | 111 | $linksUsed = array(); |
| 99 | - foreach ($this->Links as $autolink) |
|
| 100 | - foreach ($links as $link) { |
|
| 112 | + foreach ($this->Links as $autolink) { |
|
| 113 | + foreach ($links as $link) { |
|
| 101 | 114 | if ($link->getAttribute('data-id') == $autolink->ID) { |
| 102 | 115 | $linksUsed[$autolink->ID] = $autolink->Phrase; |
| 116 | + } |
|
| 103 | 117 | $page->OriginalLinkCount--; |
| 104 | 118 | $page->LinkCount++; |
| 105 | 119 | } |
@@ -107,7 +121,9 @@ discard block |
||
| 107 | 121 | |
| 108 | 122 | $page->Links = implode(', ', $linksUsed); |
| 109 | 123 | |
| 110 | - if ($page->LinkCount < 1) return false; |
|
| 124 | + if ($page->LinkCount < 1) { |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 111 | 127 | |
| 112 | 128 | return $page; |
| 113 | 129 | } |
@@ -122,8 +138,12 @@ discard block |
||
| 122 | 138 | */ |
| 123 | 139 | private function getPageDOM(SiteTree $page) { |
| 124 | 140 | $controllerClass = $page->class.'_Controller'; |
| 125 | - if (!class_exists($controllerClass)) $controllerClass = $page->class.'Controller'; |
|
| 126 | - if (!class_exists($controllerClass)) return false; |
|
| 141 | + if (!class_exists($controllerClass)) { |
|
| 142 | + $controllerClass = $page->class.'Controller'; |
|
| 143 | + } |
|
| 144 | + if (!class_exists($controllerClass)) { |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 127 | 147 | |
| 128 | 148 | $controller = $controllerClass::create($page); |
| 129 | 149 | $controller->invokeWithExtensions('addAutomatedLinks'); |
@@ -154,9 +174,10 @@ discard block |
||
| 154 | 174 | * @return Boolean |
| 155 | 175 | */ |
| 156 | 176 | private function checkForPossibleLinks(SiteTree $page, array $includeIn) { |
| 157 | - foreach ($this->Links as $link) |
|
| 158 | - foreach ($includeIn as $possibleField) |
|
| 177 | + foreach ($this->Links as $link) { |
|
| 178 | + foreach ($includeIn as $possibleField) |
|
| 159 | 179 | if (isset($page->$possibleField) && preg_match("/\b{$link->Phrase}\b/i", $page->$possibleField)) return true; |
| 180 | + } |
|
| 160 | 181 | |
| 161 | 182 | return false; |
| 162 | 183 | } |