Fix blocks for non-memcache
[lhc/web/wiklou.git] / includes / ParserCache.php
1 <?php
2
3 class ParserCache
4 {
5 function get( &$article, &$user ){
6 $hash = $user->getPageRenderingHash();
7 $pageid = intval( $id );
8 $res = wfQuery("SELECT pc_data FROM parsercache WHERE pc_pageid = {$pageid} ".
9 " AND pc_prefhash = '{$hash}' AND pc_expire > NOW()", DB_WRITE);
10 $row = wfFetchObject ( $res );
11 if( $row ){
12 $retVal = unserialize( gzuncompress($row->pc_data) );
13 wfProfileOut( $fname );
14 } else {
15 $retVal = false;
16 }
17 return $retVal;
18 }
19
20 function save( $parserOutput, &$article, &$user ){
21 $hash = $user->getPageRenderingHash();
22 $pageid = intval( $article->getID() );
23 $title = wfStrencode( $article->mTitle->getPrefixedDBKey() );
24 $ser = addslashes( gzcompress( serialize( $parserOutput ) ) );
25 if( $parserOutput->containsOldMagic() ){
26 $expire = "1 HOUR";
27 } else {
28 $expire = "7 DAY";
29 }
30
31 wfQuery("REPLACE INTO parsercache (pc_prefhash,pc_pageid,pc_title,pc_data, pc_expire) ".
32 "VALUES('{$hash}', {$pageid}, '{$title}', '{$ser}', ".
33 "DATE_ADD(NOW(), INTERVAL {$expire}))", DB_WRITE);
34
35 if( rand() % 50 == 0 ){ // more efficient to just do it sometimes
36 $this->purge();
37 }
38 }
39
40 function purge(){
41 wfQuery("DELETE FROM parsercache WHERE pc_expire < NOW() LIMIT 250", DB_WRITE);
42 }
43
44 function clearLinksTo( $pid ){
45 $pid = intval( $pid );
46 wfQuery("DELETE parsercache FROM parsercache,links ".
47 "WHERE pc_pageid=links.l_from AND l_to={$pid}", DB_WRITE);
48 wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE);
49 }
50
51 # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns.
52 function clearBrokenLinksTo( $title ){
53 $title = wfStrencode( $title );
54 wfQuery("DELETE parsercache FROM parsercache,brokenlinks ".
55 "WHERE pc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE);
56 }
57
58 # $pid is a page id
59 function clearPage( $pid, $namespace ){
60 $pid = intval( $pid );
61 if( $namespace == NS_MEDIAWIKI ){
62 $this->clearLinksTo( $pid );
63 } else {
64 wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE);
65 }
66 }
67 }
68
69
70 ?>