r103199 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103198‎ | r103199 | r103200 >
Date:17:38, 15 November 2011
Author:gicode
Status:ok (Comments)
Tags:
Comment:
Add wfAssembleUrl and unit tests. This is the next step towards fixing
bug 32168. This function is the inverse of wfParseUrl and is useful when you
need to modify part of a URL and have to put it back together.

Further, with the addition of this function, there is sufficient code in core
to create a proper URI class.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES-1.19 (modified) (history)
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfAssembleUrlTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/GlobalFunctions/wfAssembleUrlTest.php
@@ -0,0 +1,111 @@
 2+<?php
 3+/**
 4+ * Unit tests for wfAssembleUrl()
 5+ */
 6+
 7+class wfAssembleUrl extends MediaWikiTestCase {
 8+ /** @dataProvider provideURLParts */
 9+ public function testWfAssembleUrl( $parts, $output ) {
 10+ $partsDump = print_r( $parts, true );
 11+ $this->assertEquals(
 12+ $output,
 13+ wfAssembleUrl( $parts ),
 14+ "Testing $partsDump assembles to $output"
 15+ );
 16+ }
 17+
 18+ /**
 19+ * Provider of URL parts for testing wfAssembleUrl()
 20+ *
 21+ * @return array
 22+ */
 23+ public function provideURLParts() {
 24+ $schemes = array(
 25+ '' => array(),
 26+ '//' => array(
 27+ 'delimiter' => '//',
 28+ ),
 29+ 'http://' => array(
 30+ 'scheme' => 'http',
 31+ 'delimiter' => '://',
 32+ ),
 33+ );
 34+
 35+ $hosts = array(
 36+ '' => array(),
 37+ 'example.com' => array(
 38+ 'host' => 'example.com',
 39+ ),
 40+ 'example.com:123' => array(
 41+ 'host' => 'example.com',
 42+ 'port' => 123,
 43+ ),
 44+ 'id@example.com' => array(
 45+ 'user' => 'id',
 46+ 'host' => 'example.com',
 47+ ),
 48+ 'id@example.com:123' => array(
 49+ 'user' => 'id',
 50+ 'host' => 'example.com',
 51+ 'port' => 123,
 52+ ),
 53+ 'id:key@example.com' => array(
 54+ 'user' => 'id',
 55+ 'pass' => 'key',
 56+ 'host' => 'example.com',
 57+ ),
 58+ 'id:key@example.com:123' => array(
 59+ 'user' => 'id',
 60+ 'pass' => 'key',
 61+ 'host' => 'example.com',
 62+ 'port' => 123,
 63+ ),
 64+ );
 65+
 66+ $cases = array();
 67+ foreach ( $schemes as $scheme => $schemeParts ) {
 68+ foreach ( $hosts as $host => $hostParts ) {
 69+ foreach ( array( '', '/path' ) as $path ) {
 70+ foreach ( array( '', 'query' ) as $query ) {
 71+ foreach ( array( '', 'fragment' ) as $fragment ) {
 72+ $parts = array_merge(
 73+ $schemeParts,
 74+ $hostParts
 75+ );
 76+ $url = $scheme .
 77+ $host .
 78+ $path;
 79+
 80+ if ( $path ) {
 81+ $parts['path'] = $path;
 82+ }
 83+ if ( $query ) {
 84+ $parts['query'] = $query;
 85+ $url .= '?' . $query;
 86+ }
 87+ if( $fragment ) {
 88+ $parts['fragment'] = $fragment;
 89+ $url .= '#' . $fragment;
 90+ }
 91+
 92+
 93+ $cases[] = array(
 94+ $parts,
 95+ $url,
 96+ );
 97+ }
 98+ }
 99+ }
 100+ }
 101+ }
 102+
 103+ $complexURL = 'http://id:key@example.org:321' .
 104+ '/over/there?name=ferret&foo=bar#nose';
 105+ $cases[] = array(
 106+ wfParseUrl( $complexURL ),
 107+ $complexURL,
 108+ );
 109+
 110+ return $cases;
 111+ }
 112+}
