/** * Format a message for output * @param array &$res Result array * @param string $key Result key * @param Message $message */ private function formatMessage(array &$res, $key, Message $message) { switch ($this->messageFormat) { case 'none': break; case 'wikitext': $res[$key] = $message->setContext($this->module)->text(); break; case 'html': $res[$key] = $message->setContext($this->module)->parseAsBlock(); $res[$key] = Parser::stripOuterParagraph($res[$key]); break; case 'raw': $res[$key] = ['key' => $message->getKey(), 'params' => $message->getParams()]; break; } }
/** * Parse wikitext, strip paragraphs, and return the HTML. * * @param string $text * @param bool $linestart Is this the start of a line? * @param bool $interface Use interface language ($wgLang instead of * $wgContLang) while parsing language sensitive magic * words like GRAMMAR and PLURAL * @return string HTML */ public function parseInline($text, $linestart = true, $interface = false) { $parsed = $this->parse($text, $linestart, $interface); return Parser::stripOuterParagraph($parsed); }
/** * Returns message in the requested format * * @deprecated since 1.18 * * @param string $key Key of the message * @param array $options Processing rules. * Can take the following options: * parse: parses wikitext to HTML * parseinline: parses wikitext to HTML and removes the surrounding * p's added by parser or tidy * escape: filters message through htmlspecialchars * escapenoentities: same, but allows entity references like   through * replaceafter: parameters are substituted after parsing or escaping * parsemag: transform the message using magic phrases * content: fetch message for content language instead of interface * Also can accept a single associative argument, of the form 'language' => 'xx': * language: Language object or language code to fetch message for * (overridden by content). * Behavior for conflicting options (e.g., parse+parseinline) is undefined. * * @return string */ function wfMsgExt($key, $options) { wfDeprecated(__METHOD__, '1.21'); $args = func_get_args(); array_shift($args); array_shift($args); $options = (array) $options; $validOptions = array('parse', 'parseinline', 'escape', 'escapenoentities', 'replaceafter', 'parsemag', 'content'); foreach ($options as $arrayKey => $option) { if (!preg_match('/^[0-9]+|language$/', $arrayKey)) { // An unknown index, neither numeric nor "language" wfWarn("wfMsgExt called with incorrect parameter key {$arrayKey}", 1, E_USER_WARNING); } elseif (preg_match('/^[0-9]+$/', $arrayKey) && !in_array($option, $validOptions)) { // A numeric index with unknown value wfWarn("wfMsgExt called with incorrect parameter {$option}", 1, E_USER_WARNING); } } if (in_array('content', $options, true)) { $forContent = true; $langCode = true; $langCodeObj = null; } elseif (array_key_exists('language', $options)) { $forContent = false; $langCode = wfGetLangObj($options['language']); $langCodeObj = $langCode; } else { $forContent = false; $langCode = false; $langCodeObj = null; } $string = wfMsgGetKey($key, true, $langCode, false); if (!in_array('replaceafter', $options, true)) { $string = wfMsgReplaceArgs($string, $args); } $messageCache = MessageCache::singleton(); $parseInline = in_array('parseinline', $options, true); if (in_array('parse', $options, true) || $parseInline) { $string = $messageCache->parse($string, null, true, !$forContent, $langCodeObj); if ($string instanceof ParserOutput) { $string = $string->getText(); } if ($parseInline) { $string = Parser::stripOuterParagraph($string); } } elseif (in_array('parsemag', $options, true)) { $string = $messageCache->transform($string, !$forContent, $langCodeObj); } if (in_array('escape', $options, true)) { $string = htmlspecialchars($string); } elseif (in_array('escapenoentities', $options, true)) { $string = Sanitizer::escapeHtmlAllowEntities($string); } if (in_array('replaceafter', $options, true)) { $string = wfMsgReplaceArgs($string, $args); } return $string; }
public function execute() { $action = $this->getMain()->getVal('type'); $page = $this->getMain()->getVal('pageid'); try { // If post is set, get the post object by id // By fetching the post object, we also validate the id $postList = $this->getMain()->getVal('postid'); $postList = $this->parsePostList($postList); switch ($action) { case 'list': $this->executeList(); break; case 'like': if (!$postList) { $this->dieNoParam('postid'); } foreach ($postList as $post) { $post->setUserAttitude($this->getUser(), Post::ATTITUDE_LIKE); } $this->getResult()->addValue(null, $this->getModuleName(), ''); break; case 'dislike': if (!$postList) { $this->dieNoParam('postid'); } foreach ($postList as $post) { $post->setUserAttitude($this->getUser(), Post::ATTITUDE_NORMAL); } $this->getResult()->addValue(null, $this->getModuleName(), ''); break; case 'report': if (!$postList) { $this->dieNoParam('postid'); } foreach ($postList as $post) { $post->setUserAttitude($this->getUser(), Post::ATTITUDE_REPORT); } $this->getResult()->addValue(null, $this->getModuleName(), ''); break; case 'delete': if (!$postList) { $this->dieNoParam('postid'); } foreach ($postList as $post) { $post->delete($this->getUser()); } $this->getResult()->addValue(null, $this->getModuleName(), ''); break; case 'recover': if (!$postList) { $this->dieNoParam('postid'); } foreach ($postList as $post) { $post->recover($this->getUser()); } $this->getResult()->addValue(null, $this->getModuleName(), ''); break; case 'erase': if (!$postList) { $this->dieNoParam('postid'); } foreach ($postList as $post) { $post->erase($this->getUser()); } $this->getResult()->addValue(null, $this->getModuleName(), ''); break; case 'post': if (!$page) { $this->dieNoParam('pageid'); } $text = $this->getMain()->getVal('content'); if (!$text) { $this->dieNoParam('content'); } // Permission check Post::checkIfCanPost($this->getUser()); // Need to feed this to spam filter $useWikitext = $this->getMain()->getCheck('wikitext'); $filterResult = SpamFilter::validate($text, $this->getUser(), $useWikitext); $text = $filterResult['text']; // Parse as wikitext if specified if ($useWikitext) { $parser = new \Parser(); $opt = new \ParserOptions($this->getUser()); $opt->setEditSection(false); $output = $parser->parse($text, \Title::newFromId($page), $opt); $text = $output->getText(); unset($parser); unset($opt); unset($output); $text = \Parser::stripOuterParagraph($text); $text = SpamFilter::sanitize($text); } else { $text = htmlspecialchars($text); } $data = array('id' => null, 'pageid' => $page, 'userid' => $this->getUser()->getId(), 'username' => $this->getUser()->getName(), 'text' => $text, 'parentid' => count($postList) ? $postList[0]->id : null, 'status' => $filterResult['good'] ? Post::STATUS_NORMAL : Post::STATUS_SPAM, 'like' => 0, 'report' => 0); $postObject = new Post($data); global $wgFlowThreadConfig; // Restrict max nest level if ($postObject->getNestLevel() > $wgFlowThreadConfig['MaxNestLevel']) { $postObject->parentid = $postObject->getParent()->parentid; $postObject->parent = $postObject->getParent()->parent; } $postObject->post(); if (!$filterResult['good']) { global $wgTriggerFlowThreadHooks; if ($wgTriggerFlowThreadHooks) { \Hooks::run('FlowThreadSpammed', array($postObject)); } } $this->getResult()->addValue(null, $this->getModuleName(), ''); break; default: $this->dieUsage("Unrecognized value for parameter 'type': {$action}", 'unknown_type'); } } catch (\UsageException $e) { throw $e; } catch (\Exception $e) { $this->getResult()->addValue("error", 'code', 'unknown_error'); $this->getResult()->addValue("error", 'info', $e->getMessage()); } return true; }
/** * Returns the message parsed from wikitext to HTML. * * @since 1.17 * * @return string HTML */ public function toString() { $string = $this->fetchMessage(); if ($string === false) { if ($this->format === 'plain' || $this->format === 'text') { return '<' . $this->key . '>'; } return '<' . htmlspecialchars($this->key) . '>'; } # Replace $* with a list of parameters for &uselang=qqx. if (strpos($string, '$*') !== false) { $paramlist = ''; if ($this->parameters !== array()) { $paramlist = ': $' . implode(', $', range(1, count($this->parameters))); } $string = str_replace('$*', $paramlist, $string); } # Replace parameters before text parsing $string = $this->replaceParameters($string, 'before'); # Maybe transform using the full parser if ($this->format === 'parse') { $string = $this->parseText($string); $string = Parser::stripOuterParagraph($string); } elseif ($this->format === 'block-parse') { $string = $this->parseText($string); } elseif ($this->format === 'text') { $string = $this->transformText($string); } elseif ($this->format === 'escaped') { $string = $this->transformText($string); $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false); } # Raw parameter replacement $string = $this->replaceParameters($string, 'after'); return $string; }
/** * @dataProvider provideStripOuterParagraph * @covers Parser::stripOuterParagraph */ public function testStripOuterParagraph($text, $expected) { $this->assertEquals($expected, Parser::stripOuterParagraph($text)); }
/** * Returns the message parsed from wikitext to HTML. * * @since 1.17 * * @return string HTML */ public function toString() { $string = $this->fetchMessage(); if ($string === false) { // Err on the side of safety, ensure that the output // is always html safe in the event the message key is // missing, since in that case its highly likely the // message key is user-controlled. // '⧼' is used instead of '<' to side-step any // double-escaping issues. return '⧼' . htmlspecialchars($this->key) . '⧽'; } # Replace $* with a list of parameters for &uselang=qqx. if (strpos($string, '$*') !== false) { $paramlist = ''; if ($this->parameters !== []) { $paramlist = ': $' . implode(', $', range(1, count($this->parameters))); } $string = str_replace('$*', $paramlist, $string); } # Replace parameters before text parsing $string = $this->replaceParameters($string, 'before'); # Maybe transform using the full parser if ($this->format === 'parse') { $string = $this->parseText($string); $string = Parser::stripOuterParagraph($string); } elseif ($this->format === 'block-parse') { $string = $this->parseText($string); } elseif ($this->format === 'text') { $string = $this->transformText($string); } elseif ($this->format === 'escaped') { $string = $this->transformText($string); $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false); } # Raw parameter replacement $string = $this->replaceParameters($string, 'after'); return $string; }
/** * XML-style tag for page status indicators: icons (or short text snippets) usually displayed in * the top-right corner of the page, outside of the main content. * * @param string $content * @param array $attributes * @param Parser $parser * @param PPFrame $frame * @return string * @since 1.25 */ public static function indicator($content, array $attributes, Parser $parser, PPFrame $frame) { if (!isset($attributes['name']) || trim($attributes['name']) === '') { return '<span class="error">' . wfMessage('invalid-indicator-name')->inContentLanguage()->parse() . '</span>'; } $parser->getOutput()->setIndicator(trim($attributes['name']), Parser::stripOuterParagraph($parser->recursiveTagParseFully($content, $frame))); return ''; }
/** * @param array $res Result array * @param string $key Result key * @param Message[] $msgs * @param bool $joinLists */ protected function formatHelpMessages(array &$res, $key, array $msgs, $joinLists = false) { switch ($this->helpFormat) { case 'none': break; case 'wikitext': $ret = []; foreach ($msgs as $m) { $ret[] = $m->setContext($this->context)->text(); } $res[$key] = implode("\n\n", $ret); if ($joinLists) { $res[$key] = preg_replace('!^(([*#:;])[^\\n]*)\\n\\n(?=\\2)!m', "\$1\n", $res[$key]); } break; case 'html': $ret = []; foreach ($msgs as $m) { $ret[] = $m->setContext($this->context)->parseAsBlock(); } $ret = implode("\n", $ret); if ($joinLists) { $ret = preg_replace('!\\s*</([oud]l)>\\s*<\\1>\\s*!', "\n", $ret); } $res[$key] = Parser::stripOuterParagraph($ret); break; case 'raw': $res[$key] = []; foreach ($msgs as $m) { $a = ['key' => $m->getKey(), 'params' => $m->getParams()]; ApiResult::setIndexedTagName($a['params'], 'param'); if ($m instanceof ApiHelpParamValueMessage) { $a['forvalue'] = $m->getParamValue(); } $res[$key][] = $a; } ApiResult::setIndexedTagName($res[$key], 'msg'); break; } }