User:Pathoschild/Scripts/TemplateScript
| ←Scripts | User:Pathoschild (Scripts > TemplateScript) |
TemplateScript adds a menu of user-defined templates to the sidebar. Each template is specific to a given form, so that separate templates can be defined for each form type. Clicking any of these template links will add it to the predefined input box.
Contents |
[edit] Documentation
[edit] Installation
- Add your name to the user list below to get code updates. (If you're not on the list, the script might unexpectedly stop working for you one day if there's a major change.)
- Add the following code to User:your user name/common.js. To enable it for all users on a wiki, add it to MediaWiki:Common.js instead.
- Refresh your browser to reload the JavaScript. (In Chrome or Firefox, press [CTRL] and [R] at the same time.)
- Several example templates for the English Wikisource are provided (every line containing "new_template"); you can modify or delete any of these, or add new templates (see the next section).
// <pre> /************* *** TemplateScript *** by [[m:user:Pathoschild]] <http://meta.wikimedia.org/wiki/User:Pathoschild/Scripts/TemplateScript> *** - adds a menu of user-defined, form-specific templates *************/ importScriptURI('http://meta.wikimedia.org/w/index.php?title=User:Pathoschild/Scripts/TemplateScript.js&action=raw&ctype=text/javascript'); // define templates function define_templates() { // syntax (see documentation): new_template('type','name','text','optional 1','optional 2','optional 3'); new_template('edit','header','{{header\n | title =\n | author =\n | section =\n | previous =\n | next =\n | notes =\n}}'); new_template('edit','author','{{author\n |name =\n |last_initial =\n |dates =\n |description =\n |image =\n |wikipedia_link =\n |wikiquote_link =\n |commons_link =\n}}'); new_template('edit','featured talk','{{featured talk\n | month = {{subst:CURRENTMONTH}}\n | year = {{subst:CURRENTYEAR}}\n | archive = {{subst:CURRENTYEAR}}/{{subst:CURRENTMONTH}}\n}}'); new_template('edit','textinfo','{{textinfo\n| edition =\n| source =\n| contributors =\n| progress =\n| notes =\n| proofreaders =\n}}'); new_template('protect','featured','[[WS:FT|Featured text]] (see the [[WS:PP|protection policy]])'); new_template('move','standardised','[[WS:STYLE|Standardised]]'); new_template('delete','Copyvio discussion','[[WS:COPYVIO|Possible copyright violation]]'); new_template('delete','Proposed','[[WS:DEL|Proposed deletion]]'); new_template('delete','G1 no meaningful content','[[WS:CSD|Criteria for speedy deletion]] G1 ("No meaningful content or history.")'); new_template('delete','G2 recreation','[[WS:CSD|Criteria for speedy deletion]] G2 ("Reposted content previously deleted...")'); new_template('delete','G3 banned user','[[WS:CSD|Criteria for speedy deletion]] G3 ("content created and edited solely by a banned user after they were banned...")'); new_template('delete','G4 redundant','[[WS:CSD|Criteria for speedy deletion]] G4 ("Two versions of the same text on different pages...")'); new_template('delete','G5 beyond scope','[[WS:CSD|Criteria for speedy deletion]] G5 ("...clearly lies outside the [[WS:WWI|scope of Wikisource]]...")'); new_template('delete','G6 copyvio','[[WS:CSD|Criteria for speedy deletion]] G6 ("...clear and proven copyright violation...")'); new_template('delete','G6 re-copyvio','[[WS:CSD|Criteria for speedy deletion]] G6 ("...content previously deleted as a copyright violation...")'); new_template('delete','G6 copyvio author','[[WS:CSD|Criteria for speedy deletion]] G6 ("...author pages for authors whose works are all copyrighted...")'); new_template('delete','G7 author\'s request','[[WS:CSD|Criteria for speedy deletion]] G7 ("Deletion per request of the author...")'); new_template('delete','A1 transwikied','[[WS:CSD|Criteria for speedy deletion]] A1 ("Articles [[m:transwiki|transwikied]] to another project...")'); new_template('delete','A1 transwikied (commons)','[[WS:CSD|Criteria for speedy deletion]] A1 ("...images uploaded to the [[commons:|Wikimedia commons]] with the original contributor noted...")'); new_template('delete','A2 non-notable','[[WS:CSD|Criteria for speedy deletion]] A2 ("...not significantly peer-reviewed or previously published in a significant edition or forum.")'); new_template('delete','A3 no authorship info','[[WS:CSD|Criteria for speedy deletion]] A3 ("Works without authorship information...")'); new_template('delete','M1 trivial','[[WS:CSD|Criteria for speedy deletion]] M1 ("...deletion as part of a page move or history merge, as long as the action requiring the deletion is uncontroversial.")'); new_template('delete','M2 redirect (new)','[[WS:CSD|Criteria for speedy deletion]] M2 ("Unneeded redirects from page titles created within the last week...")'); new_template('delete','M2 redirect (old)','[[WS:CSD|Criteria for speedy deletion]] M2 ("...[unneeded] redirects tagged with {{subst:dated soft redirect|"[[new title]]"}} for at least two months.")'); new_template('delete','M2 redirect (broken)','[[WS:CSD|Criteria for speedy deletion]] M2 ("...Redirects to inexistant pages...)"'); new_template('delete','M3 redirect (article to other ns)','[[WS:CSD|Criteria for speedy deletion]] M3 ("Internamespace redirects from the article namespace to any other namespace.")'); new_template('delete','M4 talk page','[[WS:CSD|Criteria for speedy deletion]] M4 ("Unneeded talk: a discussion page for deleted or inexistant content.")'); } /* Load script */ addOnloadHook(function() {templatescript(define_templates())}); // </pre>
[edit] Adding and editing templates
TemplateScript is written with the explicit purpose of allowing users to define their own templates. The code provided contains several example templates which should help you understand the method.
[edit] Step-by-step
Following is a step-by-step tutorial to add {{author}} to the list of templates (although it's already present).
- First, notice two comments enclosing a block of code ("
/* ### edit templates below ### */" and "/* ### edit templates above ### */"). Add, remove, or edit templates between these two lines. - On a new line somewhere in that section, type (or copy and paste):
new_template('','','');. This is a function that adds a new template with the parameters you will specify. - In the first set of quotations, type
edit. This tells the script that this is a template for the edit form. For the various forms, see Form types. - In the second set of quotations, type
author. This is the template name displayed on the menu. - In the third set of quotations, type the template text. You can usually copy and paste this from the template documentation.
- Any new lines should be replaced with "
\n". - Any single quotation marks inside the two quotation marks enclosing the value must be escaped. For example, if you want a template that states "you're welcome", you'd type
new_template('edit','welcome','you\'re welcome');. Otherwise, the script doesn't know where the value actually ends.
- Any new lines should be replaced with "
- Optionally, you can add an edit summary. Add a comma and another set of quotations to the new_template() list, with the edit summary as the value.
Doing the above steps would add the following line (including the optional edit summary):
new_template('edit','author','{{Author\n |Name =\n |Dates = ( - )\n |FileUnder =\n |FirstLetterLastName=\n |Wikipedia =\n |Wikiquote =\n |Wikicommons =\n |MiscBio =\n |TOC =\n |Image =\n}}','+ {{author}}');
[edit] Form types
Each defined template is given a "type", which designates which forms it will appear beside. For example, 'move' templates only appear when the user is viewing the move page form. There are seven recognised form types:
[edit] Special options
Certain forms have special options that can be templated; for example, a block template can fill out the block length as well as the block reason. For this purpose, TemplateScript allows up to three special options (imaginatively named option1, option2, and option3) which can optionally be added to new_template().
| Form type | Option | Description |
|---|---|---|
| edit | option1 | edit summary |
| block | option1 | block length |
| option2 | block new account creation (this is a toggle; any value except "false" will block new account creation) |
[edit] Technical
[edit] Glitches and workarounds
[edit] Function character limit bug
The function character limit bug is caused by a limitation in JavaScript which limits the number of characters a value passed to a function can contain. For example, new templates('edit','foo','1000 characters') would likely trigger the glitch if the third value was actually 1000 characters. This halts the script entirely. There are two workarounds possible:
- The simply method is to place the long text in a wiki template, and place the transclusion in TemplateScript instead. For example, replace "
1000 characters" with "{{subst:someTemplate}}. - The second method requires a certain knowledge of JavaScripting.
- Use
new_template()as usual, but leave the text parameter blank. - After that line, write your message directly to the variable. The array syntax in this case is
templates[i][2], whereiis the unique template id. This id is an increment from 0 of the templates relevant to the current page. For example, if you are editing the third relevant template,iwill be 2.
- Use
[edit] Array structure
The templates are stored in a series of subarrays nested into a single array. The top array is templates, with each subarray identified with a unique numerical id obtained by increment from zero (templates[id]). Each subarray contains the information relevant to one template in six keys.
templates[id][field]
[id]:
-> incremental
[field]:
-> [0] # form type
-> [1] # template name (displayed on the sidebar)
-> [2] # template text (inserted)
-> [3] # option1 (depends on type)
-> [4] # option2 (depends on type)
-> [5] # option3 (depends on type)
[edit] Notes
[edit] To do before stable version
insert template at cursor, rather than at end of box(done in dev);modularise for compatibility with non-Monobook skins(done in dev);allow localisation of 'templates' header(done in dev);add autosubmit option(done in dev);- add template substitution strings:
<$=wgTitle$>should be replaced by the JavaScript valuemw.config.get("wgTitle");<$?Chapter name|The name of the chapter.$>should popup a little form allowing the user to fill in values, using the first value as the field name and the second value as the field description. The user should only be asked once for each token.
- categorization of templates or separators (example)
automatically insert certain templates at the beginning of the article no matter where the cursor is (considering some templates always go on top of everything else)(done in dev);- (mostly? for user warning levels) add multiple templates on same line (
• uw-vandal: 1/2/3/4; clicking 1 inserts {{subst:uw-vandal1}} etc.) – might be glichty 'cause of the space available in the Sidebar
[edit] History
| Date | Version | Details | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2006-07-13 | 0.1-alpha |
|
||||||||||
| 2006-07-18 | 0.2-alpha |
|
||||||||||
| 2006-08-07 | 0.5-alpha |
|
[edit] Errors
[edit] Upgrade error
If your name is not listed in the user list below, your Javascript file will not be adjusted if an update requires it. To correct the problem, simply add your username below (listing every Javascript file using the script) and Pathoschild will update your Javascript.
[edit] See also
- wikis
- wm2009 (gadget)
- Users
- version 0.6 (beta)
- Az1568 (global, enwikibooks)
- Azel Sevan (trwiki)
- Pathoschild (global)
- Shanel (enwikisource)
- Kangaroopower (meta)
- Micki (srwiki, meta)
- version 0.5 (legacy)
- Aysucilek (trwiki{sidebar.js, user.js})
- Berkay0652 (trwiki)
- Bilişimci (trwiki)
- BirgitteSB (enwikisource)
- BlueDevil (meta)
- Brk (trwiki)
- CenkX (trwiki)
- CnkALTDS (trwiki)
- Cometstyles (meta)
- Denisutku (trwiki)
- Eftal GEZER (trwiki)
- Elmacenderesi (trwiki)
- Fagus (trwiki)
- Fcn (trwiki{templateScript.js, user.js})
- Gökçe Yörük (trwiki)
- Guzelonlu (trwiki)
- Heldergeovane (ptwikibooks)
- Homonihilis (trwiki)
- Ibrahim Dede (trwiki)
- Isparta (trwiki)
- Jimqode (trwiki)
- Khutuck (trwiki)
- Kibele (trwiki)
- Kizil Saman (trwiki)
- Kumul (trwiki)
- Levent (trwiki)
- Manco Capac (trwiki{monobook-detay.js, sidebar.js})
- Mardetanha (meta)
- Melihsen (trwiki)
- Metal Militia (trwiki{sidebar.js, user.js})
- MrXenoquaten (trwiki)
- Nerval (trwiki)
- Newmanbe (enwikisource, commonswiki, enwiki)
- Nihan (trwiki)
- Onurkayabasi (trwiki)
- Philippe (meta)
- Rebel (rowiki)
- Selkem (trwiki)
- Serhantr (trwiki)
- Spacebirdy (meta, commons)
- Srhat (meta, trwikisource)
- Superyetkin (trwiki)
- Tangotango (enwiki)
- Tema (trwiki)
- Terennum (trwiki)
- Thachan.makan (mlwikisource)
- Thiago R Ramos (commonswiki)
- Vito Genovese (trwiki{sidebar.js, user.js})
- Westnest (trwiki)
- WikiFaculty (trwiki)
- Yabancı (trwiki)
- Yurekli74 (trwiki)
- Zhaladshar (enwikisource)
- version 0.6 (beta)
