|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpTek\Exodus\Transform; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
6
|
|
|
use SilverStripe\Versioned\Versioned; |
|
7
|
|
|
use PhpTek\Exodus\Transform\StaticSiteDataTypeTransformer; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* URL transformer specific to SilverStripe's `SiteTree` class for use with the module's |
|
11
|
|
|
* import content feature. |
|
12
|
|
|
* If enabled in the CMS UI, links to imported pages will be automatically re-written. |
|
13
|
|
|
* |
|
14
|
|
|
* @package phptek/silverstripe-exodus |
|
15
|
|
|
* @author Sam Minee <[email protected]> |
|
16
|
|
|
* @author Russell Michell <[email protected]> |
|
17
|
|
|
* @see {@link StaticSiteDataTypeTransformer} |
|
18
|
|
|
*/ |
|
19
|
|
|
class StaticSitePageTransformer extends StaticSiteDataTypeTransformer |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $import_root = 'import-home'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Default value to pass to usleep() to reduce load on the remote server |
|
29
|
|
|
* |
|
30
|
|
|
* @var number |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $sleep_multiplier = 100; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct(); |
|
41
|
|
|
$this->setParentId(1); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Generic function called by \ExternalContentImporter |
|
46
|
|
|
* |
|
47
|
|
|
* @param StaticSiteContentItem $item |
|
|
|
|
|
|
48
|
|
|
* @param mixed SilverStripe\ORM\DataObject|null $parentObject |
|
|
|
|
|
|
49
|
|
|
* @param string $strategy |
|
50
|
|
|
* @return mixed StaticSiteTransformResult | boolean |
|
51
|
|
|
* @throws \Exception |
|
52
|
|
|
*/ |
|
53
|
|
|
public function transform($item, $parentObject, $strategy) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->utils->log("START page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
56
|
|
|
|
|
57
|
|
|
if (!$item->checkIsType('sitetree')) { |
|
58
|
|
|
$this->utils->log(" - Item not of type \'sitetree\'. for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
59
|
|
|
$this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
60
|
|
|
|
|
61
|
|
|
return false; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$source = $item->getSource(); |
|
65
|
|
|
// Sleep to reduce load on the remote server |
|
66
|
|
|
usleep((int) self::$sleep_multiplier * 1000); |
|
67
|
|
|
// Extract content from the page |
|
68
|
|
|
$contentFields = $this->getContentFieldsAndSelectors($item, 'SiteTree'); |
|
69
|
|
|
|
|
70
|
|
|
// Default value for Title |
|
71
|
|
|
if (is_array($contentFields)) { |
|
72
|
|
|
if (empty($contentFields['Title'])) { |
|
73
|
|
|
$contentFields['Title'] = ['content' => $item->Name]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Default value for URLSegment |
|
77
|
|
|
if (empty($contentFields['URLSegment'])) { |
|
78
|
|
|
// $item->Name comes from StaticSiteContentItem::init() and is a URL |
|
79
|
|
|
$name = ($item->Name == '/' ? self::$import_root : $item->Name); |
|
80
|
|
|
$urlSegment = preg_replace('#\.[^.]*$#', '', $name); // Lose file-extensions e.g .html |
|
81
|
|
|
$contentFields['URLSegment'] = ['content' => $urlSegment]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Default value for Content (Useful for during unit-testing) |
|
85
|
|
|
if (empty($contentFields['Content'])) { |
|
86
|
|
|
$contentFields['Content'] = ['content' => 'No content found']; |
|
87
|
|
|
$this->utils->log(" - No content found for 'Content' field.", $item->AbsoluteURL, $item->ProcessedMIME); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Get a user-defined schema suited to this URL and Mime |
|
92
|
|
|
$schema = $source->getSchemaForURL($item->AbsoluteURL, $item->ProcessedMIME); |
|
93
|
|
|
|
|
94
|
|
|
if (!$schema) { |
|
95
|
|
|
$this->utils->log(" - Couldn't find an import schema for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
96
|
|
|
$this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
97
|
|
|
|
|
98
|
|
|
return false; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// TODO Exception vs return false?? |
|
102
|
|
|
if (!$schema->DataType) { |
|
103
|
|
|
$this->utils->log(" - DataType for migration schema is empty for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
104
|
|
|
$this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
105
|
|
|
throw new \Exception('DataType for migration schema is empty!'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Process incoming according to user-selected duplication strategy |
|
109
|
|
|
if (!$page = $this->duplicationStrategy($schema->DataType, $item, $source->BaseUrl, $strategy, $parentObject)) { |
|
110
|
|
|
$this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
111
|
|
|
|
|
112
|
|
|
return false; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$page->StaticSiteContentSourceID = $source->ID; |
|
116
|
|
|
$page->StaticSiteURL = $item->AbsoluteURL; |
|
117
|
|
|
$page->StaticSiteImportID = $this->getCurrentImportID(); |
|
118
|
|
|
$page->Status = 'Published'; |
|
119
|
|
|
|
|
120
|
|
|
foreach ($contentFields as $property => $map) { |
|
121
|
|
|
// Don't write anything new, if we have nothing new to write (useful during unit-testing) |
|
122
|
|
|
if (!empty($map['content'])) { |
|
123
|
|
|
$page->$property = $map['content']; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
Versioned::set_reading_mode('Stage.Stage'); |
|
128
|
|
|
$page->write(); |
|
129
|
|
|
$page->publishRecursive(); |
|
130
|
|
|
|
|
131
|
|
|
$this->utils->log("END page-transform for: ", $item->AbsoluteURL, $item->ProcessedMIME); |
|
132
|
|
|
|
|
133
|
|
|
return StaticSiteTransformResult::create($page, $item->stageChildren()); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths