Skip to content

Commit

Permalink
Merge pull request #35 from liayn/code-cleanup
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
fsuter authored Oct 20, 2016
2 parents 0534bf7 + 7c0ab68 commit de5daaf
Show file tree
Hide file tree
Showing 22 changed files with 305 additions and 327 deletions.
73 changes: 73 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true
charset = utf-8

# Get rid of whitespace to avoid diffs with a bunch of EOL changes
trim_trailing_whitespace = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# CSS-Files
[*.css]
indent_style = tab
indent_size = 4

# HTML-Files
[*.html]
indent_style = tab
indent_size = 2

# TypoScript
[*.ts]
indent_style = space
indent_size = 2

# TMPL-Files
[*.tmpl]
indent_style = tab
indent_size = 4

# XLF-Files
[*.xlf]
indent_style = tab
indent_size = 4

# SCSS-Files
[*.scss]
indent_style = tab
indent_size = 2

# JS-Files
[*.js]
indent_style = tab
indent_size = 4

# JSON-Files
[*.json]
indent_style = tab
indent_size = 4

# PHP-Files
[*.php]
indent_style = space
indent_size = 4

# ReST-Files
[*.rst]
indent_style = space
indent_size = 3

# MD-Files
[*.md]
indent_style = space
indent_size = 4

