From e59f873bef67ef8531e2d19124b8019feca3b307 Mon Sep 17 00:00:00 2001 From: River Tarnell Date: Thu, 10 May 2007 19:13:02 +0000 Subject: [PATCH] - Http::request(), Http::post() - don't redirect page views on POST - Special:Export shouldn't do POST special case when page name provided - Special:Import should use POST for interwiki fetches --- includes/HttpFunctions.php | 20 ++++++++++++++++++-- includes/SpecialExport.php | 2 +- includes/SpecialImport.php | 7 ++++--- includes/Wiki.php | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php index a9fb13cae9..e14484eba1 100644 --- a/includes/HttpFunctions.php +++ b/includes/HttpFunctions.php @@ -4,12 +4,20 @@ * Various HTTP related functions */ class Http { + static function get( $url, $timeout = 'default' ) { + return request( "GET", $url, $timeout ); + } + + static function post( $url, $timeout = 'default' ) { + return request( "POST", $url, $timeout ); + } + /** * Get the contents of a file by HTTP * * if $timeout is 'default', $wgHTTPTimeout is used */ - static function get( $url, $timeout = 'default' ) { + static function request( $method, $url, $timeout = 'default' ) { global $wgHTTPTimeout, $wgHTTPProxy, $wgVersion, $wgTitle; # Use curl if available @@ -26,6 +34,10 @@ class Http { } curl_setopt( $c, CURLOPT_TIMEOUT, $timeout ); curl_setopt( $c, CURLOPT_USERAGENT, "MediaWiki/$wgVersion" ); + if ( $method == 'POST' ) + curl_setopt( $c, CURLOPT_POST, true ); + else + curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $method ); # Set the referer to $wgTitle, even in command-line mode # This is useful for interwiki transclusion, where the foreign @@ -49,8 +61,12 @@ class Http { } else { # Otherwise use file_get_contents, or its compatibility function from GlobalFunctions.php # This may take 3 minutes to time out, and doesn't have local fetch capabilities + + $opts = array('http' => array( 'method' => $method ) ); + $ctx = stream_context_create($opts); + $url_fopen = ini_set( 'allow_url_fopen', 1 ); - $text = file_get_contents( $url ); + $text = file_get_contents( $url, false, $ctx ); ini_set( 'allow_url_fopen', $url_fopen ); } return $text; diff --git a/includes/SpecialExport.php b/includes/SpecialExport.php index a597fdd07d..681e7f9789 100644 --- a/includes/SpecialExport.php +++ b/includes/SpecialExport.php @@ -71,7 +71,7 @@ function wfSpecialExport( $page = '' ) { } } } - else if( $wgRequest->wasPosted() ) { + else if( $wgRequest->wasPosted() && $page == '' ) { $page = $wgRequest->getText( 'pages' ); $curonly = $wgRequest->getCheck( 'curonly' ); $rawOffset = $wgRequest->getVal( 'offset' ); diff --git a/includes/SpecialImport.php b/includes/SpecialImport.php index 10e734ca7a..f03faefe88 100644 --- a/includes/SpecialImport.php +++ b/includes/SpecialImport.php @@ -857,13 +857,13 @@ class ImportStreamSource { } } - function newFromURL( $url ) { + function newFromURL( $url, $method = 'GET' ) { wfDebug( __METHOD__ . ": opening $url\n" ); # Use the standard HTTP fetch function; it times out # quicker and sorts out user-agent problems which might # otherwise prevent importing from large sites, such # as the Wikimedia cluster, etc. - $data = Http::get( $url ); + $data = Http::request( $method, $url ); if( $data !== false ) { $file = tmpfile(); fwrite( $file, $data ); @@ -882,7 +882,8 @@ class ImportStreamSource { } else { $params = $history ? 'history=1' : ''; $url = $link->getFullUrl( $params ); - return ImportStreamSource::newFromURL( $url ); + # For interwikis, use POST to avoid redirects. + return ImportStreamSource::newFromURL( $url, "POST" ); } } } diff --git a/includes/Wiki.php b/includes/Wiki.php index 612e58eebc..601a7227d3 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -158,7 +158,7 @@ class MediaWiki { $title = SpecialPage::getTitleFor( 'Badtitle' ); throw new ErrorPageError( 'badtitle', 'badtitletext' ); } - } else if ( ( $action == 'view' ) && + } else if ( ( $action == 'view' ) && !$wgRequest->wasPosted() && (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) && !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) ) { -- 2.20.1