Page MenuHomePhabricator

PageImages' og:image interferes with the one defined by OpenGraphMeta
Closed, ResolvedPublic

Description

Since MW 1.29, PagesImages defines the og:image tag using the images it gets from a page. The OpenGraphMeta extension allows defining an og:image using the #setmainimage tag. This results in the meta head tag being defined twice and the tag from PageImages overriding OpenGraphMeta's.

To reproduce the bug:

  1. have both extensions installed;
  2. go to a page that contains an image;
  3. use #setmainimage to define an og:image;
  4. view the source of the page, the meta og:image tag is defined twice.

See the proposed fix in PageImages::onBeforePageDisplay, it skips the displaying of the og:image tag if it has already been defined, giving a chance to other extensions to set a more appropriate one.

	/**
	 * @param OutputPage &$out The page being output.
	 * @param Skin &$skin Skin object used to generate the page. Ignored
	 */
	public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
		
		// If the og:image tag has already been defined, skip.
		foreach($out->getMetaTags() as $tag) {
			if($tag[0] == 'og:image') { return; }
		}
		
		if(isset($out->getHeadItemsArray()['meta:property:og:image'])){
			return;
		}
		
		$imageFile = self::getPageImage( $out->getContext()->getTitle() );
		if ( !$imageFile ) {
			return;
		}

		// See https://developers.facebook.com/docs/sharing/best-practices?locale=en_US#tags
		$thumb = $imageFile->transform( [ 'width' => 1200 ] );
		if ( !$thumb ) {
			return;
		}

		$out->addMeta( 'og:image', wfExpandUrl( $thumb->getUrl(), PROTO_CANONICAL ) );
	}

Event Timeline

Jdlrobson subscribed.

This sounds sensible and non-controversial to me!

Change 735052 had a related patch set uploaded (by Simon04; author: Simon04):

[mediawiki/extensions/PageImages@master] Skip if the og:image tag has already been defined

https://gerrit.wikimedia.org/r/735052

Change 735052 abandoned by Simon04:

[mediawiki/extensions/PageImages@master] Skip if the og:image tag has already been defined

Reason:

Expected interaction between PageImages/OpenGraphMeta extensions is unclear.

https://gerrit.wikimedia.org/r/735052

I also observe this issue with SemanticMetaTags adding an og:image according to a specific semantic (SMW) property.

I fixed it by adding a parameter $wgPageImagesOpenGraph true/false to deactivate the og:image from PageImages where SemanticMetaTags is activated, I will propose my change on Gerrit, but it’s a global parameter, not page-per-page.

Reading the discussion on the proposed patch by Simon04, I feel that on the longer term, a more robust mechanism should be created, perhaps where extensions propose an og:image with an associated weight, and MediaWiki choose the winner amongst them. For instance PageImages candidate has weight 0 (=choose this one by default) and OpenGraphMeta candidate has weight 300 (=when the user defines their og:image, it’s really important to use it).

Change 818143 had a related patch set uploaded (by Seb35; author: Seb35):

[mediawiki/extensions/PageImages@master] Add a parameter to deactivate <meta property="og:image">

https://gerrit.wikimedia.org/r/818143

Change 818143 merged by jenkins-bot:

[mediawiki/extensions/PageImages@master] Add a parameter to deactivate OpenGraph meta tags

https://gerrit.wikimedia.org/r/818143