MediaWiki
REL1_24
|
00001 <?php 00029 class LinkCache { 00030 // Increment $mClassVer whenever old serialized versions of this class 00031 // becomes incompatible with the new version. 00032 private $mClassVer = 4; 00033 00034 private $mGoodLinks = array(); 00035 private $mGoodLinkFields = array(); 00036 private $mBadLinks = array(); 00037 private $mForUpdate = false; 00038 00042 protected static $instance; 00043 00049 static function &singleton() { 00050 if ( self::$instance ) { 00051 return self::$instance; 00052 } 00053 self::$instance = new LinkCache; 00054 00055 return self::$instance; 00056 } 00057 00063 static function destroySingleton() { 00064 self::$instance = null; 00065 } 00066 00074 static function setSingleton( LinkCache $instance ) { 00075 self::$instance = $instance; 00076 } 00077 00084 public function forUpdate( $update = null ) { 00085 return wfSetVar( $this->mForUpdate, $update ); 00086 } 00087 00092 public function getGoodLinkID( $title ) { 00093 if ( array_key_exists( $title, $this->mGoodLinks ) ) { 00094 return $this->mGoodLinks[$title]; 00095 } else { 00096 return 0; 00097 } 00098 } 00099 00107 public function getGoodLinkFieldObj( $title, $field ) { 00108 $dbkey = $title->getPrefixedDBkey(); 00109 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) { 00110 return $this->mGoodLinkFields[$dbkey][$field]; 00111 } else { 00112 return null; 00113 } 00114 } 00115 00120 public function isBadLink( $title ) { 00121 return array_key_exists( $title, $this->mBadLinks ); 00122 } 00123 00134 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, 00135 $revision = 0, $model = null 00136 ) { 00137 $dbkey = $title->getPrefixedDBkey(); 00138 $this->mGoodLinks[$dbkey] = (int)$id; 00139 $this->mGoodLinkFields[$dbkey] = array( 00140 'length' => (int)$len, 00141 'redirect' => (int)$redir, 00142 'revision' => (int)$revision, 00143 'model' => $model ? (string)$model : null, 00144 ); 00145 } 00146 00154 public function addGoodLinkObjFromRow( $title, $row ) { 00155 $dbkey = $title->getPrefixedDBkey(); 00156 $this->mGoodLinks[$dbkey] = intval( $row->page_id ); 00157 $this->mGoodLinkFields[$dbkey] = array( 00158 'length' => intval( $row->page_len ), 00159 'redirect' => intval( $row->page_is_redirect ), 00160 'revision' => intval( $row->page_latest ), 00161 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null, 00162 ); 00163 } 00164 00168 public function addBadLinkObj( $title ) { 00169 $dbkey = $title->getPrefixedDBkey(); 00170 if ( !$this->isBadLink( $dbkey ) ) { 00171 $this->mBadLinks[$dbkey] = 1; 00172 } 00173 } 00174 00175 public function clearBadLink( $title ) { 00176 unset( $this->mBadLinks[$title] ); 00177 } 00178 00182 public function clearLink( $title ) { 00183 $dbkey = $title->getPrefixedDBkey(); 00184 unset( $this->mBadLinks[$dbkey] ); 00185 unset( $this->mGoodLinks[$dbkey] ); 00186 unset( $this->mGoodLinkFields[$dbkey] ); 00187 } 00188 00189 public function getGoodLinks() { 00190 return $this->mGoodLinks; 00191 } 00192 00193 public function getBadLinks() { 00194 return array_keys( $this->mBadLinks ); 00195 } 00196 00203 public function addLink( $title ) { 00204 $nt = Title::newFromDBkey( $title ); 00205 if ( $nt ) { 00206 return $this->addLinkObj( $nt ); 00207 } else { 00208 return 0; 00209 } 00210 } 00211 00218 public function addLinkObj( $nt ) { 00219 global $wgAntiLockFlags, $wgContentHandlerUseDB; 00220 00221 wfProfileIn( __METHOD__ ); 00222 00223 $key = $nt->getPrefixedDBkey(); 00224 if ( $this->isBadLink( $key ) || $nt->isExternal() ) { 00225 wfProfileOut( __METHOD__ ); 00226 00227 return 0; 00228 } 00229 $id = $this->getGoodLinkID( $key ); 00230 if ( $id != 0 ) { 00231 wfProfileOut( __METHOD__ ); 00232 00233 return $id; 00234 } 00235 00236 if ( $key === '' ) { 00237 wfProfileOut( __METHOD__ ); 00238 00239 return 0; 00240 } 00241 00242 # Some fields heavily used for linking... 00243 if ( $this->mForUpdate ) { 00244 $db = wfGetDB( DB_MASTER ); 00245 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) { 00246 $options = array( 'FOR UPDATE' ); 00247 } else { 00248 $options = array(); 00249 } 00250 } else { 00251 $db = wfGetDB( DB_SLAVE ); 00252 $options = array(); 00253 } 00254 00255 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ); 00256 if ( $wgContentHandlerUseDB ) { 00257 $f[] = 'page_content_model'; 00258 } 00259 00260 $s = $db->selectRow( 'page', $f, 00261 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ), 00262 __METHOD__, $options ); 00263 # Set fields... 00264 if ( $s !== false ) { 00265 $this->addGoodLinkObjFromRow( $nt, $s ); 00266 $id = intval( $s->page_id ); 00267 } else { 00268 $this->addBadLinkObj( $nt ); 00269 $id = 0; 00270 } 00271 00272 wfProfileOut( __METHOD__ ); 00273 00274 return $id; 00275 } 00276 00280 public function clear() { 00281 $this->mGoodLinks = array(); 00282 $this->mGoodLinkFields = array(); 00283 $this->mBadLinks = array(); 00284 } 00285 }