Documentation for this module may be created at Module:Ns has subpages/doc

  1. -- This module implements [[Template:Ns has subpages]].
  2. -- While the template is fairly simple, this information is made available to
  3. -- Lua directly, so using a module means that we don't have to update the
  4. -- template as new namespaces are added.
  5.  
  6. local p = {}
  7.  
  8. function p._main(ns, frame)
  9. -- Get the current namespace if we were not passed one.
  10. if not ns then
  11. ns = mw.title.getCurrentTitle().namespace
  12. end
  13.  
  14. -- Look up the namespace table from mw.site.namespaces. This should work
  15. -- for a majority of cases.
  16. local nsTable = mw.site.namespaces[ns]
  17.  
  18. -- Try using string matching to get the namespace from page names.
  19. -- Do a quick and dirty bad title check to try and make sure we do the same
  20. -- thing as {{NAMESPACE}} in most cases.
  21. if not nsTable and type(ns) == 'string' and not ns:find('[<>|%[%]{}]') then
  22. local nsStripped = ns:gsub('^[_%s]*:', '')
  23. nsStripped = nsStripped:gsub(':.*$', '')
  24. nsTable = mw.site.namespaces[nsStripped]
  25. end
  26.  
  27. -- If we still have no match then try the {{NAMESPACE}} parser function,
  28. -- which should catch the remainder of cases. Don't use a mw.title object,
  29. -- as this would increment the expensive function count for each new page
  30. -- tested.
  31. if not nsTable then
  32. frame = frame or mw.getCurrentFrame()
  33. local nsProcessed = frame:callParserFunction('NAMESPACE', ns)
  34. nsTable = nsProcessed and mw.site.namespaces[nsProcessed]
  35. end
  36. return nsTable and nsTable.hasSubpages
  37. end
  38.  
  39. function p.main(frame)
  40. local ns = frame:getParent().args[1]
  41. if ns then
  42. ns = ns:match('^%s*(.-)%s*$') -- trim whitespace
  43. ns = tonumber(ns) or ns
  44. end
  45. local hasSubpages = p._main(ns, frame)
  46. return hasSubpages and 'yes' or ''
  47. end
  48.  
  49. return p