mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Description2
synced 2024-11-23 14:26:44 +00:00
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:
parent
dec5c26eb8
commit
74dfa5fdd9
5
.phpcs.xml
Normal file
5
.phpcs.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0"?>
|
||||
<ruleset name="MediaWiki">
|
||||
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki" />
|
||||
<file>.</file>
|
||||
</ruleset>
|
|
@ -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' ],
|
||||
];
|
||||
|
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -1,4 +1,13 @@
|
|||
<?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
|
||||
*
|
||||
|
@ -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( '%<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 ) ) {
|
||||
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' );
|
Loading…
Reference in a new issue