2 changed files with 99 additions and 0 deletions
			
			
		@ -0,0 +1 @@ | 
			
		|||||
 | 
				pub mod translation;
 | 
			
		||||
@ -0,0 +1,98 @@ | 
			
		|||||
 | 
				use regex::Regex;
 | 
			
		||||
 | 
				use std::cmp::max;
 | 
			
		||||
 | 
				use std::error::Error;
 | 
			
		||||
 | 
				use std::fmt;
 | 
			
		||||
 | 
				use std::fs::File as FsFile;
 | 
			
		||||
 | 
				use std::io;
 | 
			
		||||
 | 
				use std::io::Read;
 | 
			
		||||
 | 
				use std::ops::Deref;
 | 
			
		||||
 | 
				use std::path::Path;
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				use command_runner::CommandRunner;
 | 
			
		||||
 | 
				use symbols::Symbol;
 | 
			
		||||
 | 
				use resources::Resource;
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				pub struct WordpressTranslation<'a, C, D, R> where C: Deref<Target=str> + fmt::Display, D: AsRef<str> + fmt::Display, R: 'a + CommandRunner {
 | 
			
		||||
 | 
				  path: D,
 | 
			
		||||
 | 
				  language: C,
 | 
			
		||||
 | 
				  command_runner: &'a R
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<'a, C, R> WordpressTranslation<'a, C, String, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
 | 
			
		||||
 | 
				  pub fn new<D: AsRef<str> + fmt::Display>(path: D, language: C, command_runner: &'a R) -> Self {
 | 
			
		||||
 | 
				    WordpressTranslation {
 | 
			
		||||
 | 
				      path: Path::new(path.as_ref()).join("wp-content/languages").to_string_lossy().to_string(),
 | 
			
		||||
 | 
				      language: language,
 | 
			
		||||
 | 
				      command_runner: command_runner
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				/*
 | 
			
		||||
 | 
				https://translate.wordpress.org/projects/wp/4.7.x/de/default/export-translations?format=po > /home/de_adrianheine_blog/WordPress/wp-content/languages/de_DE.po
 | 
			
		||||
 | 
				https://translate.wordpress.org/projects/wp/4.7.x/de/default/export-translations?format=mo > /home/de_adrianheine_blog/WordPress/wp-content/languages/de_DE.mo
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				https://translate.wordpress.org/projects/wp/4.7.x/cc/de/default/export-translations?format=po > /home/de_adrianheine_blog/WordPress/wp-content/languages/continents-cities-de_DE.po
 | 
			
		||||
 | 
				https://translate.wordpress.org/projects/wp/4.7.x/cc/de/default/export-translations?format=mo > /home/de_adrianheine_blog/WordPress/wp-content/languages/continents-cities-de_DE.mo
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				https://translate.wordpress.org/projects/wp/4.7.x/admin/de/default/export-translations?format=po > /home/de_adrianheine_blog/WordPress/wp-content/languages/admin-de_DE.po
 | 
			
		||||
 | 
				https://translate.wordpress.org/projects/wp/4.7.x/admin/de/default/export-translations?format=mo > /home/de_adrianheine_blog/WordPress/wp-content/languages/admin-de_DE.mo
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				https://translate.wordpress.org/projects/wp/4.7.x/admin/network/de/default/export-translations?format=po > /home/de_adrianheine_blog/WordPress/wp-content/languages/admin-network-de_DE.po
 | 
			
		||||
 | 
				https://translate.wordpress.org/projects/wp/4.7.x/admin/network/de/default/export-translations?format=mo > /home/de_adrianheine_blog/WordPress/wp-content/languages/admin-network-de_DE.mo
 | 
			
		||||
 | 
				*/
 | 
			
		||||
 | 
				impl<'a, C, D, R> WordpressTranslation<'a, C, D, R> where C: Deref<Target=str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
 | 
			
		||||
 | 
				  fn get_pairs(&self) -> Vec<(String, String)> {
 | 
			
		||||
 | 
				    let mut res = vec![];
 | 
			
		||||
 | 
				    for &(in_slug, out_slug) in [("", ""), ("cc/", "continents-cities-"), ("admin/", "admin-"), ("admin/network/", "admin-network-")].into_iter() {
 | 
			
		||||
 | 
				      for format in ["po", "mo"].into_iter() {
 | 
			
		||||
 | 
				        res.push((format!("https://translate.wordpress.org/projects/wp/4.7.x/{}{}/default/export-translations?format={}", in_slug, self.language, format), format!("{}/{}de_DE.{}", self.path, out_slug, format)))
 | 
			
		||||
 | 
				      }
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				    res
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<'a, C, D, R> Symbol for WordpressTranslation<'a, C, D, R> where C: Deref<Target=str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
 | 
			
		||||
 | 
				  fn target_reached(&self) -> Result<bool, Box<Error>> {
 | 
			
		||||
 | 
				    let mut newest = String::new();
 | 
			
		||||
 | 
				    let match_date = Regex::new("(?m)^\"PO-Revision-Date: (.+)\\+0000\\\\n\"$").unwrap();
 | 
			
		||||
 | 
				    for (_, target) in self.get_pairs() {
 | 
			
		||||
 | 
				      let file = FsFile::open(target.clone());
 | 
			
		||||
 | 
				      // Check if file exists
 | 
			
		||||
 | 
				      if let Err(e) = file {
 | 
			
		||||
 | 
				        return if e.kind() == io::ErrorKind::NotFound {
 | 
			
		||||
 | 
				          Ok(false)
 | 
			
		||||
 | 
				        } else {
 | 
			
		||||
 | 
				          Err(Box::new(e))
 | 
			
		||||
 | 
				        };
 | 
			
		||||
 | 
				      }
 | 
			
		||||
 | 
				      if target.ends_with(".po") {
 | 
			
		||||
 | 
				        let mut content = String::new();
 | 
			
		||||
 | 
				        try!(file.unwrap().read_to_string(&mut content));
 | 
			
		||||
 | 
				        let file_date = &match_date.captures(&content).unwrap()[1];
 | 
			
		||||
 | 
				        newest = max(newest, file_date.to_string());
 | 
			
		||||
 | 
				      }
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				    let upstream = try!(self.command_runner.get_output("curl", &["https://api.wordpress.org/core/version-check/1.7/?version=4.7.5&locale=de_DE"]));
 | 
			
		||||
 | 
				    Ok(upstream.contains(&format!(r###"language":"de_DE","version":"4.7.5","updated":"{}"###, newest)))
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				  fn execute(&self) -> Result<(), Box<Error>> {
 | 
			
		||||
 | 
				    for (source, target) in self.get_pairs() {
 | 
			
		||||
 | 
				      try!(self.command_runner.run_successfully("curl", &["--compressed", "-o", &target, &source]));
 | 
			
		||||
 | 
				    }
 | 
			
		||||
 | 
				    Ok(())
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				  fn get_prerequisites(&self) -> Vec<Resource> {
 | 
			
		||||
 | 
				    vec![ Resource::new("dir", Path::new(self.path.as_ref()).join("wp-content/languages").to_string_lossy() ) ]
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				impl<'a, C, D, R> fmt::Display for WordpressTranslation<'a, C, D, R> where C: Deref<Target=str> + fmt::Display, D: AsRef<str> + fmt::Display, R: CommandRunner {
 | 
			
		||||
 | 
				  fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
 | 
			
		||||
 | 
				    write!(f, "WordpressTranslation {}", self.path)
 | 
			
		||||
 | 
				  }
 | 
			
		||||
 | 
				}
 | 
			
		||||
 | 
				
 | 
			
		||||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue