/** * clearCache * * clear memcached key for city * * @author eloy@wikia * @access public * @static * * @param integer $city_id: wiki id in city_list * * @return boolean status */ public static function clearCache($city_id) { global $wgMemc, $wgWikicitiesReadOnly; if (!self::isUsed()) { Wikia::log(__METHOD__, "", "WikiFactory is not used."); return false; } if (!is_numeric($city_id)) { return false; } wfProfileIn(__METHOD__); if ($wgWikicitiesReadOnly) { Wikia::log(__METHOD__, "", "wgWikicitiesReadOnly mode. Skipping update."); } else { /** * increase number in city_list */ $dbw = self::db(DB_MASTER); $dbw->update("city_list", ["city_factory_timestamp" => wfTimestampNow()], ["city_id" => $city_id], __METHOD__); } /** * clear tags cache */ $tags = new WikiFactoryTags($city_id); $tags->clearCache(); /** * clear domains cache */ self::clearDomainCache($city_id); /** * clear variables cache */ $wgMemc->delete("WikiFactory::getCategory:" . $city_id); //ugly cat clearing (fb#9937) $wgMemc->delete(self::getVarsKey($city_id)); $city_dbname = self::IDtoDB($city_id); $wgMemc->delete(self::getWikiaCacheKey($city_id)); if (!empty($city_dbname)) { $wgMemc->delete(self::getWikiaDBCacheKey($city_dbname)); } wfProfileOut(__METHOD__); return true; }
/** * main entry point, create wiki with given parameters * * @throw CreateWikiException an exception with status of operation set */ public function create() { global $wgExternalSharedDB, $wgSharedDB, $wgUser; $then = microtime(true); // Set this flag to ensure that all select operations go against master // Slave lag can cause random errors during wiki creation process global $wgForceMasterDatabase; $wgForceMasterDatabase = true; wfProfileIn(__METHOD__); if (wfReadOnly()) { wfProfileOut(__METHOD__); throw new CreateWikiException('DB is read only', self::ERROR_READONLY); } // check founder if ($this->mFounder->isAnon()) { wfProfileOut(__METHOD__); throw new CreateWikiException('Founder is anon', self::ERROR_USER_IN_ANON); } // check executables $status = $this->checkExecutables(); if ($status != 0) { wfProfileOut(__METHOD__); throw new CreateWikiException('checkExecutables() failed', $status); } // check domains $status = $this->checkDomain(); if ($status != 0) { wfProfileOut(__METHOD__); throw new CreateWikiException('Check domain failed', $status); } // prepare all values needed for creating wiki $this->prepareValues(); // prevent domain to be registered more than once if (!AutoCreateWiki::lockDomain($this->mDomain)) { wfProfileOut(__METHOD__); throw new CreateWikiException('Domain name taken', self::ERROR_DOMAIN_NAME_TAKEN); } // start counting time $this->mCurrTime = wfTime(); // check and create database $this->mDBw = wfGetDB(DB_MASTER, array(), $wgExternalSharedDB); # central /// // local database handled is handler to cluster we create new wiki. // It doesn't have to be the same like wikifactory cluster or db cluster // where Special:CreateWiki exists. // // @todo do not use hardcoded name, code below is only for test // // set $activeCluster to false if you want to create wikis on first // cluster // $this->mClusterDB = self::ACTIVE_CLUSTER ? "wikicities_" . self::ACTIVE_CLUSTER : "wikicities"; $this->mNewWiki->dbw = wfGetDB(DB_MASTER, array(), $this->mClusterDB); // database handler, old $dbwTarget // check if database is creatable // @todo move all database creation checkers to canCreateDatabase if (!$this->canCreateDatabase()) { wfProfileOut(__METHOD__); throw new CreateWikiException('DB exists - ' . $this->mNewWiki->dbname, self::ERROR_DATABASE_ALREADY_EXISTS); } else { $this->mNewWiki->dbw->query(sprintf("CREATE DATABASE `%s`", $this->mNewWiki->dbname)); wfDebugLog("createwiki", "Database {$this->mNewWiki->dbname} created\n", true); } /** * create position in wiki.factory * (I like sprintf construction, so sue me) */ if (!$this->addToCityList()) { wfDebugLog("createwiki", __METHOD__ . ": Cannot set data in city_list table\n", true); wfProfileOut(__METHOD__); throw new CreateWikiException('Cannot add wiki to city_list', self::ERROR_DATABASE_WRITE_TO_CITY_LIST_BROKEN); } // set new city_id $this->mNewWiki->city_id = $this->mDBw->insertId(); if (empty($this->mNewWiki->city_id)) { wfProfileOut(__METHOD__); throw new CreateWikiException('Cannot set data in city_list table. city_id is empty after insert', self::ERROR_DATABASE_WIKI_FACTORY_TABLES_BROKEN); } wfDebugLog("createwiki", __METHOD__ . ": Row added added into city_list table, city_id = {$this->mNewWiki->city_id}\n", true); /** * add domain and www.domain to the city_domains table */ if (!$this->addToCityDomains()) { wfProfileOut(__METHOD__); throw new CreateWikiException('Cannot set data in city_domains table', self::ERROR_DATABASE_WRITE_TO_CITY_DOMAINS_BROKEN); } wfDebugLog("createwiki", __METHOD__ . ": Row added into city_domains table, city_id = {$this->mNewWiki->city_id}\n", true); /** * create image folder */ global $wgEnableSwiftFileBackend; if (empty($wgEnableSwiftFileBackend)) { wfMkdirParents("{$this->mNewWiki->images_dir}"); wfDebugLog("createwiki", __METHOD__ . ": Folder {$this->mNewWiki->images_dir} created\n", true); } // Force initialize uploader user from correct shared db $uploader = User::newFromName('CreateWiki script'); $uploader->getId(); $oldUser = $wgUser; $wgUser = $uploader; /** * wikifactory variables */ wfDebugLog("createwiki", __METHOD__ . ": Populating city_variables\n", true); $this->setWFVariables(); $tmpSharedDB = $wgSharedDB; $wgSharedDB = $this->mNewWiki->dbname; $this->mDBw->commit(__METHOD__); // commit shared DB changes /** * we got empty database created, now we have to create tables and * populate it with some default values */ wfDebugLog("createwiki", __METHOD__ . ": Creating tables in database\n", true); $this->mNewWiki->dbw = wfGetDB(DB_MASTER, array(), $this->mNewWiki->dbname); if (!$this->createTables()) { wfProfileOut(__METHOD__); throw new CreateWikiException('Creating tables not finished', self::ERROR_SQL_FILE_BROKEN); } /** * import language starter */ if (!$this->importStarter()) { wfProfileOut(__METHOD__); throw new CreateWikiException('Starter import failed', self::ERROR_SQL_FILE_BROKEN); } /** * making the wiki founder a sysop/bureaucrat */ wfDebugLog("createwiki", __METHOD__ . ": Create user sysop/bureaucrat for user: {$this->mNewWiki->founderId} \n", true); if (!$this->addUserToGroups()) { wfDebugLog("createwiki", __METHOD__ . ": Create user sysop/bureaucrat for user: {$this->mNewWiki->founderId} failed \n", true); } /** * init site_stats table (add empty row) */ $this->mNewWiki->dbw->insert("site_stats", array("ss_row_id" => "1"), __METHOD__); /** * copy default logo */ $res = ImagesService::uploadImageFromUrl(self::CREATEWIKI_LOGO, (object) ['name' => 'Wiki.png'], $uploader); if ($res['status'] === true) { wfDebugLog("createwiki", __METHOD__ . ": Default logo has been uploaded\n", true); } else { wfDebugLog("createwiki", __METHOD__ . ": Default logo has not been uploaded - " . print_r($res['errors'], true) . "\n", true); } /** * destroy connection to newly created database */ $this->waitForSlaves(__METHOD__); $wgSharedDB = $tmpSharedDB; $oHub = WikiFactoryHub::getInstance(); $oHub->setVertical($this->mNewWiki->city_id, $this->mNewWiki->vertical, "CW Setup"); wfDebugLog("createwiki", __METHOD__ . ": Wiki added to the vertical: {$this->mNewWiki->vertical} \n", true); for ($i = 0; $i < count($this->mNewWiki->categories); $i++) { $oHub->addCategory($this->mNewWiki->city_id, $this->mNewWiki->categories[$i]); wfDebugLog("createwiki", __METHOD__ . ": Wiki added to the category: {$this->mNewWiki->categories[$i]} \n", true); } /** * define wiki type */ $wiki_type = 'default'; /** * modify variables */ global $wgUniversalCreationVariables; if (!empty($wgUniversalCreationVariables) && !empty($wiki_type) && isset($wgUniversalCreationVariables[$wiki_type])) { $this->addCustomSettings(0, $wgUniversalCreationVariables[$wiki_type], "universal"); wfDebugLog("createwiki", __METHOD__ . ": Custom settings added for wiki_type: {$wiki_type} \n", true); } /** * set variables per language */ global $wgLangCreationVariables; $langCreationVar = isset($wgLangCreationVariables[$wiki_type]) ? $wgLangCreationVariables[$wiki_type] : $wgLangCreationVariables; $this->addCustomSettings($this->mNewWiki->language, $langCreationVar, "language"); wfDebugLog("createwiki", __METHOD__ . ": Custom settings added for wiki_type: {$wiki_type} and language: {$this->mNewWiki->language} \n", true); /** * set tags per language and per hub * @FIXME the switch is !@#$ creazy, but I didn't find a core function */ $tags = new WikiFactoryTags($this->mNewWiki->city_id); $langTag = $this->mNewWiki->language; if ($langTag !== 'en') { switch ($langTag) { case 'pt-br': $langTag = 'pt'; break; case 'zh-tw': case 'zh-hk': case 'zh-clas': case 'zh-class': case 'zh-classical': case 'zh-cn': case 'zh-hans': case 'zh-hant': case 'zh-min-': case 'zh-min-n': case 'zh-mo': case 'zh-sg': case 'zh-yue': $langTag = 'zh'; break; } $tags->addTagsByName($langTag); } /** * move main page -> this code exists in CreateWikiLocalJob - so it is not needed anymore */ /** * Unset database from mNewWiki, because database objects cannot be serialized from MW1.19 */ unset($this->mNewWiki->dbw); // Restore wgUser $wgUser = $oldUser; unset($oldUser); /** * Schedule an async task */ $creationTask = new \Wikia\Tasks\Tasks\CreateNewWikiTask(); $job_params = new stdClass(); foreach ($this->mNewWiki as $id => $value) { if (!is_object($value)) { $job_params->{$id} = $value; } } // BugId:15644 - I need to pass this to CreateWikiLocalJob::changeStarterContributions $job_params->sDbStarter = $this->sDbStarter; $task_id = (new \Wikia\Tasks\AsyncTaskList())->wikiId($this->mNewWiki->city_id)->prioritize()->add($creationTask->call('postCreationSetup', $job_params))->add($creationTask->call('maintenance', rtrim($this->mNewWiki->url, "/")))->queue(); wfDebugLog("createwiki", __METHOD__ . ": Local maintenance task added as {$task_id}\n", true); $this->info(__METHOD__ . ': done', ['task_id' => $task_id, 'took' => microtime(true) - $then]); wfProfileOut(__METHOD__); }
static function getHubIdFromTitle($title) { global $wgHubsPages, $wgContLanguageCode; if (empty($wgHubsPages[$wgContLanguageCode])) { return false; } if (self::isHubsPage($title)) { if (is_array($wgHubsPages[$wgContLanguageCode][strtolower($title->getUserCaseDBKey())])) { return WikiFactoryTags::idFromName($wgHubsPages[$wgContLanguageCode][strtolower($title->getUserCaseDBKey())]['name']); } return WikiFactoryTags::idFromName($wgHubsPages[$wgContLanguageCode][strtolower($title->getUserCaseDBKey())]); } else { return false; } }
foreach (explode("\n", $csv) as $line) { $tags = explode(",", $line); $city_id = array_shift($tags); $city_dbname = array_shift($tags); $city_cat = array_shift($tags); $city_url = array_shift($tags); /** * add category as tag */ $tags[] = $city_cat; /** * skip first row = labels */ if (!is_numeric($city_id)) { continue; } $wfTags = new WikiFactoryTags($city_id); $wfTags->removeTagsAll(); /** * first remove all tags */ foreach ($tags as $tag) { if (strlen($tag)) { $tag = str_replace(" ", "_", strtolower(trim($tag))); $ready[$tag]++; $wfTags->addTagsByName($tag); } } } ksort($ready); print_r($ready);
$lang = $row->city_lang; switch ( $lang ) { case 'zh-tw': case 'zh-hk': case 'zh-clas': case 'zh-class': case 'zh-cn': case 'zh-hans': case 'zh-hant': case 'zh-min-': case 'zh-min-n': case 'zh-mo': case 'zh-sg': case 'zh-yue': $lang = 'zh'; break; case 'pt-br': $lang = 'pt'; break; } /** * set this as tag */ $tags = new WikiFactoryTags( $row->city_id ); wfWaitForSlaves( 5 ); $tags->addTagsByName( $lang ); Wikia::log( "AddLangTag", false, "$hub added as tag in {$row->city_id}" ); }
/** * Get category id for given cityId * * @param int $city_id city id * * @return stdClass ($row->cat_id $row->cat_name) */ private static function getCategoryIdForCity($cityId) { $categoryId = null; if (self::isCorporatePage($cityId)) { $categoryId = WikiFactoryHub::CATEGORY_ID_CORPORATE; } else { $category = WikiFactory::getCategory($cityId); if ($category) { $categoryId = $category->cat_id; } } // Look for Comscore tag $wftags = new WikiFactoryTags($cityId); $tags = $wftags->getTags(); if (is_array($tags)) { foreach ($tags as $name) { if (startsWith($name, self::$comscore_prefix, false)) { $catName = substr($name, strlen(self::$comscore_prefix)); $category = WikiFactoryHub::getInstance()->getCategoryByName($catName); if ($category) { return $category['id']; } } } } return $categoryId; }
/** * doMultiRemoveTags * * @access private * @author uberfuzzy@wikia-inc.com * * @return text message (use Wikia::*box functions) */ private function doMultiRemoveTags() { /* working data is stored in object prior to call $this->mRemoveTag; has the tag to remove $this->mRemoveTags; has int array of wiki id to remove from */ /* in theory, these should never trigger, but BSTS */ if (empty($this->mRemoveTag)) { return Wikia::errorbox("no tag to remove?"); } if (empty($this->mRemoveTags)) { return Wikia::errorbox("no items to remove?"); } /* turn the tag string into the tag id */ $tagID = WikiFactoryTags::idFromName($this->mRemoveTag); if ($tagID === false) { return Wikia::errorbox("tag [{$this->mRemoveTag}] doesnt exist"); } /* to get list of all wikis with this tag, and later, use this to cache clear */ $tagsQuery = new WikiFactoryTagsQuery($this->mRemoveTag); $fails = array(); foreach ($this->mRemoveTags as $wkid) { $oTag = new WikiFactoryTags($wkid); $ret = $oTag->removeTagsById(array($tagID)); if ($ret === false) { $fails[] = $wkid; } } /* force dump of the tag_map in memcache */ $tagsQuery->clearCache(); /* since we /hopefully/ removed some tags from wikis, force the search results for this pageload to be empty. */ $this->mTagWikiIds = array(); #print "(forcing mTagWikiIds to null at ".gmdate('r').")"; if (empty($fails)) { return Wikia::successbox("ok!"); } else { return Wikia::errorbox("ok, but failed at " . count($fails) . " wikis" . " (" . implode(", ", $fails) . ")"); } }
/** * connect to statsdb and processing events table */ public function receiveFromEvents() { global $wgStatsDB, $wgCityId, $wgMemc, $wgStatsDBEnabled, $wgSharedDB, $wgIP; wfProfileIn(__METHOD__); if (empty($wgStatsDBEnabled)) { wfProfileOut(__METHOD__); return false; } try { while (1) { $dbr = wfGetDB(DB_SLAVE, array(), $wgStatsDB); $where = array(" rev_timestamp >= '" . $this->mDate . "' ", " (event_type = 2 or event_type = 1 ) "); if (!empty($wgStatsIgnoreWikis)) { $where[] = 'wiki_id not in (' . $dbr->makeList($wgStatsIgnoreWikis) . ')'; } $oRes = $dbr->select(array('events'), array('wiki_id, page_id, page_ns, user_id, rev_timestamp, user_is_bot'), $where, __METHOD__); $result = array(); $loop = 0; while ($oRow = $dbr->fetchObject($oRes)) { if ($oRow->rev_timestamp > $this->mDate) { $this->mDate = $oRow->rev_timestamp; } $result[$oRow->wiki_id][$oRow->page_id] = $oRow; $loop++; } $dbr->freeResult($oRes); Wikia::log(__METHOD__, 'events', 'Read ' . $loop . ' events (for ' . count($result) . ' Wikis) successfully. Next timestamp: ' . $this->mDate); $records = count($result); if (!empty($result)) { $producerDB = new WikiaStatsAutoHubsConsumerDB(DB_MASTER); $data = array('blogs' => array(), 'articles' => array(), 'user' => array(), 'tags' => array()); $loop = 0; foreach ($result as $city_id => $rows) { $start = time(); $loop++; Wikia::log(__METHOD__, 'events', 'Wikia ' . $city_id . ' (' . $loop . '/' . $records . ') processing: ' . count($rows) . ' rows'); $memkey = sprintf("%s:wikia:%d", __METHOD__, $city_id); $info = $wgMemc->get($memkey); if (empty($info)) { # wikia $oWikia = WikiFactory::getWikiByID($city_id); if (!is_object($oWikia)) { Wikia::log(__METHOD__, "Wikia not found: " . $city_id); continue; } # server $server = WikiFactory::getVarValueByName("wgServer", $city_id); $info = array('lang' => $oWikia->city_lang, 'db' => $oWikia->city_dbname, 'sitename' => $oWikia->city_title, 'server' => $server); $wgMemc->set($memkey, $info, 60 * 60 * 2); } if (!isset($info['db']) && !isset($info['sitename']) && !isset($info['lang']) && !isset($info['server'])) { Wikia::log(__METHOD__, "Wikia not found: " . $city_id); continue; } # initial table $lang = $info['lang']; if (!isset($data['blogs'][$lang])) { $data['blogs'][$lang] = array(); } if (!isset($data['articles'][$lang])) { $data['articles'][$lang] = array(); } if (!isset($data['user'][$lang])) { $data['user'][$lang] = array(); } # tags $oWFTags = new WikiFactoryTags($city_id); $tags = $oWFTags->getTags(); foreach ($rows as $oRow) { if (is_object($oRow)) { $oUser = User::newFromId($oRow->user_id); if (!is_object($oUser)) { continue; } if (NS_BLOG_ARTICLE == $oRow->page_ns) { if (!empty($tags)) { foreach ($tags as $id => $val) { if (!isset($data['blogs'][$lang][$id])) { $data['blogs'][$lang][$id] = array(); } # prepare insert data $data['blogs'][$lang][$id][] = array('tb_city_id' => $city_id, 'tb_page_id' => $oRow->page_id, 'tb_tag_id' => $id, 'tb_date' => date("Y-m-d"), 'tb_city_lang' => $lang, 'tb_count' => 1); } } } else { $memkey = sprintf("%s:%s:user:%d", __METHOD__, $wgSharedDB, $oRow->user_id); $user = $wgMemc->get($memkey); if (empty($user)) { $groups = $oUser->getGroups(); $user_groups = implode(";", $groups); $user = array('name' => $oUser->getName(), 'groups' => $user_groups); $wgMemc->set($memkey, $user, 60 * 60 * 2); } if (!isset($user['name'])) { continue; } if ($user['name'] == $wgIP || User::isIP($user['name'])) { continue; } if (!empty($tags)) { foreach ($tags as $id => $val) { $date = date("Y-m-d"); $mcKey = wfSharedMemcKey("auto_hubs", "unique_control", $city_id, $oRow->page_id, $oRow->user_id, $id, $date); $out = $wgMemc->get($mcKey, null); if ($out == 1) { continue; } $wgMemc->set($mcKey, 1, 24 * 60 * 60); $allowed = $oRow->user_is_bot != 'Y' && !in_array($oUser->getName(), $producerDB->getBanedUsers()); if (!isset($data['user'][$lang][$id]) && $allowed) { $data['user'][$lang][$id] = array(); } if (!isset($data['articles'][$lang][$id])) { $data['articles'][$lang][$id] = array(); } # # prepare insert data $data['articles'][$lang][$id][] = array('ta_city_id' => $city_id, 'ta_page_id' => $oRow->page_id, 'ta_tag_id' => $id, 'ta_date' => $date, 'ta_city_lang' => $lang, 'ta_count' => 1); if ($allowed) { $data['user'][$lang][$id][] = array('tu_user_id' => $oRow->user_id, 'tu_tag_id' => $id, 'tu_date' => $date, 'tu_groups' => $user['groups'], 'tu_username' => addslashes($user['name']), 'tu_city_lang' => $lang, 'tu_count' => 1); } } } } } } $end = time(); $time = Wikia::timeDuration($end - $start); Wikia::log(__METHOD__, 'events', 'Wikia ' . $city_id . ' processed in: ' . $time); } // insert data to database # blogs $start = time(); Wikia::log(__METHOD__, 'events', 'Insert ' . count($data['blogs']) . ' blogs'); $producerDB->insertBlogComment($data['blogs']); $end = time(); $time = Wikia::timeDuration($end - $start); Wikia::log(__METHOD__, 'events', 'Inserts done in: ' . $time); # articles $start = time(); Wikia::log(__METHOD__, 'events', 'Insert ' . count($data['articles']) . ' articles'); $producerDB->insertArticleEdit($data['articles']); $end = time(); $time = Wikia::timeDuration($end - $start); Wikia::log(__METHOD__, 'events', 'Inserts done in: ' . $time); $start = time(); Wikia::log(__METHOD__, 'events', 'Insert ' . count($data['user']) . ' users'); $producerDB->insertUserEdit($data['user']); $end = time(); $time = Wikia::timeDuration($end - $start); Wikia::log(__METHOD__, 'events', 'Inserts done in: ' . $time); // unset data unset($data); } else { Wikia::log(__METHOD__, "No data found in events table. Last timestamp: " . $this->mDate); } Wikia::log(__METHOD__, "Wait " . self::sleepTime . " sec. "); sleep(self::sleepTime); } } catch (MWException $e) { $mesg = $e->getMessage(); $class = get_class($e); Wikia::log(__METHOD__, 'events', $mesg); die('Cannot proceed events data. Message was: ' . $mesg . '. Class was:' . $class); } wfProfileOut(__METHOD__); }
<?php /** * @package MediaWiki * @addtopackage maintenance * @author eloy@wikia * * move category to tags table * */ ini_set("include_path", dirname(__FILE__) . "/.."); require_once "commandLine.inc"; ini_set("include_path", dirname(__FILE__) . "/.."); require_once "commandLine.inc"; $dbr = WikiFactory::db(DB_SLAVE); $sth = $dbr->select(array("city_list"), array("*"), array("city_public" => 1), __METHOD__); while ($row = $dbr->fetchObject($sth)) { /** * get category for wiki */ $hub = WikiFactoryHub::getInstance()->getCategoryName($row->city_id); /** * set this as tag */ $tags = new WikiFactoryTags($row->city_id); $tags->addTagsByName($hub); Wikia::log("CatAsTag", false, "{$hub} added as tag in {$row->city_id}"); }
private function tagSelectedWikis($tagName, array $tagWikis) { global $wgCityId; if (!class_exists('WikiFactoryTags', true)) { return "WikiFactory extension must be enabled"; } if (!empty($tagName)) { foreach ($tagWikis as $wikiId) { $wikiTags = new WikiFactoryTags($wikiId); $wikiTags->addTagsByName($tagName); } $wikiFactoryUrl = Title::makeTitle(NS_SPECIAL, 'WikiFactory')->getFullUrl() . '/' . $wgCityId . '/tags/' . $tagName; $msg = count($tagWikis) . " wiki(s) tagged with tag: <a href=\"{$wikiFactoryUrl}\">{$tagName}</a>"; } else { $msg = "Empty tag name"; } return $msg; }
/** * main entry point, create wiki with given parameters * * @return integer status of operation, 0 for success, non 0 for error */ public function create() { global $wgWikiaLocalSettingsPath, $wgExternalSharedDB, $wgSharedDB, $wgUser; wfProfileIn(__METHOD__); if (wfReadOnly()) { wfProfileOut(__METHOD__); return self::ERROR_READONLY; } if (wfIsDBLightMode()) { wfProfileOut(__METHOD__); return self::ERROR_DBLIGHTMODE; } // check founder if ($this->mFounder->isAnon()) { wfProfileOut(__METHOD__); return self::ERROR_USER_IN_ANON; } // check executables $status = $this->checkExecutables(); if ($status != 0) { wfProfileOut(__METHOD__); return $status; } // check domains $status = $this->checkDomain(); if ($status != 0) { wfProfileOut(__METHOD__); return $status; } // prepare all values needed for creating wiki $this->prepareValues(); // prevent domain to be registered more than once if (!AutoCreateWiki::lockDomain($this->mDomain)) { wfProfileOut(__METHOD__); return self::ERROR_DOMAIN_NAME_TAKEN; } // start counting time $this->mCurrTime = wfTime(); $startTime = $this->mCurrTime; // check and create database $this->mDBw = wfGetDB(DB_MASTER, array(), $wgExternalSharedDB); # central /// // local database handled is handler to cluster we create new wiki. // It doesn't have to be the same like wikifactory cluster or db cluster // where Special:CreateWiki exists. // // @todo do not use hardcoded name, code below is only for test // // set $activeCluster to false if you want to create wikis on first // cluster // $clusterdb = self::ACTIVE_CLUSTER ? "wikicities_" . self::ACTIVE_CLUSTER : "wikicities"; $this->mNewWiki->dbw = wfGetDB(DB_MASTER, array(), $clusterdb); // database handler, old $dbwTarget // check if database is creatable // @todo move all database creation checkers to canCreateDatabase if (!$this->canCreateDatabase()) { wfDebugLog("createwiki", "Database {$this->mNewWiki->dbname} exists\n", true); wfProfileOut(__METHOD__); return self::ERROR_DATABASE_ALREADY_EXISTS; } else { $this->mNewWiki->dbw->query(sprintf("CREATE DATABASE `%s`", $this->mNewWiki->dbname)); wfDebugLog("createwiki", "Database {$this->mNewWiki->dbname} created\n", true); } /** * create position in wiki.factory * (I like sprintf construction, so sue me) */ if (!$this->addToCityList()) { wfDebugLog("createwiki", __METHOD__ . ": Cannot set data in city_list table\n", true); wfProfileOut(__METHOD__); return self::ERROR_DATABASE_WRITE_TO_CITY_LIST_BROKEN; } // set new city_id $this->mNewWiki->city_id = $this->mDBw->insertId(); if (empty($this->mNewWiki->city_id)) { wfDebugLog("createwiki", __METHOD__ . ": Cannot set data in city_list table. city_id is empty after insert\n", true); wfProfileOut(__METHOD__); return self::ERROR_DATABASE_WIKI_FACTORY_TABLES_BROKEN; } wfDebugLog("createwiki", __METHOD__ . ": Row added added into city_list table, city_id = {$this->mNewWiki->city_id}\n", true); /** * add domain and www.domain to the city_domains table */ if (!$this->addToCityDomains()) { wfDebugLog("createwiki", __METHOD__ . ": Cannot set data in city_domains table\n", true); wfProfileOut(__METHOD__); return self::ERROR_DATABASE_WRITE_TO_CITY_DOMAINS_BROKEN; } wfDebugLog("createwiki", __METHOD__ . ": Row added into city_domains table, city_id = {$this->mNewWiki->city_id}\n", true); /** * create image folder */ wfMkdirParents("{$this->mNewWiki->images_dir}"); wfDebugLog("createwiki", __METHOD__ . ": Folder {$this->mNewWiki->images_dir} created\n", true); /** * copy default logo & favicon */ wfMkdirParents("{$this->mNewWiki->images_logo}"); wfMkdirParents("{$this->mNewWiki->images_icon}"); if (file_exists(self::CREATEWIKI_LOGO)) { copy(self::CREATEWIKI_LOGO, "{$this->mNewWiki->images_logo}/Wiki.png"); wfDebugLog("createwiki", __METHOD__ . ": Default logo has been copied\n", true); } else { wfDebugLog("createwiki", __METHOD__ . ": Default logo has not been copied\n", true); } if (file_exists(self::CREATEWIKI_ICON)) { copy(self::CREATEWIKI_ICON, "{$this->mNewWiki->images_icon}/Favicon.ico"); wfDebugLog("createwiki", __METHOD__ . ": Default favicon has been copied\n", true); } else { wfDebugLog("createwiki", __METHOD__ . ": Default favicon has not been copied\n", true); } /** * wikifactory variables */ wfDebugLog("createwiki", __METHOD__ . ": Populating city_variables\n", true); $this->setWFVariables(); $tmpSharedDB = $wgSharedDB; $wgSharedDB = $this->mNewWiki->dbname; /** * we got empty database created, now we have to create tables and * populate it with some default values */ wfDebugLog("createwiki", __METHOD__ . ": Creating tables in database\n", true); $this->mNewWiki->dbw = wfGetDB(DB_MASTER, array(), $this->mNewWiki->dbname); if (!$this->createTables()) { wfDebugLog("createwiki", __METHOD__ . ": Creating tables not finished\n", true); wfProfileOut(__METHOD__); return self::ERROR_SQL_FILE_BROKEN; } /** * import language starter */ if (!$this->importStarter()) { wfProfileOut(__METHOD__); return self::ERROR_SQL_FILE_BROKEN; } /** * making the wiki founder a sysop/bureaucrat */ wfDebugLog("createwiki", __METHOD__ . ": Create user sysop/bureaucrat for user: {$this->mNewWiki->founderId} \n", true); if (!$this->addUserToGroups()) { wfDebugLog("createwiki", __METHOD__ . ": Create user sysop/bureaucrat for user: {$this->mNewWiki->founderId} failed \n", true); } /** * init site_stats table (add empty row) */ $this->mNewWiki->dbw->insert("site_stats", array("ss_row_id" => "1"), __METHOD__); /** * add local job */ $job_params = new stdClass(); foreach ($this->mNewWiki as $id => $value) { if (!is_object($value)) { $job_params->{$id} = $value; } } // BugId:15644 - I need to pass this to CreateWikiLocalJob::changeStarterContributions $job_params->sDbStarter = $this->sDbStarter; $localJob = new CreateWikiLocalJob(Title::newFromText(NS_MAIN, "Main"), $job_params); $localJob->WFinsert($this->mNewWiki->city_id, $this->mNewWiki->dbname); wfDebugLog("createwiki", __METHOD__ . ": New createWiki local job created \n", true); /** * destroy connection to newly created database */ $this->mNewWiki->dbw->commit(); wfDebugLog("createwiki", __METHOD__ . ": Database changes commited \n", true); $wgSharedDB = $tmpSharedDB; /** * set hub/category */ $oldUser = $wgUser; $wgUser = User::newFromName('CreateWiki script'); $oHub = WikiFactoryHub::getInstance(); $oHub->setCategory($this->mNewWiki->city_id, $this->mNewWiki->hub, "CW Setup"); wfDebugLog("createwiki", __METHOD__ . ": Wiki added to the category hub: {$this->mNewWiki->hub} \n", true); $wgUser = $oldUser; unset($oldUser); /** * define wiki type */ $wiki_type = !empty($this->mType) ? $this->mType : 'default'; /** * modify variables */ global $wgUniversalCreationVariables; if (!empty($wgUniversalCreationVariables) && !empty($wiki_type) && isset($wgUniversalCreationVariables[$wiki_type])) { $this->addCustomSettings(0, $wgUniversalCreationVariables[$wiki_type], "universal"); wfDebugLog("createwiki", __METHOD__ . ": Custom settings added for wiki_type: {$wiki_type} \n", true); } /** * set variables per language */ global $wgLangCreationVariables; $langCreationVar = isset($wgLangCreationVariables[$wiki_type]) ? $wgLangCreationVariables[$wiki_type] : $wgLangCreationVariables; $this->addCustomSettings($this->mNewWiki->language, $langCreationVar, "language"); wfDebugLog("createwiki", __METHOD__ . ": Custom settings added for wiki_type: {$wiki_type} and language: {$this->mNewWiki->language} \n", true); /** * set tags per language and per hub * @FIXME the switch is !@#$ creazy, but I didn't find a core function */ $tags = new WikiFactoryTags($this->mNewWiki->city_id); $langTag = $this->mNewWiki->language; if ($langTag !== 'en') { switch ($langTag) { case 'pt-br': $langTag = 'pt'; break; case 'zh-tw': case 'zh-hk': case 'zh-clas': case 'zh-class': case 'zh-classical': case 'zh-cn': case 'zh-hans': case 'zh-hant': case 'zh-min-': case 'zh-min-n': case 'zh-mo': case 'zh-sg': case 'zh-yue': $langTag = 'zh'; break; } $tags->addTagsByName($langTag); } $tags->addTagsByName($this->mNewWiki->hub); /** * move main page */ $cmd = sprintf("SERVER_ID=%d %s %s/maintenance/wikia/moveMain.php -t '%s' --conf %s", $this->mNewWiki->city_id, $this->mPHPbin, $this->mIP, $this->mNewWiki->sitename, $wgWikiaLocalSettingsPath); $output = wfShellExec($cmd); wfDebugLog("createwiki", __METHOD__ . ": Main page moved \n", true); /** * show congratulation message */ /** * Unset database from mNewWiki, because database objects cannot be serialized from MW1.19 */ unset($this->mNewWiki->dbw); /** * inform task manager */ $Task = new LocalMaintenanceTask(); $Task->createTask(array("city_id" => $this->mNewWiki->city_id, "command" => "maintenance/runJobs.php", "type" => "CWLocal", "data" => $this->mNewWiki, "server" => rtrim($this->mNewWiki->url, "/")), TASK_QUEUED, BatchTask::PRIORITY_HIGH); wfDebugLog("createwiki", __METHOD__ . ": Local maintenance task added\n", true); wfProfileOut(__METHOD__); /** * return success */ return 0; }