Jump to content
Wikimedia Meta-Wiki

Module:TrainingPages

From Meta, a Wikimedia project coordination wiki
Module documentation

The master version of this module comes from English Wikipedia: w:Module:TrainingPages.

However here on multilingual Meta, it is being fixed to handle translations using the Translate extension:

  • This code requires template expansion when parsing the index list pages (containing transclusions of TNTN (because "Special:MyLanguage/" prefixes are still not supported)
  • Wikilinks that are present in noinclude sections of the index page will no longer be indexed for navigation (these links are also hidden when trascluding index pages in the main training page); but are accessible when viewing the index pages directly (from the link present under the topic name in the training page header)
  • It fixes another issue with the "/en" suffix which may appear in links once a training page has been prepared for translation (not all of them are translated at once, this avoids breaking existing trainings or new trainings in preparation that are still not ready for translation).

The module will then now work with or without translations (but most trainings in Meta or Commons should be translated). A localized Wiki may decide to only import one translated version and make it the default by dropping language code suffixes, or by renaming them after the import (leaving redirects which may break things)

The current code assumes English is the default language of the current wiki (true on Meta-Wiki) but is easy to fix if you import this module on a non-English localized wiki.

The above documentation is transcluded from Module:TrainingPages/doc. (edit | history)
Editors can experiment in this module’s sandbox (create | mirror) and testcases (edit) pages.
Please add categories to the /doc subpage. Subpages of this module.

 --[[
 The purpose of this module is to take a list of linked page,
 and use it to determine the next and previous page in the list
 as well as the total number of pages.
 --]]
 localp={}

 functionanonymize(name)
 returnmw.ustring.gsub(name,"^"..mw.site.siteName,"Project")orname
 end

 functionout(name)
 returnmw.ustring.gsub(name,"^Project",mw.site.siteName)orname
 end

 functionkeyize(pagename)
 -- there was a complaint about "_" breaking things. Do all lookups with _ in place of any space.
 -- also spaces in the index file (non-module) were causing trouble
 pagename=mw.text.trim(pagename)
 pagename=mw.ustring.gsub(pagename," ","_")
 pagename=mw.uri.decode(pagename)
 pagename=anonymize(pagename)
 returnpagename
 end

 functionp.main(frame,displacement,varstoreturn)
 localparent=frame.getParent(frame)
 localcurrentpage,indexmodule,defaultpage,noerr,anonymizereturn
 localreturnpage,currentpagenumber,totalpages
 ---- args in the #invoke itself trump args in the parent frame
 currentpage=frame.args.pageandmw.text.trim(frame.args.page)
 defaultpage=frame.args.defaultpageandmw.text.trim(frame.args.defaultpage)
 indexmodule=frame.args.indexandmw.text.trim(frame.args.index)
 displacement=displacementorframe.args.displacement-- can be passed from the other function names at the end
 noerr=frame.args.noerr-- used as boolean
 anonymizereturn=frame.args.anonymize-- used as boolean
 ---- args in the parent frame come next
 ifparentthen
 currentpage=currentpageor(parent.args.pageandmw.text.trim(parent.args.page))
 indexmodule=indexmoduleor(parent.args.indexandmw.text.trim(parent.args.index))-- index is a module: return {'page1', 'page2', ...} or a page containing wikilinks
 defaultpage=defaultpageor(parent.args.defaultpageandmw.text.trim(parent.args.defaultpage))
 noerr=noerrorparent.args.noerr
 anonymizereturn=anonymizereturnorparent.args.anonymize
 end
 -- Default values if parameters aren't provided.
 defaultpage=defaultpageor""-- Don't know where to send people by default.
 ifnot(indexmodule)then
 returnpage="[[Module:TrainingPages]] error: no index parameter specified"
 ifnoerrthenreturnpage=""end
 ifnot(varstoreturn)thenreturnreturnpageelsereturnreturnpage,nil,nil,trueend
 end
 ifnot(currentpage)then
 localpp=mw.title.getCurrentTitle()
 ifnotppthen-- This shouldn't happen anyway, I think...
 returnpage="[[Module:TrainingPages]] error: failed to access getCurrentTitle"
 ifnoerrthenreturnpage=""end
 ifnot(varstoreturn)thenreturnreturnpageelsereturnreturnpage,nil,nil,trueend
 end
 currentpage=pp.fullText
 end
 localindex={}
 ifmw.ustring.sub(indexmodule,1,6)=="Module"then
 -- Get a table of the pages in order from indexmodule.
 index=mw.loadData(indexmodule)
 elsepp=mw.title.new(indexmodule)
 -- Get a table of the pages in order from a page containing a set of wikilinks.
 ifnot(pp)orpp.id==0then
 returnpage="[[Module:TrainingPages]] error (''index'' parameter): failed to access mw.title.new(\""..tostring(indexmodule).."\") to load the index file"
 ifnoerrthenreturnpage=""end
 ifnot(varstoreturn)thenreturnreturnpageelsereturnreturnpage,nil,nil,trueend
 end
 --[==[
  Note that this still does not work with wikilinks going to the "Special:"
  namespace, notably "Special:MyLanguage/<namespace>:<pagename>".
  A workaround is to use [[{{TNTN|<namespace>:<pagename>}}|...]] in listed
  links. So each wikilink target containing transclusions must be
  expanded.
  The links which may be present in noinclude parts of the index page are
  now also excluded from the navigation (and not counted) by this expansion
  (this hides links only accessible when viewing the index page directly).
  ]==]
 localtextindex=frame:expandTemplate{title=':'..pp.prefixedText}-- was = pp.getContent(pp)
 ifnottextindexthen
 returnpage="[[Module:TrainingPages]] error (''index'' parameter): failed to access mw.title.new(\""..indexmodule.."\"):getContent() to load the index data"
 ifnoerrthenreturnpage=""end
 ifnot(varstoreturn)thenreturnreturnpageelsereturnreturnpage,nil,nil,trueend
 end
 index={}
 prowl=mw.ustring.gmatch(textindex,"%[%[(.-)[%]|]")-- first half of any wikilink
 whiletruedo
 link=prowl()-- iterate to next match
 ifnot(link)thenbreakend-- no more match
 link=mw.text.trim(link)
 iflink~=""thentable.insert(index,link)end
 end
 end
 displacement=displacementor0-- assume a null parameter is just display the same
 --[[
  Set up the reverse lookup in lookup.
  It would be faster to set this up in the indexmodule,
  but we don't want inconsistencies from user input!
  ]]
 locallookup={}
 locali=1
 whiletruedo
 localj=index[i]
 ifnotjthenbreakend
 lookup[keyize(j)]=i-- lookup["namespace:pagename"] => page number
 i=i+1
 end
 --- get the page to return
 totalpages=#index
 iftonumber(currentpage)then
 currentpagenumber=tonumber(currentpage)
 returnpage=index[currentpagenumber+displacement]ordefaultpage
 elseif(lookup[keyize(currentpage)])then
 currentpagenumber=lookup[keyize(currentpage)]
 returnpage=index[currentpagenumber+displacement]ordefaultpage
 elseif(lookup[keyize(currentpage.."/en")])then
 --[==[
 	 English may exist both as base of translation and as a translated subpage
 	 The index page will expand to one or the other, depending if the page
 	 was prepared for translation or not.
 	 This is not necessary for other translations using language suffixes
 	 (but their suffixe is not variable. This code assumes that there will
 	 never be any page named like "namespace:pagename/languagecode/en")
 	 --]==]
 currentpagenumber=lookup[keyize(currentpage.."/en")]
 returnpage=index[currentpagenumber+displacement]ordefaultpage
 else
 returnpage=defaultpage
 end
 ifanonymizereturnthen
 returnpage=anonymize(returnpage)
 else
 returnpage=out(returnpage)
 end
 ifreturnpagethenreturnpage=mw.text.trim(returnpage)end
 returnpage=tostring(returnpage)
 ifnot(varstoreturn)thenreturnreturnpageelsereturnreturnpage,currentpagenumber,totalpagesend
 end

 --[[
 Return the next page in the index.
 * Used like this if on the current page that is part of the index:
  {{#invoke:TrainingPages|next_page
  | index = Project:Training/For students/Editing module index
  }}
 * Used like this to find the next page after a specified page:
  {{#invoke:TrainingPages|next_page
  | index = Project:Training/For students/Editing module index
  | currentpage = Project:Training/For students/My sandbox
  }}
 ]]
 functionp.next_page(frame)
 localreturnpage,pagenumber,totalpages,errcode=p.main(frame,1,true)
 returnreturnpage
 end
 -- Abbreviated alias.
 p.next=p.next_page

 -- Same as above, but returns the previous page.
 functionp.last_page(frame)
 localreturnpage,pagenumber,totalpages,errcode=p.main(frame,-1,true)
 returnreturnpage
 end
 -- Abbreviated alias.
 p.last=p.last_page

 -- Return the current page number as ordered in the index.
 functionp.page_number(frame)
 localreturnpage,pagenumber,totalpages,errcode=p.main(frame,0,true)
 iferrcodethenreturnreturnpageelsereturnpagenumberend
 end
 -- Abbreviated alias.
 p.page=p.page_number

 -- Return the total number of pages listed in the index.
 functionp.total_pages(frame)
 localreturnpage,pagenumber,totalpages,errcode=p.main(frame,0,true)
 iferrcodethenreturnreturnpageelsereturntotalpagesend
 end
 -- Abbreviated alias.
 p.total=p.total_pages

 returnp

AltStyle によって変換されたページ (->オリジナル) /