Index: trunk/phase3/RELEASE-NOTES-1.19
@@ -135,6 +135,7 @@
136136 * (bug 32168) Add wfRemoveDotSegments for use in wfExpandUrl
137137 * (bug 32358) Do not display "No higher resolution available" for dimensionless
138138 files (like audio files)
 139+* (bug 32168) Add wfAssembleUrl for use in wfExpandUrl
139140
140141 === API changes in 1.19 ===
141142 * (bug 19838) siprop=interwikimap can now use the interwiki cache.
Index: trunk/phase3/includes/GlobalFunctions.php
@@ -488,6 +488,60 @@
489489 }
490490
491491 /**
 492+ * This function will reassemble a URL parsed with wfParseURL. This is useful
 493+ * if you need to edit part of a URL and put it back together.
 494+ *
 495+ * This is the basic structure used (brackets contain keys for $urlParts):
 496+ * [scheme][delimiter][user]:[pass]@[host]:[port][path]?[query]#[fragment]
 497+ *
 498+ * @todo Need to integrate this into wfExpandUrl (bug 32168)
 499+ *
 500+ * @param $urlParts Array URL parts, as output from wfParseUrl
 501+ * @return string URL assembled from its component parts
 502+ */
 503+function wfAssembleUrl( $urlParts ) {
 504+ $result = '';
 505+
 506+ if ( array_key_exists( 'delimiter', $urlParts ) ) {
 507+ if ( array_key_exists( 'scheme', $urlParts ) ) {
 508+ $result .= $urlParts['scheme'];
 509+ }
 510+
 511+ $result .= $urlParts['delimiter'];
 512+ }
 513+
 514+ if ( array_key_exists( 'host', $urlParts ) ) {
 515+ if ( array_key_exists( 'user', $urlParts ) ) {
 516+ $result .= $urlParts['user'];
 517+ if ( array_key_exists( 'pass', $urlParts ) ) {
 518+ $result .= ':' . $urlParts['pass'];
 519+ }
 520+ $result .= '@';
 521+ }
 522+
 523+ $result .= $urlParts['host'];
 524+
 525+ if ( array_key_exists( 'port', $urlParts ) ) {
 526+ $result .= ':' . $urlParts['port'];
 527+ }
 528+ }
 529+
 530+ if ( array_key_exists( 'path', $urlParts ) ) {
 531+ $result .= $urlParts['path'];
 532+ }
 533+
 534+ if ( array_key_exists( 'query', $urlParts ) ) {
 535+ $result .= '?' . $urlParts['query'];
 536+ }
 537+
 538+ if ( array_key_exists( 'fragment', $urlParts ) ) {
 539+ $result .= '#' . $urlParts['fragment'];
 540+ }
 541+
 542+ return $result;
 543+}
 544+
 545+/**
492546 * Remove all dot-segments in the provided URL path. For example,
493547 * '/a/./b/../c/' becomes '/a/c/'. For details on the algorithm, please see
494548 * RFC3986 section 5.2.4.

Follow-up revisions

RevisionCommit summaryAuthorDate
r103208Make wfExpandUrl use wfRemoveDotSegments on the resulting path. This finishes...gicode18:53, 15 November 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r102587Add wfRemoveDotSegments and unit tests. This is a sane step towards fixing...gicode22:44, 9 November 2011

Comments

#Comment by 😂 (talk | contribs)   18:03, 15 November 2011

Unless you're specifically wanting to allow null values, isset() is way faster than array_key_exists() here.

#Comment by GICodeWarrior (talk | contribs)   18:47, 15 November 2011

Ah, that works. I remembered isset() but without looking at the docs I thought "functions can't do that!". PHP and its language constructs... le sigh

Status & tagging log