/** @var Content $pstContent */
private $pstContent = null;
+ /** @var bool */
+ private $contentIsDeleted = false, $contentIsSuppressed = false;
+
public function execute() {
// The data is hot but user-dependent, like page views, so we set vary cookies
$this->getMain()->setCacheMode( 'anon-public-user-private' );
$redirValues = null;
+ $needContent = isset( $prop['wikitext'] ) ||
+ isset( $prop['parsetree'] ) || $params['generatexml'];
+
// Return result
$result = $this->getResult();
$wgTitle = $titleObj;
$pageObj = WikiPage::factory( $titleObj );
list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
-
- // If for some reason the "oldid" is actually the current revision, it may be cached
- // Deliberately comparing $pageObj->getLatest() with $rev->getId(), rather than
- // checking $rev->isCurrent(), because $pageObj is what actually ends up being used,
- // and if its ->getLatest() is outdated, $rev->isCurrent() won't tell us that.
- if ( !$suppressCache && $rev->getId() == $pageObj->getLatest() ) {
- // May get from/save to parser cache
- $p_result = $this->getParsedContent( $pageObj, $popts,
- $pageid, isset( $prop['wikitext'] ) );
- } else { // This is an old revision, so get the text differently
- $this->content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
-
- if ( $this->section !== false ) {
- $this->content = $this->getSectionContent(
- $this->content, $this->msg( 'revid', $rev->getId() )
- );
- }
-
- // Should we save old revision parses to the parser cache?
- $p_result = $this->content->getParserOutput( $titleObj, $rev->getId(), $popts );
- }
+ $p_result = $this->getParsedContent(
+ $pageObj, $popts, $suppressCache, $pageid, $rev, $needContent
+ );
} else { // Not $oldid, but $pageid or $page
if ( $params['redirects'] ) {
$reqParams = [
}
list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
-
- // Don't pollute the parser cache when setting options that aren't
- // in ParserOptions::optionsHash()
- /// @todo: This should be handled closer to the actual cache instead of here, see T110269
- $suppressCache = $suppressCache ||
- $params['disablepp'] ||
- $params['disablelimitreport'] ||
- $params['preview'] ||
- $params['sectionpreview'] ||
- $params['disabletidy'];
-
- if ( $suppressCache ) {
- $this->content = $this->getContent( $pageObj, $pageid );
- $p_result = $this->content->getParserOutput( $titleObj, null, $popts );
- } else {
- // Potentially cached
- $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
- isset( $prop['wikitext'] ) );
- }
+ $p_result = $this->getParsedContent(
+ $pageObj, $popts, $suppressCache, $pageid, null, $needContent
+ );
}
} else { // Not $oldid, $pageid, $page. Hence based on $text
$titleObj = Title::newFromText( $title );
if ( $params['onlypst'] ) {
// Build a result and bail out
$result_array = [];
+ if ( $this->contentIsDeleted ) {
+ $result_array['textdeleted'] = true;
+ }
+ if ( $this->contentIsSuppressed ) {
+ $result_array['textsuppressed'] = true;
+ }
$result_array['text'] = $this->pstContent->serialize( $format );
$result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
if ( isset( $prop['wikitext'] ) ) {
$result_array['title'] = $titleObj->getPrefixedText();
$result_array['pageid'] = $pageid ?: $pageObj->getId();
+ if ( $this->contentIsDeleted ) {
+ $result_array['textdeleted'] = true;
+ }
+ if ( $this->contentIsSuppressed ) {
+ $result_array['textsuppressed'] = true;
+ }
if ( $params['disabletoc'] ) {
$p_result->setTOCEnabled( false );
}
$outputPage = null;
- if ( $skin || isset( $prop['headhtml'] ) ) {
- // Enabling the skin via 'useskin' or 'headhtml' gets OutputPage and
- // Skin involved, which (among others) applies these hooks:
+ if ( $skin || isset( $prop['headhtml'] ) || isset( $prop['categorieshtml'] ) ) {
+ // Enabling the skin via 'useskin', 'headhtml', or 'categorieshtml'
+ // gets OutputPage and Skin involved, which (among others) applies
+ // these hooks:
// - ParserOutputHooks
// - Hook: LanguageLinks
// - Hook: OutputPageParserOutput
+ // - Hook: OutputPageMakeCategoryLinks
$context = new DerivativeContext( $this->getContext() );
$context->setTitle( $titleObj );
$context->setWikiPage( $pageObj );
$context->setSkin( $skin );
// Context clones the skin, refetch to stay in sync. (T166022)
$skin = $context->getSkin();
+ } else {
+ // Make sure the context's skin refers to the context. Without this,
+ // $outputPage->getSkin()->getOutput() !== $outputPage which
+ // confuses some of the output.
+ $context->setSkin( $context->getSkin() );
}
$outputPage = new OutputPage( $context );
$result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
}
if ( isset( $prop['categorieshtml'] ) ) {
- $result_array['categorieshtml'] = $this->categoriesHtml( $p_result->getCategories() );
+ $result_array['categorieshtml'] = $outputPage->getSkin()->getCategories();
$result_array[ApiResult::META_BC_SUBELEMENTS][] = 'categorieshtml';
}
if ( isset( $prop['links'] ) ) {
Hooks::run( 'ApiMakeParserOptions',
[ $popts, $pageObj->getTitle(), $params, $this, &$reset, &$suppressCache ] );
+ // Force cache suppression when $popts aren't cacheable.
+ $suppressCache = $suppressCache || !$popts->isSafeToCache();
+
return [ $popts, $reset, $suppressCache ];
}
/**
* @param WikiPage $page
* @param ParserOptions $popts
+ * @param bool $suppressCache
* @param int $pageId
- * @param bool $getWikitext
+ * @param Revision|null $rev
+ * @param bool $getContent
* @return ParserOutput
*/
- private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
- $this->content = $this->getContent( $page, $pageId );
+ private function getParsedContent(
+ WikiPage $page, $popts, $suppressCache, $pageId, $rev, $getContent
+ ) {
+ $revId = $rev ? $rev->getId() : null;
+ $isDeleted = $rev && $rev->isDeleted( Revision::DELETED_TEXT );
+
+ if ( $getContent || $this->section !== false || $isDeleted ) {
+ if ( $rev ) {
+ $this->content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
+ if ( !$this->content ) {
+ $this->dieWithError( [ 'apierror-missingcontent-revid', $revId ] );
+ }
+ } else {
+ $this->content = $page->getContent( Revision::FOR_THIS_USER, $this->getUser() );
+ if ( !$this->content ) {
+ $this->dieWithError( [ 'apierror-missingcontent-pageid', $pageId ] );
+ }
+ }
+ $this->contentIsDeleted = $isDeleted;
+ $this->contentIsSuppressed = $rev &&
+ $rev->isDeleted( Revision::DELETED_TEXT | Revision::DELETED_RESTRICTED );
+ }
- if ( $this->section !== false && $this->content !== null ) {
- // Not cached (save or load)
- return $this->content->getParserOutput( $page->getTitle(), null, $popts );
+ if ( $this->section !== false ) {
+ $this->content = $this->getSectionContent(
+ $this->content,
+ $pageId === null ? $page->getTitle()->getPrefixedText() : $this->msg( 'pageid', $pageId )
+ );
+ return $this->content->getParserOutput( $page->getTitle(), $revId, $popts );
}
- // Try the parser cache first
- // getParserOutput will save to Parser cache if able
- $pout = $page->getParserOutput( $popts );
- if ( !$pout ) {
- $this->dieWithError( [ 'apierror-nosuchrevid', $page->getLatest() ] );
+ if ( $isDeleted ) {
+ // getParserOutput can't do revdeled revisions
+ $pout = $this->content->getParserOutput( $page->getTitle(), $revId, $popts );
+ } else {
+ // getParserOutput will save to Parser cache if able
+ $pout = $page->getParserOutput( $popts, $revId, $suppressCache );
}
- if ( $getWikitext ) {
- $this->content = $page->getContent( Revision::RAW );
+ if ( !$pout ) {
+ $this->dieWithError( [ 'apierror-nosuchrevid', $revId ?: $page->getLatest() ] );
}
return $pout;
}
- /**
- * Get the content for the given page and the requested section.
- *
- * @param WikiPage $page
- * @param int $pageId
- * @return Content
- */
- private function getContent( WikiPage $page, $pageId = null ) {
- $content = $page->getContent( Revision::RAW ); // XXX: really raw?
-
- if ( $this->section !== false && $content !== null ) {
- $content = $this->getSectionContent(
- $content,
- !is_null( $pageId )
- ? $this->msg( 'pageid', $pageId )
- : $page->getTitle()->getPrefixedText()
- );
- }
- return $content;
- }
-
/**
* Extract the requested section from the given Content
*
* @param Content $content
* @param string|Message $what Identifies the content in error messages, e.g. page title.
- * @return Content|bool
+ * @return Content
*/
private function getSectionContent( Content $content, $what ) {
// Not cached (save or load)
return $result;
}
- private function categoriesHtml( $categories ) {
- $context = $this->getContext();
- $context->getOutput()->addCategoryLinks( $categories );
-
- return $context->getSkin()->getCategories();
- }
-
private function formatLinks( $links ) {
$result = [];
foreach ( $links as $ns => $nslinks ) {