@@ -8,7 +8,9 @@ discard block |
||
| 8 | 8 | private static $exclude_classes = array('RedirectorPage', 'VirtualPage'); |
| 9 | 9 | |
| 10 | 10 | public function index() { |
| 11 | - if (!Director::is_cli()) return 'Please run this controller in CLI'; |
|
| 11 | + if (!Director::is_cli()) { |
|
| 12 | + return 'Please run this controller in CLI'; |
|
| 13 | + } |
|
| 12 | 14 | |
| 13 | 15 | libxml_use_internal_errors(true); |
| 14 | 16 | set_time_limit(600); |
@@ -44,15 +46,23 @@ discard block |
||
| 44 | 46 | $exclude = Config::inst()->get($this->class, 'exclude_classes'); |
| 45 | 47 | $exclude = ($exclude) ? "'".implode("','", $exclude)."'" : ''; |
| 46 | 48 | foreach (SiteTree::get()->where("ClassName NOT IN($exclude)") as $page) { |
| 47 | - if (!$this->checkForPossibleLinks($page, $includeInFields)) continue; |
|
| 49 | + if (!$this->checkForPossibleLinks($page, $includeInFields)) { |
|
| 50 | + continue; |
|
| 51 | + } |
|
| 48 | 52 | $page = $this->getLinkData($page, $includeInFields); |
| 49 | - if (!$page) continue; |
|
| 53 | + if (!$page) { |
|
| 54 | + continue; |
|
| 55 | + } |
|
| 50 | 56 | |
| 51 | - if (!$run_in_realtime) AutomatedLinkPageResult::add_or_update($page); |
|
| 57 | + if (!$run_in_realtime) { |
|
| 58 | + AutomatedLinkPageResult::add_or_update($page); |
|
| 59 | + } |
|
| 52 | 60 | $data->push($page); |
| 53 | 61 | } |
| 54 | 62 | |
| 55 | - if (!$run_in_realtime) AutomatedLinkPageResult::remove_old_data(); |
|
| 63 | + if (!$run_in_realtime) { |
|
| 64 | + AutomatedLinkPageResult::remove_old_data(); |
|
| 65 | + } |
|
| 56 | 66 | |
| 57 | 67 | return $data; |
| 58 | 68 | } |
@@ -70,15 +80,18 @@ discard block |
||
| 70 | 80 | // Set a list of all fields that can have autolinks created in them |
| 71 | 81 | $page->AutomateableFields = ArrayList::create(); |
| 72 | 82 | |
| 73 | - foreach ($this->getAllDatabaseFields($page->class) as $field => $type) |
|
| 74 | - if (in_array($field, $includeIn) && |
|
| 83 | + foreach ($this->getAllDatabaseFields($page->class) as $field => $type) { |
|
| 84 | + if (in_array($field, $includeIn) && |
|
| 75 | 85 | !$page->AutomateableFields->find('DataField', $field) && |
| 76 | 86 | AutomatedLink::isFieldParsable($page, $field) |
| 77 | 87 | ) $page->AutomateableFields->push(DataObject::create(array('DataField' => $field))); |
| 88 | + } |
|
| 78 | 89 | |
| 79 | 90 | // Get data Pre-Automated Links creation |
| 80 | 91 | $withLinks = $this->getPageDOM($page, true); |
| 81 | - if (!$withLinks) return false; |
|
| 92 | + if (!$withLinks) { |
|
| 93 | + return false; |
|
| 94 | + } |
|
| 82 | 95 | |
| 83 | 96 | $links = $withLinks->getElementsByTagName('a'); |
| 84 | 97 | |
@@ -88,10 +101,11 @@ discard block |
||
| 88 | 101 | |
| 89 | 102 | // List all automated links that were created in this $page |
| 90 | 103 | $linksUsed = array(); |
| 91 | - foreach ($this->Links as $autolink) |
|
| 92 | - foreach ($links as $link) { |
|
| 104 | + foreach ($this->Links as $autolink) { |
|
| 105 | + foreach ($links as $link) { |
|
| 93 | 106 | if ($link->getAttribute('data-id') == $autolink->ID) { |
| 94 | 107 | $linksUsed[$autolink->ID] = $autolink->Phrase; |
| 108 | + } |
|
| 95 | 109 | $page->OriginalLinkCount--; |
| 96 | 110 | $page->LinkCount++; |
| 97 | 111 | } |
@@ -99,7 +113,9 @@ discard block |
||
| 99 | 113 | |
| 100 | 114 | $page->Links = implode(', ', $linksUsed); |
| 101 | 115 | |
| 102 | - if ($page->LinkCount < 1) return false; |
|
| 116 | + if ($page->LinkCount < 1) { |
|
| 117 | + return false; |
|
| 118 | + } |
|
| 103 | 119 | |
| 104 | 120 | return $page; |
| 105 | 121 | } |
@@ -113,8 +129,9 @@ discard block |
||
| 113 | 129 | */ |
| 114 | 130 | private function getAllDatabaseFields($class) { |
| 115 | 131 | $fields = array(); |
| 116 | - foreach (ClassInfo::ancestry($class, true) as $cls) |
|
| 117 | - $fields = array_merge($fields, (array) DataObject::database_fields($cls)); |
|
| 132 | + foreach (ClassInfo::ancestry($class, true) as $cls) { |
|
| 133 | + $fields = array_merge($fields, (array) DataObject::database_fields($cls)); |
|
| 134 | + } |
|
| 118 | 135 | |
| 119 | 136 | return $fields; |
| 120 | 137 | } |
@@ -129,8 +146,12 @@ discard block |
||
| 129 | 146 | */ |
| 130 | 147 | private function getPageDOM(SiteTree $page) { |
| 131 | 148 | $controllerClass = $page->class.'_Controller'; |
| 132 | - if (!class_exists($controllerClass)) $controller = $page->class.'Controller'; |
|
| 133 | - if (!class_exists($controllerClass)) return false; |
|
| 149 | + if (!class_exists($controllerClass)) { |
|
| 150 | + $controller = $page->class.'Controller'; |
|
| 151 | + } |
|
| 152 | + if (!class_exists($controllerClass)) { |
|
| 153 | + return false; |
|
| 154 | + } |
|
| 134 | 155 | |
| 135 | 156 | $controller = $controllerClass::create($page); |
| 136 | 157 | $controller->invokeWithExtensions('addAutomatedLinks'); |
@@ -149,7 +170,9 @@ discard block |
||
| 149 | 170 | GlobalAutoLinkSettings::$encoding |
| 150 | 171 | ); |
| 151 | 172 | |
| 152 | - if (!$content) return false; |
|
| 173 | + if (!$content) { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 153 | 176 | |
| 154 | 177 | if( class_exists( 'HTML5_Parser' ) ){ |
| 155 | 178 | $html5 = HTML5_Parser::parse( $content ); |
@@ -158,7 +181,7 @@ discard block |
||
| 158 | 181 | while($html5->length > 0) { |
| 159 | 182 | $dom->appendChild($html5->item(0)); |
| 160 | 183 | } |
| 161 | - }else{ |
|
| 184 | + } else{ |
|
| 162 | 185 | $dom = $html5; |
| 163 | 186 | } |
| 164 | 187 | } else{ |
@@ -178,9 +201,10 @@ discard block |
||
| 178 | 201 | * @return Boolean |
| 179 | 202 | */ |
| 180 | 203 | private function checkForPossibleLinks(SiteTree $page, array $includeIn) { |
| 181 | - foreach ($this->Links as $link) |
|
| 182 | - foreach ($includeIn as $possibleField) |
|
| 204 | + foreach ($this->Links as $link) { |
|
| 205 | + foreach ($includeIn as $possibleField) |
|
| 183 | 206 | if (isset($page->$possibleField) && preg_match("/\b{$link->Phrase}\b/i", $page->$possibleField)) return true; |
| 207 | + } |
|
| 184 | 208 | |
| 185 | 209 | return false; |
| 186 | 210 | } |
@@ -73,12 +73,16 @@ discard block |
||
| 73 | 73 | private function getSettings() { |
| 74 | 74 | if ($this->settings === null) { |
| 75 | 75 | $this->settings = GlobalAutoLinkSettings::get_current(); |
| 76 | - if (!$this->settings) return $this->addLinks = false; |
|
| 76 | + if (!$this->settings) { |
|
| 77 | + return $this->addLinks = false; |
|
| 78 | + } |
|
| 77 | 79 | |
| 78 | 80 | $this->excludeTags = (array) $this->settings->ExcludeTags(); |
| 79 | 81 | $this->maxLinks = (int) ($this->settings->MaxLinksPerPage) ? $this->settings->MaxLinksPerPage : PHP_INT_MAX; |
| 80 | 82 | |
| 81 | - if (!in_array($this->owner->ClassName, $this->settings->AllowedIn())) $this->addLinks = false; |
|
| 83 | + if (!in_array($this->owner->ClassName, $this->settings->AllowedIn())) { |
|
| 84 | + $this->addLinks = false; |
|
| 85 | + } |
|
| 82 | 86 | } |
| 83 | 87 | |
| 84 | 88 | return $this->settings; |
@@ -114,7 +118,7 @@ discard block |
||
| 114 | 118 | while($html5->length > 0) { |
| 115 | 119 | $dom->appendChild($html5->item(0)); |
| 116 | 120 | } |
| 117 | - }else{ |
|
| 121 | + } else{ |
|
| 118 | 122 | $dom = $html5; |
| 119 | 123 | } |
| 120 | 124 | } else{ |
@@ -150,7 +154,9 @@ discard block |
||
| 150 | 154 | $excluded = array(); |
| 151 | 155 | foreach( $this->excludeTags as $eTag ){ |
| 152 | 156 | while( $tags = $html->getElementsByTagName( $eTag ) ){ |
| 153 | - if( !$tags->length ) break 1; |
|
| 157 | + if( !$tags->length ) { |
|
| 158 | + break 1; |
|
| 159 | + } |
|
| 154 | 160 | $tag = $tags->item(0); |
| 155 | 161 | $value = $html->saveHTML( $tag ); |
| 156 | 162 | $key = (string) crc32( $value ); |
@@ -169,7 +175,9 @@ discard block |
||
| 169 | 175 | $links = AutomatedLink::get()->sort('Priority'); |
| 170 | 176 | foreach( $links as $link ){ |
| 171 | 177 | // Check if self-linking is allowed and if current pagetype is allowed |
| 172 | - if( !$link->canBeAdded( $this->owner, $field ) ) continue; |
|
| 178 | + if( !$link->canBeAdded( $this->owner, $field ) ) { |
|
| 179 | + continue; |
|
| 180 | + } |
|
| 173 | 181 | |
| 174 | 182 | $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
| 175 | 183 | $escape = (string) preg_quote( $link->Phrase, '/' ); |
@@ -178,22 +186,31 @@ discard block |
||
| 178 | 186 | // Count the matches |
| 179 | 187 | preg_match_all( $regex, $content, $count ); |
| 180 | 188 | $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
| 181 | - if( $count < 1 ) continue; |
|
| 189 | + if( $count < 1 ) { |
|
| 190 | + continue; |
|
| 191 | + } |
|
| 182 | 192 | |
| 183 | - if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) |
|
| 184 | - $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 185 | - else |
|
| 186 | - $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 193 | + if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) { |
|
| 194 | + $max -= $this->maxLinksPerPage[ $link->ID ]; |
|
| 195 | + } else { |
|
| 196 | + $this->maxLinksPerPage[ $link->ID ] = 0; |
|
| 197 | + } |
|
| 187 | 198 | |
| 188 | 199 | for( $x = 0; $x < $count; $x++ ){ |
| 189 | 200 | // Stop adding links if we reached the link or page limit |
| 190 | - if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 201 | + if( $x >= $max || $this->linkCount >= $this->maxLinks ) { |
|
| 202 | + break; |
|
| 203 | + } |
|
| 191 | 204 | |
| 192 | 205 | // Check if there is anything else to replace else stop |
| 193 | 206 | preg_match( $regex, $content, $match ); |
| 194 | - if( !is_array( $match ) || !count( $match ) ) break; |
|
| 207 | + if( !is_array( $match ) || !count( $match ) ) { |
|
| 208 | + break; |
|
| 209 | + } |
|
| 195 | 210 | |
| 196 | - if( !$html = (string) $link->getHTML( $match[0] ) ) continue; |
|
| 211 | + if( !$html = (string) $link->getHTML( $match[0] ) ) { |
|
| 212 | + continue; |
|
| 213 | + } |
|
| 197 | 214 | $key = (string) crc32( $html ); |
| 198 | 215 | $excluded[ $key ] = (string) $html; |
| 199 | 216 | |
@@ -203,7 +220,9 @@ discard block |
||
| 203 | 220 | } |
| 204 | 221 | |
| 205 | 222 | // Stop Adding links if we reached the page limit |
| 206 | - if( $this->linkCount >= $this->maxLinks ) break; |
|
| 223 | + if( $this->linkCount >= $this->maxLinks ) { |
|
| 224 | + break; |
|
| 225 | + } |
|
| 207 | 226 | } |
| 208 | 227 | |
| 209 | 228 | // Re-add the excluded Tags |