Access other attributes in the same subobject in semantic-mediawiki - mediawiki

I am using subproperties in semantic mediawiki to store different kinds of information, e.g. a log.
This can look like this:
{{#subobject:
|Has date = 2020-06-18
|Has log = Diary
|Has tag = Stackoverflow
|Has agenda =
* Done Something
}}
What I would like to do is to use an #ask query inside of the agenda part that accesses the date parameter, like this:
{{#subobject:
|Has date = 2020-06-18
|Has log = Diary
|Has tag = Stackoverflow
|Has agenda =
* The date today is:
{{#ask:
[[Has log::Diary]]
[[Has date::<<Is there a way to access the date of this subobject?>>]]
|?Has date = Date
}}
}}
I know that this is not possible directly, since the text has to be stored in the database first in order to retrieve it later, but I would not mind if I had to save it twice or need another workaround.
Thank you!

The obvious solution is to wrap this code in a template, say Template:Appointment:
<includeonly>{{#subobject:
|Has date = {{{date|}}}
|Has log = {{{log|}}}
|Has tag = {{{tag|}}}
|Has agenda = {{{agenda|}}}
}}
Also on {{{date|}}} in {{{log|}}}:
{{#ask:
[[Has log::{{{log|}}}]]
[[Has date::{{date|}}}]]
|?Has date = Date
|format = ul
}}
</includeonly>
The call the template like
{{Appointment
| date = 2020-06-18
| log = Diary
| tag = Stackoverflow
| agenda = <nowiki>
* The date today is:
}}
whenever you want to add records to log.

Related

using LINQ-to-SQL in vb.net with multiple select statement

I'm very new to vb.net and LINQ to SQL.
I have been trying the whole day to do this but it doesn't seem to work at all. all your help is highly appreciated.
I have a table named users which contains 4 columns
+---------+-----------+---------------+--------------+
| user_id | user_name | user_password | user_stopped |
+---------+-----------+---------------+--------------+
| 1 | admin | admin | false |
| 2 | user2 | 2 | false |
| 3 | user3 | 3 | true |
+---------+-----------+---------------+--------------+
I have a from with three textboxes "txtuserid" & "textusername" & "txtuserpassword" and a button name "login"
1) on the "on_click" event of the "login" button , I have this query
Dim query = From check In Me.MydatabaseDataSet.users
Where check.user_id = Me.txtuserid.Text
Select check.user_name, check.user_password, check.user_stopped
I want to do something like this:
if query.check.user_stopped= true then
msgbox("this user has no permission")
else
me.txtusername.text= query.user_name
if me.txtuserID.text= query.check.user_id and
me.txtuserpassword.text= query.check.user_password then
me.hide()
form2.show()
end if
end if
I have been trying for hours but nothing seems to work at all.
I'm using VB.net 2010 with SQL database.
All your help is highly appreciated.
In fact Linq is really simple.
Dim id as Integer
integer.TryParse(Me.txtuserid.Text, id)
Dim query = From check In Me.MydatabaseDataSet.users
Where check.user_id = id
Select check
Would simply return all users with a given Id (Me.txtuserid.Text - converted to integer, it is an integer, right?).
If you specifically want 3 columns:
Dim query = From check In Me.MydatabaseDataSet.users
Where check.user_id = id
Select New With {check.user_name, check.user_password, check.user_stopped}
But keep in mind, this version is returning anonymous type where previous one returns User type.
If you think about it, user_id is a primary key (unique). Thus you don't need to get back a "collection" as the above "query", you simply need a single User:
Dim user = Me.MydatabaseDataSet.users.SingleOrDefault(Function(check) check.user_id = id)
If that ID exists than the user has properties of that user (typed data), else NULL.
This one matches to code that you later want to execute:
if user is not nothing
msgbox("unknown user")
else
if user.user_stopped= true then
msgbox("this user has no permission")
else
me.txtusername.text= user.user_name
if me.txtuserID.text= user.user_id and
me.txtuserpassword.text= user.user_password then
me.hide()
form2.show()
end if
end if
end if
Note: I assume this is just for hobby testing purposes, in real world you would never want to store plain text passwords.

Get date difference using adx studio liquid template

Im trying to get the day difference between two dates using adx studio liquid templates, my current code is
{%assign expirydate = bh_expirydate | date:'MM/dd/yyyy' %}
{%assign datenow = now | date: 'MM/dd/yyyy' %}
{%assign diffdays = expirydate | minus: datenow %}
I know that this line of code will not work, but the logic is that. I just can seem to find appropriate example. Can someone shed some light on this one?
Not sure how you would do this without javascript, but here is my solution assuming bh_expirydate exists:
Days till expiry: <span id="expiryDays"></span>
<script>
Date.daysBetween = function( date1, date2 ) {
//Get 1 day in milliseconds
var one_day=1000*60*60*24;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
return Math.round(difference_ms/one_day);
}
var dt1 = new Date();
var dt2 = new Date('{{ bh_expirydate| date: "yyyy/MM/dd" }}');
$('#expiryDays').text(Date.daysBetween(dt1, dt2));
</script>
Calculate years since first date until now
With the first assign we are taking the current time and subtracting a date of birth.
This will result in a timespan. Convert it to a string and split to get the number of days in the first array element.
The second assign will take that first string element with the number of days and convert it into an integer.
The display will divide by 365 to give the years since dob.
{% if item.dob %}
{% assign words = now | minus: item.dob | string | split: '.' %}
{% assign days = words.first | integer %}
{{ days | divided_by: 365 }}
{% endif %}

Subquerying in Django

I have a Django 1.9 project implementing small chat app. All messages from a certain recipient are grouped into dialogs, so the models are defined as follows:
class Dialog(models.Model):
# Some fields
class Message(models.Model):
dialog = models.ForeignKey(Dialog, ...)
text = models.TextField()
is_read = models.BooleanField(default = False)
My goal is to render a template with a table that renders dialogs. And for each dialog in the table, I need to see
the number of unread messages and
the text of the last message.
To illustrate, consider mock-data below:
Input:
id dialog_id message is_read
1 1 Hello, sir false
2 1 My name is true
3 1 Jack true
4 2 This site false
5 2 is perfect false
6 2 Cheers false
Desired output:
dialog_id last_message_in_dialog unread_messages_count
1 Jack 1
2 Cheers 3
In pure mysql, I would write a query like this:
select
a.dialog_id,
text as last_message_in_dialog,
(select count(*) from message
where dialog_id = a.dialog_id and is_read = false) as unread_messages_count
from message a
where id in (select max(id) from message group by dialog_id)
In Django terms, I have the code below:
max_id_qs = Message.objects.\
values('dialog__id').\
annotate(max_id = Max('id'),).values('max_id')
qs = Message.objects.filter(id__in = max_id_qs).\
values('dialog__id', 'text')
This code serves well to fetch the last message in each dialog. However, the problem is that I can't figure out how to implement the subquery (select count(*) from message where dialog_id = a.dialog_id and is_read = false) in Django. Maybe my total approach with max_id_qsis wrong, and there's more elegant and clear way to implement the query in Django ORM?
I've spent an entire day trying to solve this issue. help me please !
This will work :-
allDistinctIdWithNotReadMsg =
Message.objects.filter(is_read=False).values('id').annotate(the_count=Count('is_read',distinct('id')))
for ids in allDistinctIdWithNotReadMsg:
lastMsg = Message.objects.filter(dialog_id=ids['id']).order_by("-id")[0]
for msg in lastMsg:
print ids['id'] ,msg.message,ids['the_count']

wordpress change last modified date of the post to post date (scheduled post)

