Ejemplo n.º 1
0
// Given an array of page titles, return a mapping from page title to the array
// of categories which the page is a member of.
// If the page doesn't exist, no entry is added to the map.
// If the page has no categories, it will map to nil.
func getPageCategories(pages []string, client *mwclient.Client) map[string][]string {
	params := params.Values{
		"action":   "query",
		"titles":   mwlib.MakeTitleString(pages),
		"prop":     "categories",
		"cllimit":  "max",
		"continue": "",
	}
	json, err := client.Post(params) // Get may fail on long queries.
	if err != nil {
		fmt.Println(params)
		panic(err)
	}
	pageData, err := json.GetObject("query", "pages")
	if err != nil {
		panic(err)
	}
	pageMap := pageData.Map()
	result := make(map[string][]string)
	for pageId, page := range pageMap {
		pageObj, err := page.Object()
		if err != nil {
			panic(err)
		}
		title, err := pageObj.GetString("title")
		if err != nil {
			panic(err)
		}
		if pageId[0] == '-' {
			fmt.Println(title)
			fmt.Println("File does not exist, possibly deleted.")
			continue
		}
		categories, err := pageObj.GetObjectArray("categories")
		if err != nil {
			// Presumably the page has no categories.
			result[title] = nil
			continue
		}
		catArray := make([]string, len(categories))
		for i := range categories {
			catArray[i], err = categories[i].GetString("title")
			if err != nil {
				panic(err)
			}
		}
		result[title] = catArray
	}
	return result
}
Ejemplo n.º 2
0
// Examine the specified categories in the Wiki. For each one that actually
// exists, return the number of files that it contains. The result arrays
// give category names (in arbitrary order) and the corresponding count,
// and will have fewer entries than the input array if some categories
// were duplicated or didn't exist.
func catNumFiles(categories []string, client *mwclient.Client) ([]string, []int32) {
	params := params.Values{
		"action": "query",
		"titles": mwlib.MakeTitleString(categories),
		"prop":   "categoryinfo",
	}
	json, err := client.Post(params) // Get may fail on long queries.
	if err != nil {
		panic(err)
	}
	pages, err := json.GetObject("query", "pages")
	if err != nil {
		panic(err)
	}
	pageMap := pages.Map()
	var resultCats = make([]string, len(pageMap))
	var resultCounts = make([]int32, len(pageMap))
	var idx int32 = 0
	for pageId, page := range pageMap {
		pageObj, err := page.Object()
		if err != nil {
			panic(err)
		}
		if pageId[0] != '-' {
			resultCats[idx], err = pageObj.GetString("title")
			if err != nil {
				panic(err)
			}
			info, err := pageObj.GetObject("categoryinfo")
			// An error here means that the category is probably
			// empty, so just leave count at 0.
			if err == nil {
				files, err := info.GetInt64("files")
				if err != nil {
					panic(err)
				}
				resultCounts[idx] = int32(files)
			}
			idx++
		}
	}
	resultCats = resultCats[0:idx]
	resultCounts = resultCounts[0:idx]
	return resultCats, resultCounts
}