* Deprecated the {{msg:}} syntax for inclusion from the Template namespace.
[lhc/web/wiklou.git] / includes / SquidUpdate.php
index 1c91d54..484f26d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * See deferred.doc
+ * See deferred.txt
  * @package MediaWiki
  */
 
@@ -76,6 +76,13 @@ class SquidUpdate {
                return new SquidUpdate( $blurlArr );
        }
 
+       /* static */ function newFromTitles( &$titles, $urlArr = array() ) {
+               foreach ( $titles as $title ) {
+                       $urlArr[] = $title->getInternalURL();
+               }
+               return new SquidUpdate( $urlArr );
+       }
+
        /* static */ function newSimplePurge( &$title ) {
                $urlArr = $title->getSquidURLs();
                return new SquidUpdate( $blurlArr );
@@ -91,13 +98,16 @@ class SquidUpdate {
        XXX report broken Squids per mail or log */
 
        /* static */ function purge( $urlArr ) {
-               global  $wgSquidServers;
+               global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
 
                if ( $wgSquidServers == 'echo' ) {
-                       echo implode("<br>\n", $urlArr);
+                       echo implode("<br />\n", $urlArr);
                        return;
                }
 
+               if ( $wgHTCPMulticastAddress && $wgHTCPPort )
+                       SquidUpdate::HTCPPurge( $urlArr );
+
                $fname = 'SquidUpdate::purge';
                wfProfileIn( $fname );
                
@@ -200,6 +210,59 @@ class SquidUpdate {
                wfProfileOut( $fname );
        }
 
+       /* static */ function HTCPPurge( $urlArr ) {
+               global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
+               $fname = 'SquidUpdate::HTCPPurge';
+               wfProfileIn( $fname );
+
+               $htcpOpCLR = 4;                 // HTCP CLR
+
+               // FIXME PHP doesn't support these socket constants (include/linux/in.h)
+               define( "IPPROTO_IP", 0 );
+               define( "IP_MULTICAST_LOOP", 34 );
+               define( "IP_MULTICAST_TTL", 33 );
+
+               // pfsockopen doesn't work because we need set_sock_opt
+               $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
+               if ( $conn ) {
+                       // Set socket options
+                       socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
+                       if ( $wgHTCPMulticastTTL != 1 )
+                               socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
+                                       $wgHTCPMulticastTTL );
+
+                       foreach ( $urlArr as $url ) {
+                               // Construct a minimal HTCP request diagram
+                               // as per RFC 2756
+                               // Opcode 'CLR', no response desired, no auth
+                               $htcpTransID = rand();
+
+                               $htcpSpecifier = pack( 'na4na*na8n',
+                                       4, 'NONE', strlen( $url ), $url,
+                                       8, 'HTTP/1.0', 0 );
+
+                               $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
+                               $htcpLen = 4 + $htcpDataLen + 2;
+
+                               // Note! Squid gets the bit order of the first
+                               // word wrong, wrt the RFC. Apparently no other
+                               // implementation exists, so adapt to Squid
+                               $htcpPacket = pack( 'nxxnCxNxxa*n',
+                                       $htcpLen, $htcpDataLen, $htcpOpCLR,
+                                       $htcpTransID, $htcpSpecifier, 2);
+
+                               // Send out
+                               wfDebug( "Purging URL $url via HTCP\n" );
+                               socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
+                                       $wgHTCPMulticastAddress, $wgHTCPPort );
+                       }
+               } else {
+                       $errstr = socket_strerror( socket_last_error() );
+                       wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
+               }
+               wfProfileOut( $fname );
+       }
+
        function debug( $text ) {
                global $wgDebugSquid;
                if ( $wgDebugSquid ) {