Merge "s/Latn/latn/"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 29 Nov 2017 22:34:32 +0000 (22:34 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 29 Nov 2017 22:34:33 +0000 (22:34 +0000)
includes/libs/rdbms/database/Database.php
includes/shell/Command.php
includes/shell/Shell.php
tests/phpunit/includes/libs/rdbms/database/DatabaseMysqlBaseTest.php
tests/phpunit/includes/shell/CommandTest.php
tests/phpunit/includes/shell/ShellTest.php

index e04566e..5edf3fd 100644 (file)
@@ -461,9 +461,12 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        protected function ignoreErrors( $ignoreErrors = null ) {
                $res = $this->getFlag( self::DBO_IGNORE );
                if ( $ignoreErrors !== null ) {
-                       $ignoreErrors
-                               ? $this->setFlag( self::DBO_IGNORE )
-                               : $this->clearFlag( self::DBO_IGNORE );
+                       // setFlag()/clearFlag() do not allow DBO_IGNORE changes for sanity
+                       if ( $ignoreErrors ) {
+                               $this->mFlags |= self::DBO_IGNORE;
+                       } else {
+                               $this->mFlags &= ~self::DBO_IGNORE;
+                       }
                }
 
                return $res;
@@ -621,6 +624,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        }
 
        public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
+               if ( ( $flag & self::DBO_IGNORE ) ) {
+                       throw new \UnexpectedValueException( "Modifying DBO_IGNORE is not allowed." );
+               }
+
                if ( $remember === self::REMEMBER_PRIOR ) {
                        array_push( $this->priorFlags, $this->mFlags );
                }
@@ -628,6 +635,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        }
 
        public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
+               if ( ( $flag & self::DBO_IGNORE ) ) {
+                       throw new \UnexpectedValueException( "Modifying DBO_IGNORE is not allowed." );
+               }
+
                if ( $remember === self::REMEMBER_PRIOR ) {
                        array_push( $this->priorFlags, $this->mFlags );
                }
index 264c3b4..998b3ed 100644 (file)
@@ -106,6 +106,7 @@ class Command {
 
        /**
         * Adds parameters to the command. All parameters are sanitized via Shell::escape().
+        * Null values are ignored.
         *
         * @param string|string[] $args,...
         * @return $this
@@ -117,13 +118,14 @@ class Command {
                        // 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
@@ -135,7 +137,12 @@ class Command {
                        // 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;
        }
index 6e4fd02..084e10e 100644 (file)
@@ -142,7 +142,7 @@ class Shell {
         * 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( /* ... */ ) {
@@ -156,6 +156,9 @@ class Shell {
                $first = true;
                $retVal = '';
                foreach ( $args as $arg ) {
+                       if ( $arg === null ) {
+                               continue;
+                       }
                        if ( !$first ) {
                                $retVal .= ' ';
                        } else {
index 456447f..461ef09 100644 (file)
@@ -27,6 +27,7 @@ use Wikimedia\Rdbms\TransactionProfiler;
 use Wikimedia\Rdbms\DatabaseDomain;
 use Wikimedia\Rdbms\MySQLMasterPos;
 use Wikimedia\Rdbms\DatabaseMysqlBase;
+use Wikimedia\Rdbms\Database;
 
 /**
  * Fake class around abstract class so we can call concrete methods.
@@ -368,4 +369,24 @@ class DatabaseMysqlBaseTest extends PHPUnit_Framework_TestCase {
                        [ 1000.77 ],
                ];
        }
+
+       /**
+        * @expectedException UnexpectedValueException
+        * @covers Wikimedia\Rdbms\Database::setFlag
+        */
+       public function testDBOIgnoreSet() {
+               $db = new FakeDatabaseMysqlBase();
+
+               $db->setFlag( Database::DBO_IGNORE );
+       }
+
+       /**
+        * @expectedException UnexpectedValueException
+        * @covers Wikimedia\Rdbms\Database::clearFlag
+        */
+       public function testDBOIgnoreClear() {
+               $db = new FakeDatabaseMysqlBase();
+
+               $db->clearFlag( Database::DBO_IGNORE );
+       }
 }
index 81fae33..f7275e1 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 use MediaWiki\Shell\Command;
+use Wikimedia\TestingAccessWrapper;
 
 /**
  * @group Shell
@@ -103,6 +104,16 @@ class CommandTest extends PHPUnit_Framework_TestCase {
                $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
index 1e91074..7c96c3c 100644 (file)
@@ -25,6 +25,7 @@ class ShellTest extends PHPUnit_Framework_TestCase {
                        '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'" ],
                ];
        }
 }