* remove end of line whitespace
[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 *
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 = 4;
11
12 /* private */ var $mPageLinks;
13 /* private */ var $mGoodLinks, $mBadLinks;
14 /* private */ var $mForUpdate;
15
16 /**
17 * Get an instance of this class
18 */
19 static function &singleton() {
20 static $instance;
21 if ( !isset( $instance ) ) {
22 $instance = new LinkCache;
23 }
24 return $instance;
25 }
26
27 function __construct() {
28 $this->mForUpdate = false;
29 $this->mPageLinks = array();
30 $this->mGoodLinks = array();
31 $this->mGoodLinkFields = array();
32 $this->mBadLinks = array();
33 }
34
35 /**
36 * General accessor to get/set whether SELECT FOR UPDATE should be used
37 */
38 public function forUpdate( $update = NULL ) {
39 return wfSetVar( $this->mForUpdate, $update );
40 }
41
42 public function getGoodLinkID( $title ) {
43 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
44 return $this->mGoodLinks[$title];
45 } else {
46 return 0;
47 }
48 }
49
50 /**
51 * Get a field of a title object from cache.
52 * If this link is not good, it will return NULL.
53 * @param Title $title
54 * @param string $field ('length','redirect')
55 * @return mixed
56 */
57 public function getGoodLinkFieldObj( $title, $field ) {
58 $dbkey = $title->getPrefixedDbKey();
59 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
60 return $this->mGoodLinkFields[$dbkey][$field];
61 } else {
62 return NULL;
63 }
64 }
65
66 public function isBadLink( $title ) {
67 return array_key_exists( $title, $this->mBadLinks );
68 }
69
70 /**
71 * Add a link for the title to the link cache
72 * @param int $id
73 * @param Title $title
74 * @param int $len
75 * @param int $redir
76 */
77 public function addGoodLinkObj( $id, $title, $len = -1, $redir = NULL ) {
78 $dbkey = $title->getPrefixedDbKey();
79 $this->mGoodLinks[$dbkey] = $id;
80 $this->mGoodLinkFields[$dbkey] = array( 'length' => $len, 'redirect' => $redir );
81 $this->mPageLinks[$dbkey] = $title;
82 }
83
84 public function addBadLinkObj( $title ) {
85 $dbkey = $title->getPrefixedDbKey();
86 if ( ! $this->isBadLink( $dbkey ) ) {
87 $this->mBadLinks[$dbkey] = 1;
88 $this->mPageLinks[$dbkey] = $title;
89 }
90 }
91
92 public function clearBadLink( $title ) {
93 unset( $this->mBadLinks[$title] );
94 $this->clearLink( $title );
95 }
96
97 /* obsolete, for old $wgLinkCacheMemcached stuff */
98 public function clearLink( $title ) {}
99
100 public function getPageLinks() { return $this->mPageLinks; }
101 public function getGoodLinks() { return $this->mGoodLinks; }
102 public function getBadLinks() { return array_keys( $this->mBadLinks ); }
103
104 /**
105 * Add a title to the link cache, return the page_id or zero if non-existent
106 * @param $title String: title to add
107 * @param $len int, page size
108 * @param $redir bool, is redirect?
109 * @return integer
110 */
111 public function addLink( $title, $len = -1, $redir = NULL ) {
112 $nt = Title::newFromDBkey( $title );
113 if( $nt ) {
114 return $this->addLinkObj( $nt, $len, $redir );
115 } else {
116 return 0;
117 }
118 }
119
120 /**
121 * Add a title to the link cache, return the page_id or zero if non-existent
122 * @param $nt Title to add.
123 * @param $len int, page size
124 * @param $redir bool, is redirect?
125 * @return integer
126 */
127 public function addLinkObj( &$nt, $len = -1, $redirect = NULL ) {
128 global $wgAntiLockFlags;
129 $title = $nt->getPrefixedDBkey();
130 if ( $this->isBadLink( $title ) ) { return 0; }
131 $id = $this->getGoodLinkID( $title );
132 if ( 0 != $id ) { return $id; }
133
134 $fname = 'LinkCache::addLinkObj';
135 global $wgProfiler;
136 if ( isset( $wgProfiler ) ) {
137 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
138 }
139
140 wfProfileIn( $fname );
141
142 $ns = $nt->getNamespace();
143 $t = $nt->getDBkey();
144
145 if ( '' == $title ) {
146 wfProfileOut( $fname );
147 return 0;
148 }
149 # Some fields heavily used for linking...
150 $id = NULL;
151
152 if( !is_integer( $id ) ) {
153 if ( $this->mForUpdate ) {
154 $db = wfGetDB( DB_MASTER );
155 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
156 $options = array( 'FOR UPDATE' );
157 } else {
158 $options = array();
159 }
160 } else {
161 $db = wfGetDB( DB_SLAVE );
162 $options = array();
163 }
164
165 $s = $db->selectRow( 'page',
166 array( 'page_id', 'page_len', 'page_is_redirect' ),
167 array( 'page_namespace' => $ns, 'page_title' => $t ),
168 $fname, $options );
169 # Set fields...
170 $id = $s ? $s->page_id : 0;
171 $len = $s ? $s->page_len : -1;
172 $redirect = $s ? $s->page_is_redirect : 0;
173 }
174
175 if( 0 == $id ) {
176 $this->addBadLinkObj( $nt );
177 } else {
178 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
179 }
180 wfProfileOut( $fname );
181 return $id;
182 }
183
184 /**
185 * Clears cache
186 */
187 public function clear() {
188 $this->mPageLinks = array();
189 $this->mGoodLinks = array();
190 $this->mGoodLinkFields = array();
191 $this->mBadLinks = array();
192 }
193 }