-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppropedia.php
163 lines (153 loc) · 5.87 KB
/
Appropedia.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
<?php
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\StoreFactory;
/**
* This class contains all code not covered by more specific classes
*/
class Appropedia {
/**
* Add JS and CSS specific to Appropedia
*/
public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
$out->addModules( 'ext.Appropedia' );
$out->addModuleStyles( 'ext.Appropedia.styles' );
$out->addLink( [ 'rel' => 'manifest', 'href' => '/manifest.json' ] );
$out->addLink( [ 'rel' => 'icon', 'type' => 'image/png', 'sizes' => '32x32', 'href' => '/logos/favicon-32x32.png' ] );
$out->addLink( [ 'rel' => 'icon', 'type' => 'image/png', 'sizes' => '16x16', 'href' => '/logos/favicon-16x16.png' ] );
self::setDescriptionTag( $out, $skin );
self::setTitleTags( $out, $skin );
self::setOpenGraphTags( $out, $skin );
}
/**
* Set the description meta tag for SEO purposes
*/
public static function setDescriptionTag( OutputPage $out, Skin $skin ) {
// If the 'Title tag' semantic property is set, just use it and be done
$title = $skin->getTitle();
if ( $title->isContentPage() ) {
$property = DIProperty::newFromUserLabel( 'Description' );
$subject = DIWikiPage::newFromText( $title );
$store = StoreFactory::getStore();
$data = $store->getSemanticData( $subject );
$values = $data->getPropertyValues( $property );
if ( $values ) {
$value = array_shift( $values );
$description = $value->getString();
$out->addMeta( 'description', $description );
}
}
}
/**
* Set or refine the title meta tags for SEO purposes
*/
public static function setTitleTags( OutputPage $out, Skin $skin ) {
// If the 'Title tag' semantic property is set, just use it and be done
$title = $skin->getTitle();
if ( $title->isContentPage() ) {
$property = DIProperty::newFromUserLabel( 'Title tag' );
$subject = DIWikiPage::newFromText( $title );
$store = StoreFactory::getStore();
$data = $store->getSemanticData( $subject );
$values = $data->getPropertyValues( $property );
if ( $values ) {
$value = array_shift( $values );
$titleTag = $value->getString();
$out->setHTMLTitle( $titleTag );
$out->addMeta( 'title', $titleTag );
$out->addMeta( 'og:title', $titleTag );
return;
}
}
// Set the <meta name="title"> and <meta name="og:title"> tags
$pageTitle = $out->getPageTitle();
$pageTitle = strip_tags( $pageTitle );
$out->addMeta( 'title', $pageTitle );
$out->addMeta( 'og:title', $pageTitle );
// If the default <title> tag is too long, remove "Appropedia, the sustainability wiki"
$htmlTitle = $out->getHTMLTitle();
if ( strlen( $htmlTitle ) > 65 ) {
$out->setHTMLTitle( $pageTitle );
}
}
/**
* Set the Open Graph tags for SEO purposes
* See https://ogp.me/
*/
public static function setOpenGraphTags( OutputPage $out, Skin $skin ) {
$out->addMeta( 'og:site_name', 'Appropedia, the sustainability wiki' );
$title = $skin->getTitle();
if ( $title->isContentPage() ) {
$out->addMeta( 'og:type', 'article' );
}
}
/**
* Make "external" links like [https://www.appropedia.org/Water Water] behave as internal links
*/
public static function onLinkerMakeExternalLink( &$url, &$text, &$link, &$attribs, $linktype ) {
global $wgServerName;
$result = parse_url( $url );
if ( $result and array_key_exists( 'host', $result ) and $result['host'] === $wgServerName ) {
$attribs['target'] = '_self';
$attribs['class'] = str_replace( 'external', '', $attribs['class'] );
}
}
/**
* #arraymap parser function
*
* This method is copied from Extension:PageForms
* but we put it here rather than enabling the extension
* because it's a big extension and this is the only thing we use from it
*
* @param Parser $parser Parser object
*/
public static function onParserFirstCallInit( Parser $parser ) {
$parser->setFunctionHook( 'arraymap', function ( Parser $parser, $frame, $args ) {
$value = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$delimiter = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ',';
$variable = isset( $args[2] ) ? trim( $frame->expand( $args[2], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : 'x';
$formula = isset( $args[3] ) ? $args[3] : 'x';
$newDelimiter = isset( $args[4] ) ? trim( $frame->expand( $args[4] ) ) : ', ';
$conjunction = isset( $args[5] ) ? trim( $frame->expand( $args[5] ) ) : $newDelimiter;
$delimiter = $parser->getStripState()->unstripNoWiki( $delimiter );
$delimiter = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $delimiter );
$newDelimiter = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $newDelimiter );
$conjunction = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $conjunction );
if ( $delimiter == '' ) {
$valuesArray = preg_split( '/(.)/u', $value, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
} else {
$valuesArray = explode( $delimiter, $value );
}
$resultsArray = [];
foreach ( $valuesArray as $oldValue ) {
$oldValue = trim( $oldValue );
if ( $oldValue == '' ) {
continue;
}
$resultValue = $frame->expand( $formula, PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES );
$resultValue = str_replace( $variable, $oldValue, $resultValue );
$resultValue = $parser->preprocessToDom( $resultValue, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
$resultValue = trim( $frame->expand( $resultValue ) );
if ( $resultValue == '' ) {
continue;
}
$resultsArray[] = $resultValue;
}
$resultText = '';
if ( $conjunction != $newDelimiter ) {
$conjunction = ' ' . trim( $conjunction ) . ' ';
}
$numValues = count( $resultsArray );
for ( $i = 0; $i < $numValues; $i++ ) {
if ( $i == 0 ) {
$resultText .= $resultsArray[ $i ];
} elseif ( $i == $numValues - 1 ) {
$resultText .= $conjunction . $resultsArray[ $i ];
} else {
$resultText .= $newDelimiter . $resultsArray[ $i ];
}
}
return $resultText;
}, Parser::SFH_OBJECT_ARGS );
}
}