Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?php
2 /**
3 * Cache for article titles (prefixed DB keys) and ids linked from one source
4 * @addtogroup Cache
5 */
6
7 /**
8 * @addtogroup Cache
9 */
10 class LinkCache {
11 // Increment $mClassVer whenever old serialized versions of this class
12 // becomes incompatible with the new version.
13 /* private */ var $mClassVer = 3;
14
15 /* private */ var $mPageLinks;
16 /* private */ var $mGoodLinks, $mBadLinks;
17 /* private */ var $mForUpdate;
18
19 /**
20 * Get an instance of this class
21 */
22 static function &singleton() {
23 static $instance;
24 if ( !isset( $instance ) ) {
25 $instance = new LinkCache;
26 }
27 return $instance;
28 }
29
30 function __construct() {
31 $this->mForUpdate = false;
32 $this->mPageLinks = array();
33 $this->mGoodLinks = array();
34 $this->mBadLinks = array();
35 }
36
37 /* private */ function getKey( $title ) {
38 return wfMemcKey( 'lc', 'title', $title );
39 }
40
41 /**
42 * General accessor to get/set whether SELECT FOR UPDATE should be used
43 */
44 function forUpdate( $update = NULL ) {
45 return wfSetVar( $this->mForUpdate, $update );
46 }
47
48 function getGoodLinkID( $title ) {
49 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
50 return $this->mGoodLinks[$title];
51 } else {
52 return 0;
53 }
54 }
55
56 function isBadLink( $title ) {
57 return array_key_exists( $title, $this->mBadLinks );
58 }
59
60 function addGoodLinkObj( $id, $title ) {
61 $dbkey = $title->getPrefixedDbKey();
62 $this->mGoodLinks[$dbkey] = $id;
63 $this->mPageLinks[$dbkey] = $title;
64 }
65
66 function addBadLinkObj( $title ) {
67 $dbkey = $title->getPrefixedDbKey();
68 if ( ! $this->isBadLink( $dbkey ) ) {
69 $this->mBadLinks[$dbkey] = 1;
70 $this->mPageLinks[$dbkey] = $title;
71 }
72 }
73
74 function clearBadLink( $title ) {
75 unset( $this->mBadLinks[$title] );
76 $this->clearLink( $title );
77 }
78
79 function clearLink( $title ) {
80 global $wgMemc, $wgLinkCacheMemcached;
81 if( $wgLinkCacheMemcached )
82 $wgMemc->delete( $this->getKey( $title ) );
83 }
84
85 function getPageLinks() { return $this->mPageLinks; }
86 function getGoodLinks() { return $this->mGoodLinks; }
87 function getBadLinks() { return array_keys( $this->mBadLinks ); }
88
89 /**
90 * Add a title to the link cache, return the page_id or zero if non-existent
91 * @param $title String: title to add
92 * @return integer
93 */
94 function addLink( $title ) {
95 $nt = Title::newFromDBkey( $title );
96 if( $nt ) {
97 return $this->addLinkObj( $nt );
98 } else {
99 return 0;
100 }
101 }
102
103 /**
104 * Add a title to the link cache, return the page_id or zero if non-existent
105 * @param $nt Title to add.
106 * @return integer
107 */
108 function addLinkObj( &$nt ) {
109 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
110 $title = $nt->getPrefixedDBkey();
111 if ( $this->isBadLink( $title ) ) { return 0; }
112 $id = $this->getGoodLinkID( $title );
113 if ( 0 != $id ) { return $id; }
114
115 $fname = 'LinkCache::addLinkObj';
116 global $wgProfiling, $wgProfiler;
117 if ( $wgProfiling && isset( $wgProfiler ) ) {
118 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
119 }
120
121 wfProfileIn( $fname );
122
123 $ns = $nt->getNamespace();
124 $t = $nt->getDBkey();
125
126 if ( '' == $title ) {
127 wfProfileOut( $fname );
128 return 0;
129 }
130
131 $id = NULL;
132 if( $wgLinkCacheMemcached )
133 $id = $wgMemc->get( $key = $this->getKey( $title ) );
134 if( ! is_integer( $id ) ) {
135 if ( $this->mForUpdate ) {
136 $db =& wfGetDB( DB_MASTER );
137 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
138 $options = array( 'FOR UPDATE' );
139 } else {
140 $options = array();
141 }
142 } else {
143 $db =& wfGetDB( DB_SLAVE );
144 $options = array();
145 }
146
147 $id = $db->selectField( 'page', 'page_id',
148 array( 'page_namespace' => $ns, 'page_title' => $t ),
149 $fname, $options );
150 if ( !$id ) {
151 $id = 0;
152 }
153 if( $wgLinkCacheMemcached )
154 $wgMemc->add( $key, $id, 3600*24 );
155 }
156
157 if( 0 == $id ) {
158 $this->addBadLinkObj( $nt );
159 } else {
160 $this->addGoodLinkObj( $id, $nt );
161 }
162 wfProfileOut( $fname );
163 return $id;
164 }
165
166 /**
167 * Clears cache
168 */
169 function clear() {
170 $this->mPageLinks = array();
171 $this->mGoodLinks = array();
172 $this->mBadLinks = array();
173 }
174 }
175 ?>