Add composer for CI, and fix CS errors

Also move class to its own namespace.

Bug: T183532
Change-Id: I5a89efd3f162d73e247543c86967562a949a3bf4
This commit is contained in:
Sam Wilson 2017-11-02 17:14:52 +08:00 committed by Samwilson
parent dec5c26eb8
commit 74dfa5fdd9
6 changed files with 71 additions and 35 deletions

5
.phpcs.xml Normal file
View file

@ -0,0 +1,5 @@
<?xml version="1.0"?>
<ruleset name="MediaWiki">
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki" />
<file>.</file>
</ruleset>

View file

@ -6,12 +6,11 @@
* @ingroup Extensions * @ingroup Extensions
*/ */
$magicWords = array(); $magicWords = [];
/** English /** English
* @author Daniel Friesen * @author Daniel Friesen
*/ */
$magicWords['en'] = array( $magicWords['en'] = [
'description2' => array( 0, 'description2' ), 'description2' => [ 0, 'description2' ],
); ];

View file

@ -22,4 +22,4 @@ if ( function_exists( 'wfLoadExtension' ) ) {
return; return;
} else { } else {
die( 'This version of the Description2 extension requires MediaWiki 1.25+' ); die( 'This version of the Description2 extension requires MediaWiki 1.25+' );
} }

View file

@ -2,15 +2,18 @@
"require-dev": { "require-dev": {
"jakub-onderka/php-parallel-lint": "0.9.2", "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/minus-x": "0.2.1" "mediawiki/minus-x": "0.2.1",
"mediawiki/mediawiki-codesniffer": "14.1.0"
}, },
"scripts": { "scripts": {
"test": [ "test": [
"parallel-lint . --exclude vendor --exclude node_modules", "parallel-lint . --exclude vendor --exclude node_modules",
"minus-x check ." "minus-x check .",
"phpcs -p -s"
], ],
"fix": [ "fix": [
"minus-x fix ." "minus-x fix .",
"phpcbf"
] ]
} }
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "Description2", "name": "Description2",
"version": "0.4.0", "version": "0.4.1",
"author": [ "author": [
"[http://danf.ca/mw/ Daniel Friesen]" "[http://danf.ca/mw/ Daniel Friesen]"
], ],
@ -11,8 +11,11 @@
"config": { "config": {
"EnableMetaDescriptionFunctions": false "EnableMetaDescriptionFunctions": false
}, },
"ConfigRegistry": {
"Description2": "GlobalVarConfig::newInstance"
},
"AutoloadClasses": { "AutoloadClasses": {
"Description2": "Description2.class.php" "MediaWiki\\Extension\\Description2\\Description2": "includes/Description2.php"
}, },
"ExtensionMessagesFiles": { "ExtensionMessagesFiles": {
"Description2Magic": "Description2.i18n.magic.php" "Description2Magic": "Description2.i18n.magic.php"
@ -23,9 +26,9 @@
] ]
}, },
"Hooks": { "Hooks": {
"OutputPageParserOutput": "Description2::onOutputPageParserOutput", "OutputPageParserOutput": "MediaWiki\\Extension\\Description2\\Description2::onOutputPageParserOutput",
"ParserAfterTidy": "Description2::onParserAfterTidy", "ParserAfterTidy": "MediaWiki\\Extension\\Description2\\Description2::onParserAfterTidy",
"ParserFirstCallInit": "Description2::onParserFirstCallInit" "ParserFirstCallInit": "MediaWiki\\Extension\\Description2\\Description2::onParserFirstCallInit"
}, },
"manifest_version": 1 "manifest_version": 1
} }

View file

