Jump to content

手册:添加对新文件类型的支持

From mediawiki.org
This page is a translated version of the page Manual:Adding support for new filetypes and the translation is 22% complete.

Adding support for new file types to MediaWiki can be quite a daunting task. 这些是您可能必须采取的一些步骤。

我需要做什么?

  1. Check if no one has beat you to it. It might be that there is already an extension that handles uploads and thumbnailing for your file type. complete list in: Category:Media handling extensions zh .
  2. 如果已经支持,请确保此文件类型是允许上传的文件类型之一。 请参阅Manual:配置文件上传
  3. 您只需要上传支持,还是需要嵌入和缩略图支持?
  4. Do you need the file format to be supported on Wikimedia websites? This introduces additional requirements with regard to performance, security and support of its communities.

Support for uploads

  1. Add support for the MIME type to MediaWiki:
    1. includes/libs/mime/MimeMap.php
      • Add Mime to file-extension mapping in the MIME_EXTENSIONS array. (The preferred file extension goes first in the list)
      • Add Mime to media type matching, via one of the subarray MEDIATYPE_IMAGE, MEDIATYPE_AUDIO, MEDIATYPE_OFFICE etc.
    2. includes/libs/mime/MimeAnalyzer.php
      • If possible add the magic number to this file to make sure that we do not depend on external dependencies for detection.
    3. Add example files (as small as possible and copyright free) to tests/phpunit/data/media
    4. Add a testcase for filetype detection into tests/phpunit/includes/libs/mime/MimeAnalyzerTest.php
  2. 将文件类型添加到$wgFileExtensions 以允许在您的服务器上上传。

注意,MediaWiki默认检查zip文件格式、阻止java代码和可能被解释为 HTML文件的内容。 If your file format falls into this category, you either need to disable some of those protections (which is not advisable for publicly hosted servers) or implement additional logic.

Support thumbnailing and embedding

  1. Determine if you need support in MediaWiki core. Usually exotic formats, formats with lots of dependencies or with special functionality will go in dedicated extensions. Examples of such extensions are: 3D , PagedTiffHandler and more (complete list in: Category:Media handling extensions zh ).
  2. Create a MediaHandler subclass for your filetype. For common cases like images, you can often extend from the subclass BitmapHandler instead. Here you can implement:
    1. Reading the metadata of the file
    2. Define what the behaviour should be (thumbnailing, embedding/render, paging etc).
    3. Implement if the code to do some of that behaviour, like thumbnailing and metadata reading.
    4. Note that the file format of your thumbnail is allowed to be different from your original file format.
    See other image handlers in: includes/media subdirectory of MediaWiki
  3. Register your MediaHandler in includes/media/MediaHandlerFactory.php for core handlers, or in your extension.json if you have created an extension.

在维基媒体网站上的支持

  1. 格式应不受专利限制(自由且开放)。
  2. The format should not allow execution of code when downloaded and executed on the computer of clients, like for instance macros or an embedded scripting language (if it does you will need to scan/sanitize the uploads from within MediaWiki)
  3. If you are adding a new extension, an external library or console tool, then these will require a security review.
  4. Your thumbnail format should be a commonly supported format for all browsers.
  5. Your thumbnail (esp when interactive) should be usable when printing and for screenreader users
  6. Interactive content should not reflow the page
  7. Interactive content should perform (JS needs to be added only to the pages that require it etc).
  8. Anything over $wgMaxImageArea in size requires special support, due to memory limitations. 请参阅VipsScaler
  9. You need a thumbor engine to support making thumbnails of your images. All Wikimedia requests use a thumbor engine to do the actual thumbnail generation, instead of the thumbnail support inside the MediaHandler.
  10. Check if the Wikimedia communities support adding this file format (Hold an RFC)
  11. Update WMF analytics (where?) to classify the file type