Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusmanske committed Jun 11, 2024
1 parent 25bf9ef commit ba850a4
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/datasource_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,11 +1004,11 @@ impl SourceDatabase {
let page_title = String::from_utf8_lossy(page_title).into_owned();
let page_timestamp = String::from_utf8_lossy(page_timestamp).into_owned();
let mut entry = PageListEntry::new(Title::new(&page_title, *page_namespace));
entry.page_id = Some(*page_id);
entry.set_page_id(Some(*page_id));
entry.set_page_bytes(Some(*page_bytes));
entry.set_page_timestamp(Some(page_timestamp));
if self.params.gather_link_count {
entry.link_count = Some(*link_count);
entry.set_link_count(Some(*link_count));
}
if pages_sublist.add_entry(entry).is_ok() {}
},
Expand Down
8 changes: 4 additions & 4 deletions src/pagelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl PageList {
.map_err(|e| format!("{:?}", e))?
.par_iter()
.any(|entry| {
entry.page_id.is_none()
entry.page_id().is_none()
|| entry.page_bytes().is_none()
|| entry.get_page_timestamp().is_none()
})
Expand All @@ -458,7 +458,7 @@ impl PageList {
Ok((_page_title, _page_namespace, page_id, page_len, page_last_rev_timestamp)) => {
let page_last_rev_timestamp =
String::from_utf8_lossy(&page_last_rev_timestamp).into_owned();
entry.page_id = Some(page_id);
entry.set_page_id(Some(page_id));
entry.set_page_bytes(Some(page_len));
entry.set_page_timestamp(Some(page_last_rev_timestamp));
}
Expand Down Expand Up @@ -755,7 +755,7 @@ WHERE {} IN ({})",prefix,&field_name,namespace_id,table,term_in_lang_id,&field_n
.read()
.map_err(|e| format!("{:?}", e))?
.iter()
.filter_map(|entry| entry.page_id)
.filter_map(|entry| entry.page_id())
.collect();
let api = platform.state().get_api_for_wiki(wiki).await?;
let mut futures = vec![];
Expand All @@ -782,7 +782,7 @@ WHERE {} IN ({})",prefix,&field_name,namespace_id,table,term_in_lang_id,&field_n
return Err("Filter searches have failed".to_string());
}

self.retain_entries(&|entry: &PageListEntry| match entry.page_id {
self.retain_entries(&|entry: &PageListEntry| match entry.page_id() {
Some(page_id) => retain_page_ids.contains(&page_id),
None => false,
})?;
Expand Down
40 changes: 36 additions & 4 deletions src/pagelist_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ impl TriState {
#[derive(Debug, Clone)]
pub struct PageListEntry {
title: Title,
pub disambiguation: TriState,
pub page_id: Option<u32>,
disambiguation: TriState,
page_id: Option<u32>,
page_bytes: Option<u32>,
pub incoming_links: Option<LinkCount>,
pub link_count: Option<LinkCount>,
incoming_links: Option<LinkCount>,
link_count: Option<LinkCount>,
redlink_count: Option<LinkCount>,
sitelink_count: Option<LinkCount>,
page_timestamp: Option<String>,
Expand Down Expand Up @@ -506,6 +506,38 @@ impl PageListEntry {
pub fn set_page_bytes(&mut self, page_bytes: Option<u32>) {
self.page_bytes = page_bytes;
}

pub fn disambiguation(&self) -> &TriState {
&self.disambiguation
}

pub fn set_disambiguation(&mut self, disambiguation: TriState) {
self.disambiguation = disambiguation;
}

pub fn link_count(&self) -> Option<u32> {
self.link_count
}

pub fn set_link_count(&mut self, link_count: Option<LinkCount>) {
self.link_count = link_count;
}

pub fn incoming_links(&self) -> Option<u32> {
self.incoming_links
}

pub fn set_incoming_links(&mut self, incoming_links: Option<LinkCount>) {
self.incoming_links = incoming_links;
}

pub fn page_id(&self) -> Option<u32> {
self.page_id
}

pub fn set_page_id(&mut self, page_id: Option<u32>) {
self.page_id = page_id;
}
}

#[cfg(test)]
Expand Down
21 changes: 11 additions & 10 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,16 +787,18 @@ impl Platform {
});
}
if add_disambiguation {
entry.disambiguation = match parts.remove(0) {
let dis = match parts.remove(0) {
my::Value::NULL => TriState::No,
_ => TriState::Yes,
}
};
entry.set_disambiguation(dis);
}
if add_incoming_links {
entry.incoming_links = match parts.remove(0) {
let il = match parts.remove(0) {
my::Value::Int(i) => Some(i as LinkCount),
_ => None,
};
entry.set_incoming_links(il);
}
if add_sitelinks {
let sc = match parts.remove(0) {
Expand Down Expand Up @@ -2176,7 +2178,7 @@ mod tests {
.collect::<Vec<PageListEntry>>();
assert_eq!(entries.len(), 1);
let entry = entries.get(0).unwrap();
assert_eq!(entry.page_id, Some(1340715));
assert_eq!(entry.page_id(), Some(1340715));
let fi = entry.get_file_info();
assert!(fi.is_some());
let fi = fi.unwrap();
Expand Down Expand Up @@ -2205,16 +2207,15 @@ mod tests {
.collect::<Vec<PageListEntry>>();
assert_eq!(entries.len(), 1);
let entry = entries.get(0).unwrap();
assert_eq!(entry.page_id, Some(36995));
assert_eq!(entry.page_id(), Some(36995));
assert!(entry.page_bytes().is_some());
assert!(entry.get_page_timestamp().is_some());
assert_eq!(
entry.get_page_image(),
Some("Kings_College_(233225593).jpeg".to_string())
);
assert_eq!(entry.disambiguation, TriState::No);
assert!(entry.incoming_links.is_some());
assert!(entry.incoming_links.unwrap() > 7500);
assert_eq!(*entry.disambiguation(), TriState::No);
assert!(entry.incoming_links().unwrap() > 7500);
assert!(entry.get_coordinates().is_some());
}

Expand All @@ -2232,7 +2233,7 @@ mod tests {
.collect::<Vec<PageListEntry>>();
assert_eq!(entries.len(), 1);
let entry = entries.get(0).unwrap();
assert_eq!(entry.page_id, Some(239794));
assert_eq!(entry.page_id(), Some(239794));
assert_eq!(entry.get_wikidata_item(), Some("Q12345".to_string()));
}

Expand Down Expand Up @@ -2269,7 +2270,7 @@ mod tests {
.collect::<Vec<PageListEntry>>();
assert_eq!(entries.len(), 1);
let entry = entries.get(0).unwrap();
assert_eq!(entry.page_id, Some(13925));
assert_eq!(entry.page_id(), Some(13925));
assert_eq!(entry.get_wikidata_label(), Some("Graaf Tel".to_string()));
assert_eq!(
entry.get_wikidata_description(),
Expand Down
10 changes: 5 additions & 5 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ pub trait Render {
for (k, _) in header {
let cell = match k.as_str() {
"title" => self.render_cell_title(entry, params),
"page_id" => self.opt_u32(&entry.page_id),
"page_id" => self.opt_u32(&entry.page_id()),
"namespace" => self.render_cell_namespace(entry, params),
"size" => self.opt_u32(&entry.page_bytes()),
"timestamp" => self.opt_string(&entry.get_page_timestamp()),
"wikidata_item" => self.render_cell_wikidata_item(entry, params),
"image" => self.render_cell_image(&entry.get_page_image(), params),
"number" => params.row_number().to_string(),
"defaultsort" => self.opt_string(&entry.get_defaultsort()),
"disambiguation" => self.opt_bool(&entry.disambiguation.as_option_bool()),
"incoming_links" => self.opt_linkcount(&entry.incoming_links),
"disambiguation" => self.opt_bool(&entry.disambiguation().as_option_bool()),
"incoming_links" => self.opt_linkcount(&entry.incoming_links()),
"sitelinks" => self.opt_linkcount(&entry.sitelink_count()),

"img_size" => match &entry.get_file_info() {
Expand Down Expand Up @@ -205,8 +205,8 @@ pub trait Render {
},

"checkbox" => self.render_cell_checkbox(entry, params, platform),
"linknumber" => match &entry.link_count {
Some(lc) => format!("{}", &lc),
"linknumber" => match entry.link_count() {
Some(lc) => format!("{lc}"),
None => String::new(),
},
"redlink_count" => match entry.redlink_count() {
Expand Down
2 changes: 1 addition & 1 deletion src/render_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl Render for RenderHTML {
}
} else {
if params.autolist_wiki_server() == AUTOLIST_COMMONS {
q = match entry.page_id {
q = match entry.page_id() {
Some(id) => id.to_string(),
None => String::new(),
}
Expand Down
10 changes: 5 additions & 5 deletions src/render_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl RenderJSON {
let mut o = json!({
"n":"page",
"title":entry.title().with_underscores(),
"id":entry.page_id.unwrap_or(0),
"id":entry.page_id().unwrap_or(0),
"namespace":entry.title().namespace_id(),
"len":entry.page_bytes().unwrap_or(0),
"touched":entry.get_page_timestamp().unwrap_or_default(),
Expand Down Expand Up @@ -211,7 +211,7 @@ impl RenderJSON {
.iter()
.map(|entry| {
let mut o = json!({
"page_id" : entry.page_id.unwrap_or(0),
"page_id" : entry.page_id().unwrap_or(0),
"page_namespace" : entry.title().namespace_id(),
"page_title" : entry.title().with_underscores(),
"page_latest" : entry.get_page_timestamp().unwrap_or_default(),
Expand Down Expand Up @@ -307,11 +307,11 @@ impl RenderJSON {
"checkbox" | "number" | "page_id" | "title" | "namespace" | "size"
| "timestamp" => None,
"image" => entry.get_page_image().map(|s| json!(s)),
"linknumber" => entry.link_count.as_ref().map(|s| json!(s)),
"linknumber" => entry.link_count().map(|s| json!(s)),
"wikidata" => entry.get_wikidata_item().map(|s| json!(s)),
"defaultsort" => entry.get_defaultsort().map(|s| json!(s)),
"disambiguation" => Some(entry.disambiguation.as_json()),
"incoming_links" => entry.incoming_links.as_ref().map(|s| json!(s)),
"disambiguation" => Some(entry.disambiguation().as_json()),
"incoming_links" => entry.incoming_links().map(|s| json!(s)),
"sitelinks" => entry.sitelink_count().map(|s| json!(s)),
"coordinates" => entry
.get_coordinates()
Expand Down

0 comments on commit ba850a4

Please sign in to comment.