Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds script to remove duplicates, refs 2883 (#2932)
- Loading branch information
Showing
11 changed files
with
428 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace SMW\Maintenance; | ||
|
||
use SMW\ApplicationFactory; | ||
|
||
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( | ||
'MW_INSTALL_PATH' ) : __DIR__ . '/../../..'; | ||
|
||
require_once $basePath . '/maintenance/Maintenance.php'; | ||
|
||
/** | ||
* @license GNU GPL v2+ | ||
* @since 3.0 | ||
* | ||
* @author mwjames | ||
*/ | ||
class RemoveDuplicateEntities extends \Maintenance { | ||
|
||
/** | ||
* @since 3.0 | ||
*/ | ||
public function __construct() { | ||
$this->mDescription = 'Remove duplicates entities without active references.'; | ||
$this->addOption( 's', 'ID starting point', false, true ); | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @see Maintenance::addDefaultParams | ||
* | ||
* @since 3.0 | ||
*/ | ||
protected function addDefaultParams() { | ||
parent::addDefaultParams(); | ||
} | ||
|
||
/** | ||
* @see Maintenance::execute | ||
*/ | ||
public function execute() { | ||
|
||
if ( !defined( 'SMW_VERSION' ) ) { | ||
$this->output( "You need to have SMW enabled in order to use this maintenance script!\n\n" ); | ||
exit; | ||
} | ||
|
||
$this->reportMessage( | ||
"\nThe script will only dispose of those duplicate entities that have no active\n" . | ||
"references. The log section 'untouched' contains IDs that have not been\n" . | ||
"removed and the user is asked to verify the content and manually remove\n". | ||
"those listed entities.\n\n" | ||
); | ||
|
||
$applicationFactory = ApplicationFactory::getInstance(); | ||
$maintenanceFactory = $applicationFactory->newMaintenanceFactory(); | ||
|
||
$duplicateEntitiesDisposer = $maintenanceFactory->newDuplicateEntitiesDisposer( | ||
$applicationFactory->getStore( 'SMW\SQLStore\SQLStore' ), | ||
array( $this, 'reportMessage' ) | ||
); | ||
|
||
$duplicateEntityRecords = $duplicateEntitiesDisposer->findDuplicateEntityRecords(); | ||
$duplicateEntitiesDisposer->verifyAndDispose( $duplicateEntityRecords ); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @see Maintenance::reportMessage | ||
* | ||
* @since 1.9 | ||
* | ||
* @param string $message | ||
*/ | ||
public function reportMessage( $message ) { | ||
$this->output( $message ); | ||
} | ||
|
||
} | ||
|
||
$maintClass = 'SMW\Maintenance\RemoveDuplicateEntities'; | ||
require_once( RUN_MAINTENANCE_IF_MAIN ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace SMW\Maintenance; | ||
|
||
use Onoi\MessageReporter\MessageReporterAwareTrait; | ||
use SMW\Store; | ||
use SMW\SQLStore\SQLStore; | ||
use SMW\SQLStore\PropertyTableIdReferenceDisposer; | ||
|
||
/** | ||
* @license GNU GPL v2+ | ||
* @since 3.0 | ||
* | ||
* @author mwjames | ||
*/ | ||
class DuplicateEntitiesDisposer { | ||
|
||
use MessageReporterAwareTrait; | ||
|
||
/** | ||
* @var Store | ||
*/ | ||
private $store = null; | ||
|
||
/** | ||
* @since 3.0 | ||
* | ||
* @param Store $store | ||
*/ | ||
public function __construct( Store $store ) { | ||
$this->store = $store; | ||
} | ||
|
||
/** | ||
* @since 3.0 | ||
*/ | ||
public function findDuplicateEntityRecords() { | ||
return $this->store->getObjectIds()->findDuplicateEntityRecords(); | ||
} | ||
|
||
/** | ||
* @since 3.0 | ||
* | ||
* @param array $duplicateEntityRecords | ||
*/ | ||
public function verifyAndDispose( array $duplicateEntityRecords ) { | ||
|
||
$count = count( $duplicateEntityRecords ); | ||
$this->messageReporter->reportMessage( "Found: $count duplicates\n" ); | ||
|
||
if ( $count > 0 ) { | ||
$this->doDispose( $duplicateEntityRecords ); | ||
} | ||
} | ||
|
||
private function doDispose( array $duplicateEntityRecords ) { | ||
|
||
$propertyTableIdReferenceDisposer = new PropertyTableIdReferenceDisposer( | ||
$this->store | ||
); | ||
|
||
$propertyTableIdReferenceDisposer->setRedirectRemoval( true ); | ||
$connection = $this->store->getConnection( 'mw.db' ); | ||
|
||
$log = [ | ||
'disposed' => [], | ||
'untouched' => [] | ||
]; | ||
|
||
$i = 0; | ||
foreach ( $duplicateEntityRecords as $entityRecord ) { | ||
unset( $entityRecord['count'] ); | ||
|
||
if ( ( $i ) % 60 === 0 ) { | ||
$this->messageReporter->reportMessage( "\n" ); | ||
} | ||
|
||
$this->messageReporter->reportMessage( '.' ); | ||
|
||
$res = $connection->select( | ||
SQLStore::ID_TABLE, | ||
[ | ||
'smw_id', | ||
], | ||
[ | ||
'smw_title'=> $entityRecord['smw_title'], | ||
'smw_namespace'=> $entityRecord['smw_namespace'], | ||
'smw_iw'=> $entityRecord['smw_iw'], | ||
'smw_subobject'=> $entityRecord['smw_subobject'] | ||
], | ||
__METHOD__ | ||
); | ||
|
||
foreach ( $res as $row ) { | ||
if ( $propertyTableIdReferenceDisposer->isDisposable( $row->smw_id ) ) { | ||
$propertyTableIdReferenceDisposer->cleanUpTableEntriesById( $row->smw_id ); | ||
$log['disposed'][$row->smw_id] = $entityRecord; | ||
} else { | ||
$log['untouched'][$row->smw_id] = $entityRecord; | ||
} | ||
} | ||
|
||
$i++; | ||
} | ||
|
||
$this->messageReporter->reportMessage( | ||
"\n\nLog\n\n" . json_encode( $log, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . "\n" | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.