From a88a955ebfa5f626a3e52afc85bc036ee5dd465d Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 3 Mar 2010 02:41:14 +0000 Subject: [PATCH] Merge fixes to ?preload= from /branches/conrad/ (cf. bug 5210, r62864, r62035) --- RELEASE-NOTES | 1 + includes/Defines.php | 1 + includes/EditPage.php | 43 ++++++++++++++++++++++--------------- includes/parser/Parser.php | 30 ++++++++++++++++++++++---- maintenance/parserTests.inc | 4 +++- maintenance/parserTests.txt | 39 +++++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 22 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 06e0c01575..920e879bd2 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -30,6 +30,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 22606) The body of e-mail address confirmation message is now different when the address changed * (bug 22664) Special:Userrights now accepts '0' as a valid user name +* (bug 5210) preload parser now parses , and redirects == API changes in 1.17 == diff --git a/includes/Defines.php b/includes/Defines.php index 7be569af8d..5fa510c961 100644 --- a/includes/Defines.php +++ b/includes/Defines.php @@ -209,6 +209,7 @@ define( 'OT_HTML', 1 ); define( 'OT_WIKI', 2 ); define( 'OT_PREPROCESS', 3 ); define( 'OT_MSG' , 3 ); // b/c alias for OT_PREPROCESS +define( 'OT_PLAIN', 4 ); # Flags for Parser::setFunctionHook define( 'SFH_NO_HASH', 1 ); diff --git a/includes/EditPage.php b/includes/EditPage.php index e92b43bd6e..d25e0f2d8b 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -220,30 +220,39 @@ class EditPage { } /** - * Get the contents of a page from its title and remove includeonly tags + * Get the contents to be preloaded into the box, either set by + * an earlier setPreloadText() or by loading the given page. * - * @param $preload String: the title of the page. - * @return string The contents of the page. + * @param $preload String: representing the title to preload from. + * @return String */ protected function getPreloadedText( $preload ) { + global $wgUser, $wgParser; if ( !empty( $this->mPreloadText ) ) { return $this->mPreloadText; - } elseif ( $preload === '' ) { - return ''; - } else { - $preloadTitle = Title::newFromText( $preload ); - if ( isset( $preloadTitle ) && $preloadTitle->userCanRead() ) { - $rev = Revision::newFromTitle( $preloadTitle ); - if ( is_object( $rev ) ) { - $text = $rev->getText(); - // TODO FIXME: AAAAAAAAAAA, this shouldn't be implementing - // its own mini-parser! -ævar - $text = preg_replace( '~~', '', $text ); - return $text; - } else - return ''; + } elseif ( $preload !== '' ) { + $title = Title::newFromText( $preload ); + # Check for existence to avoid getting MediaWiki:Noarticletext + if ( isset( $title ) && $title->exists() && $title->userCanRead() ) { + $article = new Article( $title ); + + if ( $article->isRedirect() ) { + $title = Title::newFromRedirectRecurse( $article->getContent() ); + # Redirects to missing titles are displayed, to hidden pages are followed + # Copying observed behaviour from ?action=view + if ( $title->exists() ) { + if ($title->userCanRead() ) { + $article = new Article( $title ); + } else { + return ""; + } + } + } + $parserOptions = ParserOptions::newFromUser( $wgUser ); + return $wgParser->getPreloadText( $article->getContent(), $title, $parserOptions ); } } + return ''; } /* diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 06d4ac51a7..e2173fd02a 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -26,6 +26,8 @@ * Cleans a signature before saving it to preferences * extractSections() * Extracts sections from an article for section editing + * getPreloadText() + * Removes sections, and tags. * * Globals used: * objects: $wgLang, $wgContLang @@ -78,10 +80,11 @@ class Parser // Allowed values for $this->mOutputType // Parameter to startExternalParse(). - const OT_HTML = 1; - const OT_WIKI = 2; - const OT_PREPROCESS = 3; + const OT_HTML = 1; // like parse() + const OT_WIKI = 2; // like preSaveTransform() + const OT_PREPROCESS = 3; // like preprocess() const OT_MSG = 3; + const OT_PLAIN = 4; // like extractSections() - portions of the original are returned unchanged. // Marker Suffix needs to be accessible staticly. const MARKER_SUFFIX = "-QINU\x7f"; @@ -249,6 +252,7 @@ class Parser 'html' => $ot == self::OT_HTML, 'wiki' => $ot == self::OT_WIKI, 'pre' => $ot == self::OT_PREPROCESS, + 'plain' => $ot == self::OT_PLAIN, ); } @@ -488,6 +492,24 @@ class Parser return $text; } + /** + * Process the wikitext for the ?preload= feature. (bug 5210) + * + * , etc. are parsed as for template transclusion, + * comments, templates, arguments, tags hooks and parser functions are untouched. + */ + public function getPreloadText( $text, $title, $options ) { + // Parser (re)initialisation + $this->clearState(); + $this->setOutputType( self::OT_PLAIN ); + $this->mOptions = $options; + $this->setTitle( $title ); + + $flags = PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES; + $dom = $this->preprocessToDom( $text, self::PTD_FOR_INCLUSION ); + return $this->getPreprocessor()->newFrame()->expand( $dom, $flags ); + } + /** * Get a random string * @@ -4741,7 +4763,7 @@ class Parser $this->clearState(); $this->setTitle( $wgTitle ); // not generally used but removes an ugly failure mode $this->mOptions = new ParserOptions; - $this->setOutputType( self::OT_WIKI ); + $this->setOutputType( self::OT_PLAIN ); $outText = ''; $frame = $this->getPreprocessor()->newFrame(); diff --git a/maintenance/parserTests.inc b/maintenance/parserTests.inc index 6526da90ea..186bf95fe2 100644 --- a/maintenance/parserTests.inc +++ b/maintenance/parserTests.inc @@ -366,6 +366,8 @@ class ParserTest { } elseif( isset( $opts['comment'] ) ) { $linker = $user->getSkin(); $out = $linker->formatComment( $input, $title, $local ); + } elseif( isset( $opts['preload'] ) ) { + $out = $parser->getpreloadText( $input, $title, $options ); } else { $output = $parser->parse( $input, $title, $options, true, true, 1337 ); $out = $output->getText(); @@ -1716,4 +1718,4 @@ class TestFileIterator implements Iterator { } return false; } -} \ No newline at end of file +} diff --git a/maintenance/parserTests.txt b/maintenance/parserTests.txt index 860b589465..14ce80ee76 100644 --- a/maintenance/parserTests.txt +++ b/maintenance/parserTests.txt @@ -7731,7 +7731,46 @@ Screen

this is not the the title

!! end + +!! test +preload: check and +!! options +preload +!! input +Hello cruelkind world. +!! result +Hello kind world. +!! end + +!! test +preload: check +!! options +preload +!! input +Goodbye Hello world +!! result +Hello world +!! end +!! test +preload: can pass tags through if we want to +!! options +preload +!! input +<includeonly>Hello world</includeonly> +!! result +Hello world +!! end + +!! test +preload: check that it doesn't try to do tricks +!! options +preload +!! input +* ''{{world}}'' {{subst:How are you}}{{ {{{|safesubst:}}} #if:1|2|3}} +!! result +* ''{{world}}'' {{subst:How are you}}{{ {{{|safesubst:}}} #if:1|2|3}} +!! end TODO: more images -- 2.20.1