Merge "Remove all hacks for old HTML in HTTP cache or parser cache"

This commit is contained in:
jenkins-bot 2022-10-12 14:20:52 +00:00 committed by Gerrit Code Review
commit f6e271d781
3 changed files with 21 additions and 76 deletions

View file

@ -333,20 +333,13 @@ class CommentFormatter {
*/
public static function removeInteractiveTools( string $text ) {
$text = strtr( $text, [
'<!--__DTREPLY__-->' => '',
'<!--__DTREPLYBRACKETOPEN__-->' => '',
'<!--__DTREPLYBRACKETCLOSE__-->' => '',
'<!--__DTREPLYBUTTONSCONTENT__-->' => '',
'<!--__DTELLIPSISBUTTON__-->' => '',
'<!--__DTEMPTYTALKPAGE__-->' => '',
] );
$text = preg_replace( '/<!--__DTSUBSCRIBE__(.*?)-->/', '', $text );
$text = preg_replace( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', '', $text );
// (DESKTOP|MOBILE)? can be made unconditional once the un-suffixed buttons have cleared from the cache
$text = preg_replace( '/<!--__DTSUBSCRIBEBUTTON(DESKTOP|MOBILE)?__(.*?)-->/', '', $text );
// To be removed once the old version has cleared from the cache
$text = preg_replace( '/<!--__DTREPLYBUTTONS__(.*?)-->/', '', $text );
$text = preg_replace( '/<!--__DTSUBSCRIBEBUTTON(DESKTOP|MOBILE)__(.*?)-->/', '', $text );
return $text;
}
@ -367,23 +360,14 @@ class CommentFormatter {
$doc = DOMCompat::newDocument( true );
$matches = [];
preg_match_all( '/<!--__DTSUBSCRIBE(LINK)?__(.*?)-->/', $text, $matches );
preg_match_all( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', $text, $matches );
$itemNames = array_map(
static function ( string $itemName, string $link ): string {
return $link ? htmlspecialchars_decode( $itemName ) : $itemName;
static function ( string $itemName ): string {
return htmlspecialchars_decode( $itemName );
},
$matches[2], $matches[1]
$matches[1]
);
// TODO: Remove (LINK)? from regex once parser cache has expired (a few weeks):
// preg_match_all( '/<!--__DTSUBSCRIBELINK__(.*?)-->/', $text, $matches );
// $itemNames = array_map(
// static function ( string $itemName ): string {
// return htmlspecialchars_decode( $itemName );
// },
// $matches[1]
// );
$items = $subscriptionStore->getSubscriptionItemsForUser(
$user,
$itemNames
@ -394,10 +378,9 @@ class CommentFormatter {
}
$text = preg_replace_callback(
'/<!--__DTSUBSCRIBE(LINK)?__(.*?)-->/',
'/<!--__DTSUBSCRIBELINK__(.*?)-->/',
static function ( $matches ) use ( $doc, $itemsByName, $lang ) {
// TODO: Remove (LINK)? from regex
$itemName = $matches[1] ? htmlspecialchars_decode( $matches[2] ) : $matches[2];
$itemName = htmlspecialchars_decode( $matches[1] );
$isSubscribed = isset( $itemsByName[ $itemName ] ) && !$itemsByName[ $itemName ]->isMuted();
$subscribedState = isset( $itemsByName[ $itemName ] ) ? $itemsByName[ $itemName ]->getState() : null;
@ -445,14 +428,11 @@ class CommentFormatter {
);
$text = preg_replace_callback(
// (DESKTOP|MOBILE)? can be made unconditional once the un-suffixed buttons have cleared from the cache
'/<!--__DTSUBSCRIBEBUTTON(DESKTOP|MOBILE)?__(.*?)-->/',
'/<!--__DTSUBSCRIBEBUTTON(DESKTOP|MOBILE)__(.*?)-->/',
static function ( $matches ) use ( $doc, $itemsByName, $lang, $isMobile ) {
if ( $matches[1] ) {
$buttonIsMobile = $matches[1] === 'MOBILE';
if ( $buttonIsMobile !== $isMobile ) {
return '';
}
$buttonIsMobile = $matches[1] === 'MOBILE';
if ( $buttonIsMobile !== $isMobile ) {
return '';
}
$itemName = htmlspecialchars_decode( $matches[2] );
$isSubscribed = isset( $itemsByName[ $itemName ] ) && !$itemsByName[ $itemName ]->isMuted();
@ -501,17 +481,10 @@ class CommentFormatter {
$replyLinkText = wfMessage( 'discussiontools-replylink' )->inLanguage( $lang )->escaped();
$replyButtonText = wfMessage( 'discussiontools-replybutton' )->inLanguage( $lang )->escaped();
// Remove __DTREPLYBUTTONS__ once it has cleared from the cache
$text = preg_replace_callback(
'/<!--__DTREPLYBUTTONS__(.*?)__-->|<!--__DTREPLYBUTTONSCONTENT__-->/',
'/<!--__DTREPLYBUTTONSCONTENT__-->/',
static function ( $matches ) use ( $doc, $replyLinkText, $replyButtonText, $isMobile ) {
$itemJSON = $matches[1] ?? null;
$replyLinkButtons = $doc->createElement( 'span' );
if ( $itemJSON ) {
$replyLinkButtons->setAttribute( 'class', 'ext-discussiontools-init-replylink-buttons' );
$replyLinkButtons->setAttribute( 'data-mw-comment', $itemJSON );
}
// Reply
$replyLink = $doc->createElement( 'a' );
@ -544,20 +517,11 @@ class CommentFormatter {
$replyLinkButtons->appendChild( $replyLink );
$replyLinkButtons->appendChild( $bracketClose );
return $itemJSON ?
DOMCompat::getOuterHTML( $replyLinkButtons ) :
DOMCompat::getInnerHTML( $replyLinkButtons );
return DOMCompat::getInnerHTML( $replyLinkButtons );
},
$text
);
// Old style replacements for content still in parser cache
$text = strtr( $text, [
'<!--__DTREPLY__-->' => $replyLinkText,
'<!--__DTREPLYBRACKETOPEN__-->' => '[',
'<!--__DTREPLYBRACKETCLOSE__-->' => ']',
] );
return $text;
}

View file

@ -4,16 +4,6 @@ var
utils = require( './utils.js' );
var featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {};
// Handle cached HTML: don't fail if the button is missing (TODO remove this)
var dummy = new OO.ui.ButtonWidget();
function infuseOrDummy( $node ) {
if ( featuresEnabled.visualenhancements_reply && $node.length ) {
return OO.ui.infuse( $node );
}
return dummy;
}
function ReplyLinksController( $pageContainer ) {
var controller = this;
@ -27,19 +17,11 @@ function ReplyLinksController( $pageContainer ) {
this.onAddSectionLinkClickHandler = this.onAddSectionLinkClick.bind( this );
this.onAnyLinkClickHandler = this.onAnyLinkClick.bind( this );
// Handle cached HTML: move the attribute (TODO remove this)
$pageContainer.find( '.ext-discussiontools-init-replylink-reply[data-mw-comment]' ).each( function () {
var $oldLink = $( this );
var $oldSet = $oldLink.closest( '.ext-discussiontools-init-replylink-buttons' );
$oldSet.attr( 'data-mw-comment', $oldLink.attr( 'data-mw-comment' ) );
$oldLink.removeAttr( 'data-mw-comment' );
} );
// Reply links
this.$replyLinkSets = $pageContainer.find( '.ext-discussiontools-init-replylink-buttons[data-mw-comment]' );
this.$replyLinkSets.each( function () {
var replyButton = infuseOrDummy( $( this ).find( '.ext-discussiontools-init-replybutton' ) );
var replyButton = OO.ui.infuse( $( this ).find( '.ext-discussiontools-init-replybutton' ) );
var $replyLink = $( this ).find( '.ext-discussiontools-init-replylink-reply' );
$replyLink.on( 'click keypress', controller.onReplyLinkClickHandler );
replyButton.on( 'click', controller.onReplyButtonClickHandler, [ replyButton ] );
@ -188,7 +170,7 @@ ReplyLinksController.prototype.isActivationEvent = function ( e ) {
ReplyLinksController.prototype.focusLink = function ( $linkSet ) {
if ( $linkSet.is( this.$replyLinkSets ) ) {
// Focus whichever is visible, the link or the button
infuseOrDummy( $linkSet.find( '.ext-discussiontools-init-replybutton' ) ).focus();
OO.ui.infuse( $linkSet.find( '.ext-discussiontools-init-replybutton' ) ).focus();
$linkSet.find( '.ext-discussiontools-init-replylink-reply' ).trigger( 'focus' );
}
};
@ -224,7 +206,7 @@ ReplyLinksController.prototype.setActiveLink = function ( $linkSet ) {
this.$pageContainer.addClass( 'ext-discussiontools-init-replylink-open' );
this.$replyLinkSets.each( function () {
var replyButton = infuseOrDummy( $( this ).find( '.ext-discussiontools-init-replybutton' ) );
var replyButton = OO.ui.infuse( $( this ).find( '.ext-discussiontools-init-replybutton' ) );
var $replyLink = $( this ).find( '.ext-discussiontools-init-replylink-reply' );
$replyLink.attr( 'tabindex', -1 );
if ( replyButton === activeButton ) {
@ -260,7 +242,7 @@ ReplyLinksController.prototype.clearActiveLink = function () {
this.$pageContainer.removeClass( 'ext-discussiontools-init-replylink-open' );
this.$replyLinkSets.each( function () {
var replyButton = infuseOrDummy( $( this ).find( '.ext-discussiontools-init-replybutton' ) );
var replyButton = OO.ui.infuse( $( this ).find( '.ext-discussiontools-init-replybutton' ) );
var $replyLink = $( this ).find( '.ext-discussiontools-init-replylink-reply' );
$replyLink.attr( 'tabindex', 0 );
if ( replyButton === activeButton ) {
@ -287,7 +269,7 @@ ReplyLinksController.prototype.teardown = function () {
}
this.$replyLinkSets.each( function () {
var replyButton = infuseOrDummy( $( this ).find( '.ext-discussiontools-init-replybutton' ) );
var replyButton = OO.ui.infuse( $( this ).find( '.ext-discussiontools-init-replybutton' ) );
var $replyLink = $( this ).find( '.ext-discussiontools-init-replylink-reply' );
$replyLink.off( 'click keypress', controller.onReplyLinkClickHandler );
replyButton.off( 'click', controller.onReplyButtonClickHandler );

View file

@ -304,10 +304,9 @@ h1, h2, h3, h4, h5, h6 {
// Visual enhancements enabled
// Reply sub-feature
.ext-discussiontools-visualenhancements_reply-enabled {
// Handle cached HTML: don't hide if the button is missing (TODO remove the -replybutton selector)
.ext-discussiontools-init-replybutton ~ .ext-discussiontools-init-replylink-bracket,
.ext-discussiontools-init-replybutton ~ .ext-discussiontools-init-replylink-reply,
.ext-discussiontools-init-replybutton ~ .ext-discussiontools-init-replylink-divider {
.ext-discussiontools-init-replylink-bracket,
.ext-discussiontools-init-replylink-reply,
.ext-discussiontools-init-replylink-divider {
display: none;
}