This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | /** |
||
3 | * Optional upgrade script to populate the img_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 img_sha1 field. |
||
28 | * |
||
29 | * @ingroup Maintenance |
||
30 | */ |
||
31 | class PopulateImageSha1 extends LoggedUpdateMaintenance { |
||
32 | View Code Duplication | public function __construct() { |
|
33 | parent::__construct(); |
||
34 | $this->addDescription( 'Populate the img_sha1 field' ); |
||
35 | $this->addOption( 'force', "Recalculate sha1 for rows that already have a value" ); |
||
36 | $this->addOption( 'multiversiononly', "Calculate only for files with several versions" ); |
||
37 | $this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" . |
||
38 | "\t\tdefault uses Database class", false, true ); |
||
39 | $this->addOption( |
||
40 | 'file', |
||
41 | 'Fix for a specific file, without File: namespace prefixed', |
||
42 | false, |
||
43 | true |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | protected function getUpdateKey() { |
||
48 | return 'populate img_sha1'; |
||
49 | } |
||
50 | |||
51 | protected function updateSkippedMessage() { |
||
52 | return 'img_sha1 column of image table already populated.'; |
||
53 | } |
||
54 | |||
55 | public function execute() { |
||
56 | if ( $this->getOption( 'file' ) || $this->hasOption( 'multiversiononly' ) ) { |
||
57 | $this->doDBUpdates(); // skip update log checks/saves |
||
58 | } else { |
||
59 | parent::execute(); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public function doDBUpdates() { |
||
64 | $method = $this->getOption( 'method', 'normal' ); |
||
65 | $file = $this->getOption( 'file', '' ); |
||
66 | $force = $this->getOption( 'force' ); |
||
67 | $isRegen = ( $force || $file != '' ); // forced recalculation? |
||
68 | |||
69 | $t = -microtime( true ); |
||
70 | $dbw = $this->getDB( DB_MASTER ); |
||
71 | if ( $file != '' ) { |
||
72 | $res = $dbw->select( |
||
73 | 'image', |
||
74 | [ 'img_name' ], |
||
75 | [ 'img_name' => $file ], |
||
76 | __METHOD__ |
||
77 | ); |
||
78 | if ( !$res ) { |
||
79 | $this->error( "No such file: $file", true ); |
||
80 | |||
81 | return false; |
||
82 | } |
||
83 | $this->output( "Populating img_sha1 field for specified files\n" ); |
||
84 | } else { |
||
85 | if ( $this->hasOption( 'multiversiononly' ) ) { |
||
86 | $conds = []; |
||
87 | $this->output( "Populating and recalculating img_sha1 field for versioned files\n" ); |
||
88 | } elseif ( $force ) { |
||
89 | $conds = []; |
||
90 | $this->output( "Populating and recalculating img_sha1 field\n" ); |
||
91 | } else { |
||
92 | $conds = [ 'img_sha1' => '' ]; |
||
93 | $this->output( "Populating img_sha1 field\n" ); |
||
94 | } |
||
95 | if ( $this->hasOption( 'multiversiononly' ) ) { |
||
96 | $res = $dbw->select( 'oldimage', |
||
97 | [ 'img_name' => 'DISTINCT(oi_name)' ], $conds, __METHOD__ ); |
||
98 | } else { |
||
99 | $res = $dbw->select( 'image', [ 'img_name' ], $conds, __METHOD__ ); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | $imageTable = $dbw->tableName( 'image' ); |
||
104 | $oldImageTable = $dbw->tableName( 'oldimage' ); |
||
105 | |||
106 | if ( $method == 'pipe' ) { |
||
107 | // Opening a pipe allows the SHA-1 operation to be done in parallel |
||
108 | // with the database write operation, because the writes are queued |
||
109 | // in the pipe buffer. This can improve performance by up to a |
||
110 | // factor of 2. |
||
111 | global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname; |
||
112 | $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) . |
||
113 | ' -h' . wfEscapeShellArg( $wgDBserver ) . |
||
114 | ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname ); |
||
115 | $this->output( "Using pipe method\n" ); |
||
116 | $pipe = popen( $cmd, 'w' ); |
||
117 | } |
||
118 | |||
119 | $numRows = $res->numRows(); |
||
120 | $i = 0; |
||
121 | foreach ( $res as $row ) { |
||
122 | if ( $i % $this->mBatchSize == 0 ) { |
||
123 | $this->output( sprintf( |
||
124 | "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) ); |
||
125 | wfWaitForSlaves(); |
||
0 ignored issues
–
show
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. ![]() |
|||
126 | } |
||
127 | |||
128 | $file = wfLocalFile( $row->img_name ); |
||
129 | if ( !$file ) { |
||
130 | continue; |
||
131 | } |
||
132 | |||
133 | // Upgrade the current file version... |
||
134 | $sha1 = $file->getRepo()->getFileSha1( $file->getPath() ); |
||
0 ignored issues
–
show
It seems like
$file->getPath() targeting File::getPath() can also be of type boolean ; however, FileRepo::getFileSha1() does only seem to accept string , maybe add an additional type check?
This check looks at variables that are passed out again to other methods. If the outgoing method call has stricter type requirements than the method itself, an issue is raised. An additional type check may prevent trouble. ![]() |
|||
135 | if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly |
||
136 | if ( $isRegen && $file->getSha1() !== $sha1 ) { |
||
137 | // The population was probably done already. If the old SHA1 |
||
138 | // does not match, then both fix the SHA1 and the metadata. |
||
139 | $file->upgradeRow(); |
||
140 | } else { |
||
141 | $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) . |
||
142 | " WHERE img_name=" . $dbw->addQuotes( $file->getName() ); |
||
143 | if ( $method == 'pipe' ) { |
||
144 | fwrite( $pipe, "$sql;\n" ); |
||
0 ignored issues
–
show
The variable
$pipe 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
![]() |
|||
145 | } else { |
||
146 | $dbw->query( $sql, __METHOD__ ); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | // Upgrade the old file versions... |
||
151 | foreach ( $file->getHistory() as $oldFile ) { |
||
152 | $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() ); |
||
0 ignored issues
–
show
It seems like
$oldFile->getPath() targeting File::getPath() can also be of type boolean ; however, FileRepo::getFileSha1() does only seem to accept string , maybe add an additional type check?
This check looks at variables that are passed out again to other methods. If the outgoing method call has stricter type requirements than the method itself, an issue is raised. An additional type check may prevent trouble. ![]() |
|||
153 | if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly |
||
154 | if ( $isRegen && $oldFile->getSha1() !== $sha1 ) { |
||
155 | // The population was probably done already. If the old SHA1 |
||
156 | // does not match, then both fix the SHA1 and the metadata. |
||
157 | $oldFile->upgradeRow(); |
||
158 | } else { |
||
159 | $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) . |
||
160 | " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" . |
||
161 | " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")"; |
||
0 ignored issues
–
show
It seems like you code against a specific sub-type and not the parent class
File as the method getArchiveName() does only exist in the following sub-classes of File : OldLocalFile . Maybe you want to instanceof check for one of these explicitly?
Let’s take a look at an example: abstract class User
{
/** @return string */
abstract public function getPassword();
}
class MyUser extends User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
![]() |
|||
162 | if ( $method == 'pipe' ) { |
||
163 | fwrite( $pipe, "$sql;\n" ); |
||
164 | } else { |
||
165 | $dbw->query( $sql, __METHOD__ ); |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | $i++; |
||
171 | } |
||
172 | if ( $method == 'pipe' ) { |
||
173 | fflush( $pipe ); |
||
174 | pclose( $pipe ); |
||
175 | } |
||
176 | $t += microtime( true ); |
||
177 | $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) ); |
||
178 | |||
179 | return !$file; // we only updated *some* files, don't log |
||
180 | } |
||
181 | } |
||
182 | |||
183 | $maintClass = "PopulateImageSha1"; |
||
184 | require_once RUN_MAINTENANCE_IF_MAIN; |
||
185 |
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.