00001 <?php
00007 class LinkCache {
00008
00009
00010 var $mClassVer = 4;
00011
00012 var $mGoodLinks, $mBadLinks;
00013 var $mForUpdate;
00014
00018 static function &singleton() {
00019 static $instance;
00020 if ( !isset( $instance ) ) {
00021 $instance = new LinkCache;
00022 }
00023 return $instance;
00024 }
00025
00026 function __construct() {
00027 $this->mForUpdate = false;
00028 $this->mGoodLinks = array();
00029 $this->mGoodLinkFields = array();
00030 $this->mBadLinks = array();
00031 }
00032
00036 public function forUpdate( $update = null ) {
00037 return wfSetVar( $this->mForUpdate, $update );
00038 }
00039
00040 public function getGoodLinkID( $title ) {
00041 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
00042 return $this->mGoodLinks[$title];
00043 } else {
00044 return 0;
00045 }
00046 }
00047
00055 public function getGoodLinkFieldObj( $title, $field ) {
00056 $dbkey = $title->getPrefixedDbKey();
00057 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
00058 return $this->mGoodLinkFields[$dbkey][$field];
00059 } else {
00060 return null;
00061 }
00062 }
00063
00064 public function isBadLink( $title ) {
00065 return array_key_exists( $title, $this->mBadLinks );
00066 }
00067
00075 public function addGoodLinkObj( $id, $title, $len = -1, $redir = null ) {
00076 $dbkey = $title->getPrefixedDbKey();
00077 $this->mGoodLinks[$dbkey] = intval( $id );
00078 $this->mGoodLinkFields[$dbkey] = array(
00079 'length' => intval( $len ),
00080 'redirect' => intval( $redir ) );
00081 }
00082
00083 public function addBadLinkObj( $title ) {
00084 $dbkey = $title->getPrefixedDbKey();
00085 if ( !$this->isBadLink( $dbkey ) ) {
00086 $this->mBadLinks[$dbkey] = 1;
00087 }
00088 }
00089
00090 public function clearBadLink( $title ) {
00091 unset( $this->mBadLinks[$title] );
00092 }
00093
00094 public function clearLink( $title ) {
00095 $dbkey = $title->getPrefixedDbKey();
00096 if( isset($this->mBadLinks[$dbkey]) ) {
00097 unset($this->mBadLinks[$dbkey]);
00098 }
00099 if( isset($this->mGoodLinks[$dbkey]) ) {
00100 unset($this->mGoodLinks[$dbkey]);
00101 }
00102 if( isset($this->mGoodLinkFields[$dbkey]) ) {
00103 unset($this->mGoodLinkFields[$dbkey]);
00104 }
00105 }
00106
00107 public function getGoodLinks() { return $this->mGoodLinks; }
00108 public function getBadLinks() { return array_keys( $this->mBadLinks ); }
00109
00117 public function addLink( $title, $len = -1, $redir = null ) {
00118 $nt = Title::newFromDBkey( $title );
00119 if( $nt ) {
00120 return $this->addLinkObj( $nt, $len, $redir );
00121 } else {
00122 return 0;
00123 }
00124 }
00125
00133 public function addLinkObj( &$nt, $len = -1, $redirect = null ) {
00134 global $wgAntiLockFlags, $wgProfiler;
00135 wfProfileIn( __METHOD__ );
00136
00137 $key = $nt->getPrefixedDBkey();
00138 if ( $this->isBadLink( $key ) ) {
00139 wfProfileOut( __METHOD__ );
00140 return 0;
00141 }
00142 $id = $this->getGoodLinkID( $key );
00143 if ( $id != 0 ) {
00144 wfProfileOut( __METHOD__ );
00145 return $id;
00146 }
00147
00148 if ( $key === '' ) {
00149 wfProfileOut( __METHOD__ );
00150 return 0;
00151 }
00152
00153 # Some fields heavily used for linking...
00154 if ( $this->mForUpdate ) {
00155 $db = wfGetDB( DB_MASTER );
00156 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
00157 $options = array( 'FOR UPDATE' );
00158 } else {
00159 $options = array();
00160 }
00161 } else {
00162 $db = wfGetDB( DB_SLAVE );
00163 $options = array();
00164 }
00165
00166 $s = $db->selectRow( 'page',
00167 array( 'page_id', 'page_len', 'page_is_redirect' ),
00168 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
00169 __METHOD__, $options );
00170 # Set fields...
00171 if ( $s !== false ) {
00172 $id = intval( $s->page_id );
00173 $len = intval( $s->page_len );
00174 $redirect = intval( $s->page_is_redirect );
00175 } else {
00176 $len = -1;
00177 $redirect = 0;
00178 }
00179
00180 if ( $id == 0 ) {
00181 $this->addBadLinkObj( $nt );
00182 } else {
00183 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
00184 }
00185 wfProfileOut( __METHOD__ );
00186 return $id;
00187 }
00188
00192 public function clear() {
00193 $this->mGoodLinks = array();
00194 $this->mGoodLinkFields = array();
00195 $this->mBadLinks = array();
00196 }
00197 }