@ -1,4 +1,13 @@
<?php <?php
namespace MediaWiki\Extension\Description2;
use MediaWiki\MediaWikiServices;
use OutputPage;
use Parser;
use ParserOutput;
use PPFrame;
/** /**
* Description2 Adds meaningful description <meta> tag to MW pages and into the parser output * Description2 Adds meaningful description <meta> tag to MW pages and into the parser output
* *
@ -13,8 +22,8 @@
class Description2 { class Description2 {
/** /**
* @param Parser $parser * @param Parser $parser The parser.
* @param string $desc * @param string $desc The description text.
*/ */
public static function setDescription( Parser $parser, $desc ) { public static function setDescription( Parser $parser, $desc ) {
$parserOutput = $parser->getOutput(); $parserOutput = $parser->getOutput();
@ -25,16 +34,18 @@ class Description2 {
} }
/** /**
* @param Parser $parser * @link https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
* @param string $text * @param Parser &$parser The parser.
* @param string &$text The page text.
* @return bool * @return bool
*/ */
public static function onParserAfterTidy( Parser &$parser, &$text ) { public static function onParserAfterTidy( Parser &$parser, &$text ) {
$desc = ''; $desc = '';
$myText = preg_replace( '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i', '', $text ); $pattern = '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i';
$myText = preg_replace( $pattern, '', $text );
$paragraphs = array(); $paragraphs = [];
if ( preg_match_all( '#<p>.*?</p>#is', $myText, $paragraphs ) ) { if ( preg_match_all( '#<p>.*?</p>#is', $myText, $paragraphs ) ) {
foreach ( $paragraphs[0] as $paragraph ) { foreach ( $paragraphs[0] as $paragraph ) {
$paragraph = trim( strip_tags( $paragraph ) ); $paragraph = trim( strip_tags( $paragraph ) );
@ -54,47 +65,62 @@ class Description2 {
} }
/** /**
* @param Parser $parser * @param Parser &$parser The parser.
* @return bool * @return bool
*/ */
public static function onParserFirstCallInit( Parser &$parser ) { public static function onParserFirstCallInit( Parser &$parser ) {
global $wgEnableMetaDescriptionFunctions; $config = MediaWikiServices::getInstance()
if ( !$wgEnableMetaDescriptionFunctions ) { ->getConfigFactory()
->makeConfig( 'Description2' );
if ( !$config->get( 'EnableMetaDescriptionFunctions' ) ) {
// Functions and tags are disabled // Functions and tags are disabled
return true; return true;
} }
$parser->setFunctionHook( 'description2', array( 'Description2', 'parserFunctionCallback' ), Parser::SFH_OBJECT_ARGS ); $parser->setFunctionHook(
$parser->setFunctionTagHook( 'metadesc', array( 'Description2', 'tagCallback' ), Parser::SFH_OBJECT_ARGS ); 'description2',
[ static::class, 'parserFunctionCallback' ],
Parser::SFH_OBJECT_ARGS
);
$parser->setFunctionTagHook(
'metadesc',
[ static::class, 'tagCallback' ],
Parser::SFH_OBJECT_ARGS
);
return true; return true;
} }
/** /**
* @param Parser $parser * @param Parser $parser The parser.
* @param $frame * @param PPFrame $frame The frame.
* @param $args * @param string[] $args The arguments of the parser function call.
* @return string * @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] ) : ''; $desc = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
self::setDescription( $parser, $desc ); self::setDescription( $parser, $desc );
return ''; return '';
} }
/** /**
* @param Parser $parser * @param Parser $parser The parser.
* @param $frame * @param PPFrame $frame Not used.
* @param $content * @param string $content The contents of the tag (if any).
* @param $attributes * @param string[] $attributes The tag attributes (if any).
* @return string * @return string
*/ */
public static function tagCallback( Parser $parser, $frame, $content, $attributes ) { public static function tagCallback( Parser $parser, PPFrame $frame, $content, $attributes ) {
$desc = ( isset( $content ) ? $content : ( isset( $attributes['content'] ) ? $attributes['content'] : null ) ); $contentAttr = isset( $attributes['content'] ) ? $attributes['content'] : null;
$desc = isset( $content ) ? $content : $contentAttr;
if ( isset( $desc ) ) { if ( isset( $desc ) ) {
self::setDescription( $parser, $desc ); self::setDescription( $parser, $desc );
} }
return ''; 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 ) { public static function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parserOutput ) {
// Export the description from the main parser output into the OutputPage // Export the description from the main parser output into the OutputPage
$description = $parserOutput->getProperty( 'description' ); $description = $parserOutput->getProperty( 'description' );