Merge "Mark DBError as runtime exception"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 3 Jan 2018 20:51:55 +0000 (20:51 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 3 Jan 2018 20:51:56 +0000 (20:51 +0000)
19 files changed:
includes/actions/InfoAction.php
includes/installer/PostgresUpdater.php
includes/shell/Command.php
includes/shell/Shell.php
includes/widget/ComplexNamespaceInputWidget.php
includes/widget/ComplexTitleInputWidget.php
includes/widget/DateInputWidget.php
includes/widget/NamespaceInputWidget.php
includes/widget/SearchInputWidget.php
includes/widget/SelectWithInputWidget.php
includes/widget/TitleInputWidget.php
includes/widget/UserInputWidget.php
includes/widget/UsersMultiselectWidget.php
languages/i18n/en.json
languages/i18n/qqq.json
resources/src/mediawiki.widgets/mw.widgets.DateInputWidget.js
resources/src/mediawiki/htmlform/multiselect.js
tests/phpunit/includes/api/ApiClearHasMsgTest.php
tests/phpunit/includes/shell/CommandTest.php

index 62f7ddf..a8a8317 100644 (file)
@@ -437,6 +437,18 @@ class InfoAction extends FormlessAction {
                        ];
                }
 
+               // Display image SHA-1 value
+               if ( $title->inNamespace( NS_FILE ) ) {
+                       $fileObj = wfFindFile( $title );
+                       if ( $fileObj !== false ) {
+                               $output = $fileObj->getSha1();
+                               $pageInfo['header-basic'][] = [
+                                       $this->msg( 'pageinfo-file-hash' ),
+                                       $output
+                               ];
+                       }
+               }
+
                // Page protection
                $pageInfo['header-restrictions'] = [];
 
index e920fb7..fe8a1b1 100644 (file)
@@ -487,7 +487,7 @@ class PostgresUpdater extends DatabaseUpdater {
                        // 1.31
                        [ 'addTable', 'slots', 'patch-slots-table.sql' ],
                        [ 'addTable', 'content', 'patch-content-table.sql' ],
-                       [ 'addTable', 'content_moddels', 'patch-content_models-table.sql' ],
+                       [ 'addTable', 'content_models', 'patch-content_models-table.sql' ],
                        [ 'addTable', 'slot_roles', 'patch-slot_roles-table.sql' ],
                ];
        }
