public function saveSettings($settings, $cityId = null)
 {
     global $wgCityId, $wgUser;
     $cityId = empty($cityId) ? $wgCityId : $cityId;
     // Verify wordmark length ( CONN-116 )
     if (!empty($settings['wordmark-text'])) {
         $settings['wordmark-text'] = trim($settings['wordmark-text']);
     }
     if (empty($settings['wordmark-text'])) {
         // Do not save wordmark if its empty.
         unset($settings['wordmark-text']);
     } else {
         if (mb_strlen($settings['wordmark-text']) > 50) {
             $settings['wordmark-text'] = mb_substr($settings['wordmark-text'], 0, 50);
         }
     }
     if (isset($settings['favicon-image-name']) && strpos($settings['favicon-image-name'], 'Temp_file_') === 0) {
         $temp_file = new LocalFile(Title::newFromText($settings['favicon-image-name'], 6), RepoGroup::singleton()->getLocalRepo());
         $file = new LocalFile(Title::newFromText(self::FaviconImageName, 6), RepoGroup::singleton()->getLocalRepo());
         $file->upload($temp_file->getPath(), '', '');
         $temp_file->delete('');
         Wikia::invalidateFavicon();
         $settings['favicon-image-url'] = $file->getURL();
         $settings['favicon-image-name'] = $file->getName();
         $file->repo->forceMaster();
         $history = $file->getHistory(1);
         if (count($history) == 1) {
             $oldFaviconFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName());
         }
     }
     if (isset($settings['wordmark-image-name']) && strpos($settings['wordmark-image-name'], 'Temp_file_') === 0) {
         $temp_file = new LocalFile(Title::newFromText($settings['wordmark-image-name'], 6), RepoGroup::singleton()->getLocalRepo());
         $file = new LocalFile(Title::newFromText(self::WordmarkImageName, 6), RepoGroup::singleton()->getLocalRepo());
         $file->upload($temp_file->getPath(), '', '');
         $temp_file->delete('');
         $settings['wordmark-image-url'] = $file->getURL();
         $settings['wordmark-image-name'] = $file->getName();
         $file->repo->forceMaster();
         $history = $file->getHistory(1);
         if (count($history) == 1) {
             $oldFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName());
         }
     }
     if (isset($settings['background-image-name']) && strpos($settings['background-image-name'], 'Temp_file_') === 0) {
         $temp_file = new LocalFile(Title::newFromText($settings['background-image-name'], 6), RepoGroup::singleton()->getLocalRepo());
         $file = new LocalFile(Title::newFromText(self::BackgroundImageName, 6), RepoGroup::singleton()->getLocalRepo());
         $file->upload($temp_file->getPath(), '', '');
         $temp_file->delete('');
         $settings['background-image'] = $file->getURL();
         $settings['background-image-name'] = $file->getName();
         $settings['background-image-width'] = $file->getWidth();
         $settings['background-image-height'] = $file->getHeight();
         $imageServing = new ImageServing(null, 120, array("w" => "120", "h" => "65"));
         $settings['user-background-image'] = $file->getURL();
         $settings['user-background-image-thumb'] = wfReplaceImageServer($file->getThumbUrl($imageServing->getCut($file->getWidth(), $file->getHeight(), "origin") . "-" . $file->getName()));
         $file->repo->forceMaster();
         $history = $file->getHistory(1);
         if (count($history) == 1) {
             $oldBackgroundFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName());
         }
     }
     $reason = wfMsg('themedesigner-reason', $wgUser->getName());
     // update history
     if (!empty($GLOBALS[self::WikiFactoryHistory])) {
         $history = $GLOBALS[self::WikiFactoryHistory];
         $lastItem = end($history);
         $revisionId = intval($lastItem['revision']) + 1;
     } else {
         $history = array();
         $revisionId = 1;
     }
     // #140758 - Jakub
     // validation
     // default color values
     foreach (ThemeDesignerHelper::getColorVars() as $sColorVar => $sDefaultValue) {
         if (!isset($settings[$sColorVar]) || !ThemeDesignerHelper::isValidColor($settings[$sColorVar])) {
             $settings[$sColorVar] = $sDefaultValue;
         }
     }
     // update WF variable with current theme settings
     WikiFactory::setVarByName(self::WikiFactorySettings, $cityId, $settings, $reason);
     // add entry
     $history[] = array('settings' => $settings, 'author' => $wgUser->getName(), 'timestamp' => wfTimestampNow(), 'revision' => $revisionId);
     // limit history size to last 10 changes
     $history = array_slice($history, -self::HistoryItemsLimit);
     if (count($history) > 1) {
         for ($i = 0; $i < count($history) - 1; $i++) {
             if (isset($oldFaviconFile) && isset($history[$i]['settings']['favicon-image-name'])) {
                 if ($history[$i]['settings']['favicon-image-name'] == self::FaviconImageName) {
                     $history[$i]['settings']['favicon-image-name'] = $oldFaviconFile['name'];
                     $history[$i]['settings']['favicon-image-url'] = $oldFaviconFile['url'];
                 }
             }
             if (isset($oldFile) && isset($history[$i]['settings']['wordmark-image-name'])) {
                 if ($history[$i]['settings']['wordmark-image-name'] == self::WordmarkImageName) {
                     $history[$i]['settings']['wordmark-image-name'] = $oldFile['name'];
                     $history[$i]['settings']['wordmark-image-url'] = $oldFile['url'];
                 }
             }
             if (isset($oldBackgroundFile) && isset($history[$i]['settings']['background-image-name'])) {
                 if ($history[$i]['settings']['background-image-name'] == self::BackgroundImageName) {
                     $history[$i]['settings']['background-image-name'] = $oldBackgroundFile['name'];
                 }
             }
         }
     }
     WikiFactory::setVarByName(self::WikiFactoryHistory, $cityId, $history, $reason);
 }