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] = $id;
00078 $this->mGoodLinkFields[$dbkey] = array( 'length' => $len, 'redirect' => $redir );
00079 }
00080
00081 public function addBadLinkObj( $title ) {
00082 $dbkey = $title->getPrefixedDbKey();
00083 if ( !$this->isBadLink( $dbkey ) ) {
00084 $this->mBadLinks[$dbkey] = 1;
00085 }
00086 }
00087
00088 public function clearBadLink( $title ) {
00089 unset( $this->mBadLinks[$title] );
00090 }
00091
00092 public function clearLink( $title ) {
00093 $dbkey = $title->getPrefixedDbKey();
00094 if( isset($this->mBadLinks[$dbkey]) ) {
00095 unset($this->mBadLinks[$dbkey]);
00096 }
00097 if( isset($this->mGoodLinks[$dbkey]) ) {
00098 unset($this->mGoodLinks[$dbkey]);
00099 }
00100 if( isset($this->mGoodLinkFields[$dbkey]) ) {
00101 unset($this->mGoodLinkFields[$dbkey]);
00102 }
00103 }
00104
00105 public function getGoodLinks() { return $this->mGoodLinks; }
00106 public function getBadLinks() { return array_keys( $this->mBadLinks ); }
00107
00115 public function addLink( $title, $len = -1, $redir = NULL ) {
00116 $nt = Title::newFromDBkey( $title );
00117 if( $nt ) {
00118 return $this->addLinkObj( $nt, $len, $redir );
00119 } else {
00120 return 0;
00121 }
00122 }
00123
00131 public function addLinkObj( &$nt, $len = -1, $redirect = NULL ) {
00132 global $wgAntiLockFlags, $wgProfiler;
00133 wfProfileIn( __METHOD__ );
00134
00135 $key = $nt->getPrefixedDBkey();
00136 if ( $this->isBadLink( $key ) ) {
00137 wfProfileOut( __METHOD__ );
00138 return 0;
00139 }
00140 $id = $this->getGoodLinkID( $key );
00141 if ( $id != 0 ) {
00142 wfProfileOut( __METHOD__ );
00143 return $id;
00144 }
00145
00146 if ( $key === '' ) {
00147 wfProfileOut( __METHOD__ );
00148 return 0;
00149 }
00150
00151 # Some fields heavily used for linking...
00152 if ( $this->mForUpdate ) {
00153 $db = wfGetDB( DB_MASTER );
00154 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
00155 $options = array( 'FOR UPDATE' );
00156 } else {
00157 $options = array();
00158 }
00159 } else {
00160 $db = wfGetDB( DB_SLAVE );
00161 $options = array();
00162 }
00163
00164 $s = $db->selectRow( 'page',
00165 array( 'page_id', 'page_len', 'page_is_redirect' ),
00166 array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
00167 __METHOD__, $options );
00168 # Set fields...
00169 if ( $s !== false ) {
00170 $id = $s->page_id;
00171 $len = $s->page_len;
00172 $redirect = $s->page_is_redirect;
00173 } else {
00174 $len = -1;
00175 $redirect = 0;
00176 }
00177
00178 if ( $id == 0 ) {
00179 $this->addBadLinkObj( $nt );
00180 } else {
00181 $this->addGoodLinkObj( $id, $nt, $len, $redirect );
00182 }
00183 wfProfileOut( __METHOD__ );
00184 return $id;
00185 }
00186
00190 public function clear() {
00191 $this->mGoodLinks = array();
00192 $this->mGoodLinkFields = array();
00193 $this->mBadLinks = array();
00194 }
00195 }