From fe91f533717fc1239bf399d3c25626040d31ab99 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sun, 7 Feb 2010 14:50:55 +0000 Subject: [PATCH] Fix bug 20339 allow pipe-trick in log reasons follows on from r62069 moving logic from Parser into Linker (copying brion's technique for dealing with subpages) --- RELEASE-NOTES | 1 + includes/Linker.php | 78 +++++++++++++++++++++++++++++++++++++ includes/parser/Parser.php | 65 +++++-------------------------- maintenance/parserTests.txt | 12 ++++++ 4 files changed, 100 insertions(+), 56 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 68916aa841..59c611f273 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -835,6 +835,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 845) [[#foo|]], [[/bar|]] should be equivalent to [[#foo|foo]], [[/bar|bar]] (new use of "pipe trick") * (bug 21660) Support full-width commas for pipe trick * (bug 7264) Magic word to give Page Title as if pipe-trick performed on it {{pipetrick:}} +* (bug 20339) Allow using the pipe trick in log reasons === Languages updated in 1.16 === diff --git a/includes/Linker.php b/includes/Linker.php index 7e78f1efd0..be0d1bbe72 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -1086,6 +1086,11 @@ class Linker { # Handle link renaming [[foo|text]] will show link as "text" if( $match[3] != "" ) { $text = $match[3]; + if( $match[1] === "" && $this->commentContextTitle ) { + $match[1] = Linker::getPipeTrickLink( $text, $this->commentContextTitle ); + } + } elseif( $match[2] == "|" ) { + $text = Linker::getPipeTrickText( $match[1] ); } else { $text = $match[1]; } @@ -1205,6 +1210,79 @@ class Linker { return $ret; } + /** + * Returns valid title characters and namespace characters for pipe trick. + * + * FIXME: the namespace characters should not be specified like this... + */ + static function getPipeTrickCharacterClasses() { + global $wgLegalTitleChars; + return array( "[$wgLegalTitleChars]", '[ _0-9A-Za-z\x80-\xff-]' ); + } + + /** + * From the [[title|]] return link-text as though the used typed [[title|link-text]] + * + * For most links this be as though the user typed [[ns:title|title]] + * However [[ns:title (context)|]], [[ns:title, context|]] and [[ns:title (context), context|]] + * [[#title (context)|]] [[../context/title (context), context|]] + * all return the |title]] with no context or indicative punctuation. + * + * @param string $link from [[$link|]] + * @return string $text for [[$link|$text]] + */ + static function getPipeTrickText( $link ) { + static $rexps = FALSE; + if( !$rexps ) { + list( $tc, $nc ) = Linker::getPipeTrickCharacterClasses(); + $rexps = array ( + # try this first, to turn "[[A, B (C)|]]" into "A, B" + "/^(:?$nc+:|[:#\/]|$tc+\\/|)($tc+?)( \\($tc+\\)| ($tc+))$/", # [[ns:page (context)|]] + "/^(:?$nc+:|[:#\/]|$tc+\\/|)($tc+?)( \\($tc+\\)| ($tc+)|)((?:,|,) $tc+|)$/", # [[ns:page (context), context|]] + ); + } + $text = urldecode( $link ); + + for( $i = 0; $i < count( $rexps ); $i++) { + if( preg_match( $rexps[$i], $text, $m ) ) + return $m[2]; + } + return $text; + } + + /** + * From the [[|link-text]] return the title as though the user typed [[title|link-text]] + * + * On most pages this will return link-text or "" if the link-text is not a valid title + * On pages like [[ns:title (context)]] and [[ns:title, context]] it will act like + * [[ns:link-text (context)|link-text]] and [[ns:link-text, context|link-text]] + * + * @param string $text from [[|$text]] + * @param Title $title to resolve the link against + * @return string $link for [[$link|$text]] + */ + static function getPipeTrickLink( $text, $title ) { + static $rexps = FALSE, $tc; + if( !$rexps ) { + list( $tc, $nc ) = Linker::getPipeTrickCharacterClasses(); + $rexps = array ( + "/^($nc+:|)$tc+?( \\($tc+\\)| ($tc+))$/", # [[ns:page (context)|]] + "/^($nc+:|)$tc+?(?:(?: \\($tc+\\)| ($tc+)|)((?:,|,) $tc+|))$/" # [[ns:page (context), context|]] + ); + } + + if( !preg_match( "/^$tc+$/", $text ) ) + return ''; + + $t = $title->getText(); + + for( $i = 0; $i < count( $rexps ); $i++) { + if( preg_match( $rexps[$i], $t, $m ) ) + return "$m[1]$text$m[2]"; + } + return $text; + } + /** * Wrap a comment in standard punctuation and formatting if * it's non-empty, otherwise return empty string. diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 02e05c8806..f57c7f043d 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -1912,70 +1912,23 @@ class Parser return Linker::normalizeSubpageLink( $this->mTitle, $target, $text ); } - /** - * Returns valid title characters and namespace characters for pipe trick. - * - * FIXME: the namespace characters should not be specified like this... - */ - static function getPipeTrickCharacterClasses() { - global $wgLegalTitleChars; - return array( "[$wgLegalTitleChars]", '[ _0-9A-Za-z\x80-\xff-]' ); - } - /** * From the [[title|]] return link-text as though the used typed [[title|link-text]] - * - * For most links this be as though the user typed [[ns:title|title]] - * However [[ns:title (context)|]], [[ns:title, context|]] and [[ns:title (context), context|]] - * [[#title (context)|]] [[../context/title (context), context|]] - * all return the |title]] with no context or indicative punctuation. + * @param string $link from [[$link|]] + * @return string $text for [[$link|$text]] */ function getPipeTrickText( $link ) { - static $rexps = FALSE; - if( !$rexps ) { - list( $tc, $nc ) = Parser::getPipeTrickCharacterClasses(); - $rexps = array ( - # try this first, to turn "[[A, B (C)|]]" into "A, B" - "/^(:?$nc+:|[:#\/]|$tc+\\/|)($tc+?)( \\($tc+\\)| ($tc+))$/", # [[ns:page (context)|]] - "/^(:?$nc+:|[:#\/]|$tc+\\/|)($tc+?)( \\($tc+\\)| ($tc+)|)((?:,|,) $tc+|)$/", # [[ns:page (context), context|]] - ); - } - $text = urldecode( $link ); - - for( $i = 0; $i < count( $rexps ); $i++) { - if( preg_match( $rexps[$i], $text, $m ) ) - return $m[2]; - } - return $text; + return Linker::getPipeTrickText( $link ); } /** * From the [[|link-text]] return the title as though the user typed [[title|link-text]] - * - * On most pages this will return link-text or "" if the link-text is not a valid title - * On pages like [[ns:title (context)]] and [[ns:title, context]] it will act like - * [[ns:link-text (context)|link-text]] and [[ns:link-text, context|link-text]] + * @param string $text from [[|$text]] + * @param Title $title to resolve the link against + * @return string $link for [[$link|$text]] */ function getPipeTrickLink( $text ) { - static $rexps = FALSE, $tc; - if( !$rexps ) { - list( $tc, $nc ) = Parser::getPipeTrickCharacterClasses(); - $rexps = array ( - "/^($nc+:|)$tc+?( \\($tc+\\)| ($tc+))$/", # [[ns:page (context)|]] - "/^($nc+:|)$tc+?(?:(?: \\($tc+\\)| ($tc+)|)((?:,|,) $tc+|))$/" # [[ns:page (context), context|]] - ); - } - - if( !preg_match( "/^$tc+$/", $text ) ) - return ''; - - $t = $this->mTitle->getText(); - - for( $i = 0; $i < count( $rexps ); $i++) { - if( preg_match( $rexps[$i], $t, $m ) ) - return "$m[1]$text$m[2]"; - } - return $text; + return Linker::getPipeTrickLink( $text, $this->mTitle ); } /**#@+ @@ -4073,8 +4026,8 @@ class Parser # Links of the form [[|]] or [[|]] perform pipe tricks # Note this only allows the # in the one position it works. - list( $tc, $nc ) = Parser::getPipeTrickCharacterClasses(); - $pipeTrickRe = "/\[\[(?:(\\|)($tc+)|(#?$tc+)\\|)\]\]/"; + global $wgLegalTitleChars; + $pipeTrickRe = "/\[\[(?:(\\|)([$wgLegalTitleChars]+)|(#?[$wgLegalTitleChars]+)\\|)\]\]/"; $text = preg_replace_callback( $pipeTrickRe, array( $this, 'pstPipeTrickCallback' ), $text ); # Trim trailing whitespace diff --git a/maintenance/parserTests.txt b/maintenance/parserTests.txt index 264e207c58..4bd8fae588 100644 --- a/maintenance/parserTests.txt +++ b/maintenance/parserTests.txt @@ -7713,6 +7713,17 @@ title=[[Main Page]] #section !! end +!! test +Edit comment with pipe trick +!! options +comment +title=[[Article (context)]] +!! input +[[Hello (World)|]] [[|Entry]] +!! result +Hello Entry +!! end + !!article MediaWiki:bad image list !!text @@ -7739,6 +7750,7 @@ Bar foo !! end + TODO: more images more tables -- 2.20.1