3 require_once __DIR__ .
'/Maintenance.php';
13 parent::__construct();
15 'Clean unused rows in linktarget table'
19 'Sleep time (in seconds) between every batch. Default: 0',
23 $this->
addOption(
'dry',
'Dry run',
false );
24 $this->
addOption(
'start',
'Start after this lt_id',
false,
true );
31 $maxLtId = (int)$dbr->newSelectQueryBuilder()
32 ->select(
'MAX(lt_id)' )
33 ->from(
'linktarget' )
37 $maxLtId = min( [ $maxLtId - 1, (
int)( $maxLtId * 0.99 ) ] );
39 $ltCounter = (int)$this->
getOption(
'start', 0 );
41 $this->
output(
"Deleting unused linktarget rows...\n" );
44 while ( $ltCounter < $maxLtId ) {
45 $batchMaxLtId = min( $ltCounter + $this->
getBatchSize(), $maxLtId ) + 1;
46 $this->
output(
"Checking lt_id between $ltCounter and $batchMaxLtId...\n" );
47 $queryBuilder = $dbr->newSelectQueryBuilder()
48 ->select( [
'lt_id' ] )
49 ->from(
'linktarget' );
50 $queryBuilder->where( [
51 'lt_id < ' . $dbr->addQuotes( $batchMaxLtId ),
52 'lt_id > ' . $dbr->addQuotes( $ltCounter )
54 foreach ( $linksMigration::$mapping as $table => $tableData ) {
55 $queryBuilder->leftJoin( $table,
null, $tableData[
'target_id'] .
'=lt_id' );
56 $queryBuilder->andWhere( [
57 $tableData[
'target_id'] =>
null
60 $ltIdsToDelete = $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
61 if ( !$ltIdsToDelete ) {
68 $queryBuilder = $dbr->newSelectQueryBuilder()
69 ->select( [
'lt_id' ] )
70 ->from(
'linktarget' )
72 'lt_id' => $ltIdsToDelete,
74 foreach ( $linksMigration::$mapping as $table => $tableData ) {
75 $queryBuilder->leftJoin( $table,
null, $tableData[
'target_id'] .
'=lt_id' );
76 $queryBuilder->andWhere( [
77 $tableData[
'target_id'] =>
null
80 $ltIdsToDelete = $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
81 if ( !$ltIdsToDelete ) {
87 $dbw->delete(
'linktarget', [
'lt_id' => $ltIdsToDelete ], __METHOD__ );
89 $deleted += count( $ltIdsToDelete );
94 $sleep = (int)$this->
getOption(
'sleep', 0 );
101 "Completed clean up linktarget table, "
102 .
"$deleted rows deleted.\n"
111 require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Maintenance script that cleans unused rows in linktarget table.
__construct()
Default constructor.
execute()
Do the actual work.