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