I have an idea to change the value of post_modified, so the value will have the same value with the post_date.
But Wordpress has a revision system, so I want to change the post_modified on the revision to have a same value with post_date.
Here is my query to change it:
$query = "UPDATE $wpdb->posts
SET
post_modified = '$recent->post_date',
post_modified_gmt = '$recent->post_date_gmt'
WHERE
ID = '$update->ID'";
$wpdb->query($query);
$recent->post_date is the post_date (scheduled time / time where our post will appear in the website)
$recent->post_date_gmt is post_date_gmt
$update->ID is the revision ID in posts table.
But when I run the query, it doesn't change the value and stops my plugin.
Is there something I missed? Or is it that Wordpress itself doesn't allow us to change the post_modified?
You can use the normal wp_update_post function
$args = new stdClass;
$args->ID = $yourPostIdVal;
$args->post_modified = $yourDateString;
$args->post_modified_gmt = $yourGmDateString;
wp_update_post($args);
I have faced same like this problem when I tried to update post_date.Finally I used "`" mark to wrap column names.Do that exactly as follows.
$postID = $update->ID;
$datetime = date("Y-m-d H:i:s");
$wpdb->query( "UPDATE `$wpdb->posts` SET
`post_date` = '".$datetime."'
WHERE `ID` = '".$postID."'" );
Remember, $datetime is a valid date, like 2015-01-04 or 2015-01-04 23:59:59
Hope this would be help.
Thanks

Linq2Sql query - Pivot data with Grouping

I have the following dataset for a TimeTable that needs to be displayed in a gridview. Currently a snippet of the dataset looks like this:
SessionNum TimeStart TimeStop Details
---------- --------- -------- -------
1 08:00 09:00 Math101
1 09:00 10:00 Comp102
1 11:00 12:00 Engn101
2 08:00 09:00 Comp102
2 09:00 10:00 Math101
2 10:00 11:00 Acco103
There are a total of 5 sessions, and I would like for the dataset to look like:
TimeStart TimeStop Session1 Session2 ...
--------- -------- -------- -------- ---
08:00 09:00 Math101 Comp102
09:00 10:00 Comp102 Math101
10:00 11:00 - Acco103
11:00 12:00 Engn101 -
As you will see, there are no aggregate functions required...just grouping, but for the life of me I cannot seem to wrap my head around this one. I have the following LINQ query which generates the first dataset:
List<TimeTable> list = db.TimeTables.OrderBy(o => o.TimeStart).OrderBy(o => o.SessionNum).ToList();
This works fine, and generates the dataset sorted by SessionNum and then TimeStart. My attempt to solve this invlovled the following query:
var result = list.GroupBy(t => t.TimeStart).Select(s => new {
TimeStart = s.Key,
Session1 = s.Where(x => x.SessionNum == 1),
Session2 = s.Where(x => x.SessionNum == 2)
});
This ran, but unfortunately did not work. I know a GroupBy (or a couple) is/are required, but I'm a bit lost from this point forward. I would really appreciate any help towards solving this. Thank you in advance!
You can't directly do a pivot query in LINQ. What you can do instead is create a structure like this:
var record = new
{
TimeStart = "10:00",
TimeStop = "11:00",
Sessions = new [] { "-", "Acco103", },
};
When you have a list of these records you must ensure that the Sessions property is array that is the same length as the distinct number of sessions in your entire set of data. Then you can access the session information by indexing into the array.
This should make more sense after looking at the queries.
First, query the database for the required data:
var query =
from s in db.TimeTables
orderby s.TimeStop
orderby s.TimeStart
group s by new { s.TimeStart, s.TimeStop } into gss
select new
{
gss.Key.TimeStart,
gss.Key.TimeStop,
Sessions = gss.ToArray(),
};
Now determine the distinct set of sessions:
var sessionNums =
db.TimeTables
.Select(s => s.SessionNum)
.Distinct()
.OrderBy(n => n)
.ToArray();
Now process this data in memory (note the .ToArray() call on query):
var process =
from q in query.ToArray()
let lookup = q.Sessions
.ToLookup(s => s.SessionNum, s => s.Details)
select new
{
q.TimeStart,
q.TimeStop,
Sessions = sessionNums
.Select(n => String.Join(
", ",
lookup[n].DefaultIfEmpty("-")))
.ToArray(),
};
This is where the hard work is. The lookup creates an easy way to get session detail out for any SessionNum. Calling lookup[n].DefaultIfEmpty("-") ensures that there is at least a single value for each session. The String.Join ensures that if the source data had two sessions for the same session number at the same time that we end up with one value.
This result is safe no matter how many sessions there are as it will just extend the arrays.
The output of the process query looks like this:
Then you can do this query:
var result =
from p in process
select new
{
p.TimeStart,
p.TimeStop,
Session1 = p.Sessions[0],
Session2 = p.Sessions[1],
};
This will effectively "pivot" your results, but you need to explicitly put in each "SessionX" property.
The output of the result query looks like this: