Jack Phoenix has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/357961 )

Change subject: Version 0.5.0 -- extension registration support and more
......................................................................

Version 0.5.0 -- extension registration support and more

* Moved PHP code into a new, separate class & file
* Added extension.json for extension registration support
* Added version number to extension credits

MediaWiki 1.25 or newer is now required

Change-Id: Ied8476ee5e0143d78f291d986c7e79abe3c3b1ce
---
A OpenGraphMeta.class.php
M OpenGraphMeta.php
A extension.json
3 files changed, 149 insertions(+), 99 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/OpenGraphMeta 
refs/changes/61/357961/1

diff --git a/OpenGraphMeta.class.php b/OpenGraphMeta.class.php
new file mode 100644
index 0000000..1ed31bc
--- /dev/null
+++ b/OpenGraphMeta.class.php
@@ -0,0 +1,101 @@
+<?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
+ */
+
+class OpenGraphMeta {
+
+       public static function onParserFirstCallInit( $parser ) {
+               $parser->setFunctionHook( 'setmainimage', array( __CLASS__, 
'setMainImagePF' ) );
+               return true;
+       }
+
+       public static function setMainImagePF( $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;
+       }
+
+       public static function setMainImagePH( $out, $parserOutput, $data ) {
+               $out->mMainImage = wfFindFile( Title::newFromDBkey( 
$data['dbkey'], NS_FILE ) );
+       }
+
+       public static function onBeforePageDisplay( &$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;
+       }
+
+}
\ No newline at end of file
diff --git a/OpenGraphMeta.php b/OpenGraphMeta.php
index 93c25f9..0b62735 100644
--- a/OpenGraphMeta.php
+++ b/OpenGraphMeta.php
@@ -9,102 +9,16 @@
  * @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;
-
+if ( function_exists( 'wfLoadExtension' ) ) {
+       wfLoadExtension( 'OpenGraphMeta' );
+       // Keep i18n globals so mergeMessageFileList.php doesn't break
+       $wgMessagesDirs['OpenGraphMeta'] = __DIR__ . '/i18n';
+       wfWarn(
+               'Deprecated PHP entry point used for OpenGraphMeta extension. ' 
.
+               'Please use wfLoadExtension instead, ' .
+               'see https://www.mediawiki.org/wiki/Extension_registration for 
more details.'
+       );
+       return;
+} else {
+       die( 'This version of the OpenGraphMeta extension requires MediaWiki 
1.25+' );
+}
\ No newline at end of file
diff --git a/extension.json b/extension.json
new file mode 100644
index 0000000..5e84da3
--- /dev/null
+++ b/extension.json
@@ -0,0 +1,35 @@
+{
+       "name": "OpenGraphMeta",
+       "version": "0.5.0",
+       "author": [
+               "[http://danf.ca/mw/ Daniel Friesen]"
+       ],
+       "url": "https://www.mediawiki.org/wiki/Extension:OpenGraphMeta";,
+       "descriptionmsg": "opengraphmeta-desc",
+       "license-name": "GPL-2.0+",
+       "type": "parserhook",
+       "config": {
+               "_prefix": "eg",
+               "FacebookAppId": null,
+               "FacebookAdmins": null
+       },
+       "AutoloadClasses": {
+               "OpenGraphMeta": "OpenGraphMeta.class.php"
+       },
+       "ExtensionMessagesFiles": {
+               "OpenGraphMetaMagic": "OpenGraphMeta.magic.php"
+       },
+       "MessagesDirs": {
+               "OpenGraphMeta": [
+                       "i18n"
+               ]
+       },
+       "Hooks": {
+               "BeforePageDisplay": "OpenGraphMeta::onBeforePageDisplay",
+               "ParserFirstCallInit": "OpenGraphMeta::onParserFirstCallInit"
+       },
+       "ParserOutputHooks": {
+               "setmainimage": "OpenGraphMeta::setMainImagePH"
+       },
+       "manifest_version": 1
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/357961
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ied8476ee5e0143d78f291d986c7e79abe3c3b1ce
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/OpenGraphMeta
Gerrit-Branch: master
Gerrit-Owner: Jack Phoenix <j...@countervandalism.net>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to