-
Notifications
You must be signed in to change notification settings - Fork 0
/
wowhead.php
147 lines (119 loc) · 4.58 KB
/
wowhead.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
<?php
//
// wowhead.php -- convert <wowhead>Item Name</wowhead> to a colored wowhead link
//
// Version 1.0.2 (18 August 2011)
//
//
// The MIT License
//
// Copyright (c) 2010 Adam Backstrom <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
$wgHooks['ParserFirstCallInit'][] = 'whParserInit';
$wgHooks['BeforePageDisplay'][] = 'whPageDisplay';
$wgHooks['SkinTemplateSetupPageCss'][] = 'whSetupPageCss';
/**
* Look for <wowhead>Item Name</wowhead> tags.
*/
function whParserInit( &$parser ) {
$parser->setHook('wowhead', 'whRender');
return true;
}//end whParserInit
/**
* For each <wowhead> tag, link back to wowhead.com and add a "quality" class to the link.
*/
function whRender( $input, $args, $parser, $frame ) {
global $wgMemc;
$input_name = $input;
$input_id = null;
$key = wfMemcKey( 'wh', $input_name );
$item = $wgMemc->get( $key );
// we didn't have this cached by name; try to cache by the name + rel
if( !is_array($item) && isset($args['rel']) ) {
parse_str($args['rel'], $rel);
if( $rel['item'] ) {
$input_id = $rel['item'];
$key = wfMemcKey( 'wh', $input_id );
$item = $wgMemc->get( $key );
}
}
// additional parameters (enchants, random enchants, gems, etc.) per http://static.wowhead.com/widgets/power/demo.html
if( isset($args['rel']) ) {
$rel = sprintf(' rel="%s"', $args['rel']);
} else {
$rel = '';
}
// first, try to fetch by item id
if( $input_id ) {
$item = whFetch( $input_id );
}
// if we didn't get it from cache (by name or id) try to fetch item by name
if( !is_array($item) ) {
$item = whFetch( $input_name );
}
// still nothing, bail.
if( !is_array($item) ) {
return "[$input_name]";
}
// use the input name here, in case there was an item id override in rel (prevents "Dark Band of Agility" from becoming "Dark Band")
$output = sprintf('<a href="%s" class="q%d"%s>[%s]</a>', $item['link'], $item['quality'], $rel, $input_name);
return $output;
}//end whRender
/**
* Fetch by identifier, which could be a name or id
*/
function whFetch( $identifier ) {
global $wgMemc;
$url = sprintf('http://www.wowhead.com/item=%s&xml', urlencode($identifier));
$raw_xml = Http::get($url);
$xml = simplexml_load_string($raw_xml);
if( (string)$xml->error == 'Item not found!' ) {
return false;
}
$item = array(
'name' => (string)$xml->item->name,
'link' => (string)$xml->item->link,
'quality' => (string)$xml->item->quality['id']
);
$key = wfMemcKey( 'wh', $identifier );
$wgMemc->set( $key, $item );
return $item;
}//end whFetch
/**
* Hook into page display to add the Wowhead JavaScript file.
*/
function whPageDisplay( &$out, &$sk ) {
global $wgProto;
$out->addScript("<script src='{$wgProto}://static.wowhead.com/widgets/power.js'></script>");
return true;
}//end whPageDisplay
/**
* Add some styles to the page header to remove a flash of unstyled links before power.js loads.
*/
function whSetupPageCss( &$out ) {
$out .= '<style type="text/css">
/* copied from http://static.wowhead.com/css/basic.css?699 */
.q{color:#ffd100!important;}.q0,.q0 a{color:#9d9d9d!important;}.q1,.q1 a{color:#fff!important;}.q2,.q2 a{color:#1eff00!important;}.q3,.q3 a{color:#0070dd!important;}.q4,.q4 a{color:#a335ee!important;}.q5,.q5 a{color:#ff8000!important;}.q6,.q6 a{color:#e5cc80!important;}.q7,.q7 a{color:#e5cc80!important;}.q8,.q8 a{color:#ffff98!important;}.q9,.q9 a{color:#71d5ff!important;}.q10,.q10 a{color:#f00!important;}
html body .q1 { color: #000 !important; }
.wowhead-tooltip table { background-color: transparent; }
.wowhead-tooltip .q1 { color: #fff !important; }';
return true;
}//end whSetupPageCss