forked from tinymighty/wiki-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikiSEO.body.php
224 lines (192 loc) · 6.04 KB
/
WikiSEO.body.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* Body file for extension WikiSEO.
*
* @file
* @ingroup Extensions
*/
class WikiSEO{
//array of valid parameter names
protected static $valid_params = array(
'title',
'title_mode', //append, prepend, replace
'keywords',
'description',
'google-site-verification'
);
protected static $tag_types = array(
'title' => 'title',
'keywords' => 'meta',
'description' => 'meta',
'google-site-verification' => 'meta'
);
//valid title modes
protected static $valid_title_modes = array('prepend', 'append', 'replace');
//allow other parameter names... these will be converted internally
protected static $convert_params = array(
'metakeywords'=>'keywords',
'metak'=>'keywords',
'metadescription'=>'description',
'metad'=>'description',
'titlemode'=>'title_mode',
'title mode'=>'title_mode'
);
//parameters which should be parsed if possible to allow for the expansion of templates
protected static $parse_params = array('title', 'description', 'keywords');
protected static $title = '';
protected static $title_mode = 'replace';
protected static $meta = array();
//do not allow this class to be instantiated, it is static
private function __construct(){ }
public static function init(Parser $wgParser){
$wgParser->setHook( 'seo', 'WikiSEO::parserTag' );
$wgParser->setFunctionHook( 'seo', 'WikiSEO::parserFunction' );
return true;
}
/**
* Parse the values input from the <seo> tag extension
* @param String $text The text content of the tag
* @param Array $params The HTML attributes of the tag
* @param Parser $parser The active Parser instance
* @return String The HTML comments of cached attributes
*/
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
if( empty($params) ){
return '<div class="errorbox">' . wfMsgForContent('seo-empty-attr') . '</div>';
}
//render the tags
$html = self::renderParamsAsHtmlComments( $params );
return $cached;
}
/**
* Parse the values input from the {{#seo}} parser function
* @param Parser $parser The active Parser instance
* @return Array Parser options and the HTML comments of cached attributes
*/
public static function parserFunction(Parser $parser ){
$args = func_get_args();
$args = array_slice($args, 1, count($args) );
$params = array();
foreach($args as $a){
if(strpos($a, '=')){
$exploded = explode('=', $a);
$params[trim($exploded[0])] = trim($exploded[1]);
}
}
$params = self::processParams($params, $parser);
if( empty($params) ){
return '<div class="errorbox">' . wfMsgForContent('seo-empty-attr') . '</div>';
}
$html = self::renderParamsAsHtmlComments( $params );
return array( $html, 'noparse' => true, 'isHTML' => true );
}
/**
* Processes params (assumed valid) and sets them as class properties
* @param Array $params Array of pre-validated params
* @param Parser $parser If passed, the parser will be used to recursively parse all params
* @return Array An array of processed params
*/
protected static function processParams($params, $parser=null){
//correct params for compatibility with "HTML Meta and Title" extension
foreach(self::$convert_params as $from => $to){
if( isset($params[$from]) ){
$params[$to] = $params[$from];
unset($params[$from]);
}
}
$processed = array();
//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 in
$processed[$p] = ($parser && in_array($p, self::$parse_params)) ? $parser->recursiveTagParse($params[$p]) : $params[$p];
}
}
//set the processed values as class properties
foreach($processed as $k => $v){
if($k==='title'){
self::$title = $v;
}
else
if(self::$tag_types[$k]==='meta'){
self::$meta[$k] = $v;
}
}
return $processed;
}
/**
* Renders the parameters as HTML comment tags in order to cache them in the Wiki text.
*
* When MediaWiki caches pages it does not cache the contents of the <head> tag, so
* to propagate the information in cached pages, the information is stored
* as HTML comments in the Wiki text.
*
* @param Array $params Array of params to render into HTML comments
* @return String A HTML string of comments
*/
protected static function renderParamsAsHtmlComments( $params ){
$html = '';
foreach($params as $k => $v){
$html .= '<!-- WikiSEO:'.$k.';'.base64_encode($v).' -->';
}
return $html;
}
/**
* Convert the attributed cached as HTML comments back into an attribute array
*
* This method is called by the OutputPageBeforeHTML hook
*
* @param OutputPage $out
* @param String $text
*/
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;
}
/**
* Modify the HTML to set the relevant tags to the specified values
*
* This method is called by the BeforePageDisplay hook
*
* @param OutputPage $out
*/
public static function modifyHTML ( $out ) {
//set title
if(!empty(self::$title)){
switch(self::$title_mode){
case 'append':
$title = $out->getPageTitle() . ' - ' . self::$title;
break;
case 'prepend':
$title = self::$title . ' - ' . $out->getPageTitle();
break;
case 'replace':
default:
$title = self::$title;
}
$out->setHTMLTitle($title);
}
//set meta tags
if(!empty(self::$meta)){
foreach(self::$meta as $name => $content){
$out->addMeta( $name, $content );
}
}
return true;
}
}