setFunctionHook( 'setmainimage', [ __CLASS__, 'setMainImagePF' ] ); $parser->setFunctionHook( 'setmaintitle', [ __CLASS__, 'setMainTitlePF' ] ); } /** * @param Parser $parser * @param string $mainImage * @return string */ public static function setMainImagePF( Parser $parser, $mainImage ) { $parserOutput = $parser->getOutput(); $setMainImage = $parserOutput->getExtensionData( 'setmainimage' ); if ( $setMainImage !== null ) { return $mainImage; } if ( $mainImage !== '' ) { $file = Title::newFromText( $mainImage, NS_FILE ); $parserOutput->setExtensionData( 'setmainimage', $file->getDBKey() ); return $mainImage; } } /** * @param Parser $parser * @param string $mainTitle * @return string */ public static function setMainTitlePF( Parser $parser, $mainTitle ) { $parserOutput = $parser->getOutput(); $setMainTitle = $parserOutput->getExtensionData( 'setmaintitle' ); if ( $setMainTitle !== null ) { return $mainTitle; } $parserOutput->setExtensionData( 'setmaintitle', $mainTitle ); return $mainTitle; } /** * @param OutputPage &$out * @param ParserOutput $parserOutput */ public static function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parserOutput ) { global $wgLogo, $wgSitename, $wgXhtmlNamespaces, $egFacebookAppId, $egFacebookAdmins, $bannerImage; $setMainImage = $parserOutput->getExtensionData( 'setmainimage' ); $setMainTitle = $parserOutput->getExtensionData( 'setmaintitle' ); if ( $setMainImage !== null ) { // $mainImage = wfFindFile( Title::newFromDBkey( $setMainImage ) ); if (filter_var($setMainImage, FILTER_VALIDATE_URL) === FALSE) { $mainImage = wfFindFile(Title::newFromText( $setMainImage , NS_FILE )); } else { $mainImage = $setMainImage; } } else { $mainImage = false; } $wgXhtmlNamespaces['og'] = 'http://opengraphprotocol.org/schema/'; $title = $out->getTitle(); $isMainpage = $title->isMainPage(); $meta = []; if ( $isMainpage ) { $meta['og:type'] = 'website'; $meta['og:title'] = $wgSitename; } else { $meta['og:type'] = 'article'; $meta['og:site_name'] = $wgSitename; // Try to choose the most appropriate title for showing in news feeds. if ( ( defined( 'NS_BLOG_ARTICLE' ) && $title->getNamespace() == NS_BLOG_ARTICLE ) || ( defined( 'NS_BLOG_ARTICLE_TALK' ) && $title->getNamespace() == NS_BLOG_ARTICLE_TALK ) ) { $meta['og:title'] = $title->getSubpageText(); } else { $meta['og:title'] = $title->getText(); } } // {{#setmaintitle}} was used, override og:title value if ( $setMainTitle !== null ) { $meta['og:title'] = $setMainTitle; } if ( ( $mainImage !== false ) ) { if (filter_var($mainImage, FILTER_VALIDATE_URL) === FALSE) { // The official OpenGraph documentation says: // - thumbnail previews can't be smaller than 200px x 200px // - thumbnail previews look best at 1200px x 630px // @see https://developers.facebook.com/docs/sharing/best-practices/ // @see https://phabricator.wikimedia.org/T193986 $meta['og:image'] = wfExpandUrl( $mainImage->createThumb( 1200, 630 ) ); $bannerImage = wfExpandUrl( $mainImage->createThumb( 1200, 630 ) ); } else { // In some edge-cases we won't have defined an object but rather a full URL. $meta['og:image'] = $mainImage; $bannerImage = $mainImage; } } elseif ( $isMainpage ) { $meta['og:image'] = wfExpandUrl( $wgLogo ); } $description = $parserOutput->getProperty( 'description' ); if ( $description !== false ) { // set by Description2 extension, install it if you want proper og:description support $meta['og:description'] = $description; } $meta['og:url'] = $title->getFullURL(); if ( $egFacebookAppId ) { $meta['fb:app_id'] = $egFacebookAppId; } if ( $egFacebookAdmins ) { $meta['fb:admins'] = $egFacebookAdmins; } foreach ( $meta as $property => $value ) { if ( $value ) { $out->addHeadItem( "meta:property:$property", ' ' . Html::element( 'meta', [ 'property' => $property, 'content' => $value ] ) . "\n" ); } } $out->addScript(""); } }