Jump to content

Module:Mooc/Data/Item

From Wikiversity

Item is a basic class in the MOOC. It servers as model holding data for a MOOC item and as parser. Therefore it does not only provide getters and setters promising to have certain data available for the template, but also methods to interprete lines from the MOOC index and fill itself with the content extracted. The item instance will be passed to the template which accesses the model to render the data the item instance contains.

   TODO separate model and parser?

constructor (init)

[edit source]

Creates a new container filled with the fields of the header passed.

Parameters:

  • [Mooc/Data/Item.Header] header: header of the item data will be hold for

getParams

[edit source]

Provides access to the parameters extracted from the index.

   TODO add example of template accessing the item

Returns: table containing parameter values accessible via their name

Loads the item fields from the MOOC index lines left unparsed. Since the item was already created the header is not included in these lines and already assigned to the item. The default implementation extracts all parameters but ignores child items.

Parameters:

  • [Table:String] content: MOOC index lines representing the item

extractParams

[edit source]

Extracts all parameters from MOOC index lines for the current item ignoring children. This method is used once by the parser. Templates use getParams to get the table.

Parameters:

  • [Table:String->String] content: MOOC index lines representing the item

Returns: table containing parameter values accessible via their name

Each model class has a field set to the item type the class represents. The field can be used to register a type handler for this class.

   TODO add link to type handler registration page

The item type class is responsible to link wiki page calls to parser and templating implementations. This is done using identifiers since Strings are the only possible script invocation arguments.

constructor (init)

[edit source]

Create a new type with the identifier specified.

Parameters:

  • [String] identifier: identifier of the new item type

isType

[edit source]

Checks whether this type matches the identifier a user provided.

Parameters:

  • [String] typeIdentifier: identifier of the type a user desires

Returns: true if this type matches the identifier or false otherwise

[edit source]

The item header is a simple container holding data to identify an item within the MOOC.

constructor (init)

[edit source]

Creates a new item header.

Parameters:

  • [String] name: item name
  • [String] typeIdentifier: identifier determining the item type
  • [String] path: item path within the MOOC

getLevel

[edit source]

Gets the item level within the MOOC depending on the number of leading "=".

Parameters:

  • [String] itemHeader: item header line the level should be calculated for

Returns: item level starting with 1 or 0 if the passed line was no item header

parseHeader

[edit source]

Loads an instance of Mooc/Data/Item.Header from the item header line.

Parameters:

  • [String] headerLine: item header line that should be loaded
  • [String] parentPath: path of the item's parent

parseItem

[edit source]

Parses an item of the type the parser was registered for.

Parameters:

  • [Mooc/Data/Item.Header] header: item header
  • [Boolean] load: determines whether load should be called or not. Only basic information is available if the item doesn't get loaded: name, type and path (URL)
  • [Table:String->Mooc/TypeHandler] typeHandlers: type handlers to parse additional items such as children if needed

Returns: parsed item that is fully loaded if specified

parseIndex

[edit source]

Gets called by the MOOC in order to parse all items associated to the item that will be rendered. The type of the targeted item is the type the parser was registered for.

Parameters:

  • [Table:String->Table] index: extracted MOOC index including all associated items
  • [Table:String->Mooc/TypeHandler] typeHandlers: type handlers to parse additional items such as children if needed

require("Module:Exception");
local inheritance = require("Module:Inheritance");

---

local Header = inheritance.extend(inheritance.Class);

function Header:init(name, typeIdentifier, parent, path, level)
	self.name = name;
	self.typeIdentifier = typeIdentifier;
	self.parent = parent;
	self.path = path;
	self.level = level;
end

function Header:getName()
	return self.name;
end

function Header:getTypeIdentifier()
	return self.typeIdentifier;
end

function Header:getParent()
	return self.parent;
end

function Header:getPath()
	return self.path;
end

function Header:getUrl()
	local relUrl = string.gsub(self.path, " ", "_");
	return relUrl;
end

function Header:getLevel()
	return self.level;
end

function Header:getSection()
	return self.section;
end

function Header:setSection(newSection)
	self.section = newSection;
end

---

local Type = inheritance.extend(inheritance.Class);

function Type:init(identifier)
  self.identifier = identifier;
end

function Type:getIdentifier()
  return self.identifier;
end

function Type:isType(typeIdentifier)
  if self.identifier == typeIdentifier then
    return true;
  end
  return false;
end

---

local Item = inheritance.extend(Header);

function Item:init(header)
  Header.init(self, header:getName(), header:getTypeIdentifier(), header:getParent(), header:getPath(), header:getLevel());
  self:setSection(header:getSection());
end

function Item:getParams()
  return self.params;
end

function Item:setParams(params)
  self.params = params;
end

function Item:getChildren()
  return self.children;
end

function Item:addChild(child)
  if self.children == nil then
    self.children = {}
  end
  table.insert(self.children, child);
end

---

function Item.parseHeader(headerLine, parent)
  local iSplit = string.find(headerLine, "|", 1, true);
  if not iSplit then
    throw("failed to parse header: name separator is missing");
  end
  
  local parentPath = parent:getPath();
  local level = parent:getLevel() + 1;
  local typeIdentifier = string.sub(headerLine, level + 1, iSplit - 1);
  local name = string.sub(headerLine, iSplit + 1, -level - 1);
  local itemPath = name;
  if parentPath ~= "" then
    itemPath = parentPath .. "/" .. name;
  end
  
  return Header(name, typeIdentifier, parent, itemPath, level);
end

Item.Header = Header;
Item.Type = Type;

return Item;