Improve detection of php binary.
[lhc/web/wiklou.git] / maintenance / purgeDeletedFiles.php
1 <?php
2 /**
3 * Scans 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 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class PurgeDeletedFiles extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Scan the logging table and purge files that where deleted.";
29 $this->addOption( 'starttime', 'Starting timestamp', false, true );
30 $this->addOption( 'endtime', 'Ending timestamp', false, true );
31 }
32
33 public function execute() {
34 $this->output( "Purging cache and thumbnails for deleted files...\n" );
35 $this->purgeFromLogType( 'delete' );
36 $this->output( "...deleted files purged.\n\n" );
37
38 $this->output( "Purging cache and thumbnails for suppressed files...\n" );
39 $this->purgeFromLogType( 'suppress' );
40 $this->output( "...suppressed files purged.\n" );
41 }
42
43 protected function purgeFromLogType( $logType ) {
44 $repo = RepoGroup::singleton()->getLocalRepo();
45 $db = $repo->getSlaveDB();
46
47 $conds = array(
48 'log_namespace' => NS_FILE,
49 'log_type' => $logType,
50 'log_action' => array( 'delete', 'revision' )
51 );
52 $start = $this->getOption( 'starttime' );
53 if ( $start ) {
54 $conds[] = 'log_timestamp >= ' . $db->addQuotes( $db->timestamp( $start ) );
55 }
56 $end = $this->getOption( 'endtime' );
57 if ( $end ) {
58 $conds[] = 'log_timestamp <= ' . $db->addQuotes( $db->timestamp( $end ) );
59 }
60
61 $res = $db->select( 'logging', array( 'log_title', 'log_timestamp' ), $conds, __METHOD__ );
62 foreach ( $res as $row ) {
63 $file = $repo->newFile( Title::makeTitle( NS_FILE, $row->log_title ) );
64
65 // Purge current version and any versions in oldimage table
66 $file->purgeCache();
67 $file->purgeHistory();
68 // Purge items from fileachive table (rows are likely here)
69 $this->purgeFromArchiveTable( $file );
70
71 $this->output( "Purged file {$row->log_title}; deleted on {$row->log_timestamp}.\n" );
72 }
73 }
74
75 protected function purgeFromArchiveTable( LocalFile $file ) {
76 $db = $file->getRepo()->getSlaveDB();
77 $res = $db->select( 'filearchive',
78 array( 'fa_archive_name' ),
79 array( 'fa_name' => $file->getName() ),
80 __METHOD__
81 );
82 foreach ( $res as $row ) {
83 $file->purgeOldThumbnails( $row->fa_archive_name );
84 }
85 }
86 }
87
88 $maintClass = "PurgeDeletedFiles";
89 require_once( RUN_MAINTENANCE_IF_MAIN );