6886e296c7169015dcd8b4219d61df90d06ee663
[lhc/web/wiklou.git] / maintenance / purgeDeletedFiles.php
1 <?php
2 /**
3 * Scan the deletion log and purges affected files within a timeframe.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once( __DIR__ . '/Maintenance.php' );
25
26 /**
27 * Maintenance script that scans the deletion log and purges affected files
28 * within a timeframe.
29 *
30 * @ingroup Maintenance
31 */
32 class PurgeDeletedFiles extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Scan the logging table and purge files that where deleted.";
36 $this->addOption( 'starttime', 'Starting timestamp', false, true );
37 $this->addOption( 'endtime', 'Ending timestamp', false, true );
38 }
39
40 public function execute() {
41 $this->output( "Purging cache and thumbnails for deleted files...\n" );
42 $this->purgeFromLogType( 'delete' );
43 $this->output( "...deleted files purged.\n\n" );
44
45 $this->output( "Purging cache and thumbnails for suppressed files...\n" );
46 $this->purgeFromLogType( 'suppress' );
47 $this->output( "...suppressed files purged.\n" );
48 }
49
50 protected function purgeFromLogType( $logType ) {
51 $repo = RepoGroup::singleton()->getLocalRepo();
52 $db = $repo->getSlaveDB();
53
54 $conds = array(
55 'log_namespace' => NS_FILE,
56 'log_type' => $logType,
57 'log_action' => array( 'delete', 'revision' )
58 );
59 $start = $this->getOption( 'starttime' );
60 if ( $start ) {
61 $conds[] = 'log_timestamp >= ' . $db->addQuotes( $db->timestamp( $start ) );
62 }
63 $end = $this->getOption( 'endtime' );
64 if ( $end ) {
65 $conds[] = 'log_timestamp <= ' . $db->addQuotes( $db->timestamp( $end ) );
66 }
67
68 $res = $db->select( 'logging', array( 'log_title', 'log_timestamp' ), $conds, __METHOD__ );
69 foreach ( $res as $row ) {
70 $file = $repo->newFile( Title::makeTitle( NS_FILE, $row->log_title ) );
71
72 // Purge current version and any versions in oldimage table
73 $file->purgeCache();
74 $file->purgeHistory();
75 // Purge items from fileachive table (rows are likely here)
76 $this->purgeFromArchiveTable( $file );
77
78 $this->output( "Purged file {$row->log_title}; deleted on {$row->log_timestamp}.\n" );
79 }
80 }
81
82 protected function purgeFromArchiveTable( LocalFile $file ) {
83 $db = $file->getRepo()->getSlaveDB();
84 $res = $db->select( 'filearchive',
85 array( 'fa_archive_name' ),
86 array( 'fa_name' => $file->getName() ),
87 __METHOD__
88 );
89 foreach ( $res as $row ) {
90 $file->purgeOldThumbnails( $row->fa_archive_name );
91 }
92 }
93 }
94
95 $maintClass = "PurgeDeletedFiles";
96 require_once RUN_MAINTENANCE_IF_MAIN;