2017-06-08 12:32:58 +00:00
|
|
|
|
<?php
|
2017-11-02 09:14:52 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Description2;
|
|
|
|
|
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
use OutputPage;
|
|
|
|
|
use Parser;
|
|
|
|
|
use ParserOutput;
|
|
|
|
|
use PPFrame;
|
|
|
|
|
|
2017-06-08 12:32:58 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
2018-04-14 00:16:14 +00:00
|
|
|
|
* @license GPL-2.0-or-later
|
2017-06-08 12:32:58 +00:00
|
|
|
|
* @link https://www.mediawiki.org/wiki/Extension:Description2 Documentation
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class Description2 {
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-02 09:14:52 +00:00
|
|
|
|
* @param Parser $parser The parser.
|
|
|
|
|
* @param string $desc The description text.
|
2017-06-08 12:32:58 +00:00
|
|
|
|
*/
|
|
|
|
|
public static function setDescription( Parser $parser, $desc ) {
|
|
|
|
|
$parserOutput = $parser->getOutput();
|
2022-02-06 12:11:38 +00:00
|
|
|
|
if ( method_exists( $parserOutput, 'getPageProperty' ) ) {
|
|
|
|
|
// MW 1.38+
|
2022-02-17 00:33:00 +00:00
|
|
|
|
if ( $parserOutput->getPageProperty( 'description' ) !== null ) {
|
2022-02-06 12:11:38 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$parserOutput->setPageProperty( 'description', $desc );
|
|
|
|
|
} else {
|
|
|
|
|
if ( $parserOutput->getProperty( 'description' ) !== false ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$parserOutput->setProperty( 'description', $desc );
|
2017-06-08 12:32:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-02 09:14:52 +00:00
|
|
|
|
* @link https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
|
|
|
|
|
* @param Parser &$parser The parser.
|
|
|
|
|
* @param string &$text The page text.
|
2017-06-08 12:32:58 +00:00
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function onParserAfterTidy( Parser &$parser, &$text ) {
|
|
|
|
|
$desc = '';
|
|
|
|
|
|
2017-11-02 09:14:52 +00:00
|
|
|
|
$pattern = '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i';
|
|
|
|
|
$myText = preg_replace( $pattern, '', $text );
|
2017-06-08 12:32:58 +00:00
|
|
|
|
|
2017-11-02 09:14:52 +00:00
|
|
|
|
$paragraphs = [];
|
2017-06-08 12:32:58 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-02 09:14:52 +00:00
|
|
|
|
* @param Parser &$parser The parser.
|
2017-06-08 12:32:58 +00:00
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public static function onParserFirstCallInit( Parser &$parser ) {
|
2017-11-02 09:14:52 +00:00
|
|
|
|
$config = MediaWikiServices::getInstance()
|
|
|
|
|
->getConfigFactory()
|
|
|
|
|
->makeConfig( 'Description2' );
|
|
|
|
|
if ( !$config->get( 'EnableMetaDescriptionFunctions' ) ) {
|
2017-06-08 12:32:58 +00:00
|
|
|
|
// Functions and tags are disabled
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-11-02 09:14:52 +00:00
|
|
|
|
$parser->setFunctionHook(
|
|
|
|
|
'description2',
|
|
|
|
|
[ static::class, 'parserFunctionCallback' ],
|
|
|
|
|
Parser::SFH_OBJECT_ARGS
|
|
|
|
|
);
|
2017-06-08 12:32:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-02 09:14:52 +00:00
|
|
|
|
* @param Parser $parser The parser.
|
|
|
|
|
* @param PPFrame $frame The frame.
|
|
|
|
|
* @param string[] $args The arguments of the parser function call.
|
2017-06-08 12:32:58 +00:00
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-11-02 09:14:52 +00:00
|
|
|
|
public static function parserFunctionCallback( Parser $parser, PPFrame $frame, $args ) {
|
2017-06-08 12:32:58 +00:00
|
|
|
|
$desc = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
|
|
|
|
|
self::setDescription( $parser, $desc );
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 09:14:52 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param OutputPage &$out The output page to add the meta element to.
|
|
|
|
|
* @param ParserOutput $parserOutput The parser output to get the description from.
|
|
|
|
|
*/
|
2017-06-08 12:32:58 +00:00
|
|
|
|
public static function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parserOutput ) {
|
|
|
|
|
// Export the description from the main parser output into the OutputPage
|
2022-02-06 12:11:38 +00:00
|
|
|
|
if ( method_exists( $parserOutput, 'getPageProperty' ) ) {
|
|
|
|
|
// MW 1.38+
|
2022-02-17 00:33:00 +00:00
|
|
|
|
$description = $parserOutput->getPageProperty( 'description' );
|
2022-02-06 12:11:38 +00:00
|
|
|
|
} else {
|
|
|
|
|
$description = $parserOutput->getProperty( 'description' );
|
2022-02-17 00:33:00 +00:00
|
|
|
|
if ( $description === false ) {
|
|
|
|
|
$description = null;
|
|
|
|
|
}
|
2022-02-06 12:11:38 +00:00
|
|
|
|
}
|
2022-02-17 00:33:00 +00:00
|
|
|
|
if ( $description !== null ) {
|
2017-06-08 12:32:58 +00:00
|
|
|
|
$out->addMeta( 'description', $description );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|