| Line 70: |
Line 70: |
| | local function getImageName(s) | | local function getImageName(s) |
| | -- Gets the image name for a given string. | | -- Gets the image name for a given string. |
| | + | local default = 'Portal-puzzle.svg|link=|alt=' |
| | if type(s) ~= 'string' or #s < 1 then | | if type(s) ~= 'string' or #s < 1 then |
| − | return 'Portal-puzzle.svg|link=|alt=' | + | return default |
| | end | | end |
| | s = mw.ustring.lower(s) | | s = mw.ustring.lower(s) |
| − | return matchImagePage(s) or matchImagePage(getAlias(s)) or 'Portal-puzzle.svg|link=|alt=' | + | return matchImagePage(s) or matchImagePage(getAlias(s)) or default |
| | + | end |
| | + | |
| | + | local function checkPortalExists(portal) |
| | + | return not (mw.title.makeTitle(100, portal).id == 0) |
| | end | | end |
| | | | |
| Line 92: |
Line 97: |
| | root:wikitext('<strong class="error">No portals specified: please specify at least one portal</strong>[[Category:Portal templates without a parameter]]') | | root:wikitext('<strong class="error">No portals specified: please specify at least one portal</strong>[[Category:Portal templates without a parameter]]') |
| | return tostring(root) | | return tostring(root) |
| | + | end |
| | + | |
| | + | -- scan for nonexistent portals, if they exist remove them from the portals table. If redlinks=yes, then don't remove |
| | + | local portallen = #portals |
| | + | -- traverse the list backwards to ensure that no portals are missed (table.remove also moves down the portals in the list, so that the next portal isn't checked if going fowards. |
| | + | -- going backwards allows us to circumvent this issue |
| | + | for i=portallen,1,-1 do |
| | + | -- the use of pcall here catches any errors that may occour when attempting to locate pages when the page name is invalid |
| | + | -- if pcall returns true, then rerun the function to find if the page exists |
| | + | if not pcall(checkPortalExists, portals[i]) or not checkPortalExists(portals[i]) then |
| | + | -- Getting here means a redlinked portal has been found |
| | + | if (args.redlinks == 'yes') or (args.redlinks == 'y') or (args.redlinks == 'true') or (args.redlinks == 'include') then |
| | + | -- if redlinks as been set to yes (or similar), add the cleanup category and then break the loop before the portal is removed from the list |
| | + | root:wikitext('[[Category:Portal templates with redlinked portals]]') |
| | + | break |
| | + | end |
| | + | -- remove the portal (this does not happen if redlinks=yes) |
| | + | table.remove(portals,i) |
| | + | end |
| | + | end |
| | + | |
| | + | -- if the length of the table is different, then rows were removed from the table, so portals were removed. If this is the case add the cleanup category |
| | + | if not (portallen == #portals) then |
| | + | if #portals == 0 then |
| | + | return '[[Category:Portal templates with all redlinked portals]]' |
| | + | end |
| | + | root:wikitext('[[Category:Portal templates with redlinked portals]]') |
| | end | | end |
| | | | |
| Line 133: |
Line 165: |
| | | | |
| | function p._image(portals) | | function p._image(portals) |
| − |
| |
| | -- Wrapper function to allow getImageName() to be accessed through #invoke. | | -- Wrapper function to allow getImageName() to be accessed through #invoke. |
| − | return getImageName(portals[1]) | + | local name = getImageName(portals[1]) |
| | + | return name:match('^(.-)|') or name -- FIXME: use a more elegant way to separate borders etc. from the image name |
| | end | | end |
| | | | |