# package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
73 changes: 19 additions & 54 deletions Classes/Command/LinkMigrationCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
*
* @package TYPO3\CMS\Extbase\Command
*/
class LinkMigrationCommandController extends CommandController {
class LinkMigrationCommandController extends CommandController
{
/**
* Default list of fields where to search for record references to migrate
*/
Expand Down Expand Up @@ -90,20 +91,14 @@ public function migrateCommand($fields = '')
$this->setFields($fields);
// Loop on all tables and fields
foreach ($this->tablesAndFields as $table => $listOfFields) {
$this->gatherRecordsToMigrate(
$table,
$listOfFields
);
$this->gatherRecordsToMigrate($table, $listOfFields);
}
// Ask the user for configuration key for each table
$this->getConfigurationKeys();
// Replace in fields and save modified data
$this->migrateRecords();
}
catch (\InvalidArgumentException $e) {
$this->outputLine(
$e->getMessage() . ' (' . $e->getCode() . ')'
);
} catch (\InvalidArgumentException $e) {
$this->outputLine($e->getMessage() . ' (' . $e->getCode() . ')');
$this->quit(1);
}
}
Expand All @@ -122,11 +117,8 @@ protected function setFields($fields)
list($table, $field) = explode('.', $aField);
if (empty($table) || empty($field)) {
throw new \InvalidArgumentException(
sprintf(
'Invalid argument "%s". Use "table.field" syntax',
$aField
),
1457434202
sprintf('Invalid argument "%s". Use "table.field" syntax', $aField),
1457434202
);
} else {
if (!array_key_exists($table, $this->tablesAndFields)) {
Expand All @@ -150,22 +142,15 @@ protected function setFields($fields)
protected function gatherRecordsToMigrate($table, $listOfFields)
{
try {
$records = $this->genericRepository->findByRecordLink(
$table,
$listOfFields
);
$records = $this->genericRepository->findByRecordLink($table, $listOfFields);
foreach ($records as $record) {
$id = (int)$record['uid'];
foreach ($listOfFields as $field) {
$matches = array();
// Find all element that have a syntax like "record:string:string(:string)"
// The last string is optional. If it exists, it is already a 4-part record reference,
// i.e. a reference using the new syntax and which does not need to be migrated.
preg_match_all(
'/record:(\w+):(\w+)(:\w+)?/',
$record[$field],
$matches
);
preg_match_all('/record:(\w+):(\w+)(:\w+)?/', $record[$field], $matches);
foreach ($matches as $index => $match) {
// Consider only matches that have 3 parts (i.e. 4th part is empty)
// NOTE: although not captured, the first part is "record:"
Expand All @@ -186,23 +171,16 @@ protected function gatherRecordsToMigrate($table, $listOfFields)
if (!array_key_exists($field, $this->recordsForMigration[$table][$id])) {
$this->recordsForMigration[$table][$id][$field] = array(
'content' => $record[$field],
'matches' => array()
'matches' => array(),
);
}
$this->recordsForMigration[$table][$id][$field]['matches'][] = $matches[0][$index];
}
}
}
}
}
catch (FailedQueryException $e) {
$this->outputLine(
sprintf(
'Table "%s" skipped. An error occurred: %s',
$table,
$e->getMessage()
)
);
} catch (FailedQueryException $e) {
$this->outputLine(sprintf('Table "%s" skipped. An error occurred: %s', $table, $e->getMessage()));
}
}

Expand All @@ -211,19 +189,16 @@ protected function gatherRecordsToMigrate($table, $listOfFields)
*
* @return void
*/
protected function getConfigurationKeys() {
protected function getConfigurationKeys()
{
foreach ($this->tablesForMigration as $table => &$dummy) {
$key = null;
do {
try {
$key = $this->console->ask(
sprintf(
'Please enter the configuration key to use for table "%s": ',
$table
)
sprintf('Please enter the configuration key to use for table "%s": ', $table)
);
}
catch (\Exception $e) {
} catch (\Exception $e) {
// Do nothing, just let it try again
}
} while ($key === null);
Expand All @@ -246,27 +221,17 @@ protected function migrateRecords()
foreach ($fieldInformation['matches'] as $link) {
$linkParts = explode(':', $link);
$newLink = 'record:' . $this->tablesForMigration[$linkParts[1]] . ':' . $linkParts[1] . ':' . $linkParts[2];
$updatedField = str_replace(
$link,
$newLink,
$updatedField
);
$updatedField = str_replace($link, $newLink, $updatedField);
}
if (!array_key_exists($id, $recordsForTable)) {
$recordsForTable[$id] = array();
}
$recordsForTable[$id][$field] = $updatedField;
}
}
$result = $this->genericRepository->massUpdate(
$table,
$recordsForTable
);
$result = $this->genericRepository->massUpdate($table, $recordsForTable);
if (!$result) {
$this->outputLine(
'Some database updates failed for table "%s"',
$table
);
$this->outputLine('Some database updates failed for table "%s"', $table);
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions Classes/Domain/Model/RecordLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
*
* @package Cobweb\Linkhandler\Domain\Model
*/
class RecordLink {
class RecordLink
{
/**
* @var string Name of the linkhandler configuration
*/
Expand Down Expand Up @@ -112,10 +113,7 @@ public function getRecordReference()
public function setRecordReference($recordReference)
{
if (empty($recordReference)) {
throw new \InvalidArgumentException(
'Record reference cannot be empty',
1457367830
);
throw new \InvalidArgumentException('Record reference cannot be empty', 1457367830);
}
$referenceParts = explode(':', $recordReference);
if (count($referenceParts) === 4) {
Expand All @@ -125,8 +123,8 @@ public function setRecordReference($recordReference)
$this->id = (int)$referenceParts[3];
} else {
throw new \InvalidArgumentException(
'Expected record reference structure is "record:key:table:id"',
1457367830
'Expected record reference structure is "record:key:table:id"',
1457367830
);
}
}
Expand Down
32 changes: 12 additions & 20 deletions Classes/Domain/Repository/GenericRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
*
* @package Cobweb\Linkhandler\Domain\Repository
*/
class GenericRepository {
class GenericRepository
{
/**
* Fetches a list of records from given table that contain links to records.
*
Expand All @@ -44,26 +45,21 @@ public function findByRecordLink($table, $listOfFields)
$where = implode(' OR ', $conditions) . BackendUtility::deleteClause($table);
try {
$fields = implode(', ', $listOfFields);
$result = $this->getDatabaseConnection()->exec_SELECTgetRows(
'uid, ' . $fields,
$table,
$where
);
$result = $this->getDatabaseConnection()->exec_SELECTgetRows('uid, ' . $fields, $table, $where);
if ($result === null) {
throw new FailedQueryException(
sprintf(
'A SQL error occurred querying table "%s" with fields "%s": %s',
$table,
$fields,
$this->getDatabaseConnection()->sql_error()
),
1457441163
sprintf(
'A SQL error occurred querying table "%s" with fields "%s": %s',
$table,
$fields,
$this->getDatabaseConnection()->sql_error()
),
1457441163
);
} else {
$records = $result;
}
}
catch (\InvalidArgumentException $e) {
} catch (\InvalidArgumentException $e) {
// Nothing to do here
}
return $records;
Expand All @@ -81,11 +77,7 @@ public function massUpdate($table, $records)
$globalResult = true;
// @todo: this could be improved to provide better reporting on errors (and maybe use transactions to roll everything back)
foreach ($records as $id => $fields) {
$result = $this->getDatabaseConnection()->exec_UPDATEquery(
$table,
'uid = ' . (int)$id,
$fields
);
$result = $this->getDatabaseConnection()->exec_UPDATEquery($table, 'uid = ' . (int)$id, $fields);
$globalResult &= $result;
}
return $globalResult;
Expand Down
26 changes: 10 additions & 16 deletions Classes/Linkvalidator/LinkhandlerLinkType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

use Cobweb\Linkhandler\Domain\Model\RecordLink;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype;
use TYPO3\CMS\Backend\Utility\BackendUtility;

Expand Down Expand Up @@ -94,18 +95,14 @@ public function checkLink($url, $softRefEntry, $linkAnalyzer)
$this->initializeRequiredClasses();

try {
$this->recordLink = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
RecordLink::class,
$url
);
}
catch (\Exception $e) {
$this->recordLink = GeneralUtility::makeInstance(RecordLink::class, $url);
} catch (\Exception $e) {
// Set error type to invalid (record reference) and return early
$this->setErrorParams(
array(
'errorType' => self::ERROR_TYPE_INVALID,
'url' => $url
)
array(
'errorType' => self::ERROR_TYPE_INVALID,
'url' => $url,
)
);
return false;
}
Expand All @@ -129,7 +126,8 @@ public function checkLink($url, $softRefEntry, $linkAnalyzer)
$errorType = self::ERROR_TYPE_MISSING;
} else {
// If the record was found, but its "delete" flag is set, it is a deleted record
$deleteFlag = (!empty($GLOBALS['TCA'][$this->recordLink->getTable()]['ctrl']['delete'])) ? $GLOBALS['TCA'][$this->recordLink->getTable()]['ctrl']['delete'] : '';
$deleteFlag = (!empty($GLOBALS['TCA'][$this->recordLink->getTable()]['ctrl']['delete']))
? $GLOBALS['TCA'][$this->recordLink->getTable()]['ctrl']['delete'] : '';
if ($deleteFlag !== '') {
$deleted = (bool)$rawRecord[$deleteFlag];
if ($deleted) {
Expand Down Expand Up @@ -224,11 +222,7 @@ protected function getRecordRow($applyEnableFields = false)
$whereStatement .= BackendUtility::BEenableFields($this->recordLink->getTable());
}

$row = $this->databaseConnection->exec_SELECTgetSingleRow(
'*',
$this->recordLink->getTable(),
$whereStatement
);
$row = $this->databaseConnection->exec_SELECTgetSingleRow('*', $this->recordLink->getTable(), $whereStatement);

// Since exec_SELECTgetSingleRow can return NULL or FALSE we
// make sure we always return NULL if no row was found.
Expand Down
3 changes: 2 additions & 1 deletion Classes/ProcessLinkParametersInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*
* @package Cobweb\Linkhandler
*/
interface ProcessLinkParametersInterface {
interface ProcessLinkParametersInterface
{
/**
* @param \Cobweb\Linkhandler\TypolinkHandler $linkHandler Back-reference to the calling object
* @return void
Expand Down
Loading

0 comments on commit de5daaf

Please sign in to comment.