local wp =
-- BTW: Lua allows literal strings declarations, but the syntax-- highlighter doesn't support them properly, fails if the string contains-- another set of square brackets...
-- Number of articles in each list if the page invoking this module is transcludedlocal inc_limit = 2
-- Mapping of article assessment ratings to low/medium/high as numberslocal rating_numbers = local rating_names =
-- Mapping of SuggestBot's task categories (see)-- to section titles and descriptions, and the order in which they will be presentedlocal taskcat_order = local taskcat_map =
-- Mapping of needs (specific tasks to improve an article) to descriptionslocal needs_list = local needs_map =
-- Local support functions follow below
local function has_key(tbl, key) -- Check if the given table has the given key return tbl[key] ~= nil end
local function sort_articles(arglist, task_cats) -- Sort the given argument list into a table mapping task categories -- to a list of articles in that task category. local sorted_arts = for i, task_cat in pairs(task_cats) do sorted_arts[task_cat] = end
-- 0-based index of the current article being processed local cur_art = 0 while arglist[(cur_art * 6) + 1] do local cur_idx = cur_art * 6 local task_cat = arglist[cur_idx + 1] -- Do we have this task category in our table? if sorted_arts[task_cat] then table.insert(sorted_arts[task_cat],) end cur_art = cur_art + 1 end return sorted_artsend
local function randomizeArray(t, limit) -- Randomizes an array. It works by iterating through the list backwards, each time swapping the entry -- "i" with a random entry. Courtesy of Xinhuan at http://forums.wowace.com/showthread.php?p=279756 -- If the limit parameter is set, the array is shortened to that many elements after being randomized. -- The lowest possible value is 0, and the highest possible is the length of the array. local len = #t for i = len, 2, -1 do local r = math.random(i) t[i], t[r] = t[r], t[i] end if limit and limit < len then local ret = for i, v in ipairs(t) do if i > limit then break end ret[i] = v end return ret else return t endend
local function make_ratingdesc(assessment, prediction) -- Make a string that describes the article's quality -- Quality: Low, Assessed class: Stub, Predicted class: Stub return "Quality: " .. rating_names[prediction] .. ", Assessed class: " .. assessment .. ", Predicted class: " .. predictionend
local function make_needs(needs) -- From the given comma-separated list of needs, make a string that -- describes those needs based on needs_map local result = for i, task in ipairs(needs_list) do if string.find(needs, task) then result = result .. ", " .. needs_map[task] end end -- Remove the first two characters (", ") result = mw.ustring.sub(result, 3) -- Return with the first character in upper-case, from http://lua-users.org/wiki/StringRecipes return result:gsub("^%l", string.upper)end
-- Globally exposed functions follow belowfunction wp.suggestions(frame) -- Process the suggested articles given in the frame's arguments -- and build lists of articles for each task category defined in the global -- list of categories above. -- Keyword arguments: -- * is_included=yes: passed in to limit lists to two items each when -- the page of the call is transcluded -- * list_col=[colour value]: defines the CSS colour value of list headers -- * item_col=[colour value]: defines the CSS colour value of list items -- -- The rest of the arguments are sets of six parameters describing each -- suggested article, as follows: -- 1: task category -- 2: article title -- 3: number of average views/day (14 days prior to the update) -- 4: assessed quality -- 5: predicted quality -- 6: comma-separated list of specific tasks for improving this article local list_col = '#086' local item_col = '#37f' if has_key(frame, 'list_col') then list_col = frame['list_col'] end if has_key(frame, 'item_col') then item_col = frame['item_col'] end local is_included = false if has_key(frame.args, 'is_included') then is_included = true end -- When was the page this is invoked from last updated? local last_update = frame:expandTemplate local result = -- sort the supplied suggested articles and process into lists… local sorted_articles = sort_articles(frame.args, taskcat_order) for i, task_cat in pairs(taskcat_order) do -- write list start local list_params = if is_included then list_params['constrained'] = 'yes' end result = result .. frame:expandTemplate -- write some articles local articles = sorted_articles[task_cat] if is_included then articles = randomizeArray(articles, inc_limit) end for j, article in pairs(articles) do result = result .. frame:expandTemplate end -- write list end result = result .. frame:expandTemplate end return resultend
function wp.hello(frame) return "Hello World!" end
function wp.test_args(frame) result = "The method got the following arguments:" if has_key(frame, 'is_included') then result = result .. "\n* is_included" .. frame.args['is_included'] end for k, v in pairs(frame.args) do result = result .. "\n* " .. k .. "=" .. v end return resultend
return wp