Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/219442
Change subject: MediaWiki.php: Factor out tryNormaliseRedirect
......................................................................
MediaWiki.php: Factor out tryNormaliseRedirect
This is in preparation for fixing T67402, which requires adding
logic inside this condition block. However the added code still
influences whether or not a redirect will be made.
In case a redirect is not made, it has to fall through to the next
'elseif' handler in MediaWiki::performRequest(), which is not possible
from inside the 'if' block.
Hence, move it out in a separate block and use a boolean return value
to communicate whether the case has been handled.
This leaves an awkward empty block.
Change-Id: If3157f2ff1fd3ab2ca20a5d1f550d864ea62c493
---
M includes/MediaWiki.php
1 file changed, 70 insertions(+), 39 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/42/219442/1
diff --git a/includes/MediaWiki.php b/includes/MediaWiki.php
index 7a0d7b7..5e3836f 100644
--- a/includes/MediaWiki.php
+++ b/includes/MediaWiki.php
@@ -240,45 +240,7 @@
throw new BadTitleError();
}
// Redirect loops, no title in URL, $wgUsePathInfo URLs, and
URLs with a variant
- } elseif ( $request->getVal( 'action', 'view' ) == 'view' &&
!$request->wasPosted()
- && ( $request->getVal( 'title' ) === null
- || $title->getPrefixedDBkey() !=
$request->getVal( 'title' ) )
- && !count( $request->getValueNames( array( 'action',
'title' ) ) )
- && Hooks::run( 'TestCanonicalRedirect', array(
$request, $title, $output ) )
- ) {
- if ( $title->isSpecialPage() ) {
- list( $name, $subpage ) =
SpecialPageFactory::resolveAlias( $title->getDBkey() );
- if ( $name ) {
- $title = SpecialPage::getTitleFor(
$name, $subpage );
- }
- }
- $targetUrl = wfExpandUrl( $title->getFullURL(),
PROTO_CURRENT );
- // Redirect to canonical url, make it a 301 to allow
caching
- if ( $targetUrl == $request->getFullRequestURL() ) {
- $message = "Redirect loop detected!\n\n" .
- "This means the wiki got confused about
what page was " .
- "requested; this sometimes happens when
moving a wiki " .
- "to a new server or changing the server
configuration.\n\n";
-
- if ( $this->config->get( 'UsePathInfo' ) ) {
- $message .= "The wiki is trying to
interpret the page " .
- "title from the URL path
portion (PATH_INFO), which " .
- "sometimes fails depending on
the web server. Try " .
- "setting \"\$wgUsePathInfo =
false;\" in your " .
- "LocalSettings.php, or check
that \$wgArticlePath " .
- "is correct.";
- } else {
- $message .= "Your web server was
detected as possibly not " .
- "supporting URL path components
(PATH_INFO) correctly; " .
- "check your LocalSettings.php
for a customized " .
- "\$wgArticlePath setting and/or
toggle \$wgUsePathInfo " .
- "to true.";
- }
- throw new HttpError( 500, $message );
- } else {
- $output->setSquidMaxage( 1200 );
- $output->redirect( $targetUrl, '301' );
- }
+ } elseif ( $this->tryNormaliseRedirect( $title ) ) {
// Special pages
} elseif ( NS_SPECIAL == $title->getNamespace() ) {
// Actions that need to be made when we have a special
pages
@@ -299,6 +261,75 @@
}
/**
+ * Handle redirects for uncanonical title requests.
+ *
+ * Handles:
+ * - Redirect loops.
+ * - No title in URL.
+ * - $wgUsePathInfo URLs.
+ * - URLs with a variant.
+ * - Other non-standard URLs (as long as they have no extra query
parameters).
+ *
+ * Behaviour:
+ * - Normalise title values:
+ * /wiki/Foo%20Bar -> /wiki/Foo_Bar
+ * - Normalise empty title:
+ * /wiki/ -> /wiki/Main
+ * /w/index.php?title= -> /wiki/Main
+ * - Don't redirect anything with query parameters other than 'title'
or 'action=view'.
+ *
+ * @return bool True if a redirect was set.
+ */
+ private function tryNormaliseRedirect( $title ) {
+ $request = $this->context->getRequest();
+ $output = $this->context->getOutput();
+
+ if ( $request->getVal( 'action', 'view' ) != 'view'
+ || $request->wasPosted()
+ || ( $request->getVal( 'title' ) !== null
+ && $title->getPrefixedDBkey() ==
$request->getVal( 'title' ) )
+ || count( $request->getValueNames( array( 'action',
'title' ) ) )
+ || !Hooks::run( 'TestCanonicalRedirect', array(
$request, $title, $output ) )
+ ) {
+ return false;
+ }
+
+ if ( $title->isSpecialPage() ) {
+ list( $name, $subpage ) =
SpecialPageFactory::resolveAlias( $title->getDBkey() );
+ if ( $name ) {
+ $title = SpecialPage::getTitleFor( $name,
$subpage );
+ }
+ }
+ // Redirect to canonical url, make it a 301 to allow caching
+ $targetUrl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
+ if ( $targetUrl == $request->getFullRequestURL() ) {
+ $message = "Redirect loop detected!\n\n" .
+ "This means the wiki got confused about what
page was " .
+ "requested; this sometimes happens when moving
a wiki " .
+ "to a new server or changing the server
configuration.\n\n";
+
+ if ( $this->config->get( 'UsePathInfo' ) ) {
+ $message .= "The wiki is trying to interpret
the page " .
+ "title from the URL path portion
(PATH_INFO), which " .
+ "sometimes fails depending on the web
server. Try " .
+ "setting \"\$wgUsePathInfo = false;\"
in your " .
+ "LocalSettings.php, or check that
\$wgArticlePath " .
+ "is correct.";
+ } else {
+ $message .= "Your web server was detected as
possibly not " .
+ "supporting URL path components
(PATH_INFO) correctly; " .
+ "check your LocalSettings.php for a
customized " .
+ "\$wgArticlePath setting and/or toggle
\$wgUsePathInfo " .
+ "to true.";
+ }
+ throw new HttpError( 500, $message );
+ }
+ $output->setSquidMaxage( 1200 );
+ $output->redirect( $targetUrl, '301' );
+ return true;
+ }
+
+ /**
* Initialize the main Article object for "standard" actions (view, etc)
* Create an Article object for the page, following redirects if needed.
*
--
To view, visit https://gerrit.wikimedia.org/r/219442
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: If3157f2ff1fd3ab2ca20a5d1f550d864ea62c493
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits