protected function clearMetadata()
 {
     // remove the entries from metadata table.
     $groupId = $this->page->getMessageGroupId();
     TranslateMetadata::set($groupId, 'prioritylangs', false);
     TranslateMetadata::set($groupId, 'priorityforce', false);
     TranslateMetadata::set($groupId, 'priorityreason', false);
     // remove the page from aggregate groups, if present in any of them.
     $groups = MessageGroups::getAllGroups();
     foreach ($groups as $group) {
         if ($group instanceof AggregateMessageGroup) {
             $subgroups = TranslateMetadata::get($group->getId(), 'subgroups');
             if ($subgroups !== false) {
                 $subgroups = explode(',', $subgroups);
                 $subgroups = array_flip($subgroups);
                 if (isset($subgroups[$groupId])) {
                     unset($subgroups[$groupId]);
                     $subgroups = array_flip($subgroups);
                     TranslateMetadata::set($group->getId(), 'subgroups', implode(',', $subgroups));
                 }
             }
         }
     }
 }
 protected function moveMetadata($oldGroupId, $newGroupId)
 {
     $prioritylangs = TranslateMetadata::get($oldGroupId, 'prioritylangs');
     $priorityforce = TranslateMetadata::get($oldGroupId, 'priorityforce');
     $priorityreason = TranslateMetadata::get($oldGroupId, 'priorityreason');
     TranslateMetadata::set($oldGroupId, 'prioritylangs', false);
     TranslateMetadata::set($oldGroupId, 'priorityforce', false);
     TranslateMetadata::set($oldGroupId, 'priorityreason', false);
     if ($prioritylangs) {
         TranslateMetadata::set($newGroupId, 'prioritylangs', $prioritylangs);
     }
     if ($priorityforce) {
         TranslateMetadata::set($newGroupId, 'priorityforce', $priorityforce);
     }
     if ($priorityreason !== false) {
         TranslateMetadata::set($newGroupId, 'priorityreason', $priorityreason);
     }
     // make the changes in aggregate groups metadata, if present in any of them.
     $groups = MessageGroups::getAllGroups();
     foreach ($groups as $group) {
         if ($group instanceof AggregateMessageGroup) {
             $subgroups = TranslateMetadata::get($group->getId(), 'subgroups');
             if ($subgroups !== false) {
                 $subgroups = explode(',', $subgroups);
                 $subgroups = array_flip($subgroups);
                 if (isset($subgroups[$oldGroupId])) {
                     $subgroups[$newGroupId] = $subgroups[$oldGroupId];
                     unset($subgroups[$oldGroupId]);
                     $subgroups = array_flip($subgroups);
                     TranslateMetadata::set($group->getId(), 'subgroups', implode(',', $subgroups));
                 }
             }
         }
     }
 }
 /**
  * @param WebRequest $request
  * @param TranslatablePage $page
  */
 protected function handlePriorityLanguages(WebRequest $request, TranslatablePage $page)
 {
     // new priority languages
     $npLangs = rtrim(trim($request->getVal('prioritylangs')), ',');
     $npForce = $request->getCheck('forcelimit') ? 'on' : 'off';
     $npReason = trim($request->getText('priorityreason'));
     // Normalize
     $npLangs = array_map('trim', explode(',', $npLangs));
     $npLangs = array_unique($npLangs);
     // Remove invalid language codes.
     $languages = Language::fetchLanguageNames();
     foreach ($npLangs as $index => $language) {
         if (!array_key_exists($language, $languages)) {
             unset($npLangs[$index]);
         }
     }
     $npLangs = implode(',', $npLangs);
     if ($npLangs === '') {
         $npLangs = false;
         $npForce = false;
         $npReason = false;
     }
     $groupId = $page->getMessageGroupId();
     // old priority languages
     $opLangs = TranslateMetadata::get($groupId, 'prioritylangs');
     $opForce = TranslateMetadata::get($groupId, 'priorityforce');
     $opReason = TranslateMetadata::get($groupId, 'priorityreason');
     TranslateMetadata::set($groupId, 'prioritylangs', $npLangs);
     TranslateMetadata::set($groupId, 'priorityforce', $npForce);
     TranslateMetadata::set($groupId, 'priorityreason', $npReason);
     if ($opLangs !== $npLangs || $opForce !== $npForce || $opReason !== $npReason) {
         $params = array('languages' => $npLangs, 'force' => $npForce, 'reason' => $npReason);
         $entry = new ManualLogEntry('pagetranslation', 'prioritylanguages');
         $entry->setPerformer($this->getUser());
         $entry->setTarget($page->getTitle());
         $entry->setParameters($params);
         $entry->setComment($npReason);
         $logid = $entry->insert();
         $entry->publish($logid);
     }
 }
 public function execute()
 {
     if (!$this->getUser()->isAllowed(self::$right)) {
         $this->dieUsage('Permission denied', 'permissiondenied');
     }
     $params = $this->extractRequestParams();
     $action = $params['do'];
     $output = array();
     if ($action === 'associate' || $action === 'dissociate') {
         // Group is mandatory only for these two actions
         if (!isset($params['group'])) {
             $this->dieUsageMsg(array('missingparam', 'group'));
         }
         if (!isset($params['aggregategroup'])) {
             $this->dieUsageMsg(array('missingparam', 'aggregategroup'));
         }
         $aggregateGroup = $params['aggregategroup'];
         $subgroups = TranslateMetadata::getSubgroups($aggregateGroup);
         if (count($subgroups) === 0) {
             // For newly created groups the subgroups value might be empty,
             // but check that.
             if (TranslateMetadata::get($aggregateGroup, 'name') === false) {
                 $this->dieUsage('Invalid aggregate message group', 'invalidaggregategroup');
             }
             $subgroups = array();
         }
         $subgroupId = $params['group'];
         $group = MessageGroups::getGroup($subgroupId);
         // Add or remove from the list
         if ($action === 'associate') {
             if (!$group instanceof WikiPageMessageGroup) {
                 $this->dieUsage('Group does not exist or invalid', 'invalidgroup');
             }
             $subgroups[] = $subgroupId;
             $subgroups = array_unique($subgroups);
         } elseif ($action === 'dissociate') {
             // Allow removal of non-existing groups
             $subgroups = array_flip($subgroups);
             unset($subgroups[$subgroupId]);
             $subgroups = array_flip($subgroups);
         }
         TranslateMetadata::setSubgroups($aggregateGroup, $subgroups);
         $logParams = array('aggregategroup' => TranslateMetadata::get($aggregateGroup, 'name'), 'aggregategroup-id' => $aggregateGroup);
         /* Note that to allow removing no longer existing groups from
          * aggregate message groups, the message group object $group
          * might not always be available. In this case we need to fake
          * some title. */
         $title = $group ? $group->getTitle() : Title::newFromText("Special:Translate/{$subgroupId}");
         $entry = new ManualLogEntry('pagetranslation', $action);
         $entry->setPerformer($this->getUser());
         $entry->setTarget($title);
         // @todo
         // $entry->setComment( $comment );
         $entry->setParameters($logParams);
         $logid = $entry->insert();
         $entry->publish($logid);
     } elseif ($action === 'remove') {
         if (!isset($params['aggregategroup'])) {
             $this->dieUsageMsg(array('missingparam', 'aggregategroup'));
         }
         TranslateMetadata::deleteGroup($params['aggregategroup']);
         // @todo Logging
     } elseif ($action === 'add') {
         if (!isset($params['groupname'])) {
             $this->dieUsageMsg(array('missingparam', 'groupname'));
         }
         $name = trim($params['groupname']);
         if (strlen($name) === 0) {
             $this->dieUsage('Invalid aggregate message group name', 'invalidaggregategroupname');
         }
         if (!isset($params['groupdescription'])) {
             $this->dieUsageMsg(array('missingparam', 'groupdescription'));
         }
         $desc = trim($params['groupdescription']);
         $aggregateGroupId = self::generateAggregateGroupId($name);
         // Throw error if group already exists
         $nameExists = MessageGroups::labelExists($name);
         if ($nameExists) {
             $this->dieUsage('Message group already exists', 'duplicateaggregategroup');
         }
         // ID already exists- Generate a new ID by adding a number to it.
         $idExists = MessageGroups::getGroup($aggregateGroupId);
         if ($idExists) {
             $i = 1;
             while ($idExists) {
                 $tempId = $aggregateGroupId . "-" . $i;
                 $idExists = MessageGroups::getGroup($tempId);
                 $i++;
             }
             $aggregateGroupId = $tempId;
         }
         TranslateMetadata::set($aggregateGroupId, 'name', $name);
         TranslateMetadata::set($aggregateGroupId, 'description', $desc);
         TranslateMetadata::setSubgroups($aggregateGroupId, array());
         // Once new aggregate group added, we need to show all the pages that can be added to that.
         $output['groups'] = self::getAllPages();
         $output['aggregategroupId'] = $aggregateGroupId;
         // @todo Logging
     } elseif ($action === 'update') {
         if (!isset($params['groupname'])) {
             $this->dieUsageMsg(array('missingparam', 'groupname'));
         }
         $name = trim($params['groupname']);
         if (strlen($name) === 0) {
             $this->dieUsage('Invalid aggregate message group name', 'invalidaggregategroupname');
         }
         $desc = trim($params['groupdescription']);
         $aggregateGroupId = $params['aggregategroup'];
         $oldName = TranslateMetadata::get($aggregateGroupId, 'name');
         $oldDesc = TranslateMetadata::get($aggregateGroupId, 'description');
         // Error if the label exists already
         $exists = MessageGroups::labelExists($name);
         if ($exists && $oldName !== $name) {
             $this->dieUsage('Message group name already exists', 'duplicateaggregategroup');
         }
         if ($oldName === $name && $oldDesc === $desc) {
             $this->dieUsage('Invalid update', 'invalidupdate');
         }
         TranslateMetadata::set($aggregateGroupId, 'name', $name);
         TranslateMetadata::set($aggregateGroupId, 'description', $desc);
     }
     // If we got this far, nothing has failed
     $output['result'] = 'ok';
     $this->getResult()->addValue(null, $this->getModuleName(), $output);
     // Cache needs to be cleared after any changes to groups
     MessageGroups::singleton()->recache();
     MessageIndexRebuildJob::newJob()->insert();
 }