-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate extions to classes and new load scheme
Signed-off-by: Igor Shishkin <[email protected]>
- Loading branch information
Showing
5 changed files
with
245 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
<?php | ||
/** | ||
* GoogleRichCards | ||
* Google Rich Cards metadata generator for Articles | ||
* | ||
* PHP version 5.4 | ||
* | ||
* @category Extension | ||
* @package GoogleRichCards | ||
* @author Igor Shishkin <[email protected]> | ||
* @license GPL http://www.gnu.org/licenses/gpl.html | ||
* @link https://github.com/teran/mediawiki-GoogleRichCards | ||
*/ | ||
|
||
namespace MediaWiki\Extension\GoogleRichCards; | ||
|
||
use OutputPage; | ||
use Title; | ||
|
||
if (!defined('MEDIAWIKI')) { | ||
echo("This is a Mediawiki extension and doesn't provide standalone functionality\n"); | ||
die(1); | ||
} | ||
|
||
class Article { | ||
/** | ||
* @var static Article instance to use for Singleton pattern | ||
*/ | ||
private static $instance; | ||
|
||
/** | ||
* @var Title current instance of Title received from global $wgTitle | ||
*/ | ||
private $title; | ||
|
||
/** | ||
* @var string Site name received from global $wgSitename | ||
*/ | ||
private $sitename; | ||
|
||
/** | ||
* @var string Server URL received from global $wgServer | ||
*/ | ||
private $server; | ||
|
||
/** | ||
* @var string Wiki logo path received from global $wgLogo | ||
*/ | ||
private $logo; | ||
|
||
/** | ||
* Singleon pattern getter | ||
* | ||
* @return Article | ||
*/ | ||
public static function getInstance() { | ||
if(!isset(self::$instance)) { | ||
self::$instance = new Article(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
/** | ||
* Class constructor | ||
*/ | ||
public function __construct() { | ||
global $wgLogo, $wgServer, $wgSitename, $wgTitle; | ||
|
||
$this->title = $wgTitle; | ||
$this->sitename = $wgSitename; | ||
$this->server = $wgServer; | ||
$this->logo = $wgLogo; | ||
} | ||
|
||
/** | ||
* Return creation time or 0 from current Article | ||
* | ||
* @return int | ||
*/ | ||
public function getCTime() { | ||
$ctime = \DateTime::createFromFormat('YmdHis', $this->title->getEarliestRevTime()); | ||
if($ctime) { | ||
return $ctime->format('c'); | ||
} | ||
return 0; | ||
} | ||
|
||
/** | ||
* Return modification time or 0 from current Article | ||
* | ||
* @return int | ||
*/ | ||
public function getMTime() { | ||
$mtime = \DateTime::createFromFormat('YmdHis', $this->title->getTouched()); | ||
if($mtime) { | ||
return $mtime->format('c'); | ||
} | ||
return 0; | ||
} | ||
|
||
/** | ||
* Return first image and it' resolution from the current Article | ||
* | ||
* @param OutputPage OutputPage instance referencce | ||
* @return array | ||
*/ | ||
public function getIllustration(OutputPage &$out) { | ||
$image = key($out->getFileSearchOptions()); | ||
if($image && $image_object = wfFindFile($image)) { | ||
$image_url = $image_object->getFullURL(); | ||
$image_width = $image_object->getWidth(); | ||
$image_height = $image_object->getHeight(); | ||
} else { | ||
$image_url = $this->server.$this->logo; // Mediawiki logo to be used by default | ||
$image_width = 135; // Default max logo width | ||
$image_height = 135; // Default max logo height | ||
} | ||
|
||
return array($image_url, $image_width, $image_height); | ||
} | ||
|
||
/** | ||
* Render head item with metadata for Google Rich Snippet | ||
* | ||
* @param OutputPage OutputPage instance referencce | ||
*/ | ||
function render(OutputPage &$out) { | ||
if($this->title instanceof Title && $this->title->isContentPage()) { | ||
$mtime = $this->getMtime(); | ||
|
||
$created_timestamp = $this->getCTime(); | ||
$modified_timestamp = $this->getMTime(); | ||
|
||
$first_revision = $this->title->getFirstRevision(); | ||
if($first_revision) { | ||
$author = $first_revision->getUserText(); | ||
} else { | ||
$author = 'None'; | ||
} | ||
|
||
$image = $this->getIllustration($out); | ||
|
||
$article = array( | ||
'@context' => 'http://schema.org', | ||
'@type' => 'Article', | ||
'mainEntityOfPage' => array( | ||
'@type' => 'WebPage', | ||
'@id' => $this->title->getFullURL(), | ||
), | ||
'author' => array( | ||
'@type' => 'Person', | ||
'name' => $author, | ||
), | ||
'headline' => $this->title->getText(), | ||
'dateCreated' => $created_timestamp, | ||
'datePublished' => $created_timestamp, | ||
'dateModified' => $modified_timestamp, | ||
'discussionUrl' => $this->server.'/'.$this->title->getTalkPage(), | ||
'image' => array( | ||
'@type' => 'ImageObject', | ||
'url' => $image[0], | ||
'height' => $image_height[2], | ||
'width' => $image_width[1], | ||
), | ||
'publisher' => array( | ||
'@type' => 'Organization', | ||
'name' => $this->sitename, | ||
'logo' => array( | ||
'@type' => 'ImageObject', | ||
'url' => $this->server.$this->logo, | ||
), | ||
), | ||
'description' => $this->title->getText(), | ||
); | ||
|
||
$out->addHeadItem( | ||
'GoogleRichCardsArticle', | ||
'<script type="application/ld+json">'.json_encode($article).'</script>' | ||
); | ||
} | ||
} | ||
} | ||
|
||
?> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* GoogleRichCards | ||
* Google Rich Cards metadata generator | ||
* | ||
* PHP version 5.4 | ||
* | ||
* @category Extension | ||
* @package GoogleRichCards | ||
* @author Igor Shishkin <[email protected]> | ||
* @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 Skin; | ||
|
||
class Hooks { | ||
/** | ||
* Handle meta elements and page title modification. | ||
* @link https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay | ||
* @param OutputPage &$out The output page. | ||
* @param Skin &$skin The current skin. | ||
* @return bool | ||
*/ | ||
public static function onBeforePageDisplay(OutputPage &$out, Skin &$skin) { | ||
$article = Article::getInstance(); | ||
$article->render($out); | ||
return true; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "GoogleRichCards", | ||
"version": "0.1", | ||
"author": [ | ||
"Igor Shishkin" | ||
], | ||
"url": "https://github.com/teran/mediawiki-GoogleRichCards", | ||
"descriptionmsg": "Google Rich Cards extension", | ||
"license-name": "GPL", | ||
"type": "parserhook", | ||
"MessagesDirs": {}, | ||
"requires": { | ||
"MediaWiki": ">= 1.29.0" | ||
}, | ||
"AutoloadClasses": { | ||
"MediaWiki\\Extension\\GoogleRichCards\\Hooks": "Hooks.php", | ||
"MediaWiki\\Extension\\GoogleRichCards\\Article": "Article.php" | ||
}, | ||
"Hooks": { | ||
"BeforePageDisplay": "MediaWiki\\Extension\\GoogleRichCards\\Hooks::onBeforePageDisplay" | ||
}, | ||
"manifest_version": 2 | ||
} |