* (bug 8324) LinkSearch: search for all protocols defined in wgUrlProtocols incl...
authorRaimond Spekking <raymond@users.mediawiki.org>
Mon, 19 Mar 2007 16:19:13 +0000 (16:19 +0000)
committerRaimond Spekking <raymond@users.mediawiki.org>
Mon, 19 Mar 2007 16:19:13 +0000 (16:19 +0000)
Make LinkFilter and GlobalFunctions more flexible

RELEASE-NOTES
includes/GlobalFunctions.php
includes/LinkFilter.php

index 586a549..23a1ec0 100644 (file)
@@ -274,12 +274,11 @@ lighter making things easier to read.
 * (bug 5546) Watchlist reflects logged actions like move, protection, undelete
 * (bug 9019) No warning during upload if image description page exists, but no
   image
+* Support protocols other than HTTP in LinkFilter, use $wgUrlProtocols
 * (bug 8582) Allow thumbnailing when imagesize has a space.
 * (bug 8716) Change math_inputhash and math_outputhash to byte for Postgres
 * (bug 8558) Correct display of timestamps on some pages when using Postgres
 
-Support protocols other than HTTP in LinkFilter
-
 == Languages updated ==
 
 * Arabic (ar)
index 8a39524..dedd361 100644 (file)
@@ -1932,22 +1932,45 @@ function wfRelativePath( $path, $from ) {
  * Make a URL index, appropriate for the el_index field of externallinks.
  */
 function wfMakeUrlIndex( $url ) {
-       wfSuppressWarnings();
+       global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
        $bits = parse_url( $url );
-       $prots = array( 'http', 'https', 'ftp', 'irc', 'news' );
+       wfSuppressWarnings();
        wfRestoreWarnings();
-       if ( !$bits || !in_array( $bits['scheme'], $prots ) ) {
+       if ( !$bits ) {
                return false;
        }
+       // most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
+       $delimiter = '';
+       if ( in_array( $bits['scheme'] . '://' , $wgUrlProtocols ) ) {
+               $delimiter = '://';
+       } elseif ( in_array( $bits['scheme'] .':' , $wgUrlProtocols ) ) {
+               $delimiter = ':';
+               // parse_url detects for news: and mailto: the host part of an url as path
+               // We have to correct this wrong detection
+               if ( isset ( $bits['path'] ) ) { 
+                       $bits['host'] = $bits['path'];
+                       $bits['path'] = '';
+               }
+       } else {
+               return false;
+       }
+
        // Reverse the labels in the hostname, convert to lower case
-       $reversedHost = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
+       // For emails reverse domainpart only
+       if ( $bits['scheme'] == 'mailto' ) {
+               $mailparts = explode( '@', $bits['host'] );
+               $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
+               $reversedHost = $domainpart . '@' . $mailparts[0];
+       } else {
+               $reversedHost = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
+       }
        // Add an extra dot to the end
        if ( substr( $reversedHost, -1, 1 ) !== '.' ) {
                $reversedHost .= '.';
        }
        // Reconstruct the pseudo-URL
        $prot = $bits['scheme'];
-       $index = "$prot://$reversedHost";
+       $index = "$prot$delimiter$reversedHost";
        // Leave out user and password. Add the port, path, query and fragment
        if ( isset( $bits['port'] ) )      $index .= ':' . $bits['port'];
        if ( isset( $bits['path'] ) ) {
index 5010009..85bafca 100644 (file)
@@ -50,7 +50,7 @@ class LinkFilter {
         * @param $filterEntry String: domainparts
         * @param $prot        String: protocol
         */
-       function makeLike( $filterEntry , $prot = 'http' ) {
+        function makeLike( $filterEntry , $prot = 'http://' ) {
                if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
                        $subdomains = true;
                        $filterEntry = substr( $filterEntry, 2 );
@@ -76,17 +76,31 @@ class LinkFilter {
                        $path = '/';
                        $host = $filterEntry;
                }
-               $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
-               if ( substr( $host, -1, 1 ) !== '.' ) {
-                       $host .= '.';
-               }
-               $like = "$prot://$host";
-               
-               if ( $subdomains ) {
-                       $like .= '%';
-               }
-               if ( !$subdomains || $path !== '/' ) {
-                       $like .= $path . '%';
+               // Reverse the labels in the hostname, convert to lower case
+               // For emails reverse domainpart only
+               if ( $prot == 'mailto:' && strpos($host, '@') ) {
+                       // complete email adress 
+                       $mailparts = explode( '@', $host );
+                       $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
+                       $host = $domainpart . '@' . $mailparts[0];
+                       $like = "$prot$host%";
+               } elseif ( $prot == 'mailto:' ) {
+                       // domainpart of email adress only. do not add '.'
+                       $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );   
+                       $like = "$prot$host%";                  
+               } else {
+                       $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );   
+                       if ( substr( $host, -1, 1 ) !== '.' ) {
+                               $host .= '.';
+                       }
+                       $like = "$prot$host";
+
+                       if ( $subdomains ) {
+                               $like .= '%';
+                       }
+                       if ( !$subdomains || $path !== '/' ) {
+                               $like .= $path . '%';
+                       }
                }
                return $like;
        }