aboutsummaryrefslogtreecommitdiffstats
path: root/includes/Output
diff options
context:
space:
mode:
authorC. Scott Ananian <cscott@cscott.net>2022-02-04 18:48:03 -0500
committerC. Scott Ananian <cscott@cscott.net>2024-10-17 23:46:19 -0400
commit62d6c235395afe2a4a58e27341fc6e6124f5d190 (patch)
tree95dd3fb193b92ca95e98b250b6f8038f28e6d6f6 /includes/Output
parente0ef813778f854504f10b0f86fb2d58069135b39 (diff)
downloadmediawikicore-62d6c235395afe2a4a58e27341fc6e6124f5d190.tar.gz
mediawikicore-62d6c235395afe2a4a58e27341fc6e6124f5d190.zip
Use OutputPage::$metadata to store index policy
As a first step in a plan to reduce redundancy in OutputPage, replace the $mIndexPolicy property of OutputPage with the corresponding getter/setter methods in a ParserOutput held for the purpose of storing metadata. Bug: T301020 Change-Id: I6073f4033be936b669532ecf8104a8e5ff498e24
Diffstat (limited to 'includes/Output')
-rw-r--r--includes/Output/OutputPage.php56
1 files changed, 48 insertions, 8 deletions
diff --git a/includes/Output/OutputPage.php b/includes/Output/OutputPage.php
index 16af9cc10d2f..6bad42dde327 100644
--- a/includes/Output/OutputPage.php
+++ b/includes/Output/OutputPage.php
@@ -402,8 +402,6 @@ class OutputPage extends ContextSource {
protected $styles = [];
/** @var string */
- private $mIndexPolicy = 'index';
- /** @var string */
private $mFollowPolicy = 'follow';
/** @var array */
@@ -480,6 +478,16 @@ class OutputPage extends ContextSource {
private string $cspOutputMode = self::CSP_HEADERS;
/**
+ * To eliminate redundancy between information kept in OutputPage
+ * for non-article pages and metadata kept by the Parser for
+ * article pages, we create a ParserOutput for the OutputPage
+ * which will collect metadata such as categories, index policy,
+ * modules, etc, even if no parse actually occurs during the
+ * rendering of this page.
+ */
+ private ParserOutput $metadata;
+
+ /**
* @var array A cache of the names of the cookies that will influence the cache
*/
private static $cacheVaryCookies = null;
@@ -504,6 +512,7 @@ class OutputPage extends ContextSource {
$this->deprecatePublicProperty( 'mHideNewSectionLink', '1.38', __CLASS__ );
$this->deprecatePublicProperty( 'mNoGallery', '1.38', __CLASS__ );
$this->setContext( $context );
+ $this->metadata = new ParserOutput( null );
$this->CSP = new ContentSecurityPolicy(
$context->getRequest()->response(),
$context->getConfig(),
@@ -554,6 +563,21 @@ class OutputPage extends ContextSource {
}
/**
+ * Return a ParserOutput that can be used to set metadata properties
+ * for the current page.
+ * @return ParserOutput
+ * @internal
+ */
+ public function getMetadata(): ParserOutput {
+ // This is @internal at the moment, but in the future we may
+ // wish to make this public and deprecate the redundant
+ // methods on OutputPage which simply turn around
+ // and invoke the corresponding method on the metadata
+ // ParserOutput.
+ return $this->metadata;
+ }
+
+ /**
* Add a new "<meta>" tag
* To add an http-equiv meta tag, precede the name with "http:"
*
@@ -1008,7 +1032,8 @@ class OutputPage extends ContextSource {
* @return string
*/
public function getRobotPolicy() {
- return "{$this->mIndexPolicy},{$this->mFollowPolicy}";
+ $indexPolicy = $this->getIndexPolicy();
+ return "{$indexPolicy},{$this->mFollowPolicy}";
}
/**
@@ -1050,11 +1075,11 @@ class OutputPage extends ContextSource {
*/
private function getRobotsContent(): string {
$robotOptionString = $this->formatRobotsOptions();
- $robotArgs = ( $this->mIndexPolicy === 'index' &&
+ $robotArgs = ( $this->getIndexPolicy() === 'index' &&
$this->mFollowPolicy === 'follow' ) ?
[] :
[
- $this->mIndexPolicy,
+ $this->getIndexPolicy(),
$this->mFollowPolicy,
];
if ( $robotOptionString ) {
@@ -1067,13 +1092,22 @@ class OutputPage extends ContextSource {
* Set the index policy for the page, but leave the follow policy un-
* touched.
*
+ * Since 1.43, setting 'index' after 'noindex' is deprecated. In
+ * a future release, index policy on OutputPage will behave as
+ * it does in ParserOutput, where 'noindex' takes precedence.
+ *
* @param string $policy Either 'index' or 'noindex'.
*/
public function setIndexPolicy( $policy ) {
$policy = trim( $policy );
- if ( in_array( $policy, [ 'index', 'noindex' ] ) ) {
- $this->mIndexPolicy = $policy;
+ if ( $policy === 'index' && $this->metadata->getIndexPolicy() === 'noindex' ) {
+ wfDeprecated( __METHOD__ . ' with index after noindex', '1.43' );
+ // ParserOutput::setIndexPolicy has noindex take precedence
+ // (T16899) but the OutputPage version did not. Preserve
+ // the behavior but deprecate it for future removal.
+ $this->metadata->setOutputFlag( ParserOutputFlags::NO_INDEX_POLICY, false );
}
+ $this->metadata->setIndexPolicy( $policy );
}
/**
@@ -1082,7 +1116,13 @@ class OutputPage extends ContextSource {
* @return string
*/
public function getIndexPolicy() {
- return $this->mIndexPolicy;
+ // Unlike ParserOutput, in OutputPage getIndexPolicy() defaults to
+ // 'index' if unset.
+ $policy = $this->metadata->getIndexPolicy();
+ if ( $policy === '' ) {
+ $policy = 'index';
+ }
+ return $policy;
}
/**