R/page_cat.R

#' List all categories where a page is located
#'
#' @param x Either a number giving page id or a string giving his title.
#' @param domain The domain where the wiki is located.
#'
#' @return A character vector giving 
#' @export
#'
#' @examples
#' # Downloading the list of categories in the action page
#' page_cat('Action') 

page_cat <- function(x, domain = "fr") {
  
  result <- vector(mode = "character")
  
  query = list(
    action = "query", 
    prop = "categories", 
    cllimit = "max", 
    redirects = "")
  
  if(is.numeric(x)) {
    query["pageids"] <- x
  } else {
    query["titles"] <- x
  }
  
  repeat {
    
    exec <- exec_query(query, domain = domain)
    
    if (!is.null(exec)) {
      
      dat <- exec$query$pages[[1]]$categories %>%
        sapply("[[", "title")
      
      result <- c(result, dat)
      
    } else {
      
      warning("Network problem - results may be incomplete")
      
    }
    
    query["clcontinue"] <- exec$continue$clcontinue
    
    if (is.null(query["clcontinue"])) {
      break
    }
  }

  result
  
}
leojoubert/WikiSocio documentation built on May 21, 2019, 5:08 a.m.