/** * setVarByName * * used for saving new variable value, logging changes and update city_list * values * * @access public * @author eloy@wikia * @static * * @param string $variable: variable name in city_variables_pool * @param integer $wiki: wiki id in city list * @param mixed $value: new value for variable * @param string $reason: optional reason text * * @return boolean: transaction status */ public static function setVarByName($variable, $wiki, $value, $reason = null) { $oVariable = self::getVarByName($variable, $wiki); return WikiFactory::setVarByID($oVariable->cv_variable_id, $wiki, $value, $reason); }
/** * axWFactorySaveVariable * * ajax method, save variable from form * * @author Krzysztof Krzyżaniak (eloy) <*****@*****.**> * @access public * * @return string encoded in JSON format */ function axWFactorySaveVariable() { global $wgUser, $wgRequest; $error = 0; $return = ""; if (!$wgUser->isAllowed('wikifactory')) { $error++; $return = Wikia::errormsg("You are not allowed to change variable value"); } else { $cv_id = $wgRequest->getVal('varId'); $city_id = $wgRequest->getVal('cityid'); $cv_name = $wgRequest->getVal('varName'); $cv_value = $wgRequest->getVal('varValue'); $cv_variable_type = $wgRequest->getVal('varType'); $reason = $wgRequest->getVal('reason', null); $tag_name = $wgRequest->getVal('tagName'); $tag_wiki_count = 0; $form_id = $wgRequest->getVal("formId", null); #--- check if variable is valid switch ($cv_variable_type) { case "boolean": if ((bool) $cv_value != $cv_value) { $error++; $return = Wikia::errormsg("Syntax error: value is not boolean. Variable not saved."); } else { $return = Wikia::successmsg("Parse OK, variable saved"); $cv_value = (bool) $cv_value; } break; case "integer": if ((int) $cv_value != $cv_value) { $error++; $return = Wikia::errormsg("Syntax error: value is not integer. Variable not saved."); } else { $return = Wikia::successmsg("Parse OK, variable saved."); $cv_value = (int) $cv_value; } break; case "string": $return = Wikia::successmsg("Parse OK, variable saved."); break; default: $tEval = "\$__var_value = {$cv_value};"; /** * catch parse errors */ ob_start(); if (eval($tEval) === FALSE) { $error++; $return = Wikia::errormsg("Syntax error, value is not valid PHP structure. Variable not saved."); } else { $cv_value = $__var_value; /** * now check if it's actually array when we want array) */ if (in_array($cv_variable_type, array("array", "struct", "hash"))) { if (is_array($cv_value)) { $return = Wikia::successmsg("Syntax OK (array), variable saved."); } else { $error++; $return = Wikia::errormsg("Syntax error: value is not array. Variable not saved."); } } else { $return = Wikia::successmsg("Parse OK, variable saved."); } } ob_end_clean(); #--- puts parse error to /dev/null } if (empty($error)) { $varInfo = WikiFactory::getVarById($cv_id, $city_id); if ($varInfo->cv_is_unique) { $wikis = WikiFactory::getCityIDsFromVarValue($cv_id, $cv_value, '='); $count = count($wikis); if ($count == 1 && $wikis[0] != $city_id || $count > 1) { $return = Wikia::errormsg("Value of this variable need to be unique."); $error++; } } } wfRunHooks('WikiFactoryVarSave::AfterErrorDetection', array($cv_id, $city_id, $cv_name, $cv_value, &$return, &$error)); # Save to DB, but only if no errors occurred if (empty($error)) { if (!WikiFactory::setVarByID($cv_id, $city_id, $cv_value, $reason)) { $error++; $return = Wikia::errormsg("Variable not saved because of problems with database. Try again."); } else { /* the one "set" that used this is now disabled, so disabling the call until needed again $tied = WikiFactory::getTiedVariables( $cv_name ); if( $tied ) { $return .= Wikia::successmsg( " This variable is tied with others. Check: ". implode(", ", $tied ) ); } */ if (!empty($tag_name)) { // apply changes to all wikis with given tag $tagsQuery = new WikiFactoryTagsQuery($tag_name); foreach ($tagsQuery->doQuery() as $tagged_wiki_id) { if (WikiFactory::setVarByID($cv_id, $tagged_wiki_id, $cv_value, $reason)) { $tag_wiki_count++; } } $return .= Wikia::successmsg(" ({$tag_wiki_count} wikis affected)"); } } } } if (empty($form_id)) { $div_name = "wf-variable-parse"; } else { $div_name = "wf-variable-parse-{$form_id}"; } return json_encode(array("div-body" => $return, "is-error" => $error, "tag-name" => $tag_name, "tag-wikis" => $tag_wiki_count, "div-name" => $div_name)); }