Add mw.hash to Scribunto

Provides a simple wrapper for PHP's hash() and
hash_algos() functions.

I will add docs to the Lua reference manual once
this is merged.

Bug: T142585
Change-Id: I6697463974a175e99f9b77428a1085247165ebc9
This commit is contained in:
Marius Hoch 2016-08-16 00:14:29 +02:00
parent c460c5a792
commit 0f4db74148
7 changed files with 143 additions and 0 deletions

View file

@ -139,6 +139,7 @@ $wgAutoloadClasses['Scribunto_LuaMessageLibrary'] =
$wgAutoloadClasses['Scribunto_LuaTitleLibrary'] = __DIR__ . '/engines/LuaCommon/TitleLibrary.php';
$wgAutoloadClasses['Scribunto_LuaTextLibrary'] = __DIR__ . '/engines/LuaCommon/TextLibrary.php';
$wgAutoloadClasses['Scribunto_LuaHtmlLibrary'] = __DIR__ . '/engines/LuaCommon/HtmlLibrary.php';
$wgAutoloadClasses['Scribunto_LuaHashLibrary'] = __DIR__ . '/engines/LuaCommon/HashLibrary.php';
/***** Configuration *****/

View file

@ -426,6 +426,7 @@ WIKI;
'engines/LuaCommon/TitleLibraryTest.php',
'engines/LuaCommon/TextLibraryTest.php',
'engines/LuaCommon/HtmlLibraryTest.php',
'engines/LuaCommon/HashLibraryTest.php',
'engines/LuaCommon/LanguageLibraryTest.php',
'engines/LuaCommon/UstringLibraryPureLuaTest.php',
'engines/LuaCommon/LibraryUtilTest.php',

View file

@ -0,0 +1,44 @@
<?php
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
class Scribunto_LuaHashLibrary extends Scribunto_LuaLibraryBase {
public function register() {
$lib = array(
'listAlgorithms' => array( $this, 'listAlgorithms' ),
'hashValue' => array( $this, 'hashValue' ),
);
return $this->getEngine()->registerInterface( 'mw.hash.lua', $lib );
}
/**
* Returns a list of known/ supported hash algorithms
*
* @return string[][]
*/
public function listAlgorithms() {
$algos = hash_algos();
$algos = array_combine( range( 1, count( $algos ) ), $algos );
return array( $algos );
}
/**
* Hash a given value.
*
* @param string $algo
* @param string $value
* @return string[]
*/
public function hashValue( $algo, $value ) {
if ( !in_array( $algo, hash_algos() ) ) {
throw new Scribunto_LuaError( "Unknown hashing algorithm: $algo" );
}
$hash = hash( $algo, $value );
return array( $hash );
}
}

View file

@ -15,6 +15,7 @@ abstract class Scribunto_LuaEngine extends ScribuntoEngineBase {
'mw.title' => 'Scribunto_LuaTitleLibrary',
'mw.text' => 'Scribunto_LuaTextLibrary',
'mw.html' => 'Scribunto_LuaHtmlLibrary',
'mw.hash' => 'Scribunto_LuaHashLibrary',
);
/**

View file

@ -0,0 +1,30 @@
local hash = {}
local php
local util = require 'libraryUtil'
local checkType = util.checkType
function hash.listAlgorithms()
return php.listAlgorithms()
end
function hash.hashValue( algo, value )
checkType( 'hashValue', 1, algo, 'string' )
checkType( 'hashValue', 2, value, 'string' )
return php.hashValue( algo, value )
end
function hash.setupInterface()
-- Boilerplate
php = mw_interface
mw_interface = nil
-- Register this library in the "mw" global
mw = mw or {}
mw.hash = hash
package.loaded['mw.hash'] = hash
end
return hash

View file

@ -0,0 +1,13 @@
<?php
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
class Scribunto_LuaHashLibraryTests extends Scribunto_LuaEngineTestBase {
protected static $moduleName = 'HashLibraryTests';
protected function getTestModules() {
return parent::getTestModules() + array(
'HashLibraryTests' => __DIR__ . '/HashLibraryTests.lua',
);
}
}

View file

@ -0,0 +1,53 @@
--[[
Tests for the mw.hash module
@license GNU GPL v2+
@author Marius Hoch < hoo@online.de >
]]
local testframework = require 'Module:TestFramework'
local function testListAlgorithms()
local algos = mw.hash.listAlgorithms()
if type( algos ) ~= 'table' then
return 'algo list was expected to be a table'
end
for i, v in ipairs( algos ) do
if v == 'md5' then
return true
end
end
return 'md5 was expected to be in the algo list'
end
-- Tests
local tests = {
{ name = 'mw.hash.listAlgorithms', func = testListAlgorithms,
expect = { true }
},
{ name = 'mw.hash.hashValue sha1', func = mw.hash.hashValue,
args = { 'sha1', 'abc' },
expect = { 'a9993e364706816aba3e25717850c26c9cd0d89d' }
},
{ name = 'mw.hash.hashValue md5', func = mw.hash.hashValue,
args = { 'md5', 'abc' },
expect = { '900150983cd24fb0d6963f7d28e17f72' }
},
{ name = 'mw.hash.hashValue bad argument type #1', func = mw.hash.hashValue,
args = { nil, 'a-string' },
expect = "bad argument #1 to 'hashValue' (string expected, got nil)"
},
{ name = 'mw.hash.hashValue bad argument type #2', func = mw.hash.hashValue,
args = { 'abc', 2 },
expect = "bad argument #2 to 'hashValue' (string expected, got number)"
},
{ name = 'mw.hash.hashValue bad algorithm', func = mw.hash.hashValue,
args = { 'not-a-hashing-algorithm', 'abc' },
expect = "Unknown hashing algorithm: not-a-hashing-algorithm"
}
}
return testframework.getTestProvider( tests )