Audio migration in progress. Some responses pages / audio files may have broken file links.

Module:FileExists

From Dota 2 Wiki
Jump to: navigation, search

Documentation for Module:FileExists Jump to code ↴ [ edit | purge ]

This module provides a "safe" version of mw.title's .fileExists method, that does not raise an error when the expensive parser function limit is exceeded.

Usage[edit]

local fileExists = require('Module:FileExists')

-- ...

local file
if fileExists('File:My example file.png') then
  file = 'File:My example file.png'
end


local checkType = require( 'libraryUtil' ).checkType

-- Check if a file exists on the wiki. Will fail if the expensive parser function
-- limit is exceeded.
--
-- @param string fileName
-- @return boolean
local function scribuntoCheck( fileName )
  local file = mw.title.new( fileName )
  return file.fileExists
end

-- Use Cargo as a fallback to check if a file exists.
--
-- @param string fileName
-- @return boolean
local function cargoCheck( fileName )
  local cargoData = mw.ext.cargo.query( '_pageData', '_pageID', {
    where = string.format( '_pageName="%s"', fileName )
  } )
  return ( #cargoData ~= 0 )
end

return function ( fileName )
  checkType( 'fileExists', 1, fileName, 'string' )

  local success, result = pcall( scribuntoCheck, fileName )
  if not success then
    result = cargoCheck( fileName )
  end

  return result
end