phpcs: More require/include is not a function
[lhc/web/wiklou.git] / maintenance / deleteImageMemcached.php
1 <?php
2 /**
3 * Delete image information from the object 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 * @file
24 * @ingroup Maintenance
25 */
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * Maintenance script that deletes image information from the object cache.
31 *
32 * @ingroup Maintenance
33 */
34 class DeleteImageCache extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->mDescription = "Delete image information from the cache";
38 $this->addOption( 'sleep', 'How many seconds to sleep between deletions', true, true );
39 $this->addOption( 'until', 'Timestamp to delete all entries prior to', true, true );
40 }
41
42 public function execute() {
43 global $wgMemc;
44
45 $until = preg_replace( "/[^\d]/", '', $this->getOption( 'until' ) );
46 $sleep = (int)$this->getOption( 'sleep' ) * 1000; // milliseconds
47
48 ini_set( 'display_errors', false );
49
50 $dbr = wfGetDB( DB_SLAVE );
51
52 $res = $dbr->select( 'image',
53 array( 'img_name' ),
54 array( "img_timestamp < {$until}" ),
55 __METHOD__
56 );
57
58 $i = 0;
59 $total = $this->getImageCount();
60
61 foreach ( $res as $row ) {
62 if ( $i % $this->report == 0 ) {
63 $this->output( sprintf( "%s: %13s done (%s)\n", wfWikiID(), "$i/$total", wfPercent( $i / $total * 100 ) ) );
64 }
65 $md5 = md5( $row->img_name );
66 $wgMemc->delete( wfMemcKey( 'Image', $md5 ) );
67
68 if ( $sleep != 0 ) {
69 usleep( $sleep );
70 }
71
72 ++$i;
73 }
74 }
75
76 private function getImageCount() {
77 $dbr = wfGetDB( DB_SLAVE );
78 return $dbr->selectField( 'image', 'COUNT(*)', array(), __METHOD__ );
79 }
80 }
81
82 $maintClass = "DeleteImageCache";
83 require_once RUN_MAINTENANCE_IF_MAIN;