Is it possible to have predefined values on a template? I.e.
{{Infobox Employee
|age = {{{age}}}
|gender = {male,female}
|occupation = {nurse, doctor, caretaker}
}}
So whenever someone uses this template they have to chose either male or female and if neither is chosen nothing shows. Very much like a select in html.
In core MediaWiki, you can make the template output an error or ignore unexpected parameters but cannot prevent the user from entering an invalid value. E.g. you can do something like this in the infobox:
{{#switch:{{{gender}}}
| male = Male
| female = Female
| #default = <span style="color:red">Invalid gender</span>[[Category:Pages with invalid template parameters]]
}}
(This example uses #switch from the ParserFunctions extension. You can do the same thing without ParserFunctions but it requires some complex and awkward template code.)
With the TemplateData extension you can specify allowed values, but it will only affect editors which rely on TemplateData (e.g. VisualEditor); it won't influence what someone can do with the template by plaintext editing.
The Semantic Forms extension can display an actual form for editing the infobox, with select boxes and whatever else you specify.
Related
Example for what I want to achieve
I have many patch pages ("Patch 1.4", "Patch 1.5" etc.) that list the changes that were made to a project, where the affected/changed things are linked to their corresponding pages ("confirmation dialog", "foo", etc.):
Patch 1.4
Fixed spelling in the [[confirmation dialog]]
Patch 1.5
Added two options: [[foo]], [[bar]]
On the pages about the things that were changed ("confirmation dialog", "foo", …), I want to automatically show all corresponding changes:
Foo
[[Patch 1.5]]: Added two options: [[foo]], [[bar]]
Semantic MediaWiki’s subobjects can do this
#subobject allows me to create an (anonymous) object for each change on the patch pages:
{{#subobject:|
|Changes=Added two options: [[foo]], [[bar]]
|Affects=Foo|Bar
}}
And on each page ("foo" etc.) I can include an #ask subobject query to list all the matching subobjects:
{{#ask: [[Affects::{{FULLPAGENAME}}]]
|? Changes
}}
Great.
Problem: I have to duplicate the change entry.
On the patch pages, a change entry looks like this:
* Added two options: [[foo]], [[bar]] {{#subobject:|
|Changes=Added two options: [[foo]], [[bar]]
|Affects=Foo|Bar
}}
So I have to specify "Added two options: [[foo]], [[bar]]" two times: one time for the visible content, one time for the invisible subobject.
Is there a way in (Semantic) MediaWiki to do this without having to duplicate the content?
The ideal solution would just require me to enclose the change entry and specify the affected pages next to it, e.g.:
* {{ Added two options: [[foo]], [[bar]] }}((foo|bar))
As each patch page can list hundreds of changes, I don’t want to have to create a separate page for each change.
If I understand your question clearly, it seems you just need a simple query:
{{#ask: [[-Has subobject::{{FULLPAGENAME}}]]
| ?Changes
| format = ul
| headers = hide
| mainlabel = -
}}
Since using SMW markup may be tedious and error-prone, you might also use MediaWiki templates. You can simplify adding patch changes:
Template:Change
<includeonly><!--
-->{{#subobject:|
| Changes = {{{1|}}}
| Affects = {{{2|}}}|+sep=;
}}<!--
--></includeonly><nowiki/>
{{{1}}} and {{{2}}} are positional parameter,s and the Affects subobject property uses the ; separator (as a pipe | is ambiguous and may break templates, parser functions, etc). The <nowiki/> is a sort of a hack saving from whitespace bloating at the call-site pages.
You can also add a special template that would encapsulate the changes query:
Template:Patch changes
<includeonly><!--
-->{{#ask: [[-Has subobject::{{{1|{{FULLPAGENAME}}}}}]]
| ?Changes
| format = ul
| headers = hide
| mainlabel = -
}}<!--
--></includeonly><nowiki/>
By default, the template asks for the changes list for the current page (if the positional parameter #1 argument is empty), or you can explicitly override it at the call-site later (say, {{Patch changes|Patch 1.5}}).
Patch 1.4
{{Change | Fixed spelling in the [[confirmation dialog]] | Confirmation dialog}}
{{Patch changes}}
Patch 1.5
{{Change | Added two options: [[foo]], [[bar]] | Foo; Bar}}
{{Patch changes}}
respectively.
These links might be useful later:
SMW subobjects and queries
SMW standard parameters for inline queries
SMW templates
MW templates anonymous parameters
I am a college student just learning HTML. The assignment that I have to complete is a simple grade calculator. There is a single button that calculates everything, and the values go to three empty text fields(total, average, and percentage). The problem that I am having is the total is calculated and shows up in the field, but the number is followed by [object]. The average field shows NaN and the percentage field remains empty. Here is my code.
<input type="button" value="Click to calculate"
onclick="fullmark = parseFloat(document.getElementById('fullBox').value);
science = parseFloat(document.getElementById ('scienceBox').value);
math = parseFloat(document.getElementById('mathBox').value);
computer = parseFloat(document.getElementById('computerBox').value);
english = parseFloat(document.getElementById('englishBox').value);
History = parseFloat(document.getElementById('historyBox').value);
total=science+math+computer+english+history;
average=(total/5);
percentage=(total/fullmark)*100;
document.getElementById('totalBox').value=total;
document.getElementById('averageBox').value=average;
document.getElementById('percentageBox').value=percentage;">
Since you wrote History as the variable name first, but then used history when computing/assigning the value for total, that is a different variable – identifiers in JavaScript are case sensitive.
The only reason you don’t get your script aborted with an error about an undefined variable at that point, is that window.history exists – it’s the object that keeps the current navigation history of the window. (All global variables are properties of the window object, so that’s what your use of history gets resolved to.) And since it’s an object, [object] is what you get as result when transferring it into a string context (which happens through the use of +, which is both the addition and the string concatenation operator).
Apart from that, writing all that code into an onclick handler is kinda terrible style. Go learn how to use proper functions for stuff like this next ;-)
Folks,
I'd like to have similar set up to the one discussed in this question
Can I use templates to form a set of variables that I can then later include in various points in my wiki pages.
so for example have varables in my single template called MAIN_WEB_URL and MAIN_TEAM_DISTRIBUTION_LIST that I can store in a single template and reference site-wide where needed.
I'd like to avoid a template per variable if possible, but the other question seems to imply this is the only way to do it...
One way to do this would be to create a template called something like Template:Variable. In it, you would use one big {{#switch}} that would contain the variables and their values (this requires the ParserFunctions extension):
{{#switch: {{{1}}}
| MAIN_WEB_URL = some value
| MAIN_TEAM_DISTRIBUTION_LIST = another value
}}
You would then call it like this:
{{variable|MAIN_WEB_URL}}
(BTW, I am not a fan of ALL_CAPS names and I think there is no reason to use them here, but that's up to you.)
Is there a way I can create a database from which to pull data into my mediawiki table? Or is there a way to have a database like drupal and place a mediawiki type interface on it?
There is no way to directly do this in stock MediaWiki, although you can fake it up somewhat with templates. For example, you could can a template something like this:
{{#switch:{{{key}}}
| key1 = value1
| key2 = value2
| key3 = value3
...
}}
Template:NUMBEROF/data on the English Wikipedia is an example of this style (with two levels of keys).
Or you can create a set of templates, one for each "record", that each take an "output formatter" template as a parameter and pass that output formatter a named parameter for each column in the record. The Country data templates on the English Wikipedia are an example of this pattern.
Or you could combine the above two styles, with one parameter to select the row (as in the first style) and a second to provide the output formatter (as in the second).
If you don't mind installing extensions, you could use the Labeled Section Transclusion extension to transclude sections of a data page. Or you could install the Semantic MediaWiki extension, which I hear allows all sorts of querying of data from the wiki's pages. Or you could install one of the many Database extensions that may allow you to do what you want. Or you could write your own database extension.
You could also have a look at http://www.mediawiki.org/wiki/Extension:Data_Transfer, which do not require Semantic MediaWiki even though it's written for use with SMW. (If you use SMW there are, as noted in an earlier reply, plenty extensions and built in options.)
I receive input in the form of URL strings (aka controller/action?example=yes), and I'm wondering if I need to escape the content of the string for security.
For example, if I assign the param to a variable:
example = params[:example].to_s
do I need to escape anything? or do I only apply h() when I put the value of :example back in the view file?
It depends on what you are doing with it, if you are worried of SQL injections , then you can trust ActiveRecord, like doing:
Examples.find_by_name params[:example]
or
Examples.find(:conditions=> ["name = ?", params[:example]])
On the other side, the common strategy of filtering is on display side, so you save the input as is, and you filter on display(views) by using h().
If you still want to save some HTML input from the user like when doing in rich editors, then you have to pay extra attention to XSS attacks, and so you have to filter the input. One great gem for filtering HTML is Sanitize, use it to save a modified filtered version of user input to use it in views.