Module:Hashtitle
Module documentation
[create]
--[[
This module was created with the help of ChatGPT v4.
It generated a deterministic short hash for a page title,
with the purpose of being used in an Etherpad URL such as:
https://etherpad.wikimedia.org/p/{{hash}}
To implement this function and generate the etherpad URL,
simply add the following code:
[https://etherpad.wikimedia.org/p/{{#invoke:Hashtitle|hashTitle}} Etherpad]
The Module was developed for WikiIndaba 2023 held in Agadir Morocco.
]]
local p = {}
-- Bitwise XOR
function bxor(a, b)
local result = 0
local bitval = 1
while a > 0 or b > 0 do
local abit = a % 2
local bbit = b % 2
if abit ~= bbit then
result = result + bitval
end
a = math.floor(a / 2)
b = math.floor(b / 2)
bitval = bitval * 2
end
return result
end
-- Bitwise AND
function band(a, b)
local result = 0
local bitval = 1
while a > 0 and b > 0 do
if (a % 2 == 1) and (b % 2 == 1) then
result = result + bitval
end
a = math.floor(a / 2)
b = math.floor(b / 2)
bitval = bitval * 2
end
return result
end
-- FNV-1a hash function
function fnv_1a(str)
local FNV_prime = 0x01000193
local FNV_offset_basis = 0x811C9DC5
local hash = FNV_offset_basis
for i = 1, #str do
hash = bxor(hash, str:byte(i))
hash = band(hash * FNV_prime, 0xFFFFFFFF)
end
return hash
end
-- Function to convert a title to a unique hash
function p.hashTitle(frame)
local title = mw.title.getCurrentTitle().text
local hash = fnv_1a(title)
return string.format("%X", hash)
end
return p