diff --git a/Book.php b/Book.php new file mode 100755 index 0000000..7257082 --- /dev/null +++ b/Book.php @@ -0,0 +1,143 @@ + + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link https://github.com/teran/mediawiki-GoogleRichCards + */ + +namespace MediaWiki\Extension\GoogleRichCards; + +use OutputPage; +use Parser; +use PPFrame; + +if (!defined('MEDIAWIKI')) { + echo("This is a Mediawiki extension and doesn't provide standalone functionality\n"); + die(1); +} + +class Book { + /** + * @var static Book instance for Signleton pattern + */ + public static $instance; + + /** + * Singleon pattern getter + * + * @return Book + */ + public static function getInstance() { + if(!isset(self::$instance)) { + self::$instance = new Book(); + } + + return self::$instance; + } + + public function parse($text, $params = [], Parser $parser, PPFrame $frame) { + $o = array(); + foreach($params as $k => $v) { + $o[$k] = $parser->recursiveTagParse($v, $frame); + } + return ''; + } + + public function render(OutputPage &$out) { + if($out->isArticle()) { + foreach($this->getData($out->mBodytext) as $book) { + $e = json_decode($book); + + $book = array( + '@context' => 'http://schema.org', + '@type' => 'book', + 'name' => $this->getMetadataField($e, 'name', 'name'), + 'publisher' => $e->{'publisher'}, + 'numberOfPages' => $e->{'numberOfPages'}, + 'isbn' => $e->{'isbn'}, + 'bookEdition' => $e->{'bookEdition'}, + 'description' => $this->getMetadataField($e, 'description', 'description'), + 'image' => $this->getRawURL($e->{'image'}), + ); + + + + if($e->{'author'}) { + $book['author'] = array( + '@type' => 'PerformingGroup', + 'name' => $this->getMetadataField($e, 'author', 'author'), + ); + + if($e->{'offer'}) { + $book['offers'] = array( + '@type' => 'Offer', + 'url' => $this->getRawURL($e->{'offerurl'}), + 'price' => $this->getMetadataField($e, 'offerPrice', 'offerprice'), + 'priceCurrency' => $this->getMetadataField($e, 'offerCurrency', 'offercurrency'), + 'availability' => $this->getItemAvailability($e->{'offeravailability'}), + 'validFrom' => $this->getMetadataField($e, 'validFrom', 'validfrom'), + ); + } + } + + $out->addHeadItem( + 'GoogleRichCardsBook_'.$e->{'name'}, + '' + ); + } + } + } + + private function getData($pageText) { + $matches = preg_match_all('//m', $pageText, $extracted); + + return $extracted[1]; + } + + private function getRawURL($htmlLink) { + $matches = preg_match_all('/((https?:\\/\\/)([a-z0-9\.\/_-]+))/i', $htmlLink, $extracted); + + return $extracted[0][0]; + } + + private function getItemAvailability($value) { + switch ($value) { + case "Discontinued": + return "http://schema.org/Discontinued"; + case "InStock": + return "http://schema.org/InStock"; + case "InStoreOnly": + return "http://schema.org/InStoreOnly"; + case "LimitedAvailability": + return "http://schema.org/LimitedAvailability"; + case "OnlineOnly": + return "http://schema.org/OnlineOnly"; + case "OutOfStock": + return "http://schema.org/OutOfStock"; + case "PreOrder": + return "http://schema.org/PreOrder"; + case "PreSale": + return "http://schema.org/PreSale"; + case "SoldOut": + return "http://schema.org/SoldOut"; + } + return ""; + } + + private function getMetadataField($obj, $field, $name) { + $value = $obj->{$name}; + if ($value == '{{{'.$field.'}}}') { + return ""; + } + return $value; + } +} + +?> diff --git a/Hooks.php b/Hooks.php old mode 100644 new mode 100755 index ae5a217..913253d --- a/Hooks.php +++ b/Hooks.php @@ -27,7 +27,7 @@ class Hooks { * @return bool */ public static function onBeforePageDisplay(OutputPage &$out, Skin &$skin) { - global $wgGoogleRichCardsAnnotateArticles, $wgGoogleRichCardsAnnotateEvents, $wgGoogleRichCardsAnnotateWebSite; + global $wgGoogleRichCardsAnnotateArticles, $wgGoogleRichCardsAnnotateEvents, $wgGoogleRichCardsAnnotateBooks, $wgGoogleRichCardsAnnotateWebSite; if($wgGoogleRichCardsAnnotateArticles) { $article = Article::getInstance(); @@ -38,6 +38,12 @@ public static function onBeforePageDisplay(OutputPage &$out, Skin &$skin) { $event = Event::getInstance(); $event->render($out); } + + if($wgGoogleRichCardsAnnotateBooks) { + $book = Event::getInstance(); + $book->render($out); + + } if($wgGoogleRichCardsAnnotateWebSite) { $website = WebSite::getInstance(); @@ -60,7 +66,16 @@ public static function onParserFirstCallInit(Parser &$parser) { $event = Event::getInstance(); $parser->setHook('event', [$event, 'parse']); } - } + + + /* public static function onParserFirstCallInit(Parser &$parser) {*/ + global $wgGoogleRichCardsAnnotateBooks; + + if($wgGoogleRichCardsAnnotateBooks) { + $book = Event::getInstance(); + $parser->setHook('book', [$book, 'parse']); + } + } } diff --git a/README.md b/README.md index 6aec25c..ec793f6 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ $wgGoogleRichCardsAnnotateArticles = true; // Enable annotations for events $wgGoogleRichCardsAnnotateEvents = true; +// Enable annotations for books +$wgGoogleRichCardsAnnotateBooks = true; + // Enable WebSite annotations $wgGoogleRichCardsAnnotateWebSite = true; ``` @@ -55,7 +58,22 @@ Performer: {{{performer}}} Please note, you're free to update this template in order to setup events publishing in your own flavour -### Usage of Event template +### Template:Book +``` + +### Usage of book template + +== Livre == +=== {{{name}}} === + +{{{description}}} + + + +auteur: {{{author}}} + + +