function axWFactoryDomainCRUD($type = "add")
{
    global $wgRequest, $wgUser, $wgExternalSharedDB, $wgOut;
    $sDomain = $wgRequest->getVal("domain");
    $city_id = $wgRequest->getVal("cityid");
    $reason = $wgRequest->getVal("reason");
    if (!$wgUser->isAllowed('wikifactory')) {
        $wgOut->readOnlyPage();
        #--- later change to something reasonable
        return;
    }
    if (empty($city_id)) {
        $wgOut->readOnlyPage();
        #--- later change to something reasonable
        return;
    }
    $dbw = wfGetDB(DB_MASTER, array(), $wgExternalSharedDB);
    $aDomains = array();
    $aResponse = array();
    $sInfo = "";
    switch ($type) {
        case "add":
            if (!preg_match("/^[\\w\\.\\-]+\$/", $sDomain)) {
                /**
                 * check if domain is valid (a im sure that there is function
                 * somewhere for such purpose
                 */
                $sInfo .= "Error: Domain <em>{$sDomain}</em> is invalid (or empty) so it's not added.";
            } else {
                $added = WikiFactory::addDomain($city_id, $sDomain, $reason);
                if ($added) {
                    $sInfo .= "Success: Domain <em>{$sDomain}</em> added.";
                } else {
                    $sInfo .= "Error: Domain <em>{$sDomain}</em> is already used so it's not added.";
                }
            }
            break;
        case "change":
            $sNewDomain = $wgRequest->getVal("newdomain");
            #--- first, check if domain is not used
            $oRes = $dbw->select("city_domains", "count(*) as count", array("city_domain" => $sNewDomain), __METHOD__);
            $oRow = $dbw->fetchObject($oRes);
            $dbw->freeResult($oRes);
            if ($oRow->count > 0) {
                #--- domain is used already
                $sInfo .= "<strong>Error: Domain <em>{$sNewDomain}</em> is already used so no change was done.</strong>";
            } elseif (!preg_match("/^[\\w\\.\\-]+\$/", $sNewDomain)) {
                #--- check if domain is valid (a im sure that there is function
                #--- somewhere for such purpose
                $sInfo .= "<strong>Error: Domain <em>{$sNewDomain}</em> is invalid so no change was done..</strong>";
            } else {
                #--- reall change domain
                $dbw->update("city_domains", array("city_domain" => strtolower($sNewDomain)), array("city_id" => $city_id, "city_domain" => strtolower($sDomain)));
                $sLogMessage = "Domain <em>{$sDomain}</em> changed to <em>{$sNewDomain}</em>.";
                if (!empty($reason)) {
                    $sLogMessage .= " (reason: {$reason})";
                }
                WikiFactory::log(WikiFactory::LOG_DOMAIN, $sLogMessage, $city_id);
                $dbw->commit();
                $sInfo .= "Success: Domain <em>{$sDomain}</em> changed to <em>{$sNewDomain}</em>.";
            }
            break;
        case "remove":
            $removed = WikiFactory::removeDomain($city_id, $sDomain, $reason);
            if ($removed) {
                $sInfo .= "Success: Domain <em>{$sDomain}</em> removed.";
            } else {
                $sInfo .= "Failed: Domain <em>{$sDomain}</em> was not removed.";
            }
            break;
        case "status":
            $iNewStatus = $wgRequest->getVal("status");
            if (in_array($iNewStatus, array(0, 1, 2))) {
                #--- updatec city_list table
                $dbw->update("city_list", array("city_public" => $iNewStatus), array("city_id" => $city_id));
                $dbw->commit();
                switch ($iNewStatus) {
                    case 0:
                        $aResponse["div-body"] = "<strong>changed to disabled</strong>";
                        break;
                    case 1:
                        $aResponse["div-body"] = "<strong>changed to enabled</strong>";
                        break;
                    case 2:
                        $aResponse["div-body"] = "<strong>changed to redirected</strong>";
                        break;
                }
            } else {
                $aResponse["div-body"] = "wrong status number";
            }
            $aResponse["div-name"] = "wf-domain-span";
            break;
        case "cancel":
            $sInfo .= "<em>Action cancelled</em>";
            break;
        case "setmain":
            try {
                $setmain = WikiFactory::setmainDomain($city_id, $sDomain, $reason);
                if ($setmain) {
                    $sInfo .= "Success: Domain <em>{$sDomain}</em> set as main.";
                } else {
                    $sInfo .= "Failed: Domain <em>{$sDomain}</em> was not set as main.";
                }
            } catch (WikiFactoryDuplicateWgServer $error) {
                $sInfo .= "Failed: " . $error->getMessage();
            }
            break;
    }
    #--- get actuall domain list
    $aDomains = WikiFactory::getDomains($city_id, true);
    #--- send response, return domain array
    $aResponse["domains"] = $aDomains;
    $aResponse["info"] = $sInfo;
    return json_encode($aResponse);
}
Exemple #2
0
 /**
  * Given an array of category ids, set the wiki to be in those categories.
  * Also delete any categories that the wiki WAS in.
  * @param  [type] $city_id    [description]
  * @param  array  $categories [description]
  * @return [type]             [description]
  */
 public function updateCategories($city_id, array $categories, $reason)
 {
     global $wgExternalSharedDB;
     $values = array();
     // MySQL style multi-row insert
     foreach ($categories as $category) {
         $values[] = ["city_id" => $city_id, "cat_id" => $category];
     }
     $dbw = wfGetDB(DB_MASTER, array(), $wgExternalSharedDB);
     // Clear categories, add any new ones
     // Note: this allows a wiki to be in zero categories, which may affect other biz logic
     $dbw->begin();
     $dbw->delete("city_cat_mapping", array("city_id" => $city_id), __METHOD__);
     if (!empty($values)) {
         $dbw->insert("city_cat_mapping", $values, __METHOD__);
     }
     $dbw->commit();
     $this->clearCache($city_id);
     $aHookParams = ['city_id' => $city_id, 'categories' => $categories];
     wfRunHooks('CityCatMappingUpdated', array($aHookParams));
     # pretty clunky way to load all the categories just for the name, maybe refactor this?
     $this->loadCategories();
     $cat_names = array();
     foreach ($categories as $id) {
         $cat_names[] = $this->mAllCategories[$id]['name'];
     }
     $message = join(", ", $cat_names);
     if (!empty($reason)) {
         $reason = " ( {$reason} )";
     }
     WikiFactory::log(WikiFactory::LOG_CATEGORY, "Categories changed to {$message}. {$reason}", $city_id);
 }
 /**
  * setCategory
  *
  * remove previous value in database and insert new one
  *
  * @param integer   $city_id    identifier from city_list
  * @param integer   $cat_id     category identifier
  * @param string    $reason     optional extra reason string for logging
  */
 public function setCategory($city_id, $cat_id, $reason = '')
 {
     global $wgExternalSharedDB;
     wfProfileIn(__METHOD__);
     $dbw = wfGetDB(DB_MASTER, array(), $wgExternalSharedDB);
     $dbw->begin();
     $dbw->delete("city_cat_mapping", array("city_id" => $city_id), __METHOD__);
     $dbw->insert("city_cat_mapping", array("city_id" => $city_id, "cat_id" => $cat_id), __METHOD__);
     $categories = $this->getCategories();
     if (!empty($reason)) {
         $reason = " (" . (string) $reason . ")";
     }
     WikiFactory::log(WikiFactory::LOG_CATEGORY, "Category changed to {$categories[$cat_id]['name']}" . $reason, $city_id);
     $dbw->commit();
     wfProfileOut(__METHOD__);
 }
 /**
  * adopt a wiki - set admin rights for passed user and remove bureacrat rights for current users
  *
  * @return boolean success/fail
  * @author Maciej Błaszkowski <marooned at wikia-inc.com>
  */
 static function adoptWiki($wikiId, $user)
 {
     wfProfileIn(__METHOD__);
     global $wgMemc;
     $dbr = wfGetDB(DB_SLAVE);
     //get all current admins of this wiki
     $res = $dbr->select('user_groups', 'ug_user', array('ug_group' => 'sysop'), __METHOD__);
     //group to remove
     $removedGroup = 'bureaucrat';
     //groups to add
     $addGroups = array('sysop', 'bureaucrat');
     $wiki_name = WikiFactory::IDtoDB($wikiId);
     //remove bureacrat for current admins
     while ($row = $dbr->fetchObject($res)) {
         $admin = User::newFromId($row->ug_user);
         if ($admin) {
             //get old groups - for log purpose
             $oldGroups = $admin->getGroups();
             //do not remove groups for staff or for user who is now adopting (he might be a bureaucrat)
             if (in_array('staff', $oldGroups) || $admin->getId() == $user->getId()) {
                 continue;
             }
             //create new groups list - for log purpose
             $newGroups = array_diff($oldGroups, array($removedGroup));
             if ($oldGroups != $newGroups) {
                 //remove group
                 $admin->removeGroup($removedGroup);
                 wfRunHooks('UserRights', array(&$admin, array(), array($removedGroup)));
                 // get email params
                 $magicwords = array('#WIKINAME' => $wiki_name);
                 $admin_name = $admin->getName();
                 $globalTitleUserRights = GlobalTitle::newFromText('UserRights', -1, $wikiId);
                 $specialUserRightsUrl = $globalTitleUserRights->getFullURL();
                 $globalTitlePreferences = GlobalTitle::newFromText('Preferences', -1, $wikiId);
                 $specialPreferencesUrl = $globalTitlePreferences->getFullURL();
                 //sent e-mail
                 $admin->sendMail(strtr(wfMsgForContent("wikiadoption-mail-adoption-subject"), $magicwords), strtr(wfMsgForContent("wikiadoption-mail-adoption-content", $admin_name, $specialUserRightsUrl, $specialPreferencesUrl), $magicwords), null, null, 'AutomaticWikiAdoption', strtr(wfMsgForContent("wikiadoption-mail-adoption-content-HTML", $admin_name, $specialUserRightsUrl, $specialPreferencesUrl), $magicwords));
                 //log
                 self::addLogEntry($admin, $oldGroups, $newGroups);
                 //Unset preference for receiving future adoption emails
                 $admin->setLocalPreference("adoptionmails", $wikiId, 0);
                 $admin->saveSettings();
             }
         }
     }
     //get old groups - for log purpose
     $oldGroups = $user->getGroups();
     //create new groups list - for log purpose
     $newGroups = array_unique(array_merge($oldGroups, $addGroups));
     if ($oldGroups != $newGroups) {
         //add groups to user who just adopted this wiki
         foreach ($addGroups as $addGroup) {
             $user->addGroup($addGroup);
         }
         wfRunHooks('UserRights', array(&$user, $addGroups, array()));
         //log
         self::addLogEntry($user, $oldGroups, $newGroups);
         WikiFactory::log(WikiFactory::LOG_STATUS, $user->getName() . " adopted wiki " . $wiki_name);
     }
     //set date of adoption - this will be used to check when next adoption is possible
     $user->setGlobalAttribute('LastAdoptionDate', time());
     //Set preference for receiving future adoption emails
     $user->setLocalPreference("adoptionmails", 1, $wikiId);
     $user->saveSettings();
     // Block user from seeing the adoption page again or adopting another wiki
     $memcKey = wfMemcKey($user->getId(), 'AutomaticWikiAdoption-user-allowed-to-adopt');
     $allowed = self::REASON_ADOPTED_RECENTLY;
     $wgMemc->set($memcKey, $allowed, 3600);
     // Block the wiki from being adopted again for 14 days
     $wgMemc->set(wfMemcKey("AutomaticWikiAdoption-WikiAdopted"), $allowed, 60 * 60 * 24 * 14);
     //Reset the flags for this wiki
     self::dismissNotification();
     $flags = WikiFactory::FLAG_ADOPTABLE | WikiFactory::FLAG_ADOPT_MAIL_FIRST | WikiFactory::FLAG_ADOPT_MAIL_SECOND;
     WikiFactory::resetFlags($wikiId, $flags);
     wfProfileOut(__METHOD__);
     //TODO: log on central that wiki has been adopted (by who)
     //TODO: is there a way to check if user got sysop rights to return true/false as a result?
     return true;
 }