Index: trunk/extensions/Description2/Description2.php |
— | — | @@ -0,0 +1,76 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Description2.php -- Adds meaningful description <meta> tag to MW pages and into the parser output |
| 5 | + * Copyright 2010 Daniel Friesen |
| 6 | + * |
| 7 | + * @file |
| 8 | + * @ingroup Extensions |
| 9 | + * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name> |
| 10 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
| 11 | + * |
| 12 | + * This program is free software; you can redistribute it and/or |
| 13 | + * modify it under the terms of the GNU General Public License |
| 14 | + * as published by the Free Software Foundation; either version 2 |
| 15 | + * of the License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License |
| 23 | + * along with this program; if not, write to the Free Software |
| 24 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 25 | + */ |
| 26 | + |
| 27 | +if ( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." ); |
| 28 | + |
| 29 | +$wgExtensionCredits['other'][] = array( |
| 30 | + 'path' => __FILE__, |
| 31 | + 'name' => 'Description2', |
| 32 | + 'version' => "0.1", |
| 33 | + 'author' => 'Daniel Friesen', |
| 34 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:Description2', |
| 35 | + 'description' => 'Adds a description meta-tag to MW pages and into the ParserOutput for other extensions to use', |
| 36 | +); |
| 37 | + |
| 38 | +$wgHooks['ParserAfterTidy'][] = 'egDescription2ParserAfterTidy'; |
| 39 | +function egDescription2ParserAfterTidy( &$parser, &$text ) { |
| 40 | + global $wgContLang; |
| 41 | + $desc = ''; |
| 42 | + |
| 43 | + $myText = preg_replace('%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i', '', $text); |
| 44 | + |
| 45 | + $paragraphs = array(); |
| 46 | + if ( preg_match_all('#<p>.*?</p>#is', $myText, $paragraphs) ) { |
| 47 | + foreach ( $paragraphs[0] as $paragraph ) { |
| 48 | + $paragraph = trim(strip_tags($paragraph)); |
| 49 | + if ( !$paragraph ) |
| 50 | + continue; |
| 51 | + $desc = $paragraph; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if ( $desc ) { |
| 57 | + $pOut = $parser->getOutput(); |
| 58 | + $pOut->setProperty("description", $desc); |
| 59 | + $pOut->addOutputHook("setdescription"); |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | +} |
| 64 | + |
| 65 | +$wgParserOutputHooks['setdescription'] = 'egDescription2ParserOutputSetDescription'; |
| 66 | +function egDescription2ParserOutputSetDescription( $out, $parserOutput, $data ) { |
| 67 | + // Export the description from the main parser output into the OutputPage |
| 68 | + $out->mDescription = $parserOutput->getProperty("description"); |
| 69 | +} |
| 70 | + |
| 71 | +$wgHooks['BeforePageDisplay'][] = 'egDescription2PageHook'; |
| 72 | +function egDescription2PageHook( &$out, &$sk ) { |
| 73 | + if ( isset($out->mDescription) && $out->mDescription ) |
| 74 | + $out->addMeta("description", $out->mDescription); |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
Property changes on: trunk/extensions/Description2/Description2.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 78 | + native |
Index: trunk/extensions/OpenGraphMeta/OpenGraphMeta.magic.php |
— | — | @@ -0,0 +1,11 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +$magicWords = array(); |
| 5 | + |
| 6 | +/** |
| 7 | + * English |
| 8 | + */ |
| 9 | +$magicWords['en'] = array( |
| 10 | + 'setmainimage' => array( 0, 'setmainimage' ), |
| 11 | +); |
| 12 | + |
Property changes on: trunk/extensions/OpenGraphMeta/OpenGraphMeta.magic.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 13 | + native |
Index: trunk/extensions/OpenGraphMeta/OpenGraphMeta.php |
— | — | @@ -0,0 +1,95 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * OpenGraphMeta |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name> |
| 9 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
| 10 | + * |
| 11 | + * This program is free software; you can redistribute it and/or |
| 12 | + * modify it under the terms of the GNU General Public License |
| 13 | + * as published by the Free Software Foundation; either version 2 |
| 14 | + * of the License, or (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 24 | + */ |
| 25 | + |
| 26 | +if ( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." ); |
| 27 | + |
| 28 | +$wgExtensionCredits['parserhook'][] = array ( |
| 29 | + "path" => __FILE__, |
| 30 | + "name" => "OpenGraphMeta", |
| 31 | + "author" => "[http://mediawiki.org/wiki/User:Dantman Daniel Friesen]", |
| 32 | + "description" => "Adds OpenGraph meta tags used by FaceBook's Like button", |
| 33 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:OpenGraphMeta', |
| 34 | +); |
| 35 | + |
| 36 | +$wgExtensionMessagesFiles['OpenGraphMetaMagic'] = dirname( __FILE__ ) . '/OpenGraphMeta.magic.php'; |
| 37 | + |
| 38 | +$wgHooks['ParserFirstCallInit'][] = 'efOpenGraphMetaParserInit'; |
| 39 | +function efOpenGraphMetaParserInit( $parser ) { |
| 40 | + $parser->setFunctionHook( 'setmainimage', 'efSetMainImagePF' ); |
| 41 | + return true; |
| 42 | +} |
| 43 | + |
| 44 | +function efSetMainImagePF( $parser, $mainimage ) { |
| 45 | + $parserOutput = $parser->getOutput(); |
| 46 | + if ( isset($parserOutput->eHasMainImageAlready) && $parserOutput->eHasMainImageAlready ) |
| 47 | + return $mainimage; |
| 48 | + $file = Title::newFromText( $mainimage, NS_FILE ); |
| 49 | + $parserOutput->addOutputHook( 'setmainimage', array( 'dbkey' => $file->getDBkey() ) ); |
| 50 | + $parserOutput->eHasMainImageAlready = true; |
| 51 | + |
| 52 | + return $mainimage; |
| 53 | +} |
| 54 | + |
| 55 | +$wgParserOutputHooks['setmainimage'] = 'efSetMainImagePH'; |
| 56 | +function efSetMainImagePH( $out, $parserOutput, $data ) { |
| 57 | + $out->mMainImage = wfFindFile( Title::newFromDBkey($data['dbkey'], NS_FILE) ); |
| 58 | +} |
| 59 | + |
| 60 | +$wgHooks['BeforePageDisplay'][] = 'efOpenGraphMetaPageHook'; |
| 61 | +function efOpenGraphMetaPageHook( &$out, &$sk ) { |
| 62 | + global $wgArticle, $wgLogo, $wgSitename, $wgXhtmlNamespaces, $egFacebookAppId, $egFacebookAdmins; |
| 63 | + $wgXhtmlNamespaces["og"] = "http://opengraphprotocol.org/schema/"; |
| 64 | + $title = $out->getTitle(); |
| 65 | + $isMainpage = $title->equals(Title::newMainPage()); |
| 66 | + |
| 67 | + $meta = array(); |
| 68 | + |
| 69 | + $meta["og:type"] = $isMainpage ? "website" : "article"; |
| 70 | + $meta["og:site_name"] = $wgSitename; |
| 71 | + $meta["og:title"] = $title->getPrefixedText(); |
| 72 | + if ( isset($out->mMainImage) ) { |
| 73 | + $meta["og:image"] = wfExpandUrl($out->mMainImage->createThumb(100*3, 100)); |
| 74 | + } else if ( $isMainpage ) { |
| 75 | + $meta["og:image"] = $wgLogo; |
| 76 | + } |
| 77 | + if ( isset($out->mDescription) ) // set by Description2 extension, install it if you want proper og:description support |
| 78 | + $meta["og:description"] = $out->mDescription; |
| 79 | + $meta["og:url"] = $title->getFullURL(); |
| 80 | + if ( $egFacebookAppId ) |
| 81 | + $meta["fb:app_id"] = $egFacebookAppId; |
| 82 | + if ( $egFacebookAdmins ) |
| 83 | + $meta["fb:admins"] = $egFacebookAdmins; |
| 84 | + |
| 85 | + foreach( $meta as $property => $value ) { |
| 86 | + if ( $value ) |
| 87 | + //$out->addMeta($property, $value ); // FB wants property= instead of name= blech, is that even valid html? |
| 88 | + $out->addHeadItem("meta:property:$property", " ".Html::element( 'meta', array( 'property' => $property, 'content' => $value ) )."\n"); |
| 89 | + } |
| 90 | + |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +$egFacebookAppId = null; |
| 95 | +$egFacebookAdmins = null; |
| 96 | + |
Property changes on: trunk/extensions/OpenGraphMeta/OpenGraphMeta.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 97 | + native |