-
Notifications
You must be signed in to change notification settings - Fork 2
/
gzipped-xml-types-1.21.diff
181 lines (169 loc) · 6.22 KB
/
gzipped-xml-types-1.21.diff
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
commit 39bae5c32f39dfdf06f6b8494df2bc40459f3848
Author: Vitaliy Filippov <[email protected]>
Date: Thu Jan 16 20:07:21 2014 +0400
Patch: gzipped-xml-types
Type: feature
SVGZ and generic XML-GZIP mime-types support. CustIS Bug 63356
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index ee77e25..2494fa2 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -777,6 +777,7 @@ $wgTrustedMediaFormats = array(
MEDIATYPE_AUDIO, //all audio formats
MEDIATYPE_VIDEO, //all plain video formats
"image/svg+xml", //svg (only needed if inline rendering of svg is not supported)
+ "image/svg+xml+gzip", //gzipped svg
"application/pdf", //PDF files
#"application/x-shockwave-flash", //flash/shockwave movie
);
@@ -795,6 +796,7 @@ $wgMediaHandlers = array(
'image/x-xcf' => 'XCFHandler',
'image/svg+xml' => 'SvgHandler', // official
'image/svg' => 'SvgHandler', // compat
+ 'image/svg+xml+gzip' => 'SvgHandler', // gzipped
'image/vnd.djvu' => 'DjVuHandler', // official
'image/x.djvu' => 'DjVuHandler', // compat
'image/x-djvu' => 'DjVuHandler', // compat
@@ -1106,6 +1108,11 @@ $wgXMLMimeTypes = array(
'html' => 'text/html', // application/xhtml+xml?
);
+$wgCompressedXMLTypes = array(
+ 'image/svg+xml' => 'image/svg+xml+gzip',
+ 'application/x-dia-diagram' => 'application/x-dia-diagram',
+);
+
/**
* Limit images on image description pages to a user-selectable limit. In order
* to reduce disk usage, limits can only be selected from a list.
diff --git a/includes/MimeMagic.php b/includes/MimeMagic.php
index edabd54..616d1e1 100644
--- a/includes/MimeMagic.php
+++ b/includes/MimeMagic.php
@@ -483,7 +483,7 @@ class MimeMagic {
'webp',
// XML formats we sure hope we recognize reliably
- 'svg',
+ 'svg', 'svgz',
);
return in_array( strtolower( $extension ), $types );
}
@@ -675,11 +675,16 @@ class MimeMagic {
*/
$xml = new XmlTypeCheck( $file );
if ( $xml->wellFormed ) {
- global $wgXMLMimeTypes;
- if ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
- return $wgXMLMimeTypes[$xml->getRootElement()];
- } else {
- return 'application/xml';
+ global $wgXMLMimeTypes, $wgCompressedXMLTypes;
+ $t = $wgXMLMimeTypes[$xml->getRootElement()];
+ if ( !$xml->compressed ) {
+ if ( $t ) {
+ return $t;
+ } else {
+ return 'application/xml';
+ }
+ } elseif ( isset( $wgCompressedXMLTypes[$t] ) ) {
+ return $wgCompressedXMLTypes[$t];
}
}
diff --git a/includes/XmlTypeCheck.php b/includes/XmlTypeCheck.php
index 2e18460..e6a8a7a 100644
--- a/includes/XmlTypeCheck.php
+++ b/includes/XmlTypeCheck.php
@@ -40,7 +40,13 @@ class XmlTypeCheck {
public $rootElement = '';
/**
- * @param string $file filename
+ * Name of file compression type (can be only 'gzip' by now),
+ * or FALSE if the file is uncompressed.
+ */
+ public $compressed = false;
+
+ /**
+ * @param $file string filename
* @param $filterCallback callable (optional)
* Function to call to do additional custom validity checks from the
* SAX element handler event. This gives you access to the element
@@ -75,6 +81,19 @@ class XmlTypeCheck {
if ( file_exists( $fname ) ) {
$file = fopen( $fname, "rb" );
if ( $file ) {
+ $gz = fread( $file, 2 );
+ if ( $gz == "\x1F\x8B" ) {
+ if ( function_exists( 'gzopen' ) ) {
+ fclose( $file );
+ $this->compressed = 'gzip';
+ $file = gzopen( $fname, "rb" );
+ } else {
+ return;
+ }
+ } else {
+ fseek( $file, 0, SEEK_SET );
+ }
+
do {
$chunk = fread( $file, 32768 );
$ret = xml_parse( $parser, $chunk, feof( $file ) );
diff --git a/includes/media/SVGMetadataExtractor.php b/includes/media/SVGMetadataExtractor.php
index 0de212b..409697a 100644
--- a/includes/media/SVGMetadataExtractor.php
+++ b/includes/media/SVGMetadataExtractor.php
@@ -64,16 +64,24 @@ class SVGReader {
throw new MWException( "Error getting filesize of SVG." );
}
- if ( $size > $wgSVGMetadataCutoff ) {
- $this->debug( "SVG is $size bytes, which is bigger than $wgSVGMetadataCutoff. Truncating." );
- $contents = file_get_contents( $source, false, null, -1, $wgSVGMetadataCutoff );
- if ( $contents === false ) {
- throw new MWException( 'Error reading SVG file.' );
+ $fp = fopen( $source, 'rb' );
+ if ( !$fp ) {
+ throw new MWException( 'Error reading SVG file.' );
+ }
+ if ( function_exists( 'gzopen' ) && fread( $fp, 3 ) == "\x1f\x8b\x08" ) {
+ fclose( $fp );
+ $fp = gzopen( $source, 'rb' );
+ if ( !$fp ) {
+ throw new MWException( 'Error reading gzip-compressed SVG file.' );
}
- $this->reader->XML( $contents, null, LIBXML_NOERROR | LIBXML_NOWARNING );
+ $this->metadata['compressed'] = true;
} else {
- $this->reader->open( $source, null, LIBXML_NOERROR | LIBXML_NOWARNING );
+ fseek( $fp, 0, 0 );
+ $this->metadata['compressed'] = false;
}
+ $contents = stream_get_contents( $fp, $wgSVGMetadataCutoff );
+ fclose( $fp );
+ $this->reader->XML( $contents, null, LIBXML_NOERROR | LIBXML_NOWARNING );
// Expand entities, since Adobe Illustrator uses them for xmlns
// attributes (bug 31719). Note that libxml2 has some protection
@@ -88,6 +96,7 @@ class SVGReader {
$this->metadata['width'] = self::DEFAULT_WIDTH;
$this->metadata['height'] = self::DEFAULT_HEIGHT;
+ $this->metadata['viewBox'] = '';
// The size in the units specified by the SVG file
// (for the metadata box)
@@ -294,7 +303,8 @@ class SVGReader {
if( $this->reader->getAttribute( 'viewBox' ) ) {
// min-x min-y width height
- $viewBox = preg_split( '/\s+/', trim( $this->reader->getAttribute( 'viewBox' ) ) );
+ $this->metadata['viewBox'] = trim( $this->reader->getAttribute('viewBox') );
+ $viewBox = preg_split( '/\s+/', $this->metadata['viewBox'] );
if( count( $viewBox ) == 4 ) {
$viewWidth = $this->scaleSVGUnit( $viewBox[2] );
$viewHeight = $this->scaleSVGUnit( $viewBox[3] );
diff --git a/includes/mime.types b/includes/mime.types
index a89d229..e9856f5 100644
--- a/includes/mime.types
+++ b/includes/mime.types
@@ -85,6 +85,7 @@ image/jp2 j2k jp2 jpg2
image/jpeg jpeg jpg jpe
image/png png apng
image/svg+xml svg
+image/svg+xml+gzip svgz
image/tiff tiff tif
image/vnd.djvu djvu djv
image/vnd.wap.wbmp wbmp