r60284 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60283‎ | r60284 | r60285 >
Date:23:24, 21 December 2009
Author:catrope
Status:ok
Tags:
Comment:
Add ApiSVGProxy extension that proxies SVG files from a (possibly remote) file repository to the local domain. This is needed for Google's SVG support in Internet Explorer to circumvent cross-domain restrictions when grabbing SVGs from Commons.
Modified paths:
  • /trunk/extensions/ApiSVGProxy/ApiSVGProxy.body.php (added) (history)
  • /trunk/extensions/ApiSVGProxy/ApiSVGProxy.php (added) (history)

Diff [purge]

Index: trunk/extensions/ApiSVGProxy/ApiSVGProxy.php
@@ -0,0 +1,36 @@
 2+<?php
 3+/**
 4+ * This program is free software; you can redistribute it and/or modify
 5+ * it under the terms of the GNU General Public License as published by
 6+ * the Free Software Foundation; either version 3 of the License, or
 7+ * (at your option) any later version.
 8+ *
 9+ * @file
 10+ * @ingroup Extensions
 11+ * @author Roan Kattouw <roan.kattouw@gmail.com>
 12+ * @copyright Copyright © 2009 Roan Kattouw
 13+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 14+ */
 15+# Alert the user that this is not a valid entry point to MediaWiki if they try to access the extension file directly.
 16+if( !defined( 'MEDIAWIKI' ) ) {
 17+ echo <<<EOT
 18+To install the ChangeAuthor extension, put the following line in LocalSettings.php:
 19+require_once( "\$IP/extensions/ApiSVGProxy/ApiSVGProxy.php" );
 20+EOT;
 21+ exit( 1 );
 22+}
 23+
 24+// Extension credits that will show up on Special:Version
 25+$wgExtensionCredits['other'][] = array(
 26+ 'path' => __FILE__,
 27+ 'name' => 'ApiSVGProxy',
 28+ 'author' => 'Roan Kattouw',
 29+ 'url' => 'http://www.mediawiki.org/wiki/Extension:ApiSVGProxy',
 30+ 'version' => '1.0',
 31+ 'description' => "Proxies SVG files from a (possibly remote) file repository to the local domain",
 32+);
 33+
 34+// Set up the new special page
 35+$dir = dirname(__FILE__) . '/';
 36+$wgAutoloadClasses['ApiSVGProxy'] = $dir . 'ApiSVGProxy.body.php';
 37+$wgAPIModules['svgproxy'] = 'ApiSVGProxy';
\ No newline at end of file
Index: trunk/extensions/ApiSVGProxy/ApiSVGProxy.body.php
@@ -0,0 +1,89 @@
 2+<?php
 3+/**
 4+ * This program is free software; you can redistribute it and/or modify
 5+ * it under the terms of the GNU General Public License as published by
 6+ * the Free Software Foundation; either version 3 of the License, or
 7+ * (at your option) any later version.
 8+ *
 9+ * @file
 10+ * @ingroup Extensions
 11+ * @author Roan Kattouw <roan.kattouw@gmail.com>
 12+ * @copyright Copyright © 2009 Roan Kattouw
 13+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 14+ */
 15+# Alert the user that this is not a valid entry point to MediaWiki if they try to access the extension file directly.
 16+if( !defined( 'MEDIAWIKI' ) ) {
 17+ echo <<<EOT
 18+To install the ApiSVGProxy extension, put the following line in LocalSettings.php:
 19+require_once( "\$IP/extensions/ApiSVGProxy/ApiSVGProxy.php" );
 20+EOT;
 21+ exit( 1 );
 22+}
 23+
 24+class ApiSVGProxy extends ApiBase
 25+{
 26+ public function __construct($main, $action)
 27+ {
 28+ parent::__construct($main, $action);
 29+ }
 30+
 31+ public function execute()
 32+ {
 33+ $params = $this->extractRequestParams();
 34+ $title = Title::newFromText($params['file']);
 35+ $file = wfFindFile($title);
 36+
 37+ // Verify that the file exists and is an SVG file
 38+ if(!$file)
 39+ $this->dieUsage('The specified file does not exist',
 40+ 'nosuchfile', 404);
 41+ if($file->getExtension() != 'svg' || $file->getMimeType() != 'image/svg+xml')
 42+ $this->dieUsage('The specified file is not an SVG file',
 43+ 'notsvg', 403);
 44+
 45+ // Grab the file's contents
 46+ $contents = Http::get($file->getFullUrl());
 47+ if($contents === false)
 48+ $this->dieUsage('The specified file could not be fetched',
 49+ 'fetchfailed', 500);
 50+
 51+ // Output the file's contents raw
 52+ $this->getResult()->addValue(null, 'text', $contents);
 53+ $this->getResult()->addValue(null, 'mime', 'image/svg+xml');
 54+ }
 55+
 56+ public function getCustomPrinter()
 57+ {
 58+ return new ApiFormatRaw($this->getMain(),
 59+ $this->getMain()->createPrinterByName('xml'));
 60+ }
 61+
 62+ public function getAllowedParams()
 63+ {
 64+ return array(
 65+ 'file' => null,
 66+ );
 67+ }
 68+
 69+ public function getParamDescription()
 70+ {
 71+ return array(
 72+ 'file' => 'Name of the file to proxy (including File: prefix)',
 73+ );
 74+ }
 75+
 76+ public function getDescription()
 77+ {
 78+ return array('Proxy an SVG file from a (possibly remote) repository.',
 79+ 'The file must have the .svg extension and the image/svg+xml MIME type.',
 80+ 'If not, this module will return a 403 response. If the file doesn\'t exist,',
 81+ 'a 404 response will be returned. If fetching the file failed, a 500 response',
 82+ 'will be returned.',
 83+ );
 84+ }
 85+
 86+ public function getVersion()
 87+ {
 88+ return __CLASS__ . ': $Id$';
 89+ }
 90+}

Status & tagging log