7 function SquidUpdate( $urlArr = Array() ) {
8 $this->urlArr
= $urlArr;
11 /* static */ function newFromLinksTo( &$title ) {
12 # Get a list of URLs linking to this page
13 $id = $title->getArticleID();
14 $sql = "SELECT cur_namespace,cur_title FROM links,cur WHERE l_to={$id} and l_from=cur_id" ;
15 $res = wfQuery ( $sql, DB_READ
) ;
16 $blurlArr = $title->getSquidURLs();
17 while ( $BL = wfFetchObject ( $res ) )
19 $tobj = Title
::makeTitle( $BL->cur_namespace
, $BL->cur_title
) ;
20 $blurlArr[] = $tobj->getInternalURL();
22 wfFreeResult ( $res ) ;
23 return new SquidUpdate( $blurlArr );
26 /* static */ function newFromBrokenLinksTo( &$title ) {
27 # Get a list of URLs linking to this (currently non-existent) page
28 $encTitle = $title->getPrefixedDBkey();
29 $sql = "SELECT cur_namespace,cur_title FROM brokenlinks,cur WHERE bl_to={$encTitle} AND bl_from=cur_id";
30 $res = wfQuery( $sql, DB_READ
);
32 while ( $BL = wfFetchObject( $res ) )
34 $tobj = Title
::makeTitle( $BL->cur_namespace
, $BL->cur_title
);
35 $blurlArr[] = $tobj->getInternalURL();
38 return new SquidUpdate( $blurlArr );
41 /* static */ function newSimplePurge( &$title ) {
42 $urlArr = $title->getSquidURLs();
43 return new SquidUpdate( $blurlArr );
47 SquidUpdate
::purge( $this->urlArr
);
50 /* Purges a list of Squids defined in $wgSquidServers.
51 $urlArr should contain the full URLs to purge as values
52 (example: $urlArr[] = 'http://my.host/something')
53 XXX report broken Squids per mail or log */
55 /* static */ function purge( $urlArr ) {
56 global $wgSquidServers;
58 if ( $wgSquidServers == "echo" ) {
59 echo implode("<br>\n", $urlArr);
63 $maxsocketspersquid = 8; // socket cap per Squid
64 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
65 $firsturl = $urlArr[0];
67 $urlArr = array_values($urlArr);
68 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
69 if ($sockspersq == 1) {
70 /* the most common case */
71 $urlspersocket = count($urlArr);
72 } else if ($sockspersq > $maxsocketspersquid ) {
73 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
74 $sockspersq = $maxsocketspersquid;
76 $totalsockets = count($wgSquidServers) * $sockspersq;
79 /* this sets up the sockets and tests the first socket for each server. */
80 for ($ss=0;$ss < count($wgSquidServers);$ss++
) {
83 while ($so < $sockspersq && !$failed) {
85 /* first socket for this server, do the tests */
86 @list
($server, $port) = explode(':', $wgSquidServers[$ss]);
87 if(!isset($port)) $port = 80;
88 $this->debug("Opening socket to $server:$port");
89 $socket = @fsockopen
($server, $port, $error, $errstr, 3);
93 $totalsockets -= $sockspersq;
95 $msg ="PURGE " . $firsturl . " HTTP/1.0\r\n".
96 "Connection: Keep-Alive\r\n\r\n";
100 $res = @fread
($socket,512);
102 /* Squid only returns http headers with 200 or 404 status,
103 if there's more returned something's wrong */
104 if (strlen($res) > 250) {
107 $totalsockets -= $sockspersq;
109 @stream_set_blocking
($socket,false);
110 $sockets[] = $socket;
114 /* open the remaining sockets for this server */
115 list($server, $port) = explode(':', $wgSquidServers[$ss]);
116 if(!isset($port)) $port = 80;
117 $sockets[] = @fsockopen
($server, $port, $error, $errstr, 2);
118 @stream_set_blocking
($sockets[$s],false);
124 if ($urlspersocket > 0) {
125 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
126 for ($r=0;$r < $urlspersocket;$r++
) {
127 for ($s=0;$s < $totalsockets;$s++
) {
131 while (strlen($res) < 100 && $esc < 200 ) {
132 $res .= @fread
($sockets[$s],512);
137 $urindex = $r +
$urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
138 $msg = "PURGE " . $urlArr[$urindex] . " HTTP/1.0\r\n".
139 "Connection: Keep-Alive\r\n\r\n";
141 @fputs
($sockets[$s],$msg);
146 $this->debug("Reading response...");
147 foreach ($sockets as $socket) {
150 while (strlen($res) < 100 && $esc < 200 ) {
151 $res .= @fread
($socket,1024);
161 function debug( $text ) {
162 global $wgDebugSquid;
163 if ( $wgDebugSquid ) {