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