-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArticle.php
185 lines (162 loc) · 4.59 KB
/
Article.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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[2],
'width' => $image[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>'
);
}
}
}
?>