-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOpenGraphMeta.class.php
More file actions
171 lines (146 loc) · 4.92 KB
/
OpenGraphMeta.class.php
File metadata and controls
171 lines (146 loc) · 4.92 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?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
*/
use MediaWiki\Html\Html;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
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;
}
$file = Title::newFromText( $mainImage, NS_FILE );
if ( $file !== null ) {
$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, $wgXhtmlNamespaces, $egFacebookAppId, $egFacebookAdmins;
$setMainImage = $parserOutput->getExtensionData( 'setmainimage' );
$setMainTitle = $parserOutput->getExtensionData( 'setmaintitle' );
$services = MediaWikiServices::getInstance();
$urlUtils = $services->getUrlUtils();
if ( $setMainImage !== null ) {
$mainImage = $services->getRepoGroup()->findFile( Title::newFromText( $setMainImage, NS_FILE ) );
} else {
$mainImage = false;
}
$wgXhtmlNamespaces['og'] = 'http://opengraphprotocol.org/schema/';
$title = $out->getTitle();
$isMainpage = $title->isMainPage();
$siteName = $out->msg( 'opengraphmeta-site-name' );
$meta = [];
if ( $isMainpage ) {
$meta['og:type'] = 'website';
$meta['og:title'] = $siteName;
} else {
$meta['og:type'] = 'article';
$meta['og:site_name'] = $siteName;
// 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 ( is_object( $mainImage ) ) {
// 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'] = $urlUtils->expand( $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;
}
} elseif ( $isMainpage ) {
$meta['og:image'] = $urlUtils->expand( $wgLogo );
}
$description = $parserOutput->getPageProperty( 'description' );
if ( $description !== null ) { // set by Description2 extension, install it if you want proper og:description support
$meta['og:description'] = $description;
}
$meta['og:url'] = $title->getFullURL();
if ( $egFacebookAppId ) {
// fb:app_id needs a prefix property declaring the namespace, so just add it directly
$out->addHeadItem(
'meta:property:fb:app_id',
' ' . Html::element( 'meta', [
'property' => 'fb:app_id',
'content' => $egFacebookAppId,
'prefix' => 'fb: http://www.facebook.com/2008/fbml'
] ) . "\n"
);
}
if ( $egFacebookAdmins ) {
$meta['fb:admins'] = $egFacebookAdmins;
}
// Add og:image to <meta> tags instead of as a <head> item to get it earlier in the page
if ( isset( $meta['og:image'] ) ) {
$out->addMeta( 'og:image', $meta['og:image'] );
unset( $meta['og:image'] );
}
$services->getHookContainer()->run( 'OpenGraphMetaHeaders', [ &$meta, $title, $out, $parserOutput ] );
foreach ( $meta as $property => $value ) {
if ( $value ) {
$out->addHeadItem(
"meta:property:$property",
' ' . Html::element( 'meta', [
'property' => $property,
'content' => $value
] ) . "\n"
);
}
}
}
}