private function saveBadges()
 {
     wfProfileIn(__METHOD__);
     if (count($this->mNewBadges) > 0) {
         global $wgExternalSharedDB;
         global $wgEnableAchievementsStoreLocalData;
         if (empty($wgEnableAchievementsStoreLocalData)) {
             $dbw = wfGetDB(DB_MASTER, array(), $wgExternalSharedDB);
         } else {
             $dbw = wfGetDB(DB_MASTER);
         }
         // Doing replace instead of insert prevents dupes in case of slave lag or other errors
         foreach ($this->mNewBadges as $key => $val) {
             $this->mNewBadges[$key]['user_id'] = $this->mUser->getId();
             if (empty($wgEnableAchievementsStoreLocalData)) {
                 $this->mNewBadges[$key]['wiki_id'] = $this->mCityId;
             }
             $dbw->replace('ach_user_badges', null, $this->mNewBadges[$key], __METHOD__);
         }
         $dbw->commit();
         //notify the user only if he wants to be notified
         if (!$this->mUser->getOption('hidepersonalachievements')) {
             $_SESSION['achievementsNewBadges'] = true;
             // Hook to give backend stuff something to latch onto at award-time rather than notifcation-time.
             // NOTE: This has the limitation that it is only called for a max of one badge per page.
             // If the user earned multiple badges on the same page, the hook will only be run on the badge which getBadgeToNotify() determines is more important.
             global $wgWikiaForceAIAFdebug;
             Wikia::log(__METHOD__, "", "Saving a new badge. About to run hook if badge can be re-loaded.", $wgWikiaForceAIAFdebug);
             $achNotificationService = new AchNotificationService();
             $badge = $achNotificationService->getBadgeToNotify($this->mUser->getId(), false);
             if ($badge !== null) {
                 wfRunHooks('AchievementEarned', array($this->mUser, $badge));
             }
         }
         //touch user when badges are given
         $this->mUser->invalidateCache();
         //purge the user page to update counters/ranking/badges/score, FB#2872
         $this->mUser->getUserPage()->purgeSquid();
         //run a hook to let other extensions know when Achievements-related cache should be purged
         wfRunHooks('AchievementsInvalidateCache', array($this->mUser));
     }
     wfProfileOut(__METHOD__);
 }