Compare commits
31 commits
Author | SHA1 | Date | |
---|---|---|---|
4ca967ff4d | |||
1499b9aa3c | |||
cdb35a545d | |||
43a4771737 | |||
050086dd6e | |||
b73fe26c29 | |||
43eb47183e | |||
15f2edbaee | |||
323c797f28 | |||
1f10d667fe | |||
2d68312307 | |||
6cbe68fa54 | |||
cab185a2a6 | |||
4cd70b8e7e | |||
655b11d7ea | |||
4ba5e24b71 | |||
dbc034f125 | |||
e83256f305 | |||
6b4777721f | |||
d2a5322a44 | |||
6eca5a303c | |||
bd9bc72317 | |||
d53638e642 | |||
23e0582f58 | |||
b269304773 | |||
84276905bc | |||
adc340542c | |||
6d3b7ce782 | |||
838ef90fe0 | |||
0c238dce47 | |||
f6c96a869c |
14
README.md
Normal file
14
README.md
Normal file
|
@ -0,0 +1,14 @@
|
|||
# fanwikis/Description2
|
||||
|
||||
This is a fork of the MediaWiki Description2 extension maintained by the fanwikis.org Team with tweaks and improvements for use on fanwikis.org's MediaWiki installation.
|
||||
|
||||
## Changes
|
||||
|
||||
### Removing <style> tags
|
||||
|
||||
When using the TemplateStyles extension at the top of an article, the Description may end up being just CSS. This is not what we want, so we remove the <style> tags from the description.
|
||||
|
||||
```diff
|
||||
- $pattern = '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i';
|
||||
+ $pattern = '%<(table|style)\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?(table|style)\b)<[^<]*+)*+)*+</(table|style)>%i';
|
||||
```
|
|
@ -1,9 +1,9 @@
|
|||
{
|
||||
"require-dev": {
|
||||
"mediawiki/mediawiki-codesniffer": "37.0.0",
|
||||
"mediawiki/mediawiki-codesniffer": "41.0.0",
|
||||
"mediawiki/minus-x": "1.1.1",
|
||||
"php-parallel-lint/php-console-highlighter": "0.5.0",
|
||||
"php-parallel-lint/php-parallel-lint": "1.3.0"
|
||||
"php-parallel-lint/php-console-highlighter": "1.0.0",
|
||||
"php-parallel-lint/php-parallel-lint": "1.3.2"
|
||||
},
|
||||
"scripts": {
|
||||
"test": [
|
||||
|
|
|
@ -8,14 +8,20 @@
|
|||
"descriptionmsg": "description2-desc",
|
||||
"license-name": "GPL-2.0-or-later",
|
||||
"type": "other",
|
||||
"requires": {
|
||||
"MediaWiki": ">= 1.35.0"
|
||||
},
|
||||
"config": {
|
||||
"EnableMetaDescriptionFunctions": false
|
||||
},
|
||||
"ConfigRegistry": {
|
||||
"Description2": "GlobalVarConfig::newInstance"
|
||||
},
|
||||
"AutoloadClasses": {
|
||||
"MediaWiki\\Extension\\Description2\\Description2": "includes/Description2.php"
|
||||
"ServiceWiringFiles": [
|
||||
"includes/ServiceWiring.php"
|
||||
],
|
||||
"AutoloadNamespaces": {
|
||||
"MediaWiki\\Extension\\Description2\\": "includes/"
|
||||
},
|
||||
"ExtensionMessagesFiles": {
|
||||
"Description2Magic": "Description2.i18n.magic.php"
|
||||
|
@ -25,10 +31,19 @@
|
|||
"i18n"
|
||||
]
|
||||
},
|
||||
"HookHandlers": {
|
||||
"Description2": {
|
||||
"class": "MediaWiki\\Extension\\Description2\\Hooks",
|
||||
"services": [
|
||||
"ConfigFactory",
|
||||
"Description2.DescriptionProvider"
|
||||
]
|
||||
}
|
||||
},
|
||||
"Hooks": {
|
||||
"OutputPageParserOutput": "MediaWiki\\Extension\\Description2\\Description2::onOutputPageParserOutput",
|
||||
"ParserAfterTidy": "MediaWiki\\Extension\\Description2\\Description2::onParserAfterTidy",
|
||||
"ParserFirstCallInit": "MediaWiki\\Extension\\Description2\\Description2::onParserFirstCallInit"
|
||||
"OutputPageParserOutput": "Description2",
|
||||
"ParserAfterTidy": "Description2",
|
||||
"ParserFirstCallInit": "Description2"
|
||||
},
|
||||
"manifest_version": 1
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
{
|
||||
"@metadata": {
|
||||
"authors": [
|
||||
"З. ӘЙЛЕ",
|
||||
"Мухамадеева"
|
||||
]
|
||||
},
|
||||
"description2-desc": " Башҡа киңлектәрҙә файҙаланыу өсөн MediaWiki һәм ParserOutput битенә һүрәтләү мета-тег - ын өҫтәй"
|
||||
"description2-desc": "Башҡа киңәйтеүҙәрҙә файҙаланыу өсөн MediaWiki һәм ParserOutput битенә тасуирлау мета-тамғаһын өҫтәй"
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
{
|
||||
"@metadata": {
|
||||
"authors": [
|
||||
"Cedric31"
|
||||
"Cedric31",
|
||||
"Lhanars"
|
||||
]
|
||||
},
|
||||
"description2-desc": "Apond una metaetiqueta de descripcion a las paginas de MediaWiki e a ParserOutput per las autras extensions d'utilizar"
|
||||
"description2-desc": "Apond una meta-etiqueta de descripcion a las paginas de MediaWiki e a ParserOutput per las autras extensions d'utilizar"
|
||||
}
|
||||
|
|
8
i18n/scn.json
Normal file
8
i18n/scn.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"@metadata": {
|
||||
"authors": [
|
||||
"Ajeje Brazorf"
|
||||
]
|
||||
},
|
||||
"description2-desc": "Agghiunci nu meta-tag pâ discrizzioni dê pàggini MediaWiki e ntâ ParserOutput pi putiri èssiri usata d'àutri stinzioni"
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"@metadata": {
|
||||
"authors": [
|
||||
"Vlad5250"
|
||||
"Winston Sung"
|
||||
]
|
||||
},
|
||||
"description2-desc": "Dodaje metaoznaku za opis u MediaWiki stranice i u parserski izlaz (ParserOutput) za uporabu od strane drugih dodataka"
|
8
i18n/sl.json
Normal file
8
i18n/sl.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"@metadata": {
|
||||
"authors": [
|
||||
"Eleassar"
|
||||
]
|
||||
},
|
||||
"description2-desc": "Stranem MediaWiki in v ParserOutput doda opisno metaoznako, ki jo lahko uporabljajo druge razširitve"
|
||||
}
|
6
i18n/sr-el.json
Normal file
6
i18n/sr-el.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"@metadata": {
|
||||
"authors": []
|
||||
},
|
||||
"description2-desc": "Dodaje metaoznaku opisa na Medijaviki stranice i u ParserOutput za upotrebu od strane drugih proširenja"
|
||||
}
|
|
@ -2,7 +2,8 @@
|
|||
"@metadata": {
|
||||
"authors": [
|
||||
"Base",
|
||||
"DDPAT"
|
||||
"DDPAT",
|
||||
"Ice bulldog"
|
||||
]
|
||||
},
|
||||
"description2-desc": "Додає мета-тег опису для сторінок MediaWiki і ParserOutput, для використання в інших розширеннях"
|
||||
|
|
|
@ -2,10 +2,7 @@
|
|||
|
||||
namespace MediaWiki\Extension\Description2;
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use OutputPage;
|
||||
use Parser;
|
||||
use ParserOutput;
|
||||
use PPFrame;
|
||||
|
||||
/**
|
||||
|
@ -20,68 +17,24 @@ use PPFrame;
|
|||
*/
|
||||
|
||||
class Description2 {
|
||||
|
||||
/**
|
||||
* @param Parser $parser The parser.
|
||||
* @param string $desc The description text.
|
||||
*/
|
||||
public static function setDescription( Parser $parser, $desc ) {
|
||||
$parserOutput = $parser->getOutput();
|
||||
if ( method_exists( $parserOutput, 'getPageProperty' ) ) {
|
||||
// MW 1.38+
|
||||
if ( $parserOutput->getPageProperty( 'description' ) !== null ) {
|
||||
return;
|
||||
}
|
||||
$parserOutput->setPageProperty( 'description', $desc );
|
||||
} else {
|
||||
if ( $parserOutput->getProperty( 'description' ) !== false ) {
|
||||
return;
|
||||
}
|
||||
$parserOutput->setProperty( 'description', $desc );
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 = '';
|
||||
|
||||
$pattern = '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i';
|
||||
$myText = preg_replace( $pattern, '', $text );
|
||||
|
||||
$paragraphs = [];
|
||||
if ( preg_match_all( '#<p>.*?</p>#is', $myText, $paragraphs ) ) {
|
||||
foreach ( $paragraphs[0] as $paragraph ) {
|
||||
$paragraph = trim( strip_tags( $paragraph ) );
|
||||
if ( !$paragraph ) {
|
||||
continue;
|
||||
}
|
||||
$desc = $paragraph;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $desc ) {
|
||||
self::setDescription( $parser, $desc );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Parser &$parser The parser.
|
||||
* @return bool
|
||||
*/
|
||||
public static function onParserFirstCallInit( Parser &$parser ) {
|
||||
$config = MediaWikiServices::getInstance()
|
||||
->getConfigFactory()
|
||||
->makeConfig( 'Description2' );
|
||||
if ( !$config->get( 'EnableMetaDescriptionFunctions' ) ) {
|
||||
// Functions and tags are disabled
|
||||
return true;
|
||||
}
|
||||
$parser->setFunctionHook(
|
||||
'description2',
|
||||
[ static::class, 'parserFunctionCallback' ],
|
||||
Parser::SFH_OBJECT_ARGS
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -95,16 +48,4 @@ class Description2 {
|
|||
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' );
|
||||
if ( $description !== false ) {
|
||||
$out->addMeta( 'description', $description );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
13
includes/DescriptionProvider.php
Normal file
13
includes/DescriptionProvider.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\Description2;
|
||||
|
||||
interface DescriptionProvider {
|
||||
/**
|
||||
* Extracts description from the HTML representation of a page.
|
||||
*
|
||||
* @param string $text HTML to extract the description from.
|
||||
* @return ?string
|
||||
*/
|
||||
public function derive( string $text ): ?string;
|
||||
}
|
116
includes/Hooks.php
Normal file
116
includes/Hooks.php
Normal file
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\Description2;
|
||||
|
||||
use Config;
|
||||
use ConfigFactory;
|
||||
use OutputPage;
|
||||
use Parser;
|
||||
use ParserOutput;
|
||||
|
||||
/**
|
||||
* Description2 – Adds meaningful description <meta> tag to MW pages and into the parser output
|
||||
*
|
||||
* @file
|
||||
* @ingroup Extensions
|
||||
* @author Daniel Friesen (http://danf.ca/mw/)
|
||||
* @copyright Copyright 2010 – Daniel Friesen
|
||||
* @license GPL-2.0-or-later
|
||||
* @link https://www.mediawiki.org/wiki/Extension:Description2 Documentation
|
||||
*/
|
||||
|
||||
class Hooks implements
|
||||
\MediaWiki\Hook\ParserAfterTidyHook,
|
||||
\MediaWiki\Hook\ParserFirstCallInitHook,
|
||||
\MediaWiki\Hook\OutputPageParserOutputHook
|
||||
{
|
||||
|
||||
/** @var Config */
|
||||
private Config $config;
|
||||
|
||||
/** @var DescriptionProvider */
|
||||
private DescriptionProvider $descriptionProvider;
|
||||
|
||||
/**
|
||||
* @param ConfigFactory $configFactory
|
||||
*/
|
||||
public function __construct(
|
||||
ConfigFactory $configFactory,
|
||||
DescriptionProvider $descriptionProvider
|
||||
) {
|
||||
$this->config = $configFactory->makeConfig( 'Description2' );
|
||||
$this->descriptionProvider = $descriptionProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* @link https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
|
||||
* @param Parser $parser The parser.
|
||||
* @param string &$text The page text.
|
||||
* @return bool
|
||||
*/
|
||||
public function onParserAfterTidy( $parser, &$text ) {
|
||||
$parserOutput = $parser->getOutput();
|
||||
|
||||
// Avoid running the algorithm on interface messages which may waste time
|
||||
if ( $parser->getOptions()->getInterfaceMessage() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Avoid running the algorithm multiple times if we already have determined the description. This may happen
|
||||
// on file pages.
|
||||
if ( method_exists( $parserOutput, 'getPageProperty' ) ) {
|
||||
// MW 1.38+
|
||||
$description = $parserOutput->getPageProperty( 'description' );
|
||||
} else {
|
||||
$description = $parserOutput->getProperty( 'description' );
|
||||
}
|
||||
if ( $description ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$desc = $this->descriptionProvider->derive( $text );
|
||||
|
||||
if ( $desc ) {
|
||||
Description2::setDescription( $parser, $desc );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Parser $parser The parser.
|
||||
* @return bool
|
||||
*/
|
||||
public function onParserFirstCallInit( $parser ) {
|
||||
if ( !$this->config->get( 'EnableMetaDescriptionFunctions' ) ) {
|
||||
// Functions and tags are disabled
|
||||
return true;
|
||||
}
|
||||
$parser->setFunctionHook(
|
||||
'description2',
|
||||
[ Description2::class, 'parserFunctionCallback' ],
|
||||
Parser::SFH_OBJECT_ARGS
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OutputPage $out The output page to add the meta element to.
|
||||
* @param ParserOutput $parserOutput The parser output to get the description from.
|
||||
*/
|
||||
public function onOutputPageParserOutput( $out, $parserOutput ): void {
|
||||
// Export the description from the main parser output into the OutputPage
|
||||
if ( method_exists( $parserOutput, 'getPageProperty' ) ) {
|
||||
// MW 1.38+
|
||||
$description = $parserOutput->getPageProperty( 'description' );
|
||||
} else {
|
||||
$description = $parserOutput->getProperty( 'description' );
|
||||
if ( $description === false ) {
|
||||
$description = null;
|
||||
}
|
||||
}
|
||||
if ( $description !== null ) {
|
||||
$out->addMeta( 'description', $description );
|
||||
}
|
||||
}
|
||||
}
|
13
includes/ServiceWiring.php
Normal file
13
includes/ServiceWiring.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\Extension\Description2\DescriptionProvider;
|
||||
use MediaWiki\Extension\Description2\SimpleDescriptionProvider;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
return [
|
||||
'Description2.DescriptionProvider' => static function (
|
||||
MediaWikiServices $services
|
||||
): DescriptionProvider {
|
||||
return new SimpleDescriptionProvider();
|
||||
},
|
||||
];
|
36
includes/SimpleDescriptionProvider.php
Normal file
36
includes/SimpleDescriptionProvider.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\Description2;
|
||||
|
||||
class SimpleDescriptionProvider implements DescriptionProvider {
|
||||
|
||||
/**
|
||||
* Extracts description from the HTML representation of a page.
|
||||
*
|
||||
* The algorithm:
|
||||
* 1. Removes all <table> and <style> elements and their contents.
|
||||
* 2. Selects all <p> elements.
|
||||
* 3. Iterates over those paragraphs, strips out all HTML tags and trims white-space around.
|
||||
* 4. Then the first non-empty paragraph is picked as the result.
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
public function derive( string $text ): ?string {
|
||||
$pattern = '%<(table|style)\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?(table|style)\b)<[^<]*+)*+)*+</(table|style)>%i';
|
||||
$myText = preg_replace( $pattern, '', $text );
|
||||
|
||||
$paragraphs = [];
|
||||
if ( preg_match_all( '#<p>.*?</p>#is', $myText, $paragraphs ) ) {
|
||||
foreach ( $paragraphs[0] as $paragraph ) {
|
||||
$paragraph = trim( strip_tags( $paragraph ) );
|
||||
if ( !$paragraph ) {
|
||||
continue;
|
||||
}
|
||||
return $paragraph;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
3354
package-lock.json
generated
3354
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -5,9 +5,9 @@
|
|||
"test": "grunt test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint-config-wikimedia": "0.20.0",
|
||||
"grunt": "1.5.3",
|
||||
"grunt-banana-checker": "0.9.0",
|
||||
"grunt-eslint": "23.0.0"
|
||||
"eslint-config-wikimedia": "0.25.0",
|
||||
"grunt": "1.6.1",
|
||||
"grunt-banana-checker": "0.11.0",
|
||||
"grunt-eslint": "24.0.1"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue