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
use crate::{Bot, Error, Result, Wikicode};
use log::info;
use parsoid::Result as ParsoidResult;
pub struct Page<'a> {
bot: &'a Bot,
title: String,
}
impl Page<'_> {
pub fn new<'a>(bot: &'a Bot, title: &str) -> Page<'a> {
Page {
bot,
title: title.to_string(),
}
}
pub fn title(&self) -> &str {
&self.title
}
pub async fn exists(&self) -> Result<bool> {
let resp = self
.bot
.api
.get(&[("action", "query"), ("titles", &self.title)])
.await?;
Ok(!resp["query"]["pages"][0]["missing"]
.as_bool()
.unwrap_or_default())
}
pub async fn get_html(&self) -> ParsoidResult<Wikicode> {
Ok(self.bot.parsoid.get(&self.title).await?)
}
pub async fn get_revision_html(
&self,
revid: u32,
) -> ParsoidResult<Wikicode> {
Ok(self.bot.parsoid.get_revision(&self.title, revid).await?)
}
pub async fn get_wikitext(&self) -> Result<String> {
let resp = self
.bot
.api
.get(&[
("action", "query"),
("titles", &self.title),
("prop", "revisions"),
("rvprop", "content"),
("rvslots", "main"),
])
.await?;
let page = resp["query"]["pages"][0].as_object().unwrap();
if page.contains_key("missing") {
Err(Error::PageDoesNotExist(self.title.to_string()))
} else {
let revisions = page.get("revisions").unwrap().clone();
Ok(revisions[0]["slots"]["main"]["content"]
.as_str()
.unwrap()
.to_string())
}
}
pub async fn save_html(
&self,
html: &Wikicode,
summary: &str,
) -> Result<()> {
let wikitext = self.bot.parsoid.transform_to_wikitext(html).await?;
let username = self
.bot
.username
.clone()
.unwrap_or_else(|| "unknown".to_string());
if !crate::utils::nobots(html, &username)? {
return Err(Error::ProtectedPage(
self.title.to_string(),
"{{nobots}}".to_string(),
));
}
let mut params: Vec<(String, String)> = [
("action".to_string(), "edit".to_string()),
("title".to_string(), self.title.to_string()),
("text".to_string(), wikitext),
("bot".to_string(), "1".to_string()),
("summary".to_string(), summary.to_string()),
]
.to_vec();
if let Some(revid) = html.revision_id() {
params.push(("baserevid".to_string(), revid.to_string()));
}
info!("Saving [[{}]]", &self.title);
self.bot.api.post_with_token("csrf", ¶ms).await?;
Ok(())
}
pub async fn save_wikitext(
&self,
wikitext: &str,
summary: &str,
) -> Result<()> {
self.bot
.api
.post_with_token(
"csrf",
&[
("action", "edit"),
("title", &self.title),
("text", wikitext),
("bot", "1"),
("summary", summary),
],
)
.await?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::tests::testwp;
use crate::Error;
#[tokio::test]
async fn test_exists() {
let bot = testwp().await;
let page = bot.get_page("Main Page");
assert!(page.exists().await.unwrap());
let page2 = bot.get_page("DoesNotExistPlease");
assert!(!page2.exists().await.unwrap());
}
#[tokio::test]
async fn test_get_content() {
let bot = testwp().await;
let page = bot.get_page("Main Page");
let html = page.get_html().await.unwrap();
assert_eq!(html.title().unwrap(), "Main Page".to_string());
assert_eq!(
html.select_first("b").unwrap().text_contents(),
"test wiki".to_string()
);
let wikitext = page.get_wikitext().await.unwrap();
assert!(wikitext.contains("'''test wiki'''"));
}
#[tokio::test]
async fn test_missing_page() {
let bot = testwp().await;
let page = bot.get_page("DoesNotExistPlease");
let err = page.get_html().await.unwrap_err();
match err {
Error::PageDoesNotExist(page) => {
assert_eq!(&page, "DoesNotExistPlease")
}
err => {
panic!("Unexpected error: {:?}", err)
}
}
let err2 = page.get_wikitext().await.unwrap_err();
match err2 {
Error::PageDoesNotExist(page) => {
assert_eq!(&page, "DoesNotExistPlease")
}
err => {
panic!("Unexpected error: {:?}", err)
}
}
}
}