-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSlimboxThumbs.php
161 lines (147 loc) · 4.99 KB
/
SlimboxThumbs.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
<?php
/**
* SlimboxThumbs extension /REWRITTEN/
* Originally https://www.mediawiki.org/wiki/Extension:SlimboxThumbs
* Now it does the same, but the code is totally different
* Required MediaWiki: 1.13+
*
* This extension includes a copy of Slimbox.
* It has one small modification: caption is animated together
* with image container, instead of original annoying consecutive animation.
* Also "autoloader" is removed from slimbox2.js, and there is an additional
* slimboxthumbs.js file.
*
* You can however get your own copy of Slimbox and use it by replacing the
* included one: http://www.digitalia.be/software/slimbox2
*
* @license GNU GPL 3.0 or later: http://www.gnu.org/licenses/gpl.html
* CC-BY-SA should not be used for software, moreover it's incompatible with GPL, and MW is GPL.
*
* @file SlimboxThumbs.php
*
* @author Vitaliy Filippov <[email protected]>
*/
use MediaWiki\MediaWikiServices;
if ( !defined( 'MEDIAWIKI' ) ) {
die( 'Not an entry point.' );
}
define( 'SlimboxThumbs_VERSION', '2014-04-01' );
// Register the extension credits.
$wgExtensionCredits['other'][] = array(
'path' => __FILE__,
'name' => 'SlimboxThumbs',
'url' => 'https://www.mediawiki.org/wiki/Extension:SlimboxThumbs',
'author' => array(
'[http://yourcmc.ru/wiki/User:VitaliyFilippov Vitaliy Filippov], ' .
'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw].'
),
'descriptionmsg' => 'slimboxthumbs-desc',
'version' => SlimboxThumbs_VERSION,
);
$dir = dirname( __FILE__ ) . '/';
$wgMessagesDirs['SlimboxThumbs'] = __DIR__ . '/i18n';
$wgHooks['BeforePageDisplay'][] = 'efSBTAddScripts';
$wgAjaxExportList[] = 'efSBTGetImageSizes';
$wgAjaxExportList[] = 'efSBTRemoteThumb';
$wgResourceModules += array(
'ext.slimboxthumbs' => [
'scripts' => [
'js/slimbox2.js',
'slimboxthumbs.js',
],
'styles' => 'css/slimbox2.css',
'localBasePath' => __DIR__ . '/slimbox',
'remoteExtPath' => 'SlimboxThumbs/slimbox',
] );
// Ajax handler to get image sizes
function efSBTGetImageSizes( $names ) {
$result = array();
$user = RequestContext::getMain()->getUser();
foreach ( explode( ':', $names ) as $name ) {
if ( !isset( $result[$name] ) ) {
$title = Title::makeTitle( NS_FILE, $name );
if ( $title ) {
if ( class_exists( 'MediaWiki\Permissions\PermissionManager' ) ) {
// MW 1.33+
$userCan = \MediaWiki\MediaWikiServices::getInstance()
->getPermissionManager()
->userCan( 'read', $user, $title );
} else {
$userCan = $title->userCan( 'read' );
}
if ( $userCan ) {
if ( method_exists( MediaWikiServices::class, 'getRepoGroup' ) ) {
// MediaWiki 1.34+
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $title );
} else {
$file = wfFindFile( $title );
}
if ( $file && $file->getWidth() ) {
$result[ $name ] = array(
'width' => $file->getWidth(),
'height' => $file->getHeight(),
'url' => $file->getFullUrl(),
'local' => $file->isLocal(),
);
}
}
}
}
}
return json_encode( $result );
}
// Not really an AJAX function, used to generate thumbnails for non-local images.
// Needed because thumb.php only handles local images.
function efSBTRemoteThumb( $name, $width ) {
if ( method_exists( MediaWikiServices::class, 'getRepoGroup' ) ) {
// MediaWiki 1.34+
$img = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $name );
} else {
$img = wfFindFile( $name );
}
if ( $img && $img->exists() && !$img->isLocal() ) {
if ( class_exists( 'MediaWiki\Permissions\PermissionManager' ) ) {
// MW 1.33+
$user = RequestContext::getMain()->getUser();
$userCan = \MediaWiki\MediaWikiServices::getInstance()
->getPermissionManager()
->userCan( 'read', $user, $img->getTitle() );
} else {
$userCan = $img->getTitle()->userCan( 'read' );
}
if ( $userCan ) {
try {
$thumb = $img->transform( array( 'width' => $width ), 0 );
} catch( Exception $ex ) {
$thumb = false;
}
if ( $thumb && !$thumb->isError() ) {
/**
* Thumbnails for foreign images have mPath == 'bogus'.
* So, what hack is better? Redirect to $thumb->getUrl() or
* make up the path using document root and stream the file?
* Second will work for closed intranet wikis, so I'm using it by now.
*
* Redirect code:
* header( 'HTTP/1.1 301 Moved Permanently' );
* header( 'Location: '.$thumb->getUrl() );
*/
global $IP;
require_once "$IP/includes/StreamFile.php";
StreamFile::stream( $_SERVER['DOCUMENT_ROOT'].$thumb->getUrl() );
exit;
}
}
}
return 'Error generating thumbnail';
}
// Adds javascript files and stylesheets.
function efSBTAddScripts( $out ) {
global $wgServer, $wgScriptPath, $wgArticlePath;
$out->addModules( "ext.slimboxthumbs" );
$re = str_replace( '\\$1', '[^:]+:(.*)', preg_quote( $wgArticlePath ) );
$out->addInlineScript( "$( window ).on( 'load', function() {".
"makeSlimboxThumbs( jQuery, \"".addslashes( $re ).
"\", \"".addslashes( $wgServer.$wgScriptPath )."\" ); } );" );
return true;
}