Add noratelimit right to list of core rights, was missing.
[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 * @ingroup 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 }
95
96 /* obsolete, for old $wgLinkCacheMemcached stuff */
97 public function clearLink( $title ) {}
98
99 public function getPageLinks() { return $this->mPageLinks; }
100 public function getGoodLinks() { return $this->mGoodLinks; }
101 public function getBadLinks() { return array_keys( $this->mBadLinks ); }
102
103 /**
104 * Add a title to the link cache, return the page_id or zero if non-existent
105 * @param $title String: title to add
106 * @param $len int, page size
107 * @param $redir bool, is redirect?
108 * @return integer
109 */
110 public function addLink( $title, $len = -1, $redir = NULL ) {
111 $nt = Title::newFromDBkey( $title );
112 if( $nt ) {
113 return $this->addLinkObj( $nt, $len, $redir );
114 } else {
115 return 0;
116 }
117 }
118
119 /**
120 * Add a title to the link cache, return the page_id or zero if non-existent
121 * @param $nt Title to add.
122 * @param $len int, page size
123 * @param $redir bool, is redirect?
124 * @return integer
125 */
126 public function addLinkObj( &$nt, $len = -1, $redirect = NULL ) {
127 global $wgAntiLockFlags, $wgProfiler;
128
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 if ( isset( $wgProfiler ) ) {
136 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
137 }
138
139 wfProfileIn( $fname );
140
141 $ns = $nt->getNamespace();
142 $t = $nt->getDBkey();
143
144 if ( '' == $title ) {
145 wfProfileOut( $fname );
146 return 0;
147 }
148
149 # Some fields heavily used for linking...
150 if ( $this->mForUpdate ) {
151 $db = wfGetDB( DB_MASTER );
152 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
153 $options = array( 'FOR UPDATE' );
154 } else {
155 $options = array();
156 }
157 } else {
158 $db = wfGetDB( DB_SLAVE );
159 $options = array();
160 }
161
162 $s = $db->selectRow( 'page',
163 array( 'page_id', 'page_len', 'page_is_redirect' ),
164 array( 'page_namespace' => $ns, 'page_title' => $t ),
165 $fname, $options );
166 # Set fields...
167 $id = $s ? $s->page_id : 0;
168 $len = $s ? $s->page_len : -1;
169 $redirect = $s ? $s->page_is_redirect : 0;
170
171 if( 0 == $id ) {
172 $this->addBadLinkObj( $nt );
173 } else {
174 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
175 }
176 wfProfileOut( $fname );
177 return $id;
178 }
179
180 /**
181 * Clears cache
182 */
183 public function clear() {
184 $this->mPageLinks = array();
185 $this->mGoodLinks = array();
186 $this->mGoodLinkFields = array();
187 $this->mBadLinks = array();
188 }
189 }