index f887ada..4f65e4d 100644 (file)
@@ -56,6 +56,9 @@ class Command {
        /** @var string */
        private $method;
 
+       /** @var string|null */
+       private $inputString;
+
        /** @var bool */
        private $doIncludeStderr = false;
 
@@ -189,6 +192,18 @@ class Command {
                return $this;
        }
 
+       /**
+        * Sends the provided input to the command.
+        * When set to null (default), the command will use the standard input.
+        * @param string|null $inputString
+        * @return $this
+        */
+       public function input( $inputString ) {
+               $this->inputString = is_null( $inputString ) ? null : (string)$inputString;
+
+               return $this;
+       }
+
        /**
         * Controls whether stderr should be included in stdout, including errors from limit.sh.
         * Default: don't include.
@@ -350,7 +365,7 @@ class Command {
                }
 
                $desc = [
-                       0 => [ 'file', 'php://stdin', 'r' ],
+                       0 => $this->inputString === null ? [ 'file', 'php://stdin', 'r' ] : [ 'pipe', 'r' ],
                        1 => [ 'pipe', 'w' ],
                        2 => [ 'pipe', 'w' ],
                ];
@@ -364,8 +379,13 @@ class Command {
                        $this->logger->error( "proc_open() failed: {command}", [ 'command' => $cmd ] );
                        throw new ProcOpenError();
                }
-               $outBuffer = $logBuffer = '';
-               $errBuffer = null;
+
+               $buffers = [
+                       0 => $this->inputString, // input
+                       1 => '', // stdout
+                       2 => null, // stderr
+                       3 => '', // log
+               ];
                $emptyArray = [];
                $status = false;
                $logMsg = false;
@@ -399,8 +419,6 @@ class Command {
                                }
                        }
 
-                       $readyPipes = $pipes;
-
                        // clear get_last_error without actually raising an error
                        // from http://php.net/manual/en/function.error-get-last.php#113518
                        // TODO replace with clear_last_error when requirements are bumped to PHP7
@@ -410,8 +428,17 @@ class Command {
                        @trigger_error( '' );
                        restore_error_handler();
 
+                       $readPipes = wfArrayFilterByKey( $pipes, function ( $fd ) use ( $desc ) {
+                               return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'r';
+                       } );
+                       $writePipes = wfArrayFilterByKey( $pipes, function ( $fd ) use ( $desc ) {
+                               return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'w';
+                       } );
+                       // stream_select parameter names are from the POV of us being able to do the operation;
+                       // proc_open desriptor types are from the POV of the process doing it.
+                       // So $writePipes is passed as the $read parameter and $readPipes as $write.
                        // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
-                       $numReadyPipes = @stream_select( $readyPipes, $emptyArray, $emptyArray, $timeout );
+                       $numReadyPipes = @stream_select( $writePipes, $readPipes, $emptyArray, $timeout );
                        if ( $numReadyPipes === false ) {
                                $error = error_get_last();
                                if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
@@ -422,31 +449,36 @@ class Command {
                                        break;
                                }
                        }
-                       foreach ( $readyPipes as $fd => $pipe ) {
-                               $block = fread( $pipe, 65536 );
-                               if ( $block === '' ) {
+                       foreach ( $writePipes + $readPipes as $fd => $pipe ) {
+                               // True if a pipe is unblocked for us to write into, false if for reading from
+                               $isWrite = array_key_exists( $fd, $readPipes );
+
+                               if ( $isWrite ) {
+                                       $res = fwrite( $pipe, $buffers[$fd], 65536 );
+                               } else {
+                                       $res = fread( $pipe, 65536 );
+                               }
+
+                               if ( $res === false ) {
+                                       $logMsg = 'Error ' . ( $isWrite ? 'writing to' : 'reading from' ) . ' pipe';
+                                       break 2;
+                               }
+
+                               if ( $res === '' || $res === 0 ) {
                                        // End of file
                                        fclose( $pipes[$fd] );
                                        unset( $pipes[$fd] );
                                        if ( !$pipes ) {
                                                break 2;
                                        }
-                               } elseif ( $block === false ) {
-                                       // Read error
-                                       $logMsg = "Error reading from pipe";
-                                       break 2;
-                               } elseif ( $fd == 1 ) {
-                                       // From stdout
-                                       $outBuffer .= $block;
-                               } elseif ( $fd == 2 ) {
-                                       // From stderr
-                                       $errBuffer .= $block;
-                               } elseif ( $fd == 3 ) {
-                                       // From log FD
-                                       $logBuffer .= $block;
-                                       if ( strpos( $block, "\n" ) !== false ) {
-                                               $lines = explode( "\n", $logBuffer );
-                                               $logBuffer = array_pop( $lines );
+                               } elseif ( $isWrite ) {
+                                       $buffers[$fd] = substr( $buffers[$fd], $res );
+                               } else {
+                                       $buffers[$fd] .= $res;
+                                       if ( $fd === 3 && strpos( $res, "\n" ) !== false ) {
+                                               // For the log FD, every line is a separate log entry.
+                                               $lines = explode( "\n", $buffers[3] );
+                                               $buffers[3] = array_pop( $lines );
                                                foreach ( $lines as $line ) {
                                                        $this->logger->info( $line );
                                                }
@@ -491,15 +523,15 @@ class Command {
                        $this->logger->warning( "$logMsg: {command}", [ 'command' => $cmd ] );
                }
 
-               if ( $errBuffer && $this->doLogStderr ) {
+               if ( $buffers[2] && $this->doLogStderr ) {
                        $this->logger->error( "Error running {command}: {error}", [
                                'command' => $cmd,
-                               'error' => $errBuffer,
+                               'error' => $buffers[2],
                                'exitcode' => $retval,
                                'exception' => new Exception( 'Shell error' ),
                        ] );
                }
 
-               return new Result( $retval, $outBuffer, $errBuffer );
+               return new Result( $retval, $buffers[1], $buffers[2] );
        }
 }
index 05463db..d57bf4f 100644 (file)
@@ -31,6 +31,7 @@ use MediaWiki\MediaWikiServices;
  *
  * Use call chaining with this class for expressiveness:
  *  $result = Shell::command( 'some command' )
+ *       ->input( 'foo' )
  *       ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
  *       ->limits( [ 'time' => 300 ] )
  *       ->execute();
index 1d83f51..cdcdd24 100644 (file)
@@ -113,6 +113,7 @@ class ComplexNamespaceInputWidget extends \OOUI\Widget {
                                )
                        )
                );
+               $config['namespace']['dropdown']['$overlay'] = true;
                return parent::getConfig( $config );
        }
 }
index 912537a..aec6619 100644 (file)
@@ -60,7 +60,9 @@ class ComplexTitleInputWidget extends \OOUI\Widget {
 
        public function getConfig( &$config ) {
                $config['namespace'] = $this->config['namespace'];
+               $config['namespace']['dropdown']['$overlay'] = true;
                $config['title'] = $this->config['title'];
+               $config['title']['$overlay'] = true;
                return parent::getConfig( $config );
        }
 }
index b516331..0231cac 100644 (file)
@@ -25,7 +25,6 @@ class DateInputWidget extends \OOUI\TextInputWidget {
        protected $precision = null;
        protected $mustBeAfter = null;
        protected $mustBeBefore = null;
-       protected $overlay = null;
 
        /**
         * @param array $config Configuration options
@@ -52,11 +51,6 @@ class DateInputWidget extends \OOUI\TextInputWidget {
         *   In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
         * @param string $config['mustBeBefore'] Validates the date to be before this.
         *   In the 'YYYY-MM-DD' or 'YYYY-MM' format, depending on `precision`.
-        * @param string $config['overlay'] The jQuery selector for the overlay layer on which to render
-        *   the calendar. This configuration is useful in cases where the expanded calendar is larger
-        *   than its container. The specified overlay layer is usually on top of the container and has
-        *   a larger area. Applicable only if the widget is infused. By default, the calendar uses
-        *   relative positioning.
         */
        public function __construct( array $config = [] ) {
                $config = array_merge( [
@@ -90,9 +84,6 @@ class DateInputWidget extends \OOUI\TextInputWidget {
                if ( isset( $config['placeholderLabel'] ) ) {
                        $this->placeholderLabel = $config['placeholderLabel'];
                }
-               if ( isset( $config['overlay'] ) ) {
-                       $this->overlay = $config['overlay'];
-               }
 
                // Set up placeholder text visible if the browser doesn't override it (logic taken from JS)
                if ( $this->placeholderDateFormat !== null ) {
@@ -159,9 +150,7 @@ class DateInputWidget extends \OOUI\TextInputWidget {
                if ( $this->mustBeBefore !== null ) {
                        $config['mustBeBefore'] = $this->mustBeBefore;
                }
-               if ( $this->overlay !== null ) {
-                       $config['overlay'] = $this->overlay;
-               }
+               $config['$overlay'] = true;
                return parent::getConfig( $config );
        }
 
index 5fdc710..c638891 100644 (file)
@@ -60,6 +60,7 @@ class NamespaceInputWidget extends \OOUI\DropdownInputWidget {
                $config['includeAllValue'] = $this->includeAllValue;
                $config['exclude'] = $this->exclude;
                // Skip DropdownInputWidget's getConfig(), we don't need 'options' config
+               $config['dropdown']['$overlay'] = true;
                return \OOUI\InputWidget::getConfig( $config );
        }
 }
index 70b0dcc..e2428ba 100644 (file)
@@ -68,6 +68,7 @@ class SearchInputWidget extends TitleInputWidget {
                if ( $this->dataLocation ) {
                        $config['dataLocation'] = $this->dataLocation;
                }
+               $config['$overlay'] = true;
                return parent::getConfig( $config );
        }
 }
index 3abfbd0..45dd1aa 100644 (file)
@@ -58,6 +58,7 @@ class SelectWithInputWidget extends \OOUI\Widget {
        public function getConfig( &$config ) {
                $config['textinput'] = $this->config['textinput'];
                $config['dropdowninput'] = $this->config['dropdowninput'];
+               $config['dropdowninput']['dropdown']['$overlay'] = true;
                $config['or'] = $this->config['or'];
                return parent::getConfig( $config );
        }
index a29c3dc..15f48e5 100644 (file)
@@ -75,6 +75,7 @@ class TitleInputWidget extends \OOUI\TextInputWidget {
                if ( $this->validateTitle !== null ) {
                        $config['validateTitle'] = $this->validateTitle;
                }
+               $config['$overlay'] = true;
                return parent::getConfig( $config );
        }
 }
index a058ab6..9385b48 100644 (file)
@@ -25,4 +25,9 @@ class UserInputWidget extends \OOUI\TextInputWidget {
        protected function getJavaScriptClassName() {
                return 'mw.widgets.UserInputWidget';
        }
+
+       public function getConfig( &$config ) {
+               $config['$overlay'] = true;
+               return parent::getConfig( $config );
+       }
 }
index ea32058..5c4a91f 100644 (file)
@@ -61,6 +61,7 @@ class UsersMultiselectWidget extends \OOUI\Widget {
                        $config['placeholder'] = $this->inputPlaceholder;
                }
 
+               $config['$overlay'] = true;
                return parent::getConfig( $config );
        }
 
index 18c546d..5e206ef 100644 (file)
        "pageinfo-category-subcats": "Number of subcategories",
        "pageinfo-category-files": "Number of files",
        "pageinfo-user-id": "User ID",
+       "pageinfo-file-hash": "Hash value",
        "markaspatrolleddiff": "Mark as patrolled",
        "markaspatrolledlink": "[$1]",
        "markaspatrolledtext": "Mark this page as patrolled",
index 0659453..30df1bd 100644 (file)
        "pageinfo-category-subcats": "See also:\n* {{msg-mw|Pageinfo-category-pages}}\n* {{msg-mw|Pageinfo-category-files}}",
        "pageinfo-category-files": "See also:\n* {{msg-mw|Pageinfo-category-pages}}\n* {{msg-mw|Pageinfo-category-subcats}}",
        "pageinfo-user-id": "The numeric ID for a user\n{{Identical|User ID}}",
+       "pageinfo-file-hash": "Base-36 SHA-1 value of the file",
        "markaspatrolleddiff": "{{doc-actionlink}}\nSee also:\n* {{msg-mw|Markaspatrolledtext}}\n{{Identical|Mark as patrolled}}",
        "markaspatrolledlink": "{{notranslate}}\nParameters:\n* $1 - link which has text {{msg-mw|Markaspatrolledtext}}",
        "markaspatrolledtext": "{{doc-actionlink}}\nSee also:\n* {{msg-mw|Markaspatrolleddiff}}",
index 9d2e93b..2265132 100644 (file)
@@ -89,7 +89,7 @@
         *     calendar uses relative positioning.
         */
        mw.widgets.DateInputWidget = function MWWDateInputWidget( config ) {
-               var placeholderDateFormat, mustBeAfter, mustBeBefore;
+               var placeholderDateFormat, mustBeAfter, mustBeBefore, $overlay;
 
                // Config initialization
                config = $.extend( {
                        .addClass( 'mw-widget-dateInputWidget' )
                        .append( this.$handle, this.textInput.$element, this.calendar.$element );
 
-               // config.overlay is the selector to be used for config.$overlay, specified from PHP
-               if ( config.overlay ) {
-                       config.$overlay = $( config.overlay );
-               }
+               $overlay = config.$overlay === true ? OO.ui.getDefaultOverlay() : config.$overlay;
 
-               if ( config.$overlay ) {
+               if ( $overlay ) {
                        this.calendar.setFloatableContainer( this.$element );
-                       config.$overlay.append( this.calendar.$element );
+                       $overlay.append( this.calendar.$element );
 
                        // The text input and calendar are not in DOM order, so fix up focus transitions.
                        this.textInput.$input.on( 'keydown', function ( e ) {
index 37c0554..d295ca7 100644 (file)
@@ -64,6 +64,7 @@
                        } );
                } );
                capsulesWidget = new OO.ui.CapsuleMultiselectWidget( {
+                       $overlay: true,
                        menu: {
                                items: capsulesOptions
                        }
index b7d3205..5b12407 100644 (file)
@@ -13,7 +13,7 @@ class ApiClearHasMsgTest extends ApiTestCase {
        public function testClearFlag() {
                $user = self::$users['sysop']->getUser();
                $user->setNewtalk( true );
-               $this->assertTrue( $user->getNewtalk() );
+               $this->assertTrue( $user->getNewtalk(), 'sanity check' );
 
                $data = $this->doApiRequest( [ 'action' => 'clearhasmsg' ], [] );
 
index 2bafa03..2ba7bdc 100644 (file)
@@ -154,4 +154,21 @@ class CommandTest extends PHPUnit_Framework_TestCase {
                $this->assertSame( 1, count( $logger->getBuffer() ) );
                $this->assertSame( trim( $logger->getBuffer()[0][2]['error'] ), 'ThisIsStderr' );
        }
+
+       public function testInput() {
+               $this->requirePosix();
+
+               $command = new Command();
+               $command->params( 'cat' );
+               $command->input( 'abc' );
+               $result = $command->execute();
+               $this->assertSame( 'abc', $result->getStdout() );
+
+               // now try it with something that does not fit into a single block
+               $command = new Command();
+               $command->params( 'cat' );
+               $command->input( str_repeat( '!', 1000000 ) );
+               $result = $command->execute();
+               $this->assertSame( 1000000, strlen( $result->getStdout() ) );
+       }
 }