Skip to content

Commit 3b6d02d

Browse files
committed
Major version update
Deprecated old fashion, use a more reasonable approach instead
1 parent ef854f1 commit 3b6d02d

8 files changed

+65
-30
lines changed

Avatar_body.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

44
class Avatars {
55

6-
public static function getLinkFor($username) {
6+
public static function getLinkFor($username, $res = false) {
77
global $wgScriptPath;
8-
return "$wgScriptPath/extensions/Avatar/avatar.php?$username";
8+
$path = "$wgScriptPath/extensions/Avatar/avatar.php?user=$username";
9+
if ($res !== false) {
10+
return $path . '&res=' . $res;
11+
} else {
12+
return $path;
13+
}
914
}
1015

1116
public static function normalizeResolution($res) {

README.md

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
1-
# Avatar
1+
# Avatar 1.0.0
22
Yet another avatar architecture for MediaWiki
33

4+
**Note.** There are API changes when upgrading 0.9.2 to 1.0.0. The change is very likely to break your site. See section below for details.
5+
46
## Install
57
* Install php-gd, which is a dependency of this extension
68
* Clone the respository, rename it to Avatar and copy to extensions folder
7-
* Add wfLoadExtension('Avatar'); to your LocalSettings.php
9+
* Add `wfLoadExtension('Avatar')`; to your LocalSettings.php
810
* You are done!
911

1012
## Configuration
11-
* $wgDefaultAvatar (string), should be set to the URL of the default avatar.
12-
* $wgAllowedAvatarRes (array), default value is array(64, 128). Thumbnails will be created upon request when their size is in this list.
13-
* $wgMaxAvatarResolution (integer), default value is 256. This limits maximum resolution of image to be uploaded.
14-
* $wgDefaultAvatarRes (integer), default value is 128. This is the fallback option if resolution is not specified.
13+
* `$wgDefaultAvatar` (string), should be set to the URL of the default avatar.
14+
* `$wgAllowedAvatarRes` (array), default value is array(64, 128). Thumbnails will be created upon request when their size is in this list.
15+
* `$wgMaxAvatarResolution` (integer), default value is 256. This limits maximum resolution of image to be uploaded.
16+
* `$wgDefaultAvatarRes` (integer), default value is 128. This is the fallback option if resolution is not specified.
1517
* You can set user rights:
16-
* avatarupload: User need this right to upload ones' own avatar.
17-
* avataradmin: User need this right to delete others' avatars.
18+
* `avatarupload`: User need this right to upload ones' own avatar.
19+
* `avataradmin`: User need this right to delete others' avatars.
1820

1921
## How to use
20-
* Set avatar in user preference, and then $wgScriptPath/extensions/Avatar/avatar.php?username will be redirected to your avatar.
22+
* Set avatar in user preference, and then `$wgScriptPath/extensions/Avatar/avatar.php?user=username` will be redirected to your avatar.
2123
* You can set alias for this php to make it shorter.
24+
25+
## Detailed API
26+
* Uploading Avatar: No API provided yet, but one can post to `Special:UploadAvatar` (or its localized equivalent). The only form data required is `avatar`, which should be set to the data uri of the image.
27+
* Displaying Avatar: This extension provides an entry point for MediaWiki `avatar.php`. This entry point produces result via a 302 redirect. This approach is used to maximize performance while still utilizing MediaWiki core. There are currently 4 available arguments.
28+
* `user` set to the user of who you want to enquery the avatar
29+
* `res` the preferred resolution of the avatar. Note that this is only a hint and the actual result might not be of the resolution. This parameter is valid only if `user` is set.
30+
* `ver` a version number which will be appended to the location field of redirection. Can be used to circumvent browser/CDN cache.
31+
* `nocache` if this parameter is set, then no `cache-control` header will be emitted.
2232

2333
## Extra resources
2434
* If you are using Gadgets
2535
* If you want to display the avatar on the top-right navigation bar, you may find Gadget-ShowAvatar in example folder useful.
2636
* If you want to display avatars before user link, you may find Gadget-UserLinkAvatar in example folder useful.
2737

38+
## Upgrading from <1.0.0 to 1.0.0
39+
* `wgScriptPath/extensions/Avatar/avatar.php?username` was changed to `wgScriptPath/extensions/Avatar/avatar.php?user=username`
40+
* `wgScriptPath/extensions/Avatar/avatar.php?username/resolution` was changed to `wgScriptPath/extensions/Avatar/avatar.php?user=username&res=resolution`
41+
* The change affects all Gadgets and depending extensions.
42+
* Upgrading is easy: changing all occurrence of above url to the new fashion.

SpecialView.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function execute($par) {
5858

5959
if ($haveAvatar) {
6060
$html = \Xml::tags('img', array(
61-
'src' => Avatars::getLinkFor($user) . '/original',
61+
'src' => Avatars::getLinkFor($user, 'original'),
6262
'height' => 400,
6363
), '');
6464
$html = \Xml::tags('p', array(), $html);

assets/upload.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var startY;
1717

1818
// Objects
1919
var submitButton = $('[type=submit]');
20-
var currentAvatar = $('<div>').append($('<img class="current-avatar">').attr('src', mw.config.get('wgScriptPath') + '/extensions/Avatar/avatar.php?' + mw.user.id() + '/original'));
20+
var currentAvatar = $('<div>').append($('<img class="current-avatar">').attr('src', mw.config.get('wgScriptPath') + '/extensions/Avatar/avatar.php?user=' + mw.user.id() + '&res=original&nocache&ver=' + Math.floor(Date.now()/1000).toString(16)));
2121
var container = $('<div class="cropper-container" disabled=""/>');
2222
var imageObj = $('<img src=""></img>');
2323
var selector = $('<div class="cropper"><div class="tl-resizer"/><div class="tr-resizer"/><div class="bl-resizer"/><div class="br-resizer"/><div class="round-preview"/></div>');

avatar.php

+29-14
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,49 @@
1414
return;
1515
}
1616

17-
// Parse request
18-
$par = urldecode($wgRequest->getRawQueryString());
17+
$query = $wgRequest->getQueryValues();
1918

20-
global $wgDefaultAvatar, $wgDefaultAvatarRes;
19+
global $wgDefaultAvatar;
2120
$path = $wgDefaultAvatar;
2221

23-
if ($par) {
24-
// Parse parts
25-
$parts = explode('/', $par, 2);
26-
$username = $parts[0];
27-
$res = count($parts) === 2 ? \Avatar\Avatars::normalizeResolution($parts[1]) : $wgDefaultAvatarRes;
22+
if (isset($query['user'])) {
23+
$username = $query['user'];
24+
25+
if (isset($query['res'])) {
26+
$res = \Avatar\Avatars::normalizeResolution($query['res']);
27+
} else {
28+
global $wgDefaultAvatarRes;
29+
$res = $wgDefaultAvatarRes;
30+
}
31+
2832
$user = User::newFromName($username);
2933
if ($user) {
3034
$path = \Avatar\Avatars::getAvatar($user, $res);
3135
}
3236
}
3337

38+
// ver will be propagated to the relocated image
39+
if (isset($query['ver'])) {
40+
if (strpos($path, '?') !== false) {
41+
$path .= '&ver=' . $query['ver'];
42+
} else {
43+
$path .= '?ver=' . $query['ver'];
44+
}
45+
}
46+
3447
$response = $wgRequest->response();
3548

3649
// We use send custom header, in order to control cache
3750
$response->statusHeader('302');
3851

39-
// Cache longer time if it is not the default avatar
40-
// As it is unlikely to be deleted
41-
if ($path === $wgDefaultAvatar) {
42-
$response->header('Cache-Control: public, max-age=3600');
43-
} else {
44-
$response->header('Cache-Control: public, max-age=86400');
52+
if (!isset($query['nocache'])) {
53+
// Cache longer time if it is not the default avatar
54+
// As it is unlikely to be deleted
55+
if ($path === $wgDefaultAvatar) {
56+
$response->header('Cache-Control: public, max-age=3600');
57+
} else {
58+
$response->header('Cache-Control: public, max-age=86400');
59+
}
4560
}
4661

4762
$response->header('Location: ' . $path);

example/Gadget-ShowAvatar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
var img = $('<img>').attr('src', mw.config.get('wgScriptPath') + '/extensions/Avatar/avatar.php?' + mw.user.id());
1+
var img = $('<img>').attr('src', mw.config.get('wgScriptPath') + '/extensions/Avatar/avatar.php?user=' + mw.user.id());
22
var link = $('<a>').attr('href', mw.util.getUrl('Special:UploadAvatar')).append(img);
33
$('#pt-userpage').before($('<li id="pt-avatar"></li>').append(link));

example/Gadget-UserLinkAvatar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
$('.mw-userlink').each(function(_, item) {
22
item = $(item);
3-
item.prepend($('<img/>').addClass('userlink-avatar').attr('src', mw.config.get('wgScriptPath') + '/extensions/Avatar/avatar.php?' + item.text()));
3+
item.prepend($('<img/>').addClass('userlink-avatar').attr('src', mw.config.get('wgScriptPath') + '/extensions/Avatar/avatar.php?user=' + item.text()));
44
});

extension.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"author": "Gary Guo",
44
"url": "https://github.com/nbdd0121/MW-Avatar",
55
"descriptionmsg": "avatar_desc",
6-
"version": "0.9.2",
6+
"version": "1.0.0",
77
"license-name": "BSD-2-Clause",
88
"type": "specialpage",
99
"ExtensionMessagesFiles": {

0 commit comments

Comments
 (0)