Extension:UploadedFileHasher

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
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 <x11@arturaz.net>
Last version 1.0 (2009-02-13)
MediaWiki 1.15.1
PHP 5.2.6
License BSD
Download Extension:UploadedFileHasher#Code
Hooks used
UploadComplete

Check usage (experimental)

Contents

[edit] What can this extension do?

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

[edit] Example content

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

"Panel_bottom_left.png":"7a719e3dde1919914394c064a855ade82b0b46c4",

"ZetiumIcon.png":"7c6518744d92d9b8bb07e89661659ba20af8ef53",

"Tile_texture_desert.jpg":"c47c42031404e7fca79e90ffa1d265c59f4deafb"}

[edit] Download instructions

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.

[edit] Installation

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

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

[edit] Code

<?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';
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox