func addCategory(page string, category string, client *mwclient.Client) { // There's a small chance that saving a page may fail due to // an edit conflict or other transient error. Try up to 3 // times before giving up. var saveError error for i := 0; i < 3; i++ { text, timestamp, err := client.GetPageByName(page) if err != nil { panic(fmt.Sprintf("%v %v", page, err)) } // Add the category at the end of the text, since categories // are supposed to be at the end anyway. A previous version // tried to add after the last existing category, but that // can fail when the text contains comments. last := len(text) text = text[0:last] + "\n[[" + category + "]]" editcfg := map[string]string{ "action": "edit", "title": page, "text": text, "summary": "added [[" + category + "]]", "minor": "", "bot": "", "basetimestamp": timestamp, } saveError = client.Edit(editcfg) if saveError == nil { break } } if saveError != nil { panic(fmt.Sprintf("Failed to save %v %v", page, saveError)) } }
// Create a gallery showing all the files with warnings. Page must already // exist and will be replaced. func (warnings warnings) createGallery(gallery string, client *mwclient.Client) { var saveError error sort.Sort(warnings) for i := 0; i < 3; i++ { _, timestamp, err := client.GetPageByName(gallery) if err != nil { panic(fmt.Sprintf("%v %v", gallery, err)) } // Blank the page and create a fresh gallery var buffer bytes.Buffer buffer.WriteString("<gallery>\n") for w := range warnings { buffer.WriteString(warnings[w].title) buffer.WriteByte('|') if len(warnings[w].warning) > 200 { buffer.WriteString(warnings[w].warning[0:200]) buffer.WriteString("...") } else { buffer.WriteString(warnings[w].warning) } buffer.WriteByte('\n') } buffer.WriteString("</gallery>") editcfg := map[string]string{ "action": "edit", "title": gallery, "text": buffer.String(), "bot": "", "basetimestamp": timestamp, } saveError = client.Edit(editcfg) if saveError == nil { break } } if saveError != nil { panic(fmt.Sprintf("Failed to save %v %v", gallery, saveError)) } }