5 var $urlArr, $mMaxTitles;
7 function SquidUpdate( $urlArr = Array(), $maxTitles = false ) {
8 global $wgMaxSquidPurgeTitles;
9 if ( $maxTitles === false ) {
10 $this->mMaxTitles
= $wgMaxSquidPurgeTitles;
12 $this->mMaxTitles
= $maxTitles;
14 if ( count( $urlArr ) > $this->mMaxTitles
) {
15 $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles
);
17 $this->urlArr
= $urlArr;
20 /* static */ function newFromLinksTo( &$title ) {
21 $fname = 'SquidUpdate::newFromLinksTo';
22 wfProfileIn( $fname );
24 # Get a list of URLs linking to this page
25 $id = $title->getArticleID();
27 $dbr =& wfGetDB( DB_SLAVE
);
28 $links = $dbr->tableName( 'links' );
29 $cur = $dbr->tableName( 'cur' );
31 $sql = "SELECT cur_namespace,cur_title FROM $links,$cur WHERE l_to={$id} and l_from=cur_id" ;
32 $res = $dbr->query( $sql, $fname ) ;
33 $blurlArr = $title->getSquidURLs();
34 if ( $dbr->numRows( $res ) <= $this->mMaxTitles
) {
35 while ( $BL = $dbr->fetchObject ( $res ) )
37 $tobj = Title
::makeTitle( $BL->cur_namespace
, $BL->cur_title
) ;
38 $blurlArr[] = $tobj->getInternalURL();
41 $dbr->freeResult ( $res ) ;
43 wfProfileOut( $fname );
44 return new SquidUpdate( $blurlArr );
47 /* static */ function newFromBrokenLinksTo( &$title ) {
48 $fname = 'SquidUpdate::newFromBrokenLinksTo';
49 wfProfileIn( $fname );
51 # Get a list of URLs linking to this (currently non-existent) page
52 $dbr =& wfGetDB( DB_SLAVE
);
53 $brokenlinks = $dbr->tableName( 'brokenlinks' );
54 $cur = $dbr->tableName( 'cur' );
55 $encTitle = $dbr->addQuotes( $title->getPrefixedDBkey() );
57 $sql = "SELECT cur_namespace,cur_title FROM $brokenlinks,$cur WHERE bl_to={$encTitle} AND bl_from=cur_id";
58 $res = $dbr->query( $sql, $fname );
60 if ( $dbr->numRows( $res ) <= $this->mMaxTitles
) {
61 while ( $BL = $dbr->fetchObject( $res ) )
63 $tobj = Title
::makeTitle( $BL->cur_namespace
, $BL->cur_title
);
64 $blurlArr[] = $tobj->getInternalURL();
67 $dbr->freeResult( $res );
68 wfProfileOut( $fname );
69 return new SquidUpdate( $blurlArr );
72 /* static */ function newSimplePurge( &$title ) {
73 $urlArr = $title->getSquidURLs();
74 return new SquidUpdate( $blurlArr );
78 SquidUpdate
::purge( $this->urlArr
);
81 /* Purges a list of Squids defined in $wgSquidServers.
82 $urlArr should contain the full URLs to purge as values
83 (example: $urlArr[] = 'http://my.host/something')
84 XXX report broken Squids per mail or log */
86 /* static */ function purge( $urlArr ) {
87 global $wgSquidServers;
89 if ( $wgSquidServers == "echo" ) {
90 echo implode("<br>\n", $urlArr);
94 $fname = 'SquidUpdate::purge';
95 wfProfileIn( $fname );
97 $maxsocketspersquid = 8; // socket cap per Squid
98 $urlspersocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
99 $firsturl = $urlArr[0];
101 $urlArr = array_values($urlArr);
102 $sockspersq = max(ceil(count($urlArr) / $urlspersocket ),1);
103 if ($sockspersq == 1) {
104 /* the most common case */
105 $urlspersocket = count($urlArr);
106 } else if ($sockspersq > $maxsocketspersquid ) {
107 $urlspersocket = ceil(count($urlArr) / $maxsocketspersquid);
108 $sockspersq = $maxsocketspersquid;
110 $totalsockets = count($wgSquidServers) * $sockspersq;
113 /* this sets up the sockets and tests the first socket for each server. */
114 for ($ss=0;$ss < count($wgSquidServers);$ss++
) {
117 while ($so < $sockspersq && !$failed) {
119 /* first socket for this server, do the tests */
120 @list
($server, $port) = explode(':', $wgSquidServers[$ss]);
121 if(!isset($port)) $port = 80;
122 #$this->debug("Opening socket to $server:$port");
123 $socket = @fsockopen
($server, $port, $error, $errstr, 3);
127 $totalsockets -= $sockspersq;
129 $msg ="PURGE " . $firsturl . " HTTP/1.0\r\n".
130 "Connection: Keep-Alive\r\n\r\n";
132 @fputs
($socket,$msg);
133 #$this->debug("...");
134 $res = @fread
($socket,512);
136 /* Squid only returns http headers with 200 or 404 status,
137 if there's more returned something's wrong */
138 if (strlen($res) > 250) {
141 $totalsockets -= $sockspersq;
143 @stream_set_blocking
($socket,false);
144 $sockets[] = $socket;
148 /* open the remaining sockets for this server */
149 list($server, $port) = explode(':', $wgSquidServers[$ss]);
150 if(!isset($port)) $port = 80;
151 $sockets[] = @fsockopen
($server, $port, $error, $errstr, 2);
152 @stream_set_blocking
($sockets[$s],false);
158 if ($urlspersocket > 0) {
159 /* now do the heavy lifting. The fread() relies on Squid returning only the headers */
160 for ($r=0;$r < $urlspersocket;$r++
) {
161 for ($s=0;$s < $totalsockets;$s++
) {
165 while (strlen($res) < 100 && $esc < 200 ) {
166 $res .= @fread
($sockets[$s],512);
171 $urindex = $r +
$urlspersocket * ($s - $sockspersq * floor($s / $sockspersq));
172 $msg = "PURGE " . $urlArr[$urindex] . " HTTP/1.0\r\n".
173 "Connection: Keep-Alive\r\n\r\n";
175 @fputs
($sockets[$s],$msg);
180 #$this->debug("Reading response...");
181 foreach ($sockets as $socket) {
184 while (strlen($res) < 100 && $esc < 200 ) {
185 $res .= @fread
($socket,1024);
193 wfProfileOut( $fname );
196 function debug( $text ) {
197 global $wgDebugSquid;
198 if ( $wgDebugSquid ) {