mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Description2
synced 2024-11-23 22:33:34 +00:00
444f123a4a
* Old i18n shim removed * extension.json added * PHP code moved into the new Description2 class * Old PHP entry point still exists for b/c with older installations, but MW 1.25 or newer is now required Change-Id: I0670c203b62dcf500ded1b82957e473edf864fb1
106 lines
2.9 KiB
PHP
106 lines
2.9 KiB
PHP
<?php
|
||
/**
|
||
* 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 {
|
||
|
||
/**
|
||
* @param Parser $parser
|
||
* @param string $desc
|
||
*/
|
||
public static function setDescription( Parser $parser, $desc ) {
|
||
$parserOutput = $parser->getOutput();
|
||
if ( $parserOutput->getProperty( 'description' ) !== false ) {
|
||
return;
|
||
}
|
||
$parserOutput->setProperty( 'description', $desc );
|
||
}
|
||
|
||
/**
|
||
* @param Parser $parser
|
||
* @param string $text
|
||
* @return bool
|
||
*/
|
||
public static function onParserAfterTidy( Parser &$parser, &$text ) {
|
||
$desc = '';
|
||
|
||
$myText = preg_replace( '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i', '', $text );
|
||
|
||
$paragraphs = array();
|
||
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
|
||
* @return bool
|
||
*/
|
||
public static function onParserFirstCallInit( Parser &$parser ) {
|
||
global $wgEnableMetaDescriptionFunctions;
|
||
if ( !$wgEnableMetaDescriptionFunctions ) {
|
||
// 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 );
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* @param Parser $parser
|
||
* @param $frame
|
||
* @param $args
|
||
* @return string
|
||
*/
|
||
public static function parserFunctionCallback( Parser $parser, $frame, $args ) {
|
||
$desc = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
|
||
self::setDescription( $parser, $desc );
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* @param Parser $parser
|
||
* @param $frame
|
||
* @param $content
|
||
* @param $attributes
|
||
* @return string
|
||
*/
|
||
public static function tagCallback( Parser $parser, $frame, $content, $attributes ) {
|
||
$desc = ( isset( $content ) ? $content : ( isset( $attributes['content'] ) ? $attributes['content'] : null ) );
|
||
if ( isset( $desc ) ) {
|
||
self::setDescription( $parser, $desc );
|
||
}
|
||
return '';
|
||
}
|
||
|
||
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 );
|
||
}
|
||
}
|
||
}
|