Example #1
0
 /**
  * Add value to the output data at the given path.
  *
  * Path can be an indexed array, each element specifying the branch at which to add the new
  * value. Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value.
  * If $path is null, the value will be inserted at the data root.
  *
  * @param array|string|int|null $path
  * @param string|int|null $name See ApiResult::setValue()
  * @param mixed $value
  * @param int $flags Zero or more OR-ed flags like OVERRIDE | ADD_ON_TOP.
  *   This parameter used to be boolean, and the value of OVERRIDE=1 was specifically
  *   chosen so that it would be backwards compatible with the new method signature.
  * @return bool True if $value fits in the result, false if not
  * @since 1.21 int $flags replaced boolean $override
  */
 public function addValue($path, $name, $value, $flags = 0)
 {
     $arr =& $this->path($path, $flags & ApiResult::ADD_ON_TOP ? 'prepend' : 'append');
     if ($this->checkingSize && !($flags & ApiResult::NO_SIZE_CHECK)) {
         // self::valueSize needs the validated value. Then flag
         // to not re-validate later.
         $value = self::validateValue($value);
         $flags |= ApiResult::NO_VALIDATE;
         $newsize = $this->size + self::valueSize($value);
         if ($this->maxSize !== false && $newsize > $this->maxSize) {
             /// @todo Add i18n message when replacing calls to ->setWarning()
             $msg = new ApiRawMessage('This result was truncated because it would otherwise ' . ' be larger than the limit of $1 bytes', 'truncatedresult');
             $msg->numParams($this->maxSize);
             $this->errorFormatter->addWarning('result', $msg);
             return false;
         }
         $this->size = $newsize;
     }
     self::setValue($arr, $name, $value, $flags);
     return true;
 }
 /**
  * Actually add the warning or error to the result
  * @param string $tag 'warning' or 'error'
  * @param string $moduleName
  * @param ApiMessage|ApiRawMessage $msg
  */
 protected function addWarningOrError($tag, $moduleName, $msg)
 {
     $value = ['code' => $msg->getApiCode()];
     switch ($this->format) {
         case 'wikitext':
             $value += ['text' => $msg->text(), ApiResult::META_CONTENT => 'text'];
             break;
         case 'html':
             $value += ['html' => $msg->parse(), ApiResult::META_CONTENT => 'html'];
             break;
         case 'raw':
             $value += ['message' => $msg->getKey(), 'params' => $msg->getParams()];
             ApiResult::setIndexedTagName($value['params'], 'param');
             break;
         case 'none':
             break;
     }
     $value += $msg->getApiData();
     $path = [$tag . 's', $moduleName];
     $existing = $this->result->getResultData($path);
     if ($existing === null || !in_array($value, $existing)) {
         $flags = ApiResult::NO_SIZE_CHECK;
         if ($existing === null) {
             $flags |= ApiResult::ADD_ON_TOP;
         }
         $this->result->addValue($path, null, $value, $flags);
         $this->result->addIndexedTagName($path, $tag);
     }
 }