Conditions | 13 |
Paths | 388 |
Total Lines | 188 |
Code Lines | 134 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
165 | public function getCMSFields() |
||
166 | { |
||
167 | $fields = parent::getCMSFields(); |
||
168 | |||
169 | $fields->removeFieldsFromTab('Root', [ |
||
170 | 'Pages', |
||
171 | 'Files', |
||
172 | 'ShowContentInMenu', |
||
173 | 'Name' |
||
174 | ]); |
||
175 | |||
176 | // Because we can't pass arrays to FieldList::insertBefore |
||
177 | foreach ( |
||
178 | [ |
||
179 | HeaderField::create('ProfileHeading', 'Migration Profile Configuration'), |
||
180 | LiteralField::create('ProfileIntro', '' |
||
181 | . '<p class="message notice">' |
||
182 | . 'This where the basics of your migration profile are configured.' |
||
183 | . '</p>')] as $introField |
||
184 | ) { |
||
185 | $fields->insertBefore('BaseUrl', $introField); |
||
186 | } |
||
187 | |||
188 | // Processing Options |
||
189 | $processingOptions = ['' => "No Processing"]; |
||
190 | |||
191 | foreach (ClassInfo::implementorsOf(StaticSiteUrlProcessor::class) as $processor) { |
||
192 | $processorObj = singleton($processor); |
||
193 | $processingOptions[$processor] = $processorObj->getName(); |
||
194 | } |
||
195 | |||
196 | $fields->addFieldsToTab( |
||
197 | 'Root.Main', |
||
198 | [ |
||
199 | TextField::create("BaseUrl", "Base URL") |
||
200 | ->setDescription('The base URL of the site to be crawled and imported.'), |
||
201 | DropdownField::create("UrlProcessor", "URL Transformation", $processingOptions) |
||
202 | ->setDescription('Select the way in which crawled URLs should be transformed and cleaned-up.'), |
||
203 | CheckboxField::create("ParseCSS", "Fetch external CSS") |
||
204 | ->setDescription("Fetch images defined as CSS <strong>background-image</strong> which are not ordinarily reachable by crawling alone."), |
||
205 | CheckboxField::create("AutoRunTask", "Automatically rewrite links into Silverstripe-aware links") |
||
206 | ->setDescription("This will run a link-rewrite task automatically once an import has completed.") |
||
207 | ] |
||
208 | ); |
||
209 | $fields->fieldByName('Root.Main')->setTitle('Profile'); |
||
210 | $fields->insertBefore('BaseUrl', TextField::create('Name', 'Name') |
||
211 | ->setDescription('Allows you to differentiate between profiles.')); |
||
212 | |||
213 | // Schema Gridfield |
||
214 | $fields->addFieldToTab('Root.Main', HeaderField::create('ImportConfigHeader', 'Import Schema Configuration')); |
||
215 | $addNewButton = (new GridFieldAddNewButton('before'))->setButtonName("Add Schema"); |
||
216 | $importRules = $fields->dataFieldByName('Schemas'); |
||
217 | $importRules->getConfig()->removeComponentsByType(GridFieldAddNewButton::class); |
||
218 | $importRules->getConfig()->addComponent($addNewButton); |
||
219 | $fields->removeFieldFromTab("Root", "Schemas"); |
||
220 | $fields->addFieldToTab('Root.Main', LiteralField::create( |
||
221 | 'SchemaIntro', |
||
222 | '' |
||
223 | . '<p class="message notice">Schema map MIME-Types to Silverstripe content classes and' |
||
224 | . ' are related to one or more Import Rules. Each rule determines how content located at crawled URLs' |
||
225 | . ' should be imported into a content classes\' fields with the use of CSS selectors.' |
||
226 | . ' Where more than one schema exists for a field, they\'ll be processed in the order of Priority:' |
||
227 | . ' The first Schema to match a URI Pattern will be the one used for that field.</p>' |
||
228 | )); |
||
229 | $fields->addFieldToTab("Root.Main", $importRules); |
||
230 | |||
231 | switch ($this->urlList()->getSpiderStatus()) { |
||
232 | case StaticSiteUrlList::CRAWL_STATUS_NOTSTARTED: |
||
233 | $crawlButtonText = _t('StaticSiteContentSource.CRAWL_SITE', 'Crawl'); |
||
234 | break; |
||
235 | case StaticSiteUrlList::CRAWL_STATUS_PARTIAL: |
||
236 | $crawlButtonText = _t('StaticSiteContentSource.RESUME_CRAWLING', 'Resume Crawl'); |
||
237 | break; |
||
238 | case StaticSiteUrlList::CRAWL_STATUS_COMPLETE: |
||
239 | $crawlButtonText = _t('StaticSiteContentSource.RECRAWL_SITE', 'Re-Crawl'); |
||
240 | break; |
||
241 | default: |
||
242 | throw new \LogicException("Invalid getSpiderStatus() value '" . $this->urlList()->getSpiderStatus() . ";"); |
||
243 | } |
||
244 | |||
245 | $crawlButton = FormAction::create('crawlsite', $crawlButtonText) |
||
246 | ->setAttribute('data-icon', 'arrow-circle-double') |
||
247 | ->setUseButtonTag(true) |
||
248 | ->addExtraClass('btn action btn btn-primary tool-button font-icon-plus'); |
||
249 | $crawlMsg = ''; |
||
250 | |||
251 | // Disable crawl-button if assets dir isn't writable |
||
252 | // TODO this will need to change if change the default location of crawl data. Like _why_ is it in assets? |
||
253 | if (!file_exists(ASSETS_PATH) || !is_writable(ASSETS_PATH)) { |
||
254 | $crawlMsg = '<p class="message warning">Warning: Assets directory is not writable.</p>'; |
||
255 | $crawlButton->setDisabled(true); |
||
256 | } |
||
257 | |||
258 | $fields->addFieldsToTab('Root.Crawl', [ |
||
259 | ReadonlyField::create("CrawlStatus", "Crawl Status", $this->urlList()->getSpiderStatus()), |
||
260 | ReadonlyField::create("NumURIs", "Number of URIs Crawled", $this->urlList()->getNumURIs()), |
||
261 | LiteralField::create( |
||
262 | 'CrawlActions', |
||
263 | $crawlMsg ? '<p class="message notice">' . $crawlMsg . '</p>' : '' |
||
264 | . '<div class="btn-toolbar">' . $crawlButton->forTemplate() . '</div>' |
||
265 | ) |
||
266 | ]); |
||
267 | |||
268 | // Because we can't pass arrays to FieldList::insertBefore |
||
269 | foreach ( |
||
270 | [ |
||
271 | HeaderField::create('CrawlHeading', 'Source Site Crawling'), |
||
272 | LiteralField::create('CrawlIntro', '' |
||
273 | . '<p class="message notice">' |
||
274 | . 'Before you can load any content into Silverstripe, all source URLs must first be crawled.' |
||
275 | . ' Select the button below to start or resume a crawl as applicable.' |
||
276 | . '</p>')] as $introField |
||
277 | ) { |
||
278 | $fields->insertBefore('CrawlStatus', $introField); |
||
279 | } |
||
280 | |||
281 | /* |
||
282 | * @todo use customise() and arrange this using an includes .ss template fragment |
||
283 | */ |
||
284 | if ($this->urlList()->getSpiderStatus() == StaticSiteUrlList::CRAWL_STATUS_COMPLETE) { |
||
285 | $fields->addFieldToTab( |
||
286 | 'Root.Crawl', |
||
287 | LiteralField::create( |
||
288 | 'CrawlURLListUIntro', |
||
289 | '<p class="mesage notice">Review the list of crawled URIs below. When you\'re happy with the import' |
||
290 | . ' you can proceed to the "Import" tab and follow the instructions there.</p>' |
||
291 | ), |
||
292 | LiteralField::create('CrawlURLList', $this->listofCrawledItems()) |
||
293 | ); |
||
294 | } |
||
295 | |||
296 | $fields->dataFieldByName("ExtraCrawlUrls") |
||
297 | ->setDescription("Add URIs that are not reachable via links when content scraping, eg: '/about/team'. One per line") |
||
298 | ->setTitle('Additional URIs'); |
||
299 | $fields->dataFieldByName("UrlExcludePatterns") |
||
300 | ->setDescription("URLs that should be excluded. (Supports regular expressions e.g. '/about/.*'). One per line") |
||
301 | ->setTitle('Excluded URLs'); |
||
302 | |||
303 | $hasImports = DataObject::get(StaticSiteImportDataObject::class); |
||
304 | $_source = []; |
||
305 | |||
306 | foreach ($hasImports as $import) { |
||
307 | $date = DBField::create_field(DBDatetime::class, $import->Created)->Time24(); |
||
308 | $_source[$import->ID] = $date . ' (Import #' . $import->ID . ')'; |
||
309 | } |
||
310 | |||
311 | $fields->addFieldsToTab('Root.Import', [ |
||
312 | HeaderField::create('ImportHeading', 'Source Site Import'), |
||
313 | LiteralField::create('ImportIntro', '' |
||
314 | . '<p class="message notice">' |
||
315 | . 'Use this area to configure where in the current IA imported page content should appear.' |
||
316 | . ' The same goes for imported files and images.' |
||
317 | . '</p>')]); |
||
318 | |||
319 | if ($importCount = $hasImports->count()) { |
||
320 | $clearImportButton = FormAction::create('clearimports', 'Clear selected imports') |
||
321 | ->setAttribute('data-icon', 'arrow-circle-double') |
||
322 | ->addExtraClass('btn action btn btn-primary tool-button font-icon-plus') |
||
323 | ->setUseButtonTag(true); |
||
324 | |||
325 | $clearImportField = ToggleCompositeField::create('ClearImports', 'Clear Import Metadata', [ |
||
326 | LiteralField::create('ImportCountText', '<p>Each time an import is run, some meta information is stored such as an import identifier and failed-link records.<br/><br/></p>'), |
||
327 | LiteralField::create('ImportCount', '<p>Total imports: ' . $importCount . '</p>'), |
||
328 | ListboxField::create('ShowImports', 'Select import(s) to clear:', $_source, '', null, true), |
||
329 | CheckboxField::create('ClearAllImports', 'Clear all import meta-data', 0), |
||
330 | LiteralField::create('ImportActions', '<div class="btn-toolbar">' . $clearImportButton->forTemplate() . '</div>') |
||
331 | ])->addExtraClass('clear-imports'); |
||
332 | |||
333 | $fields->addFieldToTab('Root.Import', $clearImportField); |
||
334 | } |
||
335 | |||
336 | $fields->addFieldsToTab('Root.Environment', [ |
||
337 | HeaderField::create('EnvHeading', 'Webserver Environment'), |
||
338 | LiteralField::create('EnvIntro', '' |
||
339 | . '<p class="message notice">' |
||
340 | . 'Refer to this area for information related to the PHP and Webserver environment' |
||
341 | . ' which may affect the proper function and performance of this tool.' |
||
342 | . '</p>'), |
||
343 | LiteralField::create('EnvInfo', '' |
||
344 | . '<ul>' |
||
345 | . '<li>PHP Info: ' . $_SERVER['PHP_VERSION'] . '</li>' |
||
346 | . '<li>Webserver Info: ' . $_SERVER['SERVER_SOFTWARE'] . '</li>' |
||
347 | . '<li>max_execution_time: ' . sprintf('%s seconds', ini_get('max_execution_time')) . '</li>' |
||
348 | . '<li>memory_limit: ' . sprintf('%d Mb', ini_get('memory_limit')) . '</li>' |
||
349 | . '</ul>') |
||
350 | ]); |
||
351 | |||
352 | return $fields; |
||
353 | } |
||
579 |