public function testGetTemplates() { $title = Title::makeTitle(NS_TEMPLATE, 'Cite_news'); $parserOutput = new ParserOutput(); $parserOutput->addTemplate($title, 10, 100); $searchDataExtractor = new ParserOutputSearchDataExtractor(); $this->assertEquals(['Template:Cite news'], $searchDataExtractor->getTemplates($parserOutput)); }
/** * Fetch the unparsed text of a template and register a reference to it. * @param Title $title * @return Array ( string or false, Title ) */ function fetchTemplateAndTitle($title) { $templateCb = $this->mOptions->getTemplateCallback(); # Defaults to Parser::statelessFetchTemplate() $stuff = call_user_func($templateCb, $title, $this); $text = $stuff['text']; $finalTitle = isset($stuff['finalTitle']) ? $stuff['finalTitle'] : $title; if (isset($stuff['deps'])) { foreach ($stuff['deps'] as $dep) { $this->mOutput->addTemplate($dep['title'], $dep['page_id'], $dep['rev_id']); if ($dep['title']->equals($this->getTitle())) { // If we transclude ourselves, the final result // will change based on the new version of the page $this->mOutput->setFlag('vary-revision'); } } } return array($text, $finalTitle); }
/** * Fetch the unparsed text of a template and register a reference to it. * @param Title $title * @return array ( string or false, Title ) */ public function fetchTemplateAndTitle($title) { // Defaults to Parser::statelessFetchTemplate() $templateCb = $this->mOptions->getTemplateCallback(); $stuff = call_user_func($templateCb, $title, $this); // We use U+007F DELETE to distinguish strip markers from regular text. $text = $stuff['text']; if (is_string($stuff['text'])) { $text = strtr($text, "", "?"); } $finalTitle = isset($stuff['finalTitle']) ? $stuff['finalTitle'] : $title; if (isset($stuff['deps'])) { foreach ($stuff['deps'] as $dep) { $this->mOutput->addTemplate($dep['title'], $dep['page_id'], $dep['rev_id']); if ($dep['title']->equals($this->getTitle())) { // If we transclude ourselves, the final result // will change based on the new version of the page $this->mOutput->setFlag('vary-revision'); } } } return array($text, $finalTitle); }
/** * Fetch the unparsed text of a template and register a reference to it. * @param Title $title * @return Array ( string or false, Title ) */ function fetchTemplateAndTitle($title) { $templateCb = $this->mOptions->getTemplateCallback(); # Defaults to Parser::statelessFetchTemplate() $stuff = call_user_func($templateCb, $title, $this); $text = $stuff['text']; $finalTitle = isset($stuff['finalTitle']) ? $stuff['finalTitle'] : $title; if (isset($stuff['deps'])) { foreach ($stuff['deps'] as $dep) { $this->mOutput->addTemplate($dep['title'], $dep['page_id'], $dep['rev_id']); } } return array($text, $finalTitle); }
/** * Helper function for fillParserOutput; the bulk of the actual content * @param $title Title * @param $options ParserOptions * @param &$output ParserOutput * @return string */ protected function getParsedContent(Title $title, ParserOptions $options, ParserOutput &$output) { global $wgParser; $lang = $options->getTargetLanguage(); if (!$lang) { $lang = $title->getPageLanguage(); } $html = ''; foreach ($this->getContent() as $item) { if (!isset($item['title']) || $item['title'] == '') { continue; } $spTitle = Title::newFromText($item['title']); $spRev = Revision::newFromTitle($spTitle); // open element and do header $html .= $this->makeHeader($title, $item); if (isset($spRev)) { // DO CONTENT FROM PAGE $spContent = $spRev->getContent(); $spContentModel = $spRev->getContentModel(); if ($spContentModel == 'CollaborationHubContent') { // this is dumb, but we'll just rebuild the intro here for now $text = Html::rawElement('div', ['class' => 'mw-ck-hub-image'], $spContent->getParsedImage($spContent->getImage(), 100)); $text .= $spContent->getParsedIntroduction($spTitle, $options); } elseif ($spContentModel == 'CollaborationListContent') { // convert to wikitext with maxItems limit in place $wikitext = $spContent->convertToWikitext($lang, ['includeDesc' => false, 'maxItems' => 4, 'defaultSort' => 'random']); $text = $wgParser->parse($wikitext, $title, $options)->getText(); } elseif ($spContentModel == 'wikitext') { // to grab first section only $spContent = $spContent->getSection(0); // Do template preproccessing magic // ... parse, get text into $text $rawText = $spContent->serialize(); // Get rid of all <noinclude>'s. $wgParser->startExternalParse($title, $options, Parser::OT_WIKI); $frame = $wgParser->getPreprocessor()->newFrame()->newChild([], $spTitle); $node = $wgParser->preprocessToDom($rawText, Parser::PTD_FOR_INCLUSION); $processedText = $frame->expand($node, PPFrame::RECOVER_ORIG & ~PPFrame::NO_IGNORE); $parsedWikitext = $wgParser->parse($processedText, $title, $options); $text = $parsedWikitext->getText(); $output->addModuleStyles($parsedWikitext->getModuleStyles()); } else { // Parse whatever (else) as whatever $contentOutput = $spContent->getParserOutput($spTitle, $spRev, $options); $output->addModuleStyles($contentOutput->getModuleStyles()); $text = $contentOutput->getRawText(); } $html .= $text; // register as template for stuff $output->addTemplate($spTitle, $spTitle->getArticleId(), $spRev->getId()); } else { // DO CONTENT FOR NOT YET MADE PAGE $html .= Html::rawElement('p', ['class' => 'mw-ck-hub-missingfeature-note'], wfMessage('collaborationkit-hub-missingpage-note')->inContentLanguage()->parse()); $html .= new OOUI\ButtonWidget(['label' => wfMessage('collaborationkit-hub-missingpage-create')->inContentLanguage()->text(), 'href' => SpecialPage::getTitleFor('CreateHubFeature')->getFullUrl(['collaborationhub' => $title->getFullText(), 'feature' => $spTitle->getSubpageText()])]); // register as template for stuff $output->addTemplate($spTitle, $spTitle->getArticleId(), null); } $html .= Html::closeElement('div'); } return $html; }