2010-11-19 01:40:59 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2013-08-29 09:10:04 +00:00
|
|
|
|
* Description2.php – Adds meaningful description <meta> tag to MW pages and into the parser output
|
2010-11-19 01:40:59 +00:00
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Extensions
|
2013-08-29 09:10:04 +00:00
|
|
|
|
* @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
|
2010-11-19 01:40:59 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." );
|
|
|
|
|
|
|
|
|
|
$wgExtensionCredits['other'][] = array(
|
|
|
|
|
'path' => __FILE__,
|
|
|
|
|
'name' => 'Description2',
|
2014-03-31 07:50:05 +00:00
|
|
|
|
'version' => '0.3.0',
|
2013-08-29 09:10:04 +00:00
|
|
|
|
'author' => "[http://danf.ca/mw/ Daniel Friesen]",
|
2011-12-13 23:49:33 +00:00
|
|
|
|
'url' => 'https://www.mediawiki.org/wiki/Extension:Description2',
|
2010-11-23 17:30:16 +00:00
|
|
|
|
'descriptionmsg' => 'description2-desc',
|
2010-11-19 01:40:59 +00:00
|
|
|
|
);
|
|
|
|
|
|
2010-11-23 17:30:16 +00:00
|
|
|
|
$dir = dirname( __FILE__ );
|
2014-03-31 07:50:05 +00:00
|
|
|
|
$wgMessagesDirs['Description2'] = __DIR__ . '/i18n';
|
2010-11-23 17:30:16 +00:00
|
|
|
|
$wgExtensionMessagesFiles['Description2'] = $dir . '/Description2.i18n.php';
|
2011-01-22 16:14:52 +00:00
|
|
|
|
$wgExtensionMessagesFiles['Description2Magic'] = $dir . '/Description2.i18n.magic.php';
|
|
|
|
|
|
|
|
|
|
function efDescription2SetDescription( $parser, $desc ) {
|
|
|
|
|
$pOut = $parser->getOutput();
|
|
|
|
|
if ( $pOut->getProperty("description") !== false ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$pOut->setProperty("description", $desc);
|
|
|
|
|
$pOut->addOutputHook("setdescription");
|
|
|
|
|
}
|
2010-11-23 17:30:16 +00:00
|
|
|
|
|
2010-11-19 01:40:59 +00:00
|
|
|
|
$wgHooks['ParserAfterTidy'][] = 'egDescription2ParserAfterTidy';
|
|
|
|
|
function egDescription2ParserAfterTidy( &$parser, &$text ) {
|
|
|
|
|
$desc = '';
|
2014-03-31 07:50:05 +00:00
|
|
|
|
|
2010-11-19 01:40:59 +00:00
|
|
|
|
$myText = preg_replace('%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i', '', $text);
|
2014-03-31 07:50:05 +00:00
|
|
|
|
|
2010-11-19 01:40:59 +00:00
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-31 07:50:05 +00:00
|
|
|
|
|
2010-11-19 01:40:59 +00:00
|
|
|
|
if ( $desc ) {
|
2011-01-22 16:14:52 +00:00
|
|
|
|
efDescription2SetDescription( $parser, $desc );
|
2010-11-19 01:40:59 +00:00
|
|
|
|
}
|
2014-03-31 07:50:05 +00:00
|
|
|
|
|
2010-11-19 01:40:59 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 07:50:05 +00:00
|
|
|
|
$wgHooks['ParserFirstCallInit'][] = array( 'efDescription2RegisterParser' );
|
2011-01-22 16:14:52 +00:00
|
|
|
|
function efDescription2RegisterParser( &$parser ) {
|
|
|
|
|
global $wgEnableMetaDescriptionFunctions;
|
|
|
|
|
if ( !$wgEnableMetaDescriptionFunctions ) {
|
|
|
|
|
// Functions and tags are disabled
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$parser->setFunctionHook( 'description2', 'efDescription2Function', Parser::SFH_OBJECT_ARGS );
|
|
|
|
|
$parser->setFunctionTagHook( 'metadesc', 'efDescription2Tag', Parser::SFH_OBJECT_ARGS );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function efDescription2Function( $parser, $frame, $args ) {
|
|
|
|
|
$desc = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
|
|
|
|
|
efDescription2SetDescription( $parser, $desc );
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
function efDescription2Tag( $parser, $frame, $content, $attributes ) {
|
|
|
|
|
$desc = (isset( $content ) ? $content : (isset($attributes["content"]) ? $attributes["content"] : null));
|
|
|
|
|
if ( isset($desc) ) {
|
|
|
|
|
efDescription2SetDescription( $parser, $desc );
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-19 01:40:59 +00:00
|
|
|
|
$wgParserOutputHooks['setdescription'] = 'egDescription2ParserOutputSetDescription';
|
|
|
|
|
function egDescription2ParserOutputSetDescription( $out, $parserOutput, $data ) {
|
|
|
|
|
// Export the description from the main parser output into the OutputPage
|
|
|
|
|
$out->mDescription = $parserOutput->getProperty("description");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wgHooks['BeforePageDisplay'][] = 'egDescription2PageHook';
|
|
|
|
|
function egDescription2PageHook( &$out, &$sk ) {
|
|
|
|
|
if ( isset($out->mDescription) && $out->mDescription )
|
|
|
|
|
$out->addMeta("description", $out->mDescription);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-22 16:14:52 +00:00
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
$wgEnableMetaDescriptionFunctions = false;
|
|
|
|
|
|