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
|
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
|
|
|
|
|
* @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();
|
|
|
|
|
if ( $parserOutput->getProperty( 'description' ) !== false ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$parserOutput->setProperty( 'description', $desc );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
|
);
|
|
|
|
|
$parser->setFunctionTagHook(
|
|
|
|
|
'metadesc',
|
|
|
|
|
[ static::class, 'tagCallback' ],
|
|
|
|
|
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 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).
|
2017-06-08 12:32:58 +00:00
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-11-02 09:14:52 +00:00
|
|
|
|
public static function tagCallback( Parser $parser, PPFrame $frame, $content, $attributes ) {
|
|
|
|
|
$contentAttr = isset( $attributes['content'] ) ? $attributes['content'] : null;
|
|
|
|
|
$desc = isset( $content ) ? $content : $contentAttr;
|
2017-06-08 12:32:58 +00:00
|
|
|
|
if ( isset( $desc ) ) {
|
|
|
|
|
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
|
|
|
|
|
$description = $parserOutput->getProperty( 'description' );
|
|
|
|
|
if ( $description !== false ) {
|
|
|
|
|
$out->addMeta( 'description', $description );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|