-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGraphMeta.php
More file actions
110 lines (95 loc) · 3.74 KB
/
OpenGraphMeta.php
File metadata and controls
110 lines (95 loc) · 3.74 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
<?php
/**
* OpenGraphMeta
*
* @file
* @ingroup Extensions
* @author Daniel Friesen (http://danf.ca/mw/)
* @license https://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
* @link https://www.mediawiki.org/wiki/Extension:OpenGraphMeta Documentation
*/
if ( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." );
$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name' => "OpenGraphMeta",
'author' => "[http://danf.ca/mw/ Daniel Friesen]",
'descriptionmsg' => 'opengraphmeta-desc',
'url' => 'https://www.mediawiki.org/wiki/Extension:OpenGraphMeta',
'license-name' => 'GPL-2.0+',
);
$dir = dirname( __FILE__ );
$wgExtensionMessagesFiles['OpenGraphMetaMagic'] = $dir . '/OpenGraphMeta.magic.php';
$wgMessagesDirs['OpenGraphMeta'] = __DIR__ . '/i18n';
$wgExtensionMessagesFiles['OpenGraphMeta'] = $dir . '/OpenGraphMeta.i18n.php';
$wgHooks['ParserFirstCallInit'][] = 'efOpenGraphMetaParserInit';
function efOpenGraphMetaParserInit( $parser ) {
$parser->setFunctionHook( 'setmainimage', 'efSetMainImagePF' );
return true;
}
function efSetMainImagePF( $parser, $mainimage ) {
$parserOutput = $parser->getOutput();
if ( isset($parserOutput->eHasMainImageAlready) && $parserOutput->eHasMainImageAlready )
return $mainimage;
$file = Title::newFromText( $mainimage, NS_FILE );
$parserOutput->addOutputHook( 'setmainimage', array( 'dbkey' => $file->getDBkey() ) );
$parserOutput->eHasMainImageAlready = true;
return $mainimage;
}
$wgParserOutputHooks['setmainimage'] = 'efSetMainImagePH';
function efSetMainImagePH( $out, $parserOutput, $data ) {
$out->mMainImage = wfFindFile( Title::newFromDBkey($data['dbkey'], NS_FILE) );
}
$wgHooks['BeforePageDisplay'][] = 'efOpenGraphMetaPageHook';
function efOpenGraphMetaPageHook( &$out, &$sk ) {
global $wgLogo, $wgSitename, $wgXhtmlNamespaces, $egFacebookAppId, $egFacebookAdmins;
$wgXhtmlNamespaces["og"] = "http://opengraphprotocol.org/schema/";
$title = $out->getTitle();
$isMainpage = $title->isMainPage();
$meta = array();
if ( $isMainpage ) {
$meta["og:type"] = "website";
$meta["og:title"] = $wgSitename;
} else {
$meta["og:type"] = "article";
$meta["og:site_name"] = $wgSitename;
// Try to chose 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();
}
}
if ( isset( $out->mMainImage ) && ( $out->mMainImage !== false ) ) {
if( is_object( $out->mMainImage ) ){
$meta["og:image"] = wfExpandUrl($out->mMainImage->createThumb(100*3, 100));
} else {
// In some edge-cases we won't have defined an object but rather a full URL.
$meta["og:image"] = $out->mMainImage;
}
} elseif ( $isMainpage ) {
$meta["og:image"] = wfExpandUrl($wgLogo);
}
if ( isset($out->mDescription) ) { // set by Description2 extension, install it if you want proper og:description support
$meta["og:description"] = $out->mDescription;
}
$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 ) {
if ( isset( OutputPage::$metaAttrPrefixes ) && isset( OutputPage::$metaAttrPrefixes['property'] ) ) {
$out->addMeta( "property:$property", $value );
} else {
$out->addHeadItem("meta:property:$property", " ".Html::element( 'meta', array( 'property' => $property, 'content' => $value ) )."\n");
}
}
}
return true;
}
$egFacebookAppId = null;
$egFacebookAdmins = null;