From 9ccf1699d5f58530a14621127ae5eccaf5cd0a1e Mon Sep 17 00:00:00 2001 From: Fulmir Date: Thu, 1 Jun 2023 01:41:50 -0400 Subject: [PATCH] Fixed for CSRF errors. Updated Gear export to fix errors in Engine and Heatsink defs. --- cmd/export.go | 4 ++-- export/chassisdef.go | 6 ++--- export/gear.go | 17 +++++++------ importer/mediawiki.go | 56 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 58 insertions(+), 25 deletions(-) diff --git a/cmd/export.go b/cmd/export.go index e731536..baceae8 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -51,8 +51,8 @@ var ExportCmd = &cobra.Command{ logrus.Infof("Skipping BLACKLISTED mech %s", mech.Chassis.Description.Name) continue } - - filename := fmt.Sprintf("MechDef_%s.wiki", variant) + + filename := fmt.Sprintf("MechDef_%s.wiki", strings.Replace(variant, "/", "-", -1)) logrus.Debugf("Writing mech wiki %s", filename) diff --git a/export/chassisdef.go b/export/chassisdef.go index 7d53f99..f6b397d 100644 --- a/export/chassisdef.go +++ b/export/chassisdef.go @@ -61,7 +61,7 @@ type ChassisDef struct { type ChassisLocation struct { Location string Hardpoints []struct { - WeaponMount string + WeaponMountID string Omni bool } Tonnage float64 @@ -86,9 +86,9 @@ func (cl ChassisLocation) ToWiki(chassisID string) string { omniHardpoints := []string{} for _, hardpoint := range cl.Hardpoints { if !hardpoint.Omni { - hardpoints = append(hardpoints, hardpoint.WeaponMount) + hardpoints = append(hardpoints, hardpoint.WeaponMountID) } else { - omniHardpoints = append(omniHardpoints, hardpoint.WeaponMount) + omniHardpoints = append(omniHardpoints, hardpoint.WeaponMountID) } } diff --git a/export/gear.go b/export/gear.go index cec0787..945e47b 100644 --- a/export/gear.go +++ b/export/gear.go @@ -29,17 +29,13 @@ type GearCustom struct { BonusDescriptions []string `json:",omitempty"` - EngineHeatBlock struct { - HeatSinkCount int `json:",omitempty"` - } `json:",omitempty"` + EngineHeatBlock int `json:",omitempty"` Cooling struct { HeatSinkDefId string `json:",omitempty"` } `json:",omitempty"` - EngineCore struct { - Rating int `json:",omitempty"` - } `json:",omitempty"` + EngineCore string `json:",omitempty"` ArmActuator struct { AccuracyBonus int `json:",omitempty"` @@ -111,7 +107,7 @@ type Weapon struct { ShotsWhenFired int ProjectilesPerShot int AttackRecoil int - Instability int + Instability float64 WeaponEffectID string } @@ -334,6 +330,9 @@ func (g Gear) ToWiki() string { return "" } c, ok := cat["CategoryID"] + if !ok { + return "" + } categoryString, ok := c.(string) if !ok { return "" @@ -385,9 +384,9 @@ func (g Gear) ToWiki() string { case CoolingWikiTemplate: wt.AddArg("HeatsinkDefID", g.Custom.Cooling.HeatSinkDefId) case EngineHeatBlockWikiTemplate: - wt.AddArg("HeatsinkCount", g.Custom.EngineHeatBlock.HeatSinkCount) + wt.AddArg("HeatsinkCount", g.Custom.EngineHeatBlock) case EngineCoreWikiTemplate: - wt.AddArg("Rating", g.Custom.EngineCore.Rating) + wt.AddArg("Rating", g.Custom.EngineCore) case EngineShieldWikiTemplate: wt.AddArg("ReservedSlots", g.Custom.Weights.ReservedSlots) wt.AddArg("EngineFactor", g.Custom.Weights.EngineFactor) diff --git a/importer/mediawiki.go b/importer/mediawiki.go index 536804d..9beadff 100644 --- a/importer/mediawiki.go +++ b/importer/mediawiki.go @@ -14,7 +14,7 @@ import ( ) // BATCH_SIZE is the number of wiki pages to retrieve at one time. -const BATCH_SIZE = 20 +const BATCH_SIZE = 10 func Import(wikidata string, dryrun bool, username, password, url string) error { w, err := mwclient.New(url, "") @@ -26,6 +26,10 @@ func Import(wikidata string, dryrun bool, username, password, url string) error logrus.Info("doing dry run, will not make alterations") } + w.Maxlag.On = true + w.Maxlag.Retries = 3 + w.Maxlag.Timeout = "30" + err = w.Login(username, password) if err != nil { logrus.Warnf("error logging in: %s", err) @@ -106,24 +110,54 @@ func Import(wikidata string, dryrun bool, username, password, url string) error continue } - if strings.TrimSpace(fileContent) == strings.TrimSpace(content) { - logrus.Debugf("UNCHANGED %s", pageName) - unchanged[pageTitle] = struct{}{} - } else { - if content != "" { - logrus.Infof("UPDATE %s", pageName) - updates[pageTitle] = struct{}{} - } - if !dryrun { + // if strings.TrimSpace(fileContent) == strings.TrimSpace(content) { + // logrus.Debugf("UNCHANGED %s", pageName) + // unchanged[pageTitle] = struct{}{} + // } else { + if content != "" { + logrus.Infof("UPDATING %s", pageName) + updates[pageTitle] = struct{}{} + } + if !dryrun { + errCheck := true + for i := 0; i < 5 && errCheck; i++ { if err := w.Edit(map[string]string{ "title": pageName, "text": fileContent, "summary": "automated page update", }); err != nil { - logrus.Errorf("Error writing page %s to wiki: %s\n", pageName, err) + logrus.Debugf("Error writing page %s to wiki: %s\n", pageName, err) + + errCode := strings.Split(err.Error(), ":")[0] + + if errCode == "badtoken" { + logrus.Warnf("CSRF Token auth failed while writing page %s to wiki, retrying after attempt %s: %s\n", pageName, strconv.Itoa(i+1), err) + errCheck = true + delete(w.Tokens, "csrf") + w.Logout() + w.Login(username, password) + } else if strings.Contains(errCode, "invalid") || strings.Contains(errCode, "denied") { + errCheck = false + logrus.Errorf("Error writing page %s to wiki, retrying after attempt %s: %s\n", pageName, strconv.Itoa(i+1), err) + } else if strings.Contains(errCode, "edit successful") { + errCheck = false + logrus.Warnf("Edit successful but contained no changes to page %s to wiki: %s\n", pageName, err) + } else if strings.Contains(errCode, "error occured during HTTP request") { + errCheck = true + logrus.Errorf("Error writing page %s to wiki, retrying after attempt %s: %s\n", pageName, strconv.Itoa(i+1), err) + w.Maxlag.Timeout = "90" + } else { + errCheck = true + logrus.Errorf("Error writing page %s to wiki, retrying after attempt %s: %s\n", pageName, strconv.Itoa(i+1), err) + } + } else { + w.Maxlag.Timeout = "30" + errCheck = false + logrus.Infof("SUCCESSFULLY UPDATED %s", pageName) } } } + //} } }