* API: added categories property
[mediawiki.git] / includes / LinkCache.php
blob53fb640a8ffe7e1ed5ff5ed3a7448506cc86949d
1 <?php
2 /**
3 * Cache for article titles (prefixed DB keys) and ids linked from one source
4 *
5 * @addtogroup Cache
6 */
7 class LinkCache {
8 // Increment $mClassVer whenever old serialized versions of this class
9 // becomes incompatible with the new version.
10 /* private */ var $mClassVer = 3;
12 /* private */ var $mPageLinks;
13 /* private */ var $mGoodLinks, $mBadLinks;
14 /* private */ var $mForUpdate;
16 /**
17 * Get an instance of this class
19 static function &singleton() {
20 static $instance;
21 if ( !isset( $instance ) ) {
22 $instance = new LinkCache;
24 return $instance;
27 function __construct() {
28 $this->mForUpdate = false;
29 $this->mPageLinks = array();
30 $this->mGoodLinks = array();
31 $this->mBadLinks = array();
34 /* private */ function getKey( $title ) {
35 return wfMemcKey( 'lc', 'title', $title );
38 /**
39 * General accessor to get/set whether SELECT FOR UPDATE should be used
41 function forUpdate( $update = NULL ) {
42 return wfSetVar( $this->mForUpdate, $update );
45 function getGoodLinkID( $title ) {
46 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
47 return $this->mGoodLinks[$title];
48 } else {
49 return 0;
53 function isBadLink( $title ) {
54 return array_key_exists( $title, $this->mBadLinks );
57 function addGoodLinkObj( $id, $title ) {
58 $dbkey = $title->getPrefixedDbKey();
59 $this->mGoodLinks[$dbkey] = $id;
60 $this->mPageLinks[$dbkey] = $title;
63 function addBadLinkObj( $title ) {
64 $dbkey = $title->getPrefixedDbKey();
65 if ( ! $this->isBadLink( $dbkey ) ) {
66 $this->mBadLinks[$dbkey] = 1;
67 $this->mPageLinks[$dbkey] = $title;
71 function clearBadLink( $title ) {
72 unset( $this->mBadLinks[$title] );
73 $this->clearLink( $title );
76 function clearLink( $title ) {
77 global $wgMemc, $wgLinkCacheMemcached;
78 if( $wgLinkCacheMemcached )
79 $wgMemc->delete( $this->getKey( $title ) );
82 function getPageLinks() { return $this->mPageLinks; }
83 function getGoodLinks() { return $this->mGoodLinks; }
84 function getBadLinks() { return array_keys( $this->mBadLinks ); }
86 /**
87 * Add a title to the link cache, return the page_id or zero if non-existent
88 * @param $title String: title to add
89 * @return integer
91 function addLink( $title ) {
92 $nt = Title::newFromDBkey( $title );
93 if( $nt ) {
94 return $this->addLinkObj( $nt );
95 } else {
96 return 0;
101 * Add a title to the link cache, return the page_id or zero if non-existent
102 * @param $nt Title to add.
103 * @return integer
105 function addLinkObj( &$nt ) {
106 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
107 $title = $nt->getPrefixedDBkey();
108 if ( $this->isBadLink( $title ) ) { return 0; }
109 $id = $this->getGoodLinkID( $title );
110 if ( 0 != $id ) { return $id; }
112 $fname = 'LinkCache::addLinkObj';
113 global $wgProfiling, $wgProfiler;
114 if ( $wgProfiling && isset( $wgProfiler ) ) {
115 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
118 wfProfileIn( $fname );
120 $ns = $nt->getNamespace();
121 $t = $nt->getDBkey();
123 if ( '' == $title ) {
124 wfProfileOut( $fname );
125 return 0;
128 $id = NULL;
129 if( $wgLinkCacheMemcached )
130 $id = $wgMemc->get( $key = $this->getKey( $title ) );
131 if( ! is_integer( $id ) ) {
132 if ( $this->mForUpdate ) {
133 $db = wfGetDB( DB_MASTER );
134 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
135 $options = array( 'FOR UPDATE' );
136 } else {
137 $options = array();
139 } else {
140 $db = wfGetDB( DB_SLAVE );
141 $options = array();
144 $id = $db->selectField( 'page', 'page_id',
145 array( 'page_namespace' => $ns, 'page_title' => $t ),
146 $fname, $options );
147 if ( !$id ) {
148 $id = 0;
150 if( $wgLinkCacheMemcached )
151 $wgMemc->add( $key, $id, 3600*24 );
154 if( 0 == $id ) {
155 $this->addBadLinkObj( $nt );
156 } else {
157 $this->addGoodLinkObj( $id, $nt );
159 wfProfileOut( $fname );
160 return $id;
164 * Clears cache
166 function clear() {
167 $this->mPageLinks = array();
168 $this->mGoodLinks = array();
169 $this->mBadLinks = array();