Wikipedia if statement dont work - mediawiki

This is my Template:Test
id1: {{{#if: {{{id1}}} | {{{id1}}} | text1}}} <br>
id2: {{{#if: {{{id2}}} | {{{id2}}} | text2}}} <br>
id3: {{{#if: {{{id3}}} | {{{id3}}} | text3}}} <br>
id4: {{{#if: {{{id4}}} | {{{id4}}} }}} <br>
<br>
This is code of page TestPage using this template:
{{Test
| id1 = 123
| id2 =
}}
The output of my TestPage (what user see) is:
id1: 123
id2:
id3: {{{id3}}}
id4: {{{id4}}}
I want the output to be:
id1: 123
id2: text2
id3: text3
id4:
What am I doing wrong? Why is this not outputing what I want?
Edit:
I am using mediawiki version mediawiki-1.30.0.

When a parameter is not given (as for id3 and id4 of you example inclusion, id2 is empty but given) the {{{id3}}} evaluates to the literal text {{{id3}}}. This is a truthy value in the #if condition.
Instead use {{{id3|}}}, which has a default value for "when not given" after the pipe, here it is empty - and treated as falsy in an #if condition. Consider
id1: {{{id1|text1}}}<br>
id2: {{{id2|text2}}}<br>
id3: {{{id3|text3}}}<br>
id4: {{{id4|}}}<br>
which for your inclusion would lead to the output
id1: 123
id2:
id3: text3
id4:
Since you are looking to get a default when the parameter is given but empty, you'd use
id1: {{#if: {{{id1|}}} | {{{id1}}} | text1}} <br>
id2: {{#if: {{{id2|}}} | {{{id2}}} | text2}} <br>
id3: {{#if: {{{id3|}}} | {{{id3}}} | text3}} <br>
id4: {{#if: {{{id4|}}} | {{{id4}}} }} <br>

Related

When input type submit is clicked, generate a query

beginner here. I'm currently making a matching system where owners will register their entries. Once it is done, I will click the "match" button and it'll generate the match. The logic for matching depends on the weight.
For example, if owner 1 and owner 2 registered an entry/ies that has 1900 weight, they'll
automatically be matched. (As you can see at the 2nd table)
How can I achieve these goals of mine? Thank you guys in advance.
tbl_entry
|id| entryName| lightBand| weight |
|---| --------| ----- |---------|
| 1 | owner1 | 1 | 1900 |
| 2 | owner1 | 2 | 1920 |
| 3 | owner2 | 3 | 1900 |
| 4 | owner3 | 4 | 1910 |
| 5 | owner4 | 5 | 1910 |
tbl_matching
| id |fightID| entryName| lightBand| weight | entryName| lightband1| weight1|
|---- |--------| --------| ----- |---------|-------- |----------|--------|
| 1 | 1 | owner1 | 1 | 1900 | owner2 | 3 | 1900 |
| 2 | 2 | owner3 | 4 | 1910 | owner4 | 5 | 1910 |
| 3 | - | owner2 | - | - | - | - | - |
HTML:
<form action="transaction/match" method="POST">
<input type="submit" name="doMatch"> match
</form>
Controller:
public function match {
$formSubmit = $this->input->post('doMatch');
// do the matching query here
}
(hyphen/dash) <<<< means no correspondent player to match with.
Note: I'm using codeigniter3.
First of all, I never use <input type = "submit"> before, usually I use <input type="text"> and put the submit in button.
Let's say you have
<input type="text" name="entryName">
<input type="text" name="weight">
... etc
you can put this in your Controller (for later, you should use Control -> Model)
$weight = $this->input->post('weight'); //get your weight input
//get the opponent data based on weight input
$result = $this->db->get_where('tbl_entry', ['weight' => $weight])->row_array();
//check if the result is not null
if ($result != null) {
$submit = [
//I assume that your ID is autoincrement
'fightID' => 'fight ID', //you can make a function for fight ID and put the result like $fight_ID
'entryName' => $this->input->post('entryName'),
'lightBand' => $this->input->post('lightBand'),
'weight' => $weight,
];
$data[] = array_merge($submit, $result); //merge the entry and the result into 1 array
$this->db->insert('tbl_matching', $data); //insert it into your table matching
}
else {
//like above, but just change it to '-' instead of input->post
}

mysql - Search for a key within inconsistent json structure

I know of the functions such as: JSON_SEARCH() and JSON_EXTRACT() etc. This issue is that I am searching for a key in a json string that is not standardized. for example:
SELECT
*
FROM
users
and the results could be something like this:
+---------------------------------------------+
| name | last | data |
+------+------+-------------------------------+
| john | doe | {"acctNum": "123"} |
+------+------+-------------------------------+
| john | doe | {"data":{ "acctNum": "123" }} |
+------+------+-------------------------------+
| jane | doe | {"data":{ "acctNum": "1234" }}|
+------+------+-------------------------------+
so in this example I want to get john doe with the acctNum of 123. but, also, the location of the acctNum key is different. I have seen here that you can use: JSON_SEARCH(data, $**.acctNum) but I get empty results,
In an ideal world what I would like is:
SELECT * FROM users WHERE JSON_EXTRACT(data, "$**.acctNum") = 123
and receive
+---------------------------------------------+
| name | last | data |
+------+------+-------------------------------+
| john | doe | {"acctNum": "123"} |
+------+------+-------------------------------+
| john | doe | {"data":{ "acctNum": "123" }} |
+------+------+-------------------------------+
also that in the link that is above finding the key acctNum could be extremely nested.
I had tried using the regexp with SELECT * FROM users WHERE data REGEXP '123'; but as you could imagine then the user jane doe will be matched as well
Search a defined value at any path:
SELECT *
FROM users
WHERE JSON_SEARCH(data, 'one', 123);
Search a defined value at a defined path:
SELECT *
FROM users
WHERE JSON_SEARCH(data, 'one', 123) = '$.acctNum'; -- or '$.data.acctNum'
Search a defined property name and value at any path:
SELECT *
FROM users
WHERE JSON_SEARCH(data, 'all', 123) LIKE '%.acctNum"%';
fiddle

Extracting out a string given a starting and ending pattern using MySQL/Presto

Trying to extract text out of a string given a certain starting and ending pattern.
Don't really know where to start. I've looked around and tried to make sense out of regex functions but they go over my head.
Table:
+----+------------------------------------+
| id | sentence |
+----+------------------------------------+
| 1 | Hello, I am a bird. |
| 2 | Hello, I am a cat. I like catfood. |
| 3 | Hello, I am a dog. I like bones. |
+----+------------------------------------+
Trying to extract the text between Hello, and .
Output:
+-------------+
| sentence |
+-------------+
| I am a bird |
| I am a cat |
| I am a dog |
+-------------+
Try with regexp_extract(col,regexp,capture_group) function in hive:
Hello, //match "Hello," literal
([^.]*) //then until first occurrence of .(period) capture as first group
Example:
hive> select regexp_extract(sentence,"Hello,([^.]*)",1)sentence from(
--preparing sample data
select stack(3,'Hello, I am a bird.','Hello, I am a cat. I like catfood.','Hello, I am a dog. I like bones.')
as(sentence))t;
Result:
sentence
I am a bird
I am a cat
I am a dog

In Karate DSL, how can I use the replace text for other data types such as int, float, Big, etc.?

I found the below example on github.
def text = 'hello world bye'
replace text
| token | value |
| one | 'cruel' |
| two | 'good' |
match text == 'hello cruel world good bye'
What If the value I want to replace can only accept integers or other data types? For example,
replace text
| token | value|
| hours | 90 |
| price | 123.45 |
| quantity | 999999999999 |
I was not able to put the token inside another file because the json validator does not like the <> without the double quotes. any suggestions?
Replace is meant for text not JSON, read the doc carefully please. First, there is no problem with numbers and replace:
* def text = 'hello <name> how many <hours>'
* replace text
| token | value |
| name | 'John' |
| hours | 200 |
* match text == 'hello John how many 200'
Now, if you are trying to fiddle with JSON, just use the set keyword.
* def json = { hello: '', hours: null }
* set json
| path | value |
| hello | 'John' |
| hours | 200 |
* match json == { hello: 'John', hours: 200 }
Note that the above would work even if you omit the first line. Also refer to embedded expressions as another way to substitute values in JSON, refer to the doc.

get first 3 alphanumeric characters (only numbers or letters)

I have a table which holds a field, title, I need to get first 3 alphanumeric characters of each title. Some of the values of title have ",',\t,\n, or whitespace prepended - this should be ignored.
+--------+-----------------------------------------+---------------------+
| id | title | desired output |
+--------+-----------------------------------------+---------------------+
| 1 | "abcd" | abc |
| 2 | 'lostworld | los |
| 3 | \tsonof | son |
| 4 | 12amrt | 12a |
+--------+-----------------------------------------+---------------------+
desired output is the output I am looking for. If anyone can suggest generic query which can handle all cases that would be great.
Looking for solution using MySQL only.
Your best bet is to use a regex user-defined function.
The built-in regexp functions only support matching; not string replacing like you want here