summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/OpenGraphMeta/OpenGraphMeta.class.php
blob: d015718f3b2e8bec7e5200d66b39b4c8c72be5a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
 * OpenGraphMeta
 *
 * @file
 * @ingroup Extensions
 * @author Daniel Friesen (http://danf.ca/mw/)
 * @author Southparkfan
 * @license GPL-2.0-or-later
 * @link https://www.mediawiki.org/wiki/Extension:OpenGraphMeta Documentation
 */

class OpenGraphMeta {
	/**
	 * @param Parser $parser
	 */
	public static function onParserFirstCallInit( Parser $parser ) {
		$parser->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("<style>.bannerimage {background-image:url('{$bannerImage}');}</style>");
	}

}