/**
* Adds parameters to the command. All parameters are sanitized via Shell::escape().
+ * Null values are ignored.
*
* @param string|string[] $args,...
* @return $this
// treat it as a list of arguments
$args = reset( $args );
}
- $this->command .= ' ' . Shell::escape( $args );
+ $this->command = trim( $this->command . ' ' . Shell::escape( $args ) );
return $this;
}
/**
* Adds unsafe parameters to the command. These parameters are NOT sanitized in any way.
+ * Null values are ignored.
*
* @param string|string[] $args,...
* @return $this
// treat it as a list of arguments
$args = reset( $args );
}
- $this->command .= implode( ' ', $args );
+ $args = array_filter( $args,
+ function ( $value ) {
+ return $value !== null;
+ }
+ );
+ $this->command = trim( $this->command . ' ' . implode( ' ', $args ) );
return $this;
}
* PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
*
* @param string $args,... strings to escape and glue together, or a single array of
- * strings parameter
+ * strings parameter. Null values are ignored.
* @return string
*/
public static function escape( /* ... */ ) {
$first = true;
$retVal = '';
foreach ( $args as $arg ) {
+ if ( $arg === null ) {
+ continue;
+ }
if ( !$first ) {
$retVal .= ' ';
} else {
<?php
use MediaWiki\Shell\Command;
+use Wikimedia\TestingAccessWrapper;
/**
* @group Shell
$this->assertRegExp( '/^.+no-such-file.*$/m', $result->getStderr() );
}
+ /**
+ * Test that null values are skipped by params() and unsafeParams()
+ */
+ public function testNullsAreSkipped() {
+ $command = TestingAccessWrapper::newFromObject( new Command );
+ $command->params( 'echo', 'a', null, 'b' );
+ $command->unsafeParams( 'c', null, 'd' );
+ $this->assertEquals( "'echo' 'a' 'b' c d", $command->command );
+ }
+
public function testT69870() {
$commandLine = wfIsWindows()
// 333 = 331 + CRLF
'simple' => [ [ 'true' ], "'true'" ],
'with args' => [ [ 'convert', '-font', 'font name' ], "'convert' '-font' 'font name'" ],
'array' => [ [ [ 'convert', '-font', 'font name' ] ], "'convert' '-font' 'font name'" ],
+ 'skip nulls' => [ [ 'ls', null ], "'ls'" ],
];
}
}