wiki 0.0.3

Library for interacting with MediaWiki APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use futures_util::TryStreamExt;
use reqwest::{Client, Url};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde_json::Value;
use tokio::sync::{Mutex, MutexGuard};
use tokio::time::{interval, Interval, MissedTickBehavior};
use tracing::{debug, trace};

use crate::generators::GenGen;
use crate::req::{
    self, Login, Main, MetaUserInfo, PageSpec, QueryMeta, QueryProp, QueryPropRevisions, RvProp,
    RvSlot, TokenType, UserInfoProp,
};
use crate::res::PageResponse;
use crate::url::WriteUrlParams;
use crate::{AccessExt, BotPassword, Result};

#[macro_export]
macro_rules! basic {
    (@handle( $(#[$meta:meta])*  $i:ident { $name:ident: $ty:ty } )) => {
        #[derive(Deserialize, Debug)]
        $(#[$meta])*
        pub struct $i {
            pub $name: $ty,
        }
    };
    (@handle( $i:ident { $name:ident$([$lit:literal])? } )) => {
        #[derive(Deserialize, Debug)]
        pub struct $i<__Inner> {
            $(#[serde(rename = $lit)])?
            pub $name: __Inner,
        }
    };
    (@handle( $i:ident { $T:ident => $name:ident $([$lit:literal])? : $($rest:tt)* } )) => {
        #[derive(Deserialize, Debug)]
        pub struct $i<$T> {
            $(#[serde(rename = $lit)])?
            pub $name: $($rest)*,
        }
    };
    ($( $(#[$meta:meta])* $ty:ident { $($tt:tt)* })*) => {
        $(basic!(@handle( $(#[$meta])* $ty { $($tt)* }));)*
    };
}

basic! {
    SlotsMain { main: Slot }
    QueryResponse { query }
    AbuseLog { T => abuse_log["abuselog"]: Vec<T> }
    Search { T => search: Vec<T> }
    RecentChanges { T => recent_changes["recentchanges"]: Vec<T> }
}

#[derive(Deserialize, PartialEq, Eq, Debug)]
pub struct BasicSearchResult {
    pub ns: u16,
    pub title: String,
    #[serde(rename = "pageid")]
    pub page_id: usize,
}

#[derive(Deserialize, Debug)]
pub struct RecentChangesResult {
    #[serde(rename = "type")]
    pub type_: String,
    pub ns: Option<u16>,
    pub title: Option<String>,
    pub pageid: Option<usize>,
    pub revid: Option<usize>,
    pub old_revid: Option<usize>,
    pub rcid: Option<usize>,
    pub user: Option<String>,
    pub userid: Option<usize>,
    pub oldlen: Option<usize>,
    pub newlen: Option<usize>,
    pub timestamp: Option<String>,
    pub comment: Option<String>,
    pub parsedcomment: Option<String>,
    pub redirect: Option<bool>,
    pub tags: Option<Vec<String>>,
    pub sha1: Option<String>,
    pub oresscores: Option<Value>, // TODO more precise
}

#[derive(Deserialize, Debug)]
pub struct Slot {
    #[serde(rename = "contentmodel")]
    pub content_model: String,
    #[serde(rename = "contentformat")]
    pub content_format: String,
    pub content: String,
}

#[derive(Deserialize, Debug)]
pub struct Revision<S> {
    #[serde(rename = "revid")]
    pub rev_id: u32,
    #[serde(rename = "parentid")]
    pub parent_id: u32,
    pub slots: S,
}

#[derive(Deserialize, Debug)]
pub struct MaybeContinue<T> {
    #[serde(rename = "continue", default)]
    pub cont: Option<Value>,
    #[serde(flatten)]
    pub inner: T,
}

#[derive(Deserialize, Debug)]
pub struct Q2<A, B> {
    #[serde(flatten)]
    pub a: A,
    #[serde(flatten)]
    pub b: B,
}

macro_rules! token {
    ($Name:ident = $field:literal = [$($t:expr),+$(,)?] + $token:ident) => {
        #[derive(Deserialize, Debug)]
        pub struct $Name {
            #[serde(rename = $field)]
            pub $token: String,
        }
        impl Token for $Name {
            fn types() -> TokenType { $($t)|* }
        }
    };
}

token!(LoginToken = "logintoken" = [TokenType::LOGIN] + token);
token!(CsrfToken = "csrftoken" = [TokenType::CSRF] + token);

pub trait Token: DeserializeOwned {
    fn types() -> TokenType;
}

pub trait TokenExt: Token {
    const LEN: usize;
    const QUERY: &'static str;
}

#[derive(Deserialize, Debug)]
pub struct Page<S> {
    #[serde(rename = "pageid")]
    pub page_id: u32,
    pub ns: u8,
    pub title: String,
    pub revisions: Vec<Revision<S>>,
}

#[derive(Deserialize, Debug)]
pub struct Tokens<T> {
    pub tokens: T,
}

#[derive(Deserialize, Debug)]
pub struct Revisions<S> {
    pub pages: HashMap<usize, Page<S>>,
}

pub enum PageRef<'a> {
    Title(&'a str),
    Id(u32),
}

#[derive(Deserialize, Debug)]
pub struct UserInfo<E> {
    pub userinfo: UserInfoInner<E>,
}

#[derive(Deserialize, Debug)]
pub struct UserInfoInner<E> {
    pub id: usize,
    pub name: String,
    #[serde(flatten)]
    pub extra: E,
}

#[derive(Deserialize, Debug)]
pub struct UserInfoRights {
    pub rights: Vec<String>,
}

pub fn mkurl(mut url: Url, m: Main) -> Url {
    let mut q = crate::url::Simple::default();
    if let Err(e) = m.ser(&mut q) {
        match e {}
    }
    url.set_query(Some(&q.0));
    debug!(%url, "GET");
    url
}

pub fn mkurl_with_ext(
    mut url: Url,
    m: Main,
    ext: Value,
) -> Result<Url, serde_urlencoded::ser::Error> {
    let mut q = crate::url::Simple::default();
    if let Err(e) = m.ser(&mut q) {
        match e {}
    }
    q.add_serde(ext)?;
    url.set_query(Some(&q.0));
    debug!(%url, "GET");
    Ok(url)
}

mod sealed {
    pub trait Sealed {}
    impl Sealed for reqwest::RequestBuilder {}
}

pub trait RequestBuilderExt: Sized + sealed::Sealed {
    fn send_and_report_err(
        self,
    ) -> Pin<Box<dyn Future<Output = crate::Result<Value>> + Send + Sync>>;
    fn send_parse<D: DeserializeOwned>(
        self,
    ) -> Pin<Box<dyn Future<Output = crate::Result<D>> + Send + Sync>>
    where
        Self: Send + Sync + 'static,
    {
        Box::pin(async move {
            let v = self.send_and_report_err().await?;
            Ok(serde_json::from_value(v)?)
        })
    }
}

impl RequestBuilderExt for reqwest::RequestBuilder {
    fn send_and_report_err(
        self,
    ) -> Pin<Box<dyn Future<Output = crate::Result<Value>> + Send + Sync>> {
        Box::pin(async {
            let r = self.send().await?;
            let mut v = r.json::<Value>().await?;
            if let Some(v) = v.get_mut("error") {
                Err(crate::Error::MediaWiki(v.take()))
            } else {
                Ok(v)
            }
        })
    }
}

pub async fn fetch(client: &Client, url: Url, spec: PageSpec) -> Result<crate::Page> {
    let mut q = req::Query {
        prop: Some(
            QueryProp::Revisions(QueryPropRevisions {
                prop: [RvProp::Ids, RvProp::Content].into(),
                slots: [RvSlot::Main].into(),
                limit: req::Limit::Value(1),
            })
            .into(),
        ),
        ..Default::default()
    };
    match spec {
        PageSpec::Title(title) => q.titles = vec![title].into(),
        PageSpec::PageId(id) => q.pageids = vec![id].into(),
    };

    let m = Main::query(q);

    let url = mkurl(url, m);
    let res = client.get(url).send_and_report_err().await?;
    trace!("{res}");
    let body: QueryResponse<Revisions<SlotsMain>> = serde_json::from_value(res).unwrap();
    let mut pages = body.query.pages.into_iter();
    let (_, page) = pages.next().expect("page to exist");
    assert!(pages.next().is_none());

    let [rev]: [_; 1] = page.revisions.try_into().unwrap();

    Ok(crate::Page {
        content: rev.slots.main.content,
        latest_revision: rev.rev_id,
        id: page.page_id,
        changed: false,
        bot: None,
    })
}

pub async fn get_tokens<T: Token>(url: Url, client: &Client) -> Result<T> {
    let res = client
        .get(mkurl(url, Main::tokens(T::types())))
        .send()
        .await?;
    let tokens: QueryResponse<Tokens<T>> = res.json().await?;
    Ok(tokens.query.tokens)
}

pub struct BotOptions {
    highlimits: bool,
}

impl crate::Site {
    pub fn mkurl(&self, m: Main) -> Url {
        mkurl(self.url.clone(), m)
    }

    pub async fn get_tokens<T: Token>(&self) -> Result<T> {
        get_tokens(self.url.clone(), &self.client).await
    }

    /// Returns a page with the latest revision.
    pub async fn fetch(&self, spec: PageSpec) -> Result<crate::Page> {
        fetch(&self.client, self.url.clone(), spec).await
    }

    pub async fn login(
        self,
        password: BotPassword,
        editdelay: Duration,
    ) -> Result<crate::Bot, (Self, crate::Error)> {
        async fn login_(
            this: &crate::Site,
            BotPassword { username, password }: BotPassword,
        ) -> Result<BotOptions> {
            let LoginToken { token } = this.get_tokens::<LoginToken>().await?;
            let req = this.client.post(this.url.clone());
            let l = Main::login(Login {
                name: username,
                password,
                token,
            });
            let form = l.build_form();
            let v: Value = req.multipart(form).send_and_report_err().await?;
            debug!("{v}");
            if v.get("login")
                .and_then(|v| v.get("result"))
                .map_or(false, |v| v == "Success")
            {
                let url = this.mkurl(Main::query(req::Query {
                    meta: Some(
                        QueryMeta::UserInfo(MetaUserInfo {
                            prop: UserInfoProp::Rights.into(),
                        })
                        .into(),
                    ),
                    ..Default::default()
                }));
                let res: QueryResponse<UserInfo<UserInfoRights>> =
                    this.client.get(url).send_parse().await?;

                // TODO does highlimits need to be here
                Ok(BotOptions {
                    highlimits: res
                        .query
                        .userinfo
                        .extra
                        .rights
                        .iter()
                        .any(|s| s == "apihighlimits"),
                })
            } else {
                panic!("Vandalism detected. Your actions will be logged at [[WP:LTA/BotAbuser]]")
            }
        }
        let res = login_(&self, password.clone()).await;
        let mut interval = interval(editdelay);
        interval.set_missed_tick_behavior(MissedTickBehavior::Delay);

        match res {
            Ok(options) => Ok(crate::Bot {
                inn: Arc::new(crate::BotInn {
                    pass: password,
                    control: Mutex::new(interval),
                    url: self.url,
                    options,
                }),
                client: self.client,
            }),
            Err(e) => Err((self, e)),
        }
    }
}

impl crate::Access for crate::Site {
    fn client(&self) -> &Client {
        &self.client
    }
    fn url(&self) -> &Url {
        &self.url
    }
}

pub type QueryAllGenerator<A> = GenGen<
    A,
    Main,
    fn(&Url, &Client, &Main) -> Main,
    fn(&Url, &Client, &Main, Value) -> Result<Vec<Value>>,
    Value,
    Value,
>;

impl crate::Bot {
    pub async fn fetch(&self, spec: PageSpec) -> Result<crate::Page> {
        fetch(&self.client, self.inn.url.clone(), spec)
            .await
            .map(|p| crate::Page {
                bot: Some(self.clone()),
                ..p
            })
    }

    pub async fn page_info_all<E: DeserializeOwned>(
        &self,
        mut query: req::Query,
        spec: PageSpec,
    ) -> Result<PageResponse<E>> {
        query.pageids = None;
        query.titles = None;
        match spec {
            PageSpec::PageId(id) => query.pageids = Some(vec![id]),
            PageSpec::Title(title) => query.titles = Some(vec![title]),
        }

        self.query_all(query)
            .try_fold(Value::Null, |mut acc, new| async {
                crate::util::merge_values(&mut acc, new);
                Ok(acc)
            })
            .await
            .and_then(|v| Ok(serde_json::from_value(v)?))
    }

    pub async fn control(&self) -> MutexGuard<'_, Interval> {
        self.inn.control.lock().await
    }

    pub fn options(&self) -> &BotOptions {
        &self.inn.options
    }
}