Skip to content

Commit

Permalink
Merge branch 'pr/25'
Browse files Browse the repository at this point in the history
  • Loading branch information
andru committed Jan 8, 2018
2 parents 4b94649 + 4e5f3cd commit 30bb8c3
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 42 deletions.
172 changes: 130 additions & 42 deletions WikiSEO.body.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,29 @@ class WikiSEO{
'google',
'googlebot',
'og:image',
'article:author',
'article:publisher',
'og:image:width',
'og:image:height',
'og:type',
'og:site_name',
'og:locale',
'article:tag',
'og:url',
'og:title',
'og:updated_time',
'article:author',
'article:publisher',
'article:published_time',
'article:modified_time',
'article:section',
'fb:admins',
'fb:app_id',
'article:tag',
'twitter:card',
'twitter:site',
'twitter:domain',
'twitter:creator',
'twitter:image:src',
'twitter:description'
'twitter:description',
'DC.date.issued',
'DC.date.created',
'name',
);

protected static $tag_types = array(
Expand All @@ -46,21 +54,32 @@ class WikiSEO{
'google' => 'meta',
'googlebot' => 'meta',
'og:image' => 'property',
'article:author' => 'property',
'article:publisher' => 'property',
'og:image:width' => 'property',
'og:image:height' => 'property',
'og:type' => 'property',
'og:site_name' => 'property',
'og:locale' => 'property',
'article:tag' => 'property',
'og:url' => 'property',
'og:title' => 'property',
'og:updated_time' => 'property',
'article:author' => 'property',
'article:publisher' => 'property',
'article:published_time' => 'property',
'article:modified_time' => 'property',
'article:section' => 'property',
'article:tag' => 'property',
'fb:admins' => 'property',
'fb:app_id' => 'property',
'twitter:card' => 'meta',
'twitter:site' => 'meta',
'twitter:domain' => 'meta',
'twitter:creator' => 'meta',
'twitter:image:src' => 'meta',
'twitter:description' => 'meta'
'twitter:description' => 'meta',
'DC.date.issued' => 'property',
'DC.date.created' => 'property',
'name' => 'property',
'google-site-verification' => 'meta'
);
//valid title modes
protected static $valid_title_modes = array('prepend', 'append', 'replace');
Expand Down Expand Up @@ -110,15 +129,15 @@ public static function parserTag( $text, $params = array(), Parser $parser ) {

$params = self::processParams($params, $parser);

//ensure at least one of the required parameters has been set, otherwise display an error
//ensure at least one of the required parameters has been set, otherwise display an error
if( empty($params) ){
return '<div class="errorbox">' . wfMsgForContent('seo-empty-attr') . '</div>';
}
return '<div class="errorbox">' . wfMsgForContent('seo-empty-attr') . '</div>';
}

//render the tags
$html = self::renderParamsAsHtmlComments( $params );
//render the tags
$html = self::renderParamsAsHtmlComments( $params );

return $html;
return $html;

}

Expand Down Expand Up @@ -157,6 +176,7 @@ public static function parserFunction(Parser $parser ){
* @return Array An array of processed params
*/
protected static function processParams($params, $parser=null){
global $wgGoogleSiteVerificationKey, $wgFacebookAdmins, $wgFacebookAppID;

//correct params for compatibility with "HTML Meta and Title" extension
foreach(self::$convert_params as $from => $to){
Expand All @@ -168,11 +188,35 @@ protected static function processParams($params, $parser=null){

$processed = array();

if ($wgGoogleSiteVerificationKey !== null) {
$processed['google-site-verification'] = $wgGoogleSiteVerificationKey;
}

if ($wgFacebookAppID !== null) {
$processed['fb:app_id'] = filter_var($wgFacebookAppID, FILTER_SANITIZE_NUMBER_INT);
}

if ($wgFacebookAdmins !== null && is_array($wgFacebookAdmins)) {
$admins = '';
foreach ($wgFacebookAdmins as $admin) {
$admins .= filter_var($admin, FILTER_SANITIZE_NUMBER_INT).',';
}
rtrim($admins, ',');
$processed['fb:admins'] = $admins;
} elseif ($wgFacebookAdmins !== null) {
$processed['fb:admins'] = filter_var($wgFacebookAdmins, FILTER_SANITIZE_NUMBER_INT);
}

//ensure only valid parameter names are processed
foreach(self::$valid_params as $p){
if( isset($params[$p]) ){
//if the parser has been passed and the param is parsable parse it, else simply assign it
$processed[$p] = ($parser && in_array($p, self::$parse_params)) ? $parser->recursiveTagParse($params[$p]) : $params[$p];
if ($parser !== null && in_array($p, self::$parse_params)) {
$processed[$p] = $parser->recursiveTagParseFully($params[$p]);
$processed[$p] = strip_tags($processed[$p]);
} else {
$processed[$p] = $params[$p];
}
}
}
//set the processed values as class properties
Expand Down Expand Up @@ -212,11 +256,14 @@ protected static function processParams($params, $parser=null){
* @return String A HTML string of comments
*/
protected static function renderParamsAsHtmlComments( $params ){
$html = '';
$html = '<!--seostart--><p id="wikiseo'.wfRandomString(4).'"><!--'."\n";
foreach($params as $k => $v){
$html .= '<!-- WikiSEO:'.$k.';'.base64_encode($v).' -->';
if (!empty($v))
{
$html .= 'WikiSEO:'.$k.';'.base64_encode($v)."\n";
}
}
return $html;
return $html.'--></p><!--seoend-->';
}

/**
Expand All @@ -229,22 +276,23 @@ protected static function renderParamsAsHtmlComments( $params ){
*/
public static function loadParamsFromWikitext( $out, &$text ) {

# Extract meta keywords
if (!preg_match_all(
'/<!-- WikiSEO:([:a-zA-Z_-]+);([0-9a-zA-Z\\+\\/]+=*) -->\n?/m',
$text,
$matches,
PREG_SET_ORDER)
){
return true;
}

foreach($matches as $match){
$params[$match[1]] = base64_decode($match[2]);
$text = str_replace($match[0], '', $text);
}
self::processParams($params);
return true;
# Extract meta keywords
if (!preg_match_all(
'/^(?:<p>)?WikiSEO:([\.:a-zA-Z_-]+);([0-9a-zA-Z\+\/]+=*)\n?\r?$/m',
$text,
$matches,
PREG_SET_ORDER)
){
return true;
}

$params = array();
foreach($matches as $match){
$params[$match[1]] = base64_decode($match[2]);
}
$text = preg_replace('/<!--seostart--><p id="wikiseo[a-zA-Z0-9]{4}">.*<\/p><!--seoend-->/s', '', $text);
self::processParams($params);
return true;
}

/**
Expand All @@ -255,6 +303,10 @@ public static function loadParamsFromWikitext( $out, &$text ) {
* @param OutputPage $out
*/
public static function modifyHTML ( $out ) {
global $wgAddJSONLD;

$jsonLD = '<script type="application/ld+json">{"@context" : "http://schema.org",';

//set title
if(!empty(self::$title)){
switch(self::$title_mode){
Expand All @@ -268,28 +320,64 @@ public static function modifyHTML ( $out ) {
default:
$title = self::$title;
}
$title = preg_replace( "/\r|\n/", "", $title );
$out->setHTMLTitle($title);
$out->addMeta( "twitter:title", $title );
$out->addHeadItem("og:title", "<meta property=\"og:title\" content=\"$title\" />" . "\n");
$out->addHeadItem("og:title", Html::element( 'meta', array( 'property' => 'og:title', 'content' => $title ) ));
$jsonLD .= '"name":"'.$title.'","headline":"'.$title.'",';
}
//set meta tags
if(!empty(self::$meta)){
foreach(self::$meta as $name => $content){
$content = preg_replace( "/\r|\n/", "", $content );
if ($name == 'description') {
if (strlen($content) > 150) {
$content = substr($content, 0, 150).'...';
}
$out->addMeta( $name, $content );
$out->addMeta( "twitter:description", $content );
$out->addHeadItem("og:description", Html::element( 'meta', array( 'property' => 'og:description', 'content' => $content ) ) . "\n");
}
else {
$out->addHeadItem("og:description", Html::element( 'meta', array( 'property' => 'og:description', 'content' => $content ) ));
$jsonLD .= '"description":"'.$content.'",';
} else {
$out->addMeta( $name, $content );
}

}
}
//set property tags
if(!empty(self::$property)){
if (isset(self::$property['og:type'])) {
$jsonLD .= '"@type" : "'.ucfirst(self::$property['og:type']).'"';
}

if (isset(self::$property['name'])) {
$jsonLD .= ',"name" : "'.self::$property['name'].'","headline":"'.self::$property['name'].'"';
}

if (isset(self::$property['article:modified_time'])) {
$jsonLD .= ',"datePublished" : "'.self::$property['article:modified_time'].'","dateModified" : "'.self::$property['article:modified_time'].'"';
}

if (isset(self::$property['og:image'])) {
$jsonLD .= ',"image" : "'.self::$property['og:image'].'"';
}

if (isset(self::$property['og:url'])) {
$jsonLD .= ',"url" : "'.self::$property['og:url'].'", "mainEntityOfPage":"'.self::$property['og:url'].'"';
}

if (isset(self::$property['article:author'])) {
$jsonLD .= ',"publisher":{"@type" : "Organization","name" : "Star Citizen Wiki", "logo": { "@type": "ImageObject", "url": "https://v3.star-citizen.wiki/images/e/ef/Star_Citizen_Wiki_Logo.png"}}, "author":{"@type":"Person","name":"'.self::$property['article:author'].'"}';
}

$jsonLD = $jsonLD.'}</script>';

if ($wgAddJSONLD === true){
$out->addHeadItem('jsonld', $jsonLD);
}

foreach(self::$property as $property => $content){
$out->addHeadItem("$property", Html::element( 'meta', array( 'property' => $property, 'content' => $content ) ) . "\n");
$content = preg_replace( "/\r|\n/", "", $content );
$out->addHeadItem($property, Html::element( 'meta', array( 'property' => $property, 'content' => $content ) ) . "\n");
}
}

Expand Down
5 changes: 5 additions & 0 deletions WikiSEO.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@

//set the tags
$wgHooks['BeforePageDisplay'][] = 'WikiSEO::modifyHTML';

$wgGoogleSiteVerificationKey = null;
$wgFacebookAdmins = null;
$wgFacebookAppID = null;
$wgAddJSONLD = true;
}
6 changes: 6 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
"BeforePageDisplay": [
"WikiSEO::modifyHTML"
]
},
"config": {
"GoogleSiteVerificationKey": "",
"FacebookAdmins": [],
"FacebookAppID": "",
"AddJSONLD": true
}
}

0 comments on commit 30bb8c3

Please sign in to comment.