-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWebSite.php
90 lines (77 loc) · 2.02 KB
/
WebSite.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
<?php
/**
* GoogleRichCards
* Google Rich Cards metadata generator for WebSites search
*
* PHP version 5.4
*
* @category Extension
* @package GoogleRichCards
* @author Igor Shishkin <me@teran.ru>
* @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 WebSite {
/**
* @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 Server URL received from global $wgServer
*/
private $server;
/**
* Singleon pattern getter
*
* @return WebSite
*/
public static function getInstance() {
if(!isset(self::$instance)) {
self::$instance = new WebSite();
}
return self::$instance;
}
/**
* Class constructor
*/
public function __construct() {
global $wgLogo, $wgServer, $wgSitename, $wgTitle;
$this->title = $wgTitle;
$this->server = $wgServer;
}
/**
* 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()) {
$website = array(
'@context' => 'http://schema.org',
'@type' => 'WebSite',
'url' => $this->server,
'potentialAction' => array(
'@type' => 'SearchAction',
'target' => $this->server.'/index.php?search={search_term_string}',
'query-input' => 'required name=search_term_string',
)
);
$out->addHeadItem(
'GoogleRichCardsWebSite',
'<script type="application/ld+json">'.json_encode($website).'</script>'
);
}
}
}
?>