2017-06-08 12:32:58 +00:00
|
|
|
|
<?php
|
2017-11-02 09:14:52 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Description2;
|
|
|
|
|
|
|
|
|
|
use Parser;
|
|
|
|
|
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();
|
2024-01-02 17:33:05 +00:00
|
|
|
|
if ( $parserOutput->getPageProperty( 'description' ) !== null ) {
|
|
|
|
|
return;
|
2017-06-08 12:32:58 +00:00
|
|
|
|
}
|
2024-01-02 17:33:05 +00:00
|
|
|
|
$parserOutput->setPageProperty( 'description', $desc );
|
2017-06-08 12:32:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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 '';
|
|
|
|
|
}
|
2023-12-04 20:02:52 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns no more than a requested number of characters, preserving words.
|
|
|
|
|
*
|
|
|
|
|
* Borrowed from TextExtracts.
|
|
|
|
|
*
|
|
|
|
|
* @param string $text Source plain text to extract from. HTML tags should be removed by the description provider.
|
|
|
|
|
* @param int $requestedLength Maximum number of characters to return
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getFirstChars( string $text, int $requestedLength ) {
|
|
|
|
|
if ( $requestedLength <= 0 ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$length = mb_strlen( $text );
|
|
|
|
|
if ( $length <= $requestedLength ) {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The following (although in somewhat backwards order) cuts the text at given length and restores the end if it
|
|
|
|
|
// has been cut, with the ungreedy pattern always matching a single word built of word characters (no
|
|
|
|
|
// punctuation) and/or forward slashes.
|
|
|
|
|
$pattern = '/^[\w\/]*/su';
|
|
|
|
|
preg_match( $pattern, mb_substr( $text, $requestedLength ), $m );
|
|
|
|
|
$truncatedText = mb_substr( $text, 0, $requestedLength ) . $m[0];
|
|
|
|
|
if ( $truncatedText === $text ) {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return trim( $truncatedText );
|
|
|
|
|
}
|
2017-06-08 12:32:58 +00:00
|
|
|
|
}
|