PopulateFilearchiveSha1::getUpdateKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 24.

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.

Loading history...
2
/**
3
 * Optional upgrade script to populate the fa_sha1 field
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 to populate the fa_sha1 field.
28
 *
29
 * @ingroup Maintenance
30
 * @since 1.21
31
 */
32
class PopulateFilearchiveSha1 extends LoggedUpdateMaintenance {
33
	public function __construct() {
34
		parent::__construct();
35
		$this->addDescription( 'Populate the fa_sha1 field from fa_storage_key' );
36
	}
37
38
	protected function getUpdateKey() {
39
		return 'populate fa_sha1';
40
	}
41
42
	protected function updateSkippedMessage() {
43
		return 'fa_sha1 column of filearchive table already populated.';
44
	}
45
46
	public function doDBUpdates() {
47
		$startTime = microtime( true );
48
		$dbw = $this->getDB( DB_MASTER );
49
		$table = 'filearchive';
50
		$conds = [ 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' ];
51
52
		if ( !$dbw->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) {
53
			$this->output( "fa_sha1 column does not exist\n\n", true );
54
55
			return false;
56
		}
57
58
		$this->output( "Populating fa_sha1 field from fa_storage_key\n" );
59
		$endId = $dbw->selectField( $table, 'MAX(fa_id)', false, __METHOD__ );
60
61
		$batchSize = $this->mBatchSize;
62
		$done = 0;
63
64
		do {
65
			$res = $dbw->select(
66
				$table,
67
				[ 'fa_id', 'fa_storage_key' ],
68
				$conds,
69
				__METHOD__,
70
				[ 'LIMIT' => $batchSize ]
71
			);
72
73
			$i = 0;
74
			foreach ( $res as $row ) {
75
				if ( $row->fa_storage_key == '' ) {
76
					// Revision was missing pre-deletion
77
					continue;
78
				}
79
				$sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
80
				$dbw->update( $table,
81
					[ 'fa_sha1' => $sha1 ],
82
					[ 'fa_id' => $row->fa_id ],
83
					__METHOD__
84
				);
85
				$lastId = $row->fa_id;
86
				$i++;
87
			}
88
89
			$done += $i;
90
			if ( $i !== $batchSize ) {
91
				break;
92
			}
93
94
			// print status and let replica DBs catch up
95
			$this->output( sprintf(
96
				"id %d done (up to %d), %5.3f%%  \r", $lastId, $endId, $lastId / $endId * 100 ) );
0 ignored issues
show
Bug introduced by
The variable $lastId does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
97
			wfWaitForSlaves();
0 ignored issues
show
Deprecated Code introduced by
The function wfWaitForSlaves() has been deprecated with message: since 1.27 Use LBFactory::waitForReplication

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
98
		} while ( true );
99
100
		$processingTime = microtime( true ) - $startTime;
101
		$this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
102
103
		return true; // we only updated *some* files, don't log
104
	}
105
}
106
107
$maintClass = "PopulateFilearchiveSha1";
108
require_once RUN_MAINTENANCE_IF_MAIN;
109