From eb41544de5fa31c2ed70cadaaa9204e4ca23d8c5 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Mon, 21 Jun 2010 01:17:36 +0000 Subject: [PATCH] (bug 22784) Fix normalization of whitespace in autocomment links put this specific normalisation into Sanitizer::normalizeSectionNameWhitespace instead of repeating it three times. --- RELEASE-NOTES | 1 + includes/Linker.php | 10 +++------- includes/Sanitizer.php | 12 ++++++++++++ includes/parser/Parser.php | 5 ++--- maintenance/parserTests.txt | 11 +++++++++++ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 1310543a54..b8de16db5c 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -208,6 +208,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN Special:Upload after following a red link * Correct the escaping of the autosummary URI fragments. * (bug 23642) Recognize mime types of MS OpenXML documents. +* (bug 22784) Normalise underscores and spaces in autocomments. === API changes in 1.17 === * (bug 22738) Allow filtering by action type on query=logevent. diff --git a/includes/Linker.php b/includes/Linker.php index f6e45d1408..c7f7df9df6 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -1018,17 +1018,13 @@ class Linker { if ( $title ) { $section = $auto; - # Generate a valid anchor name from the section title. - # Hackish, but should generally work - we strip wiki - # syntax, including the magic [[: that is used to - # "link rather than show" in case of images and - # interlanguage links. + # Remove links that a user may have manually put in the autosummary + # This could be improved by copying as much of Parser::stripSectionName as desired. $section = str_replace( '[[:', '', $section ); $section = str_replace( '[[', '', $section ); $section = str_replace( ']]', '', $section ); - # Most of Title:: expects fragments to be escaped - $section = Title::escapeFragmentForURL( $section ); + $section = Sanitizer::normalizeSectionNameWhitespace( $section ); # bug 22784 if ( $local ) { $sectionTitle = Title::newFromText( '#' . $section ); } else { diff --git a/includes/Sanitizer.php b/includes/Sanitizer.php index 00837fdadf..2a3f8473cf 100644 --- a/includes/Sanitizer.php +++ b/includes/Sanitizer.php @@ -1166,6 +1166,18 @@ class Sanitizer { $text ); } + /** + * Normalizes whitespace in a section name, such as might be returned + * by Parser::stripSectionName(), for use in the id's that are used for + * section links. + * + * @param $section String + * @return String + */ + static function normalizeSectionNameWhitespace( $section ) { + return trim( preg_replace( '/[ _]+/', ' ', $section ) ); + } + /** * Ensure that any entities and character references are legal * for XML and XHTML specifically. Any stray bits will be diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 7b28c14519..2a681980ce 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -3859,8 +3859,7 @@ class Parser { # For the anchor, strip out HTML-y stuff period $safeHeadline = preg_replace( '/<.*?'.'>/', '', $safeHeadline ); - $safeHeadline = preg_replace( '/[ _]+/', ' ', $safeHeadline ); - $safeHeadline = trim( $safeHeadline ); + $safeHeadline = Sanitizer::normalizeSectionNameWhitespace( $safeHeadline ); # Save headline for section edit hint before it's escaped $headlineHint = $safeHeadline; @@ -5172,7 +5171,7 @@ class Parser { public function guessSectionNameFromWikiText( $text ) { # Strip out wikitext links(they break the anchor) $text = $this->stripSectionName( $text ); - $text = trim( preg_replace( '/[ _]+/', ' ', $text ) ); + $text = Sanitizer::normalizeSectionNameWhitespace( $text ); return '#' . Sanitizer::escapeId( $text, 'noninitial' ); } diff --git a/maintenance/parserTests.txt b/maintenance/parserTests.txt index e3612b179f..89a9587f94 100644 --- a/maintenance/parserTests.txt +++ b/maintenance/parserTests.txt @@ -7816,6 +7816,17 @@ title=[[Main Page]] #section !! end +!! test +Space normalisation on autocomment (bug 22784) +!! options +comment +title=[[Main Page]] +!!input +/* __hello__world__ */ +!! result +→__hello__world__ +!! end + !!article MediaWiki:bad image list !!text -- 2.20.1