Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
32 | class PurgeChangedFiles extends Maintenance { |
||
33 | /** |
||
34 | * Mapping from type option to log type and actions. |
||
35 | * @var array |
||
36 | */ |
||
37 | private static $typeMappings = [ |
||
38 | 'created' => [ |
||
39 | 'upload' => [ 'upload' ], |
||
40 | 'import' => [ 'upload', 'interwiki' ], |
||
41 | ], |
||
42 | 'deleted' => [ |
||
43 | 'delete' => [ 'delete', 'revision' ], |
||
44 | 'suppress' => [ 'delete', 'revision' ], |
||
45 | ], |
||
46 | 'modified' => [ |
||
47 | 'upload' => [ 'overwrite', 'revert' ], |
||
48 | 'move' => [ 'move', 'move_redir' ], |
||
49 | ], |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $startTimestamp; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $endTimestamp; |
||
61 | |||
62 | public function __construct() { |
||
75 | |||
76 | public function execute() { |
||
132 | |||
133 | /** |
||
134 | * Purge cache and thumbnails for changes of the given type. |
||
135 | * |
||
136 | * @param string $type Type of change to find |
||
137 | */ |
||
138 | protected function purgeFromLogType( $type ) { |
||
139 | $repo = RepoGroup::singleton()->getLocalRepo(); |
||
140 | $dbr = $this->getDB( DB_REPLICA ); |
||
141 | |||
142 | foreach ( self::$typeMappings[$type] as $logType => $logActions ) { |
||
143 | $this->verbose( "Scanning for {$logType}/" . implode( ',', $logActions ) . "\n" ); |
||
144 | |||
145 | $res = $dbr->select( |
||
146 | 'logging', |
||
147 | [ 'log_title', 'log_timestamp', 'log_params' ], |
||
148 | [ |
||
149 | 'log_namespace' => NS_FILE, |
||
150 | 'log_type' => $logType, |
||
151 | 'log_action' => $logActions, |
||
152 | 'log_timestamp >= ' . $dbr->addQuotes( $this->startTimestamp ), |
||
153 | 'log_timestamp <= ' . $dbr->addQuotes( $this->endTimestamp ), |
||
154 | ], |
||
155 | __METHOD__ |
||
156 | ); |
||
157 | |||
158 | $bSize = 0; |
||
159 | foreach ( $res as $row ) { |
||
160 | $file = $repo->newFile( Title::makeTitle( NS_FILE, $row->log_title ) ); |
||
161 | |||
162 | if ( $this->hasOption( 'dry-run' ) ) { |
||
163 | $this->verbose( "{$type}[{$row->log_timestamp}]: {$row->log_title}\n" ); |
||
164 | continue; |
||
165 | } |
||
166 | |||
167 | // Purge current version and its thumbnails |
||
168 | $file->purgeCache(); |
||
169 | // Purge the old versions and their thumbnails |
||
170 | foreach ( $file->getHistory() as $oldFile ) { |
||
171 | $oldFile->purgeCache(); |
||
172 | } |
||
173 | |||
174 | if ( $logType === 'delete' ) { |
||
175 | // If there is an orphaned storage file... delete it |
||
176 | View Code Duplication | if ( !$file->exists() && $repo->fileExists( $file->getPath() ) ) { |
|
177 | $dpath = $this->getDeletedPath( $repo, $file ); |
||
178 | if ( $repo->fileExists( $dpath ) ) { |
||
179 | // Sanity check to avoid data loss |
||
180 | $repo->getBackend()->delete( [ 'src' => $file->getPath() ] ); |
||
181 | $this->verbose( "Deleted orphan file: {$file->getPath()}.\n" ); |
||
182 | } else { |
||
183 | $this->error( "File was not deleted: {$file->getPath()}.\n" ); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | // Purge items from fileachive table (rows are likely here) |
||
188 | $this->purgeFromArchiveTable( $repo, $file ); |
||
189 | } elseif ( $logType === 'move' ) { |
||
190 | // Purge the target file as well |
||
191 | |||
192 | $params = unserialize( $row->log_params ); |
||
193 | if ( isset( $params['4::target'] ) ) { |
||
194 | $target = $params['4::target']; |
||
195 | $targetFile = $repo->newFile( Title::makeTitle( NS_FILE, $target ) ); |
||
196 | $targetFile->purgeCache(); |
||
197 | $this->verbose( "Purged file {$target}; move target @{$row->log_timestamp}.\n" ); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | $this->verbose( "Purged file {$row->log_title}; {$type} @{$row->log_timestamp}.\n" ); |
||
202 | |||
203 | if ( $this->hasOption( 'sleep-per-batch' ) && ++$bSize > $this->mBatchSize ) { |
||
204 | $bSize = 0; |
||
205 | // sleep-per-batch is milliseconds, usleep wants micro seconds. |
||
206 | usleep( 1000 * (int)$this->getOption( 'sleep-per-batch' ) ); |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | protected function purgeFromArchiveTable( LocalRepo $repo, LocalFile $file ) { |
||
241 | |||
242 | protected function getDeletedPath( LocalRepo $repo, LocalFile $file ) { |
||
248 | |||
249 | /** |
||
250 | * Send an output message iff the 'verbose' option has been provided. |
||
251 | * |
||
252 | * @param string $msg Message to output |
||
253 | */ |
||
254 | protected function verbose( $msg ) { |
||
259 | } |
||
260 | |||
263 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.