r92024 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92023‎ | r92024 | r92025 >
Date:23:52, 12 July 2011
Author:catrope
Status:ok
Tags:needs-php-test 
Comment:
Extend wfParseUrl() to handle protocol-relative URLs. parse_url() doesn't handle these either so it needs more workaround logic
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -478,14 +478,21 @@
479479 * parse_url() work-alike, but non-broken. Differences:
480480 *
481481 * 1) Does not raise warnings on bad URLs (just returns false)
482 - * 2) Handles protocols that don't use :// (e.g., mailto: and news:) correctly
483 - * 3) Adds a "delimiter" element to the array, either '://' or ':' (see (2))
 482+ * 2) Handles protocols that don't use :// (e.g., mailto: and news: , as well as protocol-relative URLs) correctly
 483+ * 3) Adds a "delimiter" element to the array, either '://', ':' or '//' (see (2))
484484 *
485485 * @param $url String: a URL to parse
486486 * @return Array: bits of the URL in an associative array, per PHP docs
487487 */
488488 function wfParseUrl( $url ) {
489489 global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
 490+
 491+ // Protocol-relative URLs are handled really badly by parse_url(). It's so bad that the easiest
 492+ // way to handle them is to just prepend 'http:' and strip the protocol out later
 493+ $wasRelative = substr( $url, 0, 2 ) == '//';
 494+ if ( $wasRelative ) {
 495+ $url = "http:$url";
 496+ }
490497 wfSuppressWarnings();
491498 $bits = parse_url( $url );
492499 wfRestoreWarnings();
@@ -517,6 +524,12 @@
518525 $bits['path'] = '/' . $bits['path'];
519526 }
520527 }
 528+
 529+ // If the URL was protocol-relative, fix scheme and delimiter
 530+ if ( $wasRelative ) {
 531+ $bits['scheme'] = '';
 532+ $bits['delimiter'] = '//';
 533+ }
521534 return $bits;
522535 }
523536

Follow-up revisions

RevisionCommit summaryAuthorDate
r921721.17wmf1: MFT protocol-relative URL fixes: r91663, r92024, r92028, r92036, r9...catrope17:48, 14 July 2011

Status & tagging log