Extension:UploadedFileHasher

MediaWiki extensions manual
UploadedFileHasher
Release status: unmaintained
Implementation Special page
Description Saves hashes of uploaded files to a page.
Author(s) Artūras Šlajus <x11@arturaz.net>
Latest version 1.0 (2010-02-13)
MediaWiki 1.15.1
PHP 5.2.6
Database changes No
License BSD 3-clause "Modified" License
Download Extension:UploadedFileHasher#Code

The UploadedFileHasher extension 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

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

"Panel_bottom_left.png":"7a719e3dde1919914394c064a855ade82b0b46c4",

"ZetiumIcon.png":"7c6518744d92d9b8bb07e89661659ba20af8ef53",

"Tile_texture_desert.jpg":"c47c42031404e7fca79e90ffa1d265c59f4deafb"}

Download instructions

edit

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

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

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

Code

edit
UploadedFileHasher.php
<?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';