forked from wikimedia/mediawiki-extensions-SlimboxThumbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlimboxThumbs.php
131 lines (121 loc) · 3.98 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
<?php
/**
* SlimboxThumbs extension /REWRITTEN/
* Originally http://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]>
*/
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], ' .
'[http://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw].'
),
'descriptionmsg' => 'slimboxthumbs-desc',
'version' => SlimboxThumbs_VERSION,
);
$wgResourceModules['ext.SlimboxThumbs'] = array(
'localBasePath' => __DIR__,
'remoteExtPath' => 'SlimboxThumbs',
'dependencies' => [],
'styles' => [
'slimbox/css/slimbox2.css',
],
'scripts' => [
'slimbox/src/slimbox2.js',
'slimbox/slimboxthumbs.js',
],
);
$dir = dirname( __FILE__ ) . '/';
$wgMessagesDirs['SlimboxThumbs'] = __DIR__ . '/i18n';
$wgExtensionMessagesFiles['SlimboxThumbs'] = $dir . 'SlimboxThumbs.i18n.php';
$wgHooks['ResourceLoaderGetConfigVars'][] = 'efSBTGetVars';
$wgHooks['BeforePageDisplay'][] = 'efSBTAddScripts';
$wgAjaxExportList[] = 'efSBTGetImageSizes';
$wgAjaxExportList[] = 'efSBTRemoteThumb';
// Ajax handler to get image sizes
function efSBTGetImageSizes( $names ) {
$result = array();
foreach ( explode( ':', $names ) as $name ) {
if ( !isset( $result[$name] ) ) {
$title = Title::makeTitle( NS_FILE, $name );
if ( $title && $title->userCan( 'read' ) ) {
$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 ) {
$img = wfFindFile( $name );
if ( $img && $img->exists() && $img->getTitle()->userCan( 'read' ) &&
!$img->isLocal() ) {
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';
}
function efSBTGetVars( &$vars ) {
global $wgServer, $wgScriptPath, $wgArticlePath;
$vars['wgServer'] = $wgServer;
$vars['wgScriptPath'] = $wgScriptPath;
$vars['wgArticlePath'] = $wgArticlePath;
return true;
}
// Adds javascript files and stylesheets.
function efSBTAddScripts( $out ) {
$out->addModules( 'ext.SlimboxThumbs' );
return true;
}