This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathScratchSig2.php
111 lines (78 loc) · 2.36 KB
/
ScratchSig2.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
<?php
/*
* ScratchSig extension for MediaWiki
* Renders <scratchsig> tag with profile image from scratch.mit.edu
*
* Copyright 2013, Tim Radvan
* MIT Licensed
* http://opensource.org/licenses/MIT
*
*/
if (!defined('MEDIAWIKI')) {
die();
}
// Don't dump DOM errors onto page
error_reporting(0);
// Hooks
$wgExtensionFunctions[] = 'sigSetup';
$wgHooks['ParserFirstCallInit'][] = 'sigParserInit';
// Temporary cache of avatar image URLs
global $sig_imageUrls;
$sig_imageUrls = array();
// Hook callback function into parser
function sigParserInit (Parser $parser) {
// Register <scratchsig> tag
$parser->setHook('scratchsig', 'sigRenderTag');
return true;
}
// Fetch avatar thumbnail url for user from site api
function sigFetchProfile ($username) {
// Fetch page
$data = file_get_contents("http://scratch.mit.edu/site-api/users/all/$username/");
$json = json_decode($data, $assoc=true);
$pk = $json['user']['pk'];
$image_url = "http://cdn.scratch.mit.edu/get_image/user/{$pk}_18x18.png";
return $image_url;
}
// Return the url of the avatar's profile image
// Fetches it if not cached in database
function sigGetAvatarUrl ($username) {
global $sig_imageUrls;
if (!isset($sig_imageUrls[$username])) {
$sig_imageUrls[$username] = sigFetchProfile($username);
}
return $sig_imageUrls[$username];
}
// Called to output HTML for <scratchsig> tag
function sigRenderTag ($input, array $args, Parser $parser, PPFrame $frame) {
$username = $input;
$img_url = sigGetAvatarUrl($username);
$o = '<br>'
. '<span class="scratch-sig">'
. '<a href="/wiki/User:'.$username.'">'
. '<img src="' . $img_url . '" width="18px" height="18px">'
. '</a>'
. ' '
. '<a href="/wiki/User:'.$username.'">'
. '<b>'.$username.'</b>'
. '</a>'
. ' '
. '('
. '<a href="/wiki/User_Talk:'.$username.'">talk</a>'
. ' | '
. '<a href="/wiki/Special:Contributions/'.$username.'">contribs</a>'
. ')'
. '</span>';
return $o;
}
// Make wiki load resources
function sigSetup () {
global $wgOut;
$wgOut->addModules('ext.scratchSig');
}
// Define resources
$wgResourceModules['ext.scratchSig'] = array(
'styles' => 'scratchsig.css',
'localBasePath' => __DIR__,
'remoteExtPath' => 'mw-ScratchSig2'
);