Merge "language: Document sources of data and example usage"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 20 Jul 2018 06:12:44 +0000 (06:12 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 20 Jul 2018 06:12:44 +0000 (06:12 +0000)
includes/libs/CSSMin.php
languages/Language.php
tests/parser/ParserTestRunner.php
tests/parser/parserTests.php
tests/phpunit/includes/libs/CSSMinTest.php

index 74e8b54..92a4f9e 100644 (file)
@@ -72,15 +72,24 @@ class CSSMin {
                                $url = $match['file'][0];
 
                                // Skip fully-qualified and protocol-relative URLs and data URIs
-                               // Also skips the rare `behavior` property specifying application's default behavior
                                if (
                                        substr( $url, 0, 2 ) === '//' ||
-                                       parse_url( $url, PHP_URL_SCHEME ) ||
-                                       substr( $url, 0, 9 ) === '#default#'
+                                       parse_url( $url, PHP_URL_SCHEME )
                                ) {
                                        break;
                                }
 
+                               // Strip trailing anchors - T115436
+                               $anchor = strpos( $url, '#' );
+                               if ( $anchor !== false ) {
+                                       $url = substr( $url, 0, $anchor );
+
+                                       // '#some-anchors' is not a file
+                                       if ( $url === '' ) {
+                                               break;
+                                       }
+                               }
+
                                $files[] = $path . $url;
                        }
                }
