]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/deleteImageMemcached.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / deleteImageMemcached.php
1 <?php
2 /**
3  * This script delete image information from the cache.
4  *
5  * Usage example:
6  * php deleteImageMemcached.php --until "2005-09-05 00:00:00" --sleep 0
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  * http://www.gnu.org/copyleft/gpl.html
22  *
23  * @ingroup Maintenance
24  */
25
26 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
27
28 class DeleteImageCache extends Maintenance {
29         public function __construct() {
30                 parent::__construct();
31                 $this->mDescription = "Delete image information from the cache";
32                 $this->addOption( 'sleep', 'How many seconds to sleep between deletions', true, true );
33                 $this->addOption( 'until', 'Timestamp to delete all entries prior to', true, true );
34         }
35
36         public function execute() {
37                 global $wgMemc;
38
39                 $until = preg_replace( "/[^\d]/", '', $this->getOption( 'until' ) );
40                 $sleep = (int)$this->getOption( 'sleep' ) * 1000; // milliseconds
41
42                 ini_set( 'display_errors', false );
43
44                 $dbr = wfGetDB( DB_SLAVE );
45
46                 $res = $dbr->select( 'image',
47                         array( 'img_name' ),
48                         array( "img_timestamp < {$until}" ),
49                         __METHOD__
50                 );
51
52                 $i = 0;
53                 $total = $this->getImageCount();
54
55                 foreach ( $res as $row ) {
56                         if ( $i % $this->report == 0 )
57                                 $this->output( sprintf( "%s: %13s done (%s)\n", wfWikiID(), "$i/$total", wfPercent( $i / $total * 100 ) ) );
58                         $md5 = md5( $row->img_name );
59                         $wgMemc->delete( wfMemcKey( 'Image', $md5 ) );
60
61                         if ( $sleep != 0 )
62                                 usleep( $sleep );
63
64                         ++$i;
65                 }
66         }
67
68         private function getImageCount() {
69                 $dbr = wfGetDB( DB_SLAVE );
70                 return $dbr->selectField( 'image', 'COUNT(*)', array(), __METHOD__ );
71         }
72 }
73
74 $maintClass = "DeleteImageCache";
75 require_once( RUN_MAINTENANCE_IF_MAIN );