MediaWiki master
deleteArchivedFiles.php
Go to the documentation of this file.
1<?php
26require_once __DIR__ . '/Maintenance.php';
27
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Deletes all archived images.' );
37 $this->addOption( 'delete', 'Perform the deletion' );
38 $this->addOption( 'force', 'Force deletion of rows from filearchive' );
39 }
40
41 public function execute() {
42 if ( !$this->hasOption( 'delete' ) ) {
43 $this->output( "Use --delete to actually confirm this script\n" );
44 return;
45 }
46
47 # Data should come off the master, wrapped in a transaction
48 $dbw = $this->getPrimaryDB();
49 $this->beginTransaction( $dbw, __METHOD__ );
50 $repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
51
52 # Get "active" revisions from the filearchive table
53 $this->output( "Searching for and deleting archived files...\n" );
54 $res = $dbw->newSelectQueryBuilder()
55 ->select( [ 'fa_id', 'fa_storage_group', 'fa_storage_key', 'fa_sha1', 'fa_name' ] )
56 ->from( 'filearchive' )
57 ->caller( __METHOD__ )
58 ->fetchResultSet();
59
60 $count = 0;
61 foreach ( $res as $row ) {
62 $key = $row->fa_storage_key;
63 if ( !strlen( $key ) ) {
64 $this->output( "Entry with ID {$row->fa_id} has empty key, skipping\n" );
65 continue;
66 }
67
68 $file = $repo->newFile( $row->fa_name );
69 $status = $file->acquireFileLock( 10 );
70 if ( !$status->isOK() ) {
71 $this->error( "Could not acquire lock on '{$row->fa_name}', skipping\n" );
72 continue;
73 }
74
75 $group = $row->fa_storage_group;
76 $id = $row->fa_id;
77 $path = $repo->getZonePath( 'deleted' ) .
78 '/' . $repo->getDeletedHashPath( $key ) . $key;
79 if ( isset( $row->fa_sha1 ) ) {
80 $sha1 = $row->fa_sha1;
81 } else {
82 // old row, populate from key
83 $sha1 = LocalRepo::getHashFromKey( $key );
84 }
85
86 // Check if the file is used anywhere...
87 $inuse = (bool)$dbw->newSelectQueryBuilder()
88 ->select( '1' )
89 ->from( 'oldimage' )
90 ->where( [
91 'oi_sha1' => $sha1,
92 $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE
93 ] )
94 ->caller( __METHOD__ )
95 ->forUpdate()
96 ->fetchField();
97
98 $needForce = true;
99 if ( !$repo->fileExists( $path ) ) {
100 $this->output( "Notice - file '$key' not found in group '$group'\n" );
101 } elseif ( $inuse ) {
102 $this->output( "Notice - file '$key' is still in use\n" );
103 } elseif ( !$repo->quickPurge( $path ) ) {
104 $this->output( "Unable to remove file $path, skipping\n" );
105 $file->releaseFileLock();
106
107 // don't delete even with --force
108 continue;
109 } else {
110 $needForce = false;
111 }
112
113 if ( $needForce ) {
114 if ( $this->hasOption( 'force' ) ) {
115 $this->output( "Got --force, deleting DB entry\n" );
116 } else {
117 $file->releaseFileLock();
118 continue;
119 }
120 }
121
122 $count++;
123 $dbw->newDeleteQueryBuilder()
124 ->deleteFrom( 'filearchive' )
125 ->where( [ 'fa_id' => $id ] )
126 ->caller( __METHOD__ )->execute();
127 $file->releaseFileLock();
128 }
129
130 $this->commitTransaction( $dbw, __METHOD__ );
131 $this->output( "Done! [$count file(s)]\n" );
132 }
133}
134
135$maintClass = DeleteArchivedFiles::class;
136require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to delete archived (non-current) files from the database.
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.