Umherirrender has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/356940 )
Change subject: Add phpcs and make pass ...................................................................... Add phpcs and make pass Change-Id: I978a61841ba6429ce05df5a008c8c8415273e6ec --- M ApiShortenUrl.php M SpecialUrlShortener.php M UrlShortener.alias.php M UrlShortener.hooks.php M UrlShortener.notranslate-alias.php M UrlShortener.utils.php M composer.json M maintenance/dumpURLs.php A phpcs.xml M tests/phpunit/UrlShortenerUtilsTest.php 10 files changed, 118 insertions(+), 109 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UrlShortener refs/changes/40/356940/1 diff --git a/ApiShortenUrl.php b/ApiShortenUrl.php index 31575c9..be5cfc3 100644 --- a/ApiShortenUrl.php +++ b/ApiShortenUrl.php @@ -40,7 +40,7 @@ $this->getMain()->setCacheMaxAge( UrlShortenerUtils::CACHE_TIME ); $this->getResult()->addValue( null, $this->getModuleName(), - array( 'shorturl' => $shortUrl ) + [ 'shorturl' => $shortUrl ] ); } @@ -50,18 +50,17 @@ } protected function getAllowedParams() { - return array( - 'url' => array( + return [ + 'url' => [ ApiBase::PARAM_REQUIRED => true, - ) - ); + ] + ]; } public function getExamplesMessages() { - return array( + return [ 'action=shortenurl&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FArctica' => 'apihelp-shortenurl-example-1', - ); + ]; } } - diff --git a/SpecialUrlShortener.php b/SpecialUrlShortener.php index 5ce0c11..6fb5681 100644 --- a/SpecialUrlShortener.php +++ b/SpecialUrlShortener.php @@ -10,7 +10,7 @@ */ if ( !defined( 'MEDIAWIKI' ) ) { - echo( 'not a valid entry point.\n' ); + echo 'not a valid entry point.\n'; die( 1 ); } @@ -41,7 +41,7 @@ $domains = $wgUrlShortenerApprovedDomains; } else { $parsed = wfParseUrl( $wgServer ); - $domains = array( $parsed['host'] ); + $domains = [ $parsed['host'] ]; } $lang = $this->getLanguage(); @@ -61,12 +61,11 @@ $form->setSubmitTextMsg( 'urlshortener-url-input-submit' ); $form->setFooterText( $this->getApprovedDomainsMessage()->parse() ); $this->getOutput()->addModules( 'ext.urlShortener.special' ); - $this->getOutput()->addJsConfigVars( array( + $this->getOutput()->addJsConfigVars( [ 'wgUrlShortenerDomainsWhitelist' => UrlShortenerUtils::getWhitelistRegex(), 'wgUrlShortenerAllowArbitraryPorts' => $wgUrlShortenerAllowArbitraryPorts, - ) ); + ] ); } - /** * Validate the URL to ensure that we are allowed to create a shorturl for this. @@ -87,15 +86,14 @@ return $validity_check->text(); } - /** * Generate the form used to input the URL to shorten. * @return array A form defintion that can be used by HTMLForm */ protected function getFormFields() { - return array( - 'url' => array( - 'validation-callback' => array( $this, 'validateURL' ), + return [ + 'url' => [ + 'validation-callback' => [ $this, 'validateURL' ], 'required' => true, 'type' => 'url', 'name' => 'url', @@ -103,8 +101,8 @@ 'autofocus' => true, 'id' => 'mw-urlshortener-url-input', 'placeholder' => $this->msg( 'urlshortener-url-input-label' )->text() - ), - ); + ], + ]; } /** @@ -126,10 +124,10 @@ if ( !$status->isOK() ) { return $status; } - $html = new OOUI\TextInputWidget( array( + $html = new OOUI\TextInputWidget( [ 'value' => UrlShortenerUtils::makeUrl( $status->getValue() ), 'readOnly' => true, - ) ); + ] ); $out->addHTML( $html ); return true; } diff --git a/UrlShortener.alias.php b/UrlShortener.alias.php index 21a7640..0e79e3b 100644 --- a/UrlShortener.alias.php +++ b/UrlShortener.alias.php @@ -5,46 +5,45 @@ * @file * @ingroup Extensions */ -// @codingStandardsIgnoreFile -$specialPageAliases = array(); +$specialPageAliases = []; /** English (English) */ -$specialPageAliases['en'] = array( - 'UrlShortener' => array( 'UrlShortener' ), -); +$specialPageAliases['en'] = [ + 'UrlShortener' => [ 'UrlShortener' ], +]; /** Arabic (العربية) */ -$specialPageAliases['ar'] = array( - 'UrlShortener' => array( 'تقصير_المسار' ), -); +$specialPageAliases['ar'] = [ + 'UrlShortener' => [ 'تقصير_المسار' ], +]; /** Egyptian Arabic (مصرى) */ -$specialPageAliases['arz'] = array( - 'UrlShortener' => array( 'تقصير_المسار' ), -); +$specialPageAliases['arz'] = [ + 'UrlShortener' => [ 'تقصير_المسار' ], +]; /** German (Deutsch) */ -$specialPageAliases['de'] = array( - 'UrlShortener' => array( 'URL-Kürzung' ), -); +$specialPageAliases['de'] = [ + 'UrlShortener' => [ 'URL-Kürzung' ], +]; /** Hebrew (עברית) */ -$specialPageAliases['he'] = array( - 'UrlShortener' => array( 'מקצר_כתובות' ), -); +$specialPageAliases['he'] = [ + 'UrlShortener' => [ 'מקצר_כתובות' ], +]; /** Macedonian (македонски) */ -$specialPageAliases['mk'] = array( - 'UrlShortener' => array( 'СкратувачНаURL' ), -); +$specialPageAliases['mk'] = [ + 'UrlShortener' => [ 'СкратувачНаURL' ], +]; /** Simplified Chinese (中文(简体)) */ -$specialPageAliases['zh-hans'] = array( - 'UrlShortener' => array( 'URL缩短程序' ), -); +$specialPageAliases['zh-hans'] = [ + 'UrlShortener' => [ 'URL缩短程序' ], +]; /** Traditional Chinese (中文(繁體)) */ -$specialPageAliases['zh-hant'] = array( - 'UrlShortener' => array( 'URL縮短程式' ), -); \ No newline at end of file +$specialPageAliases['zh-hant'] = [ + 'UrlShortener' => [ 'URL縮短程式' ], +]; diff --git a/UrlShortener.hooks.php b/UrlShortener.hooks.php index f945c0b..c4fd079 100644 --- a/UrlShortener.hooks.php +++ b/UrlShortener.hooks.php @@ -20,7 +20,7 @@ // If a template is set, and it is not the root, register it if ( $wgUrlShortenerTemplate && $wgUrlShortenerTemplate !== '/$1' ) { $router->add( $wgUrlShortenerTemplate, - array( 'title' => SpecialPage::getTitleFor( 'UrlRedirector', '$1' )->getPrefixedText() ) + [ 'title' => SpecialPage::getTitleFor( 'UrlRedirector', '$1' )->getPrefixedText() ] ); } return true; @@ -58,12 +58,12 @@ } $linkToShorten = $skin->getTitle()->getFullURL( $query, false, PROTO_CANONICAL ); $link = SpecialPage::getTitleFor( 'UrlShortener' ) - ->getLocalURL( array( 'url' => $linkToShorten ) ); - $toolbox['urlshortener'] = array( + ->getLocalURL( [ 'url' => $linkToShorten ] ); + $toolbox['urlshortener'] = [ 'id' => 't-urlshortener', 'href' => $link, 'msg' => 'urlshortener-toolbox' - ); + ]; } /** diff --git a/UrlShortener.notranslate-alias.php b/UrlShortener.notranslate-alias.php index 53bf5e2..6f1577a 100644 --- a/UrlShortener.notranslate-alias.php +++ b/UrlShortener.notranslate-alias.php @@ -5,11 +5,10 @@ * @file * @ingroup Extensions */ -// @codingStandardsIgnoreFile -$specialPageAliases = array(); +$specialPageAliases = []; /** English (English) */ -$specialPageAliases['en'] = array( - 'UrlRedirector' => array( 'UrlRedirector' ), -); \ No newline at end of file +$specialPageAliases['en'] = [ + 'UrlRedirector' => [ 'UrlRedirector' ], +]; diff --git a/UrlShortener.utils.php b/UrlShortener.utils.php index eed8b49..12658af 100755 --- a/UrlShortener.utils.php +++ b/UrlShortener.utils.php @@ -42,7 +42,7 @@ $id = $dbw->selectField( 'urlshortcodes', 'usc_id', - array( 'usc_url_hash' => md5( $url ) ), + [ 'usc_url_hash' => md5( $url ) ], __METHOD__ ); if ( $id === false ) { @@ -57,11 +57,11 @@ return Status::newFatal( 'urlshortener-disabled' ); } - $rowData = array( + $rowData = [ 'usc_url' => $url, 'usc_url_hash' => md5( $url ) - ); - $dbw->insert( 'urlshortcodes', $rowData, __METHOD__, array( 'IGNORE' ) ); + ]; + $dbw->insert( 'urlshortcodes', $rowData, __METHOD__, [ 'IGNORE' ] ); if ( $dbw->affectedRows() ) { $id = $dbw->insertId(); @@ -70,9 +70,9 @@ $id = $dbw->selectField( 'urlshortcodes', 'usc_id', - array( 'usc_url_hash' => md5( $url ) ), + [ 'usc_url_hash' => md5( $url ) ], __METHOD__, - array( 'LOCK IN SHARE MODE' ) // ignore snapshot + [ 'LOCK IN SHARE MODE' ] // ignore snapshot ); } } @@ -143,7 +143,7 @@ $url = $dbr->selectField( 'urlshortcodes', 'usc_url', - array( 'usc_id' => $id ), + [ 'usc_id' => $id ], __METHOD__ ); @@ -164,7 +164,7 @@ ? wfGetLBFactory()->getExternalLB( $wgUrlShortenerDBCluster ) : wfGetLB( $wgUrlShortenerDBName ); - return $lb->getConnectionRef( $type, array(), $wgUrlShortenerDBName ); + return $lb->getConnectionRef( $type, [], $wgUrlShortenerDBName ); } /** @@ -202,7 +202,9 @@ } else { // Collapse the whitelist into a single string, so we have to run regex check only once $domainsWhitelist = implode( '|', array_map( - function( $item ) { return '^' . $item . '$'; }, + function( $item ) { + return '^' . $item . '$'; + }, $wgUrlShortenerDomainsWhitelist ) ); } @@ -276,13 +278,13 @@ $n = strlen( $wgUrlShortenerIdSet ); if ( self::$decodeMap === null ) { - self::$decodeMap = array(); + self::$decodeMap = []; for ( $i = 0; $i < $n; $i++ ) { self::$decodeMap[$wgUrlShortenerIdSet[$i]] = $i; } } $x = 0; - for ( $i = 0; $i < strlen( $s ); $i++ ) { + for ( $i = 0, $len = strlen( $s ); $i < $len; $i++ ) { $x *= $n; if ( isset( self::$decodeMap[$s[$i]] ) ) { $x += self::$decodeMap[$s[$i]]; diff --git a/composer.json b/composer.json index 686b65b..b8f68fb 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,14 @@ { "require-dev": { "jakub-onderka/php-parallel-lint": "0.9.2", - "jakub-onderka/php-console-highlighter": "0.3.2" + "jakub-onderka/php-console-highlighter": "0.3.2", + "mediawiki/mediawiki-codesniffer": "0.7.2" }, "scripts": { + "fix": "phpcbf", "test": [ - "parallel-lint . --exclude vendor" + "parallel-lint . --exclude vendor", + "phpcs -p -s" ] } } diff --git a/maintenance/dumpURLs.php b/maintenance/dumpURLs.php index 6e3a3df..799380e 100644 --- a/maintenance/dumpURLs.php +++ b/maintenance/dumpURLs.php @@ -33,10 +33,10 @@ $text = ''; $rows = $dbr->select( 'urlshortcodes', - array( 'usc_url', 'usc_id' ), - array( 'usc_id > ' . $dbr->addQuotes( $id ) ), + [ 'usc_url', 'usc_id' ], + [ 'usc_id > ' . $dbr->addQuotes( $id ) ], __METHOD__, - array( 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'usc_id ASC' ) + [ 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'usc_id ASC' ] ); foreach ( $rows as $row ) { $shortCode = UrlShortenerUtils::encodeId( $row->usc_id ); diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..4ffde0b --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ruleset> + <rule ref="vendor/mediawiki/mediawiki-codesniffer/MediaWiki"/> + <file>.</file> + <arg name="extensions" value="php,php5,inc"/> + <arg name="encoding" value="UTF-8"/> + <exclude-pattern>vendor</exclude-pattern> + <exclude-pattern>node_modules</exclude-pattern> +</ruleset> diff --git a/tests/phpunit/UrlShortenerUtilsTest.php b/tests/phpunit/UrlShortenerUtilsTest.php index a6af289..20a09c6 100644 --- a/tests/phpunit/UrlShortenerUtilsTest.php +++ b/tests/phpunit/UrlShortenerUtilsTest.php @@ -7,43 +7,43 @@ * @covers UrlShortenerUtils::convertToProtocol */ public function testConvertToProtocol( $input, $proto, $expected ) { - $this->setMwGlobals( array( 'wgScript' => '/w' ) ); + $this->setMwGlobals( [ 'wgScript' => '/w' ] ); $this->assertEquals( $expected, UrlShortenerUtils::convertToProtocol( $input, $proto ) ); } public static function provideConvertToProtocol() { - return array( - array( + return [ + [ 'https://example.org/foo?query=bar', PROTO_HTTP, 'http://example.org/foo?query=bar' - ), - array( + ], + [ 'http://example.org/foo?query=bar', PROTO_HTTP, 'http://example.org/foo?query=bar' - ), - array( + ], + [ '//example.org/foo?query=bar', PROTO_HTTP, 'http://example.org/foo?query=bar' - ), - array( + ], + [ 'http://example.org/foo?query=bar', PROTO_HTTPS, 'https://example.org/foo?query=bar' - ), - array( + ], + [ 'http://example.org/foo?query=bar', PROTO_RELATIVE, '//example.org/foo?query=bar' - ), - array( + ], + [ 'https://example.org/foo?query=bar', PROTO_RELATIVE, '//example.org/foo?query=bar' - ), - ); + ], + ]; } /** @@ -51,60 +51,60 @@ * @covers URLShortenerUtils::normalizeUrl */ public function testNormalizeUrl( $url, $expected ) { - $this->setMwGlobals( array( + $this->setMwGlobals( [ 'wgArticlePath' => '/wiki/$1', 'wgScript' => '/w/index.php', - ) ); + ] ); $this->assertEquals( $expected, UrlShortenerUtils::normalizeUrl( $url ) ); } public static function provideNormalizeUrl() { - return array( + return [ // HTTPS -> HTTP - array( + [ 'https://example.org', 'http://example.org' - ), + ], // Article normalized - array( + [ 'http://example.com/w/index.php?title=Main_Page', 'http://example.com/wiki/Main_Page' - ), + ], // Already normalized - array( + [ 'http://example.com/wiki/Special:Version', 'http://example.com/wiki/Special:Version' - ), + ], // Special page normalized - array( + [ 'http://example.com/w/index.php?title=Special:Version', 'http://example.com/wiki/Special:Version' - ), + ], // API not normalized - array( + [ 'http://example.com/w/api.php?action=query', 'http://example.com/w/api.php?action=query' - ), + ], // Random query parameter not normalized - array( + [ 'http://example.com/w/index.php.php?foo=bar', 'http://example.com/w/index.php.php?foo=bar' - ), + ], // Additional parameter not normalized - array( + [ 'http://example.com/w/index.php?title=Special:Version&baz=bar', 'http://example.com/w/index.php?title=Special:Version&baz=bar' - ), + ], // urldecoded - array( + [ 'http://example.org/wiki/Scott_Morrison_%28politician%29', 'http://example.org/wiki/Scott_Morrison_(politician)' - ), - array( + ], + [ 'http://example.org/wiki/Scott_Morrison_(politician)', 'http://example.org/wiki/Scott_Morrison_(politician)' - ), - ); + ], + ]; } /** -- To view, visit https://gerrit.wikimedia.org/r/356940 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I978a61841ba6429ce05df5a008c8c8415273e6ec Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/UrlShortener Gerrit-Branch: master Gerrit-Owner: Umherirrender <umherirrender_de...@web.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits