Extension:UploadedFileHasher

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
UploadedFileHasher

Release status: stable

Implementation Special page
Description Saves hashes of uploaded files to a page.
Author(s) Artūras Šlajus <[email protected]>
Latest version 1.0 (2009-02-13)
MediaWiki 1.15.1
PHP 5.2.6
License BSD
Download Extension:UploadedFileHasher#Code
Hooks used
UploadComplete

Translate the UploadedFileHasher extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

What can this extension do?[edit | edit source]

It saves hashes of uploaded files to a page (as JSON). It can be used to synchronize wiki content with local file system.

Example content[edit | edit source]

{"Login_logo.jpg":"6f8d394a529e2873d9c09936369fa82bb22cb74c",

"Panel_bottom_left.png":"7a719e3dde1919914394c064a855ade82b0b46c4",

"ZetiumIcon.png":"7c6518744d92d9b8bb07e89661659ba20af8ef53",

"Tile_texture_desert.jpg":"c47c42031404e7fca79e90ffa1d265c59f4deafb"}

Download instructions[edit | edit source]

Please cut and paste the code found below and place it in $IP/extensions/UploadedFileHasher.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit | edit source]

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/UploadedFileHasher.php");

Code[edit | edit source]

<?php
 
class UploadedFileHasher {
  const HASHES_PAGE = "FileHashes";
 
  static function uploadComplete($file) {
    $localFile = $file->mLocalFile;
    $localFile->load();
 
    // Use 40 chars sha1 for other systems compatibility.
    $sha1 = sha1_file($localFile->getPath());
    return self::updateHash(
      $file->mDestName,
      $sha1
    );
  }
 
  static function updateHash($filename, $sha1) {
    $title = Title::newFromText(self::HASHES_PAGE);
    $article = new Article($title);
    $content = $article->getRawText();
 
    $hashes = (! empty($content)) ? json_decode($content, true) : array();
    $hashes[$filename] = $sha1;
 
    // Add spaces for manual viewing
    $content = str_replace("\",", "\",\n\n", json_encode($hashes));
 
    $flag = $article->exists() ? EDIT_UPDATE : EDIT_NEW;
    $status = $article->doEdit($content, '', $flag | EDIT_FORCE_BOT);
    return $status->ok;
  }
}
 
function UploadedFileHasherUploadComplete($file) {
  return UploadedFileHasher::uploadComplete($file);
}
 
$wgHooks['UploadComplete'][] = 'UploadedFileHasherUploadComplete';