@@ -485,11 +494,11 @@ class CSSMin {
 
                // Pass thru fully-qualified and protocol-relative URLs and data URIs, as well as local URLs if
                // we can't expand them.
-               // Also skips the rare `behavior` property specifying application's default behavior
+               // Also skips anchors or the rare `behavior` property specifying application's default behavior
                if (
                        self::isRemoteUrl( $url ) ||
                        self::isLocalUrl( $url ) ||
-                       substr( $url, 0, 9 ) === '#default#'
+                       substr( $url, 0, 1 ) === '#'
                ) {
                        return $url;
                }
index 9792095..7f04a68 100644 (file)
@@ -3413,32 +3413,26 @@ class Language {
         * Take a list of strings and build a locale-friendly comma-separated
         * list, using the local comma-separator message.
         * The last two strings are chained with an "and".
-        * NOTE: This function will only work with standard numeric array keys (0, 1, 2…)
         *
-        * @param string[] $l
+        * @param string[] $list
         * @return string
         */
-       function listToText( array $l ) {
-               $m = count( $l ) - 1;
-               if ( $m < 0 ) {
+       public function listToText( array $list ) {
+               $itemCount = count( $list );
+               if ( $itemCount < 1 ) {
                        return '';
                }
-               if ( $m > 0 ) {
+               $text = array_pop( $list );
+               if ( $itemCount > 1 ) {
                        $and = $this->msg( 'and' )->escaped();
                        $space = $this->msg( 'word-separator' )->escaped();
-                       if ( $m > 1 ) {
+                       $comma = '';
+                       if ( $itemCount > 2 ) {
                                $comma = $this->msg( 'comma-separator' )->escaped();
                        }
+                       $text = implode( $comma, $list ) . $and . $space . $text;
                }
-               $s = $l[$m];
-               for ( $i = $m - 1; $i >= 0; $i-- ) {
-                       if ( $i == $m - 1 ) {
-                               $s = $l[$i] . $and . $space . $s;
-                       } else {
-                               $s = $l[$i] . $comma . $s;
-                       }
-               }
-               return $s;
+               return $text;
        }
 
        /**
index ceee432..68bc61e 100644 (file)
@@ -116,6 +116,30 @@ class ParserTestRunner {
         */
        private $normalizationFunctions = [];
 
+       /**
+        * Run disabled parser tests
+        * @var bool
+        */
+       private $runDisabled;
+
+       /**
+        * Run tests intended only for parsoid
+        * @var bool
+        */
+       private $runParsoid;
+
+       /**
+        * Disable parse on article insertion
+        * @var bool
+        */
+       private $disableSaveParse;
+
+       /**
+        * Reuse upload directory
+        * @var bool
+        */
+       private $keepUploads;
+
        /**
         * @param TestRecorder $recorder
         * @param array $options
@@ -148,6 +172,8 @@ class ParserTestRunner {
                $this->runDisabled = !empty( $options['run-disabled'] );
                $this->runParsoid = !empty( $options['run-parsoid'] );
 
+               $this->disableSaveParse = !empty( $options['disable-save-parse'] );
+
                $this->tidySupport = new TidySupport( !empty( $options['use-tidy-config'] ) );
                if ( !$this->tidySupport->isEnabled() ) {
                        $this->recorder->warning(
@@ -1184,7 +1210,8 @@ class ParserTestRunner {
                        'site_stats', 'ipblocks', 'image', 'oldimage',
                        'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
                        'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
-                       'archive', 'user_groups', 'page_props', 'category'
+                       'archive', 'user_groups', 'page_props', 'category',
+                       'slots', 'content', 'slot_roles', 'content_models',
                ];
 
                if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) {
@@ -1451,7 +1478,6 @@ class ParserTestRunner {
                $this->checkSetupDone( 'setupDatabase' );
 
                $this->dbClone->destroy();
-               $this->databaseSetupDone = false;
 
                if ( $this->useTemporaryTables ) {
                        if ( $this->db->getType() == 'sqlite' ) {
@@ -1651,11 +1677,15 @@ class ParserTestRunner {
                        );
                }
 
-               // Use mock parser, to make debugging of actual parser tests simpler.
+               // Optionally use mock parser, to make debugging of actual parser tests simpler.
                // But initialise the MessageCache clone first, don't let MessageCache
                // get a reference to the mock object.
-               MessageCache::singleton()->getParser();
-               $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser ] );
+               if ( $this->disableSaveParse ) {
+                       MessageCache::singleton()->getParser();
+                       $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser ] );
+               } else {
+                       $restore = false;
+               }
                try {
                        $status = $page->doEditContent(
                                $newContent,
@@ -1663,7 +1693,9 @@ class ParserTestRunner {
                                EDIT_NEW | EDIT_INTERNAL
                        );
                } finally {
-                       $restore();
+                       if ( $restore ) {
+                               $restore();
+                       }
                }
 
                if ( !$status->isOK() ) {
index 6a423d5..c01b416 100644 (file)
@@ -62,6 +62,8 @@ class ParserTestsMaintenance extends Maintenance {
                        'be used.', false, true );
                $this->addOption( 'run-disabled', 'run disabled tests' );
                $this->addOption( 'run-parsoid', 'run parsoid tests (normally disabled)' );
+               $this->addOption( 'disable-save-parse', 'Don\'t run the parser when ' .
+                       'inserting articles     into the database' );
                $this->addOption( 'dwdiff', 'Use dwdiff to display diff output' );
                $this->addOption( 'mark-ws', 'Mark whitespace in diffs by replacing it with symbols' );
                $this->addOption( 'norm', 'Apply a comma-separated list of normalization functions to ' .
@@ -180,6 +182,7 @@ class ParserTestsMaintenance extends Maintenance {
                        'keep-uploads' => $this->hasOption( 'keep-uploads' ),
                        'run-disabled' => $this->hasOption( 'run-disabled' ),
                        'run-parsoid' => $this->hasOption( 'run-parsoid' ),
+                       'disable-save-parse' => $this->hasOption( 'disable-save-parse' ),
                        'use-tidy-config' => $this->hasOption( 'use-tidy-config' ),
                        'file-backend' => $this->getOption( 'file-backend' ),
                        'upload-dir' => $this->getOption( 'upload-dir' ),
index c711291..04aecc9 100644 (file)
@@ -19,6 +19,30 @@ class CSSMinTest extends MediaWikiTestCase {
                ] );
        }
 
+       /**
+        * @dataProvider providesReferencedFiles
+        * @covers CSSMin::getLocalFileReferences
+        */
+       public function testGetLocalFileReferences( $input, $expected ) {
+               $output = CSSMin::getLocalFileReferences( $input, '/' );
+               $this->assertEquals(
+                       $expected,
+                       $output,
+                       'getLocalFileReferences() must find the local file properly'
+               );
+       }
+
+       public static function providesReferencedFiles() {
+               // input, array of expected local file names
+               return [
+                       [ 'url("//example.org")', [] ],
+                       [ 'url("https://example.org")', [] ],
+                       [ 'url("#default#")', [] ],
+                       [ 'url("WikiFont-Glyphs.svg#wikiglyph")', [ '/WikiFont-Glyphs.svg' ] ],
+                       [ 'url("#some-anchor")', [] ],
+               ];
+       }
+
        /**
         * @dataProvider provideSerializeStringValue
         * @covers CSSMin::serializeStringValue
@@ -292,6 +316,16 @@ class CSSMinTest extends MediaWikiTestCase {
                                [ 'foo { behavior: url(#default#bar); }', false, '/w/', false ],
                                'foo { behavior: url("#default#bar"); }',
                        ],
+                       [
+                               'Keeps anchors',
+                               [ 'url(#other)', false, '/', false ],
+                               'url("#other")'
+                       ],
+                       [
+                               'Keeps anchors after a path',
+                               [ 'url(images/file.svg#id)', false, '/', false ],
+                               'url("/images/file.svg#id")'
+                       ],
                ];
        }