SquidUpdate.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * See deferred.txt
  4. * @file
  5. * @ingroup Cache
  6. */
  7. /**
  8. * Handles purging appropriate Squid URLs given a title (or titles)
  9. * @ingroup Cache
  10. */
  11. class SquidUpdate {
  12. var $urlArr, $mMaxTitles;
  13. function __construct( $urlArr = Array(), $maxTitles = false ) {
  14. global $wgMaxSquidPurgeTitles;
  15. if ( $maxTitles === false ) {
  16. $this->mMaxTitles = $wgMaxSquidPurgeTitles;
  17. } else {
  18. $this->mMaxTitles = $maxTitles;
  19. }
  20. if ( count( $urlArr ) > $this->mMaxTitles ) {
  21. $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
  22. }
  23. $this->urlArr = $urlArr;
  24. }
  25. static function newFromLinksTo( &$title ) {
  26. $fname = 'SquidUpdate::newFromLinksTo';
  27. wfProfileIn( $fname );
  28. # Get a list of URLs linking to this page
  29. $dbr = wfGetDB( DB_SLAVE );
  30. $res = $dbr->select( array( 'links', 'page' ),
  31. array( 'page_namespace', 'page_title' ),
  32. array(
  33. 'pl_namespace' => $title->getNamespace(),
  34. 'pl_title' => $title->getDBkey(),
  35. 'pl_from=page_id' ),
  36. $fname );
  37. $blurlArr = $title->getSquidURLs();
  38. if ( $dbr->numRows( $res ) <= $this->mMaxTitles ) {
  39. while ( $BL = $dbr->fetchObject ( $res ) )
  40. {
  41. $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
  42. $blurlArr[] = $tobj->getInternalURL();
  43. }
  44. }
  45. $dbr->freeResult ( $res ) ;
  46. wfProfileOut( $fname );
  47. return new SquidUpdate( $blurlArr );
  48. }
  49. /**
  50. * Create a SquidUpdate from an array of Title objects, or a TitleArray object
  51. */
  52. static function newFromTitles( $titles, $urlArr = array() ) {
  53. global $wgMaxSquidPurgeTitles;
  54. $i = 0;
  55. foreach ( $titles as $title ) {
  56. $urlArr[] = $title->getInternalURL();
  57. if ( $i++ > $wgMaxSquidPurgeTitles ) {
  58. break;
  59. }
  60. }
  61. return new SquidUpdate( $urlArr );
  62. }
  63. static function newSimplePurge( &$title ) {
  64. $urlArr = $title->getSquidURLs();
  65. return new SquidUpdate( $urlArr );
  66. }
  67. function doUpdate() {
  68. SquidUpdate::purge( $this->urlArr );
  69. }
  70. /* Purges a list of Squids defined in $wgSquidServers.
  71. $urlArr should contain the full URLs to purge as values
  72. (example: $urlArr[] = 'http://my.host/something')
  73. XXX report broken Squids per mail or log */
  74. static function purge( $urlArr ) {
  75. global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
  76. /*if ( (@$wgSquidServers[0]) == 'echo' ) {
  77. echo implode("<br />\n", $urlArr) . "<br />\n";
  78. return;
  79. }*/
  80. if( empty( $urlArr ) ) {
  81. return;
  82. }
  83. if ( $wgHTCPMulticastAddress && $wgHTCPPort ) {
  84. return SquidUpdate::HTCPPurge( $urlArr );
  85. }
  86. $fname = 'SquidUpdate::purge';
  87. wfProfileIn( $fname );
  88. $maxsocketspersquid = 8; // socket cap per Squid
  89. $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
  90. $firsturl = SquidUpdate::expand( $urlArr[0] );
  91. unset($urlArr[0]);
  92. $urlArr = array_values($urlArr);
  93. $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
  94. if ($sockspersq == 1) {
  95. /* the most common case */
  96. $urlspersocket = count($urlArr);
  97. } else if ($sockspersq > $maxsocketspersquid ) {
  98. $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
  99. $sockspersq = $maxsocketspersquid;
  100. }
  101. $totalsockets = count($wgSquidServers) * $sockspersq;
  102. $sockets = Array();
  103. /* this sets up the sockets and tests the first socket for each server. */
  104. for ($ss=0;$ss < count($wgSquidServers);$ss++) {
  105. $failed = false;
  106. $so = 0;
  107. while ($so < $sockspersq && !$failed) {
  108. if ($so == 0) {
  109. /* first socket for this server, do the tests */
  110. @list($server, $port) = explode(':', $wgSquidServers[$ss]);
  111. if(!isset($port)) $port = 80;
  112. #$this->debug("Opening socket to $server:$port");
  113. $error = $errstr = false;
  114. $socket = @fsockopen($server, $port, $error, $errstr, 3);
  115. #$this->debug("\n");
  116. if (!$socket) {
  117. $failed = true;
  118. $totalsockets -= $sockspersq;
  119. } else {
  120. $msg = 'PURGE ' . $firsturl . " HTTP/1.0\r\n".
  121. "Connection: Keep-Alive\r\n\r\n";
  122. #$this->debug($msg);
  123. @fputs($socket,$msg);
  124. #$this->debug("...");
  125. $res = @fread($socket,512);
  126. #$this->debug("\n");
  127. /* Squid only returns http headers with 200 or 404 status,
  128. if there's more returned something's wrong */
  129. if (strlen($res) > 250) {
  130. fclose($socket);
  131. $failed = true;
  132. $totalsockets -= $sockspersq;
  133. } else {
  134. @stream_set_blocking($socket,false);
  135. $sockets[] = $socket;
  136. }
  137. }
  138. } else {
  139. /* open the remaining sockets for this server */
  140. list($server, $port) = explode(':', $wgSquidServers[$ss]);
  141. if(!isset($port)) $port = 80;
  142. $socket = @fsockopen($server, $port, $error, $errstr, 2);
  143. @stream_set_blocking($socket,false);
  144. $sockets[] = $socket;
  145. }
  146. $so++;
  147. }
  148. }
  149. if ($urlspersocket > 0) {
  150. /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
  151. for ($r=0;$r < $urlspersocket;$r++) {
  152. for ($s=0;$s < $totalsockets;$s++) {
  153. if($r != 0) {
  154. $res = '';
  155. $esc = 0;
  156. while (strlen($res) < 100 && $esc < 200 ) {
  157. $res .= @fread($sockets[$s],512);
  158. $esc++;
  159. usleep(20);
  160. }
  161. }
  162. $urindex = $r + $urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
  163. $url = SquidUpdate::expand( $urlArr[$urindex] );
  164. $msg = 'PURGE ' . $url . " HTTP/1.0\r\n".
  165. "Connection: Keep-Alive\r\n\r\n";
  166. #$this->debug($msg);
  167. @fputs($sockets[$s],$msg);
  168. #$this->debug("\n");
  169. }
  170. }
  171. }
  172. #$this->debug("Reading response...");
  173. foreach ($sockets as $socket) {
  174. $res = '';
  175. $esc = 0;
  176. while (strlen($res) < 100 && $esc < 200 ) {
  177. $res .= @fread($socket,1024);
  178. $esc++;
  179. usleep(20);
  180. }
  181. @fclose($socket);
  182. }
  183. #$this->debug("\n");
  184. wfProfileOut( $fname );
  185. }
  186. static function HTCPPurge( $urlArr ) {
  187. global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
  188. $fname = 'SquidUpdate::HTCPPurge';
  189. wfProfileIn( $fname );
  190. $htcpOpCLR = 4; // HTCP CLR
  191. // FIXME PHP doesn't support these socket constants (include/linux/in.h)
  192. if( !defined( "IPPROTO_IP" ) ) {
  193. define( "IPPROTO_IP", 0 );
  194. define( "IP_MULTICAST_LOOP", 34 );
  195. define( "IP_MULTICAST_TTL", 33 );
  196. }
  197. // pfsockopen doesn't work because we need set_sock_opt
  198. $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
  199. if ( $conn ) {
  200. // Set socket options
  201. socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
  202. if ( $wgHTCPMulticastTTL != 1 )
  203. socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
  204. $wgHTCPMulticastTTL );
  205. foreach ( $urlArr as $url ) {
  206. if( !is_string( $url ) ) {
  207. throw new MWException( 'Bad purge URL' );
  208. }
  209. $url = SquidUpdate::expand( $url );
  210. // Construct a minimal HTCP request diagram
  211. // as per RFC 2756
  212. // Opcode 'CLR', no response desired, no auth
  213. $htcpTransID = rand();
  214. $htcpSpecifier = pack( 'na4na*na8n',
  215. 4, 'HEAD', strlen( $url ), $url,
  216. 8, 'HTTP/1.0', 0 );
  217. $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
  218. $htcpLen = 4 + $htcpDataLen + 2;
  219. // Note! Squid gets the bit order of the first
  220. // word wrong, wrt the RFC. Apparently no other
  221. // implementation exists, so adapt to Squid
  222. $htcpPacket = pack( 'nxxnCxNxxa*n',
  223. $htcpLen, $htcpDataLen, $htcpOpCLR,
  224. $htcpTransID, $htcpSpecifier, 2);
  225. // Send out
  226. wfDebug( "Purging URL $url via HTCP\n" );
  227. socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
  228. $wgHTCPMulticastAddress, $wgHTCPPort );
  229. }
  230. } else {
  231. $errstr = socket_strerror( socket_last_error() );
  232. wfDebug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
  233. }
  234. wfProfileOut( $fname );
  235. }
  236. function debug( $text ) {
  237. global $wgDebugSquid;
  238. if ( $wgDebugSquid ) {
  239. wfDebug( $text );
  240. }
  241. }
  242. /**
  243. * Expand local URLs to fully-qualified URLs using the internal protocol
  244. * and host defined in $wgInternalServer. Input that's already fully-
  245. * qualified will be passed through unchanged.
  246. *
  247. * This is used to generate purge URLs that may be either local to the
  248. * main wiki or include a non-native host, such as images hosted on a
  249. * second internal server.
  250. *
  251. * Client functions should not need to call this.
  252. *
  253. * @return string
  254. */
  255. static function expand( $url ) {
  256. global $wgInternalServer;
  257. if( $url != '' && $url{0} == '/' ) {
  258. return $wgInternalServer . $url;
  259. }
  260. return $url;
  261. }
  262. }