From 74dfa5fdd993984454857d35a5de884a84ad2781 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 2 Nov 2017 17:14:52 +0800 Subject: [PATCH] Add composer for CI, and fix CS errors Also move class to its own namespace. Bug: T183532 Change-Id: I5a89efd3f162d73e247543c86967562a949a3bf4 --- .phpcs.xml | 5 ++ Description2.i18n.magic.php | 9 ++- Description2.php | 2 +- composer.json | 9 ++- extension.json | 13 ++-- .../Description2.php | 68 +++++++++++++------ 6 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 .phpcs.xml rename Description2.class.php => includes/Description2.php (53%) diff --git a/.phpcs.xml b/.phpcs.xml new file mode 100644 index 0000000..74e9072 --- /dev/null +++ b/.phpcs.xml @@ -0,0 +1,5 @@ + + + + . + diff --git a/Description2.i18n.magic.php b/Description2.i18n.magic.php index 5e5ea21..214e98c 100644 --- a/Description2.i18n.magic.php +++ b/Description2.i18n.magic.php @@ -6,12 +6,11 @@ * @ingroup Extensions */ -$magicWords = array(); +$magicWords = []; /** English * @author Daniel Friesen */ -$magicWords['en'] = array( - 'description2' => array( 0, 'description2' ), -); - +$magicWords['en'] = [ + 'description2' => [ 0, 'description2' ], +]; diff --git a/Description2.php b/Description2.php index 35fb4d4..924dd63 100644 --- a/Description2.php +++ b/Description2.php @@ -22,4 +22,4 @@ if ( function_exists( 'wfLoadExtension' ) ) { return; } else { die( 'This version of the Description2 extension requires MediaWiki 1.25+' ); -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 7d306d5..a094fa5 100644 --- a/composer.json +++ b/composer.json @@ -2,15 +2,18 @@ "require-dev": { "jakub-onderka/php-parallel-lint": "0.9.2", "jakub-onderka/php-console-highlighter": "0.3.2", - "mediawiki/minus-x": "0.2.1" + "mediawiki/minus-x": "0.2.1", + "mediawiki/mediawiki-codesniffer": "14.1.0" }, "scripts": { "test": [ "parallel-lint . --exclude vendor --exclude node_modules", - "minus-x check ." + "minus-x check .", + "phpcs -p -s" ], "fix": [ - "minus-x fix ." + "minus-x fix .", + "phpcbf" ] } } diff --git a/extension.json b/extension.json index 1cd1ba5..d11e414 100644 --- a/extension.json +++ b/extension.json @@ -1,6 +1,6 @@ { "name": "Description2", - "version": "0.4.0", + "version": "0.4.1", "author": [ "[http://danf.ca/mw/ Daniel Friesen]" ], @@ -11,8 +11,11 @@ "config": { "EnableMetaDescriptionFunctions": false }, + "ConfigRegistry": { + "Description2": "GlobalVarConfig::newInstance" + }, "AutoloadClasses": { - "Description2": "Description2.class.php" + "MediaWiki\\Extension\\Description2\\Description2": "includes/Description2.php" }, "ExtensionMessagesFiles": { "Description2Magic": "Description2.i18n.magic.php" @@ -23,9 +26,9 @@ ] }, "Hooks": { - "OutputPageParserOutput": "Description2::onOutputPageParserOutput", - "ParserAfterTidy": "Description2::onParserAfterTidy", - "ParserFirstCallInit": "Description2::onParserFirstCallInit" + "OutputPageParserOutput": "MediaWiki\\Extension\\Description2\\Description2::onOutputPageParserOutput", + "ParserAfterTidy": "MediaWiki\\Extension\\Description2\\Description2::onParserAfterTidy", + "ParserFirstCallInit": "MediaWiki\\Extension\\Description2\\Description2::onParserFirstCallInit" }, "manifest_version": 1 } diff --git a/Description2.class.php b/includes/Description2.php similarity index 53% rename from Description2.class.php rename to includes/Description2.php index c3a6683..e0a8b09 100644 --- a/Description2.class.php +++ b/includes/Description2.php @@ -1,4 +1,13 @@ tag to MW pages and into the parser output * @@ -13,8 +22,8 @@ class Description2 { /** - * @param Parser $parser - * @param string $desc + * @param Parser $parser The parser. + * @param string $desc The description text. */ public static function setDescription( Parser $parser, $desc ) { $parserOutput = $parser->getOutput(); @@ -25,16 +34,18 @@ class Description2 { } /** - * @param Parser $parser - * @param string $text + * @link https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy + * @param Parser &$parser The parser. + * @param string &$text The page text. * @return bool */ public static function onParserAfterTidy( Parser &$parser, &$text ) { $desc = ''; - $myText = preg_replace( '%]*+>(?:(?R)|[^<]*+(?:(?!%i', '', $text ); + $pattern = '%]*+>(?:(?R)|[^<]*+(?:(?!%i'; + $myText = preg_replace( $pattern, '', $text ); - $paragraphs = array(); + $paragraphs = []; if ( preg_match_all( '#

.*?

#is', $myText, $paragraphs ) ) { foreach ( $paragraphs[0] as $paragraph ) { $paragraph = trim( strip_tags( $paragraph ) ); @@ -54,47 +65,62 @@ class Description2 { } /** - * @param Parser $parser + * @param Parser &$parser The parser. * @return bool */ public static function onParserFirstCallInit( Parser &$parser ) { - global $wgEnableMetaDescriptionFunctions; - if ( !$wgEnableMetaDescriptionFunctions ) { + $config = MediaWikiServices::getInstance() + ->getConfigFactory() + ->makeConfig( 'Description2' ); + if ( !$config->get( 'EnableMetaDescriptionFunctions' ) ) { // Functions and tags are disabled return true; } - $parser->setFunctionHook( 'description2', array( 'Description2', 'parserFunctionCallback' ), Parser::SFH_OBJECT_ARGS ); - $parser->setFunctionTagHook( 'metadesc', array( 'Description2', 'tagCallback' ), Parser::SFH_OBJECT_ARGS ); + $parser->setFunctionHook( + 'description2', + [ static::class, 'parserFunctionCallback' ], + Parser::SFH_OBJECT_ARGS + ); + $parser->setFunctionTagHook( + 'metadesc', + [ static::class, 'tagCallback' ], + Parser::SFH_OBJECT_ARGS + ); return true; } /** - * @param Parser $parser - * @param $frame - * @param $args + * @param Parser $parser The parser. + * @param PPFrame $frame The frame. + * @param string[] $args The arguments of the parser function call. * @return string */ - public static function parserFunctionCallback( Parser $parser, $frame, $args ) { + public static function parserFunctionCallback( Parser $parser, PPFrame $frame, $args ) { $desc = isset( $args[0] ) ? $frame->expand( $args[0] ) : ''; self::setDescription( $parser, $desc ); return ''; } /** - * @param Parser $parser - * @param $frame - * @param $content - * @param $attributes + * @param Parser $parser The parser. + * @param PPFrame $frame Not used. + * @param string $content The contents of the tag (if any). + * @param string[] $attributes The tag attributes (if any). * @return string */ - public static function tagCallback( Parser $parser, $frame, $content, $attributes ) { - $desc = ( isset( $content ) ? $content : ( isset( $attributes['content'] ) ? $attributes['content'] : null ) ); + public static function tagCallback( Parser $parser, PPFrame $frame, $content, $attributes ) { + $contentAttr = isset( $attributes['content'] ) ? $attributes['content'] : null; + $desc = isset( $content ) ? $content : $contentAttr; if ( isset( $desc ) ) { self::setDescription( $parser, $desc ); } return ''; } + /** + * @param OutputPage &$out The output page to add the meta element to. + * @param ParserOutput $parserOutput The parser output to get the description from. + */ public static function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parserOutput ) { // Export the description from the main parser output into the OutputPage $description = $parserOutput->getProperty( 'description' );