MediaWiki  REL1_19
LinkCache.php
Go to the documentation of this file.
00001 <?php
00007 class LinkCache {
00008         // Increment $mClassVer whenever old serialized versions of this class
00009         // becomes incompatible with the new version.
00010         private $mClassVer = 4;
00011 
00012         private $mGoodLinks = array();
00013         private $mGoodLinkFields = array();
00014         private $mBadLinks = array();
00015         private $mForUpdate = false;
00016 
00022         static function &singleton() {
00023                 static $instance;
00024                 if ( !isset( $instance ) ) {
00025                         $instance = new LinkCache;
00026                 }
00027                 return $instance;
00028         }
00029 
00035         public function forUpdate( $update = null ) {
00036                 return wfSetVar( $this->mForUpdate, $update );
00037         }
00038 
00043         public function getGoodLinkID( $title ) {
00044                 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
00045                         return $this->mGoodLinks[$title];
00046                 } else {
00047                         return 0;
00048                 }
00049         }
00050 
00058         public function getGoodLinkFieldObj( $title, $field ) {
00059                 $dbkey = $title->getPrefixedDbKey();
00060                 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
00061                         return $this->mGoodLinkFields[$dbkey][$field];
00062                 } else {
00063                         return null;
00064                 }
00065         }
00066 
00071         public function isBadLink( $title ) {
00072                 return array_key_exists( $title, $this->mBadLinks );
00073         }
00074 
00084         public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
00085                 $dbkey = $title->getPrefixedDbKey();
00086                 $this->mGoodLinks[$dbkey] = intval( $id );
00087                 $this->mGoodLinkFields[$dbkey] = array(
00088                         'length' => intval( $len ),
00089                         'redirect' => intval( $redir ),
00090                         'revision' => intval( $revision ) );
00091         }
00092 
00100         public function addGoodLinkObjFromRow( $title, $row ) {
00101                 $dbkey = $title->getPrefixedDbKey();
00102                 $this->mGoodLinks[$dbkey] = intval( $row->page_id );
00103                 $this->mGoodLinkFields[$dbkey] = array(
00104                         'length' => intval( $row->page_len ),
00105                         'redirect' => intval( $row->page_is_redirect ),
00106                         'revision' => intval( $row->page_latest ),
00107                 );
00108         }
00109 
00113         public function addBadLinkObj( $title ) {
00114                 $dbkey = $title->getPrefixedDbKey();
00115                 if ( !$this->isBadLink( $dbkey ) ) {
00116                         $this->mBadLinks[$dbkey] = 1;
00117                 }
00118         }
00119 
00120         public function clearBadLink( $title ) {
00121                 unset( $this->mBadLinks[$title] );
00122         }
00123 
00127         public function clearLink( $title ) {
00128                 $dbkey = $title->getPrefixedDbKey();
00129                 unset( $this->mBadLinks[$dbkey] );
00130                 unset( $this->mGoodLinks[$dbkey] );
00131                 unset( $this->mGoodLinkFields[$dbkey] );
00132         }
00133 
00134         public function getGoodLinks() { return $this->mGoodLinks; }
00135         public function getBadLinks() { return array_keys( $this->mBadLinks ); }
00136 
00143         public function addLink( $title ) {
00144                 $nt = Title::newFromDBkey( $title );
00145                 if( $nt ) {
00146                         return $this->addLinkObj( $nt );
00147                 } else {
00148                         return 0;
00149                 }
00150         }
00151 
00158         public function addLinkObj( $nt ) {
00159                 global $wgAntiLockFlags;
00160                 wfProfileIn( __METHOD__ );
00161 
00162                 $key = $nt->getPrefixedDBkey();
00163                 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
00164                         wfProfileOut( __METHOD__ );
00165                         return 0;
00166                 }
00167                 $id = $this->getGoodLinkID( $key );
00168                 if ( $id != 0 ) {
00169                         wfProfileOut( __METHOD__ );
00170                         return $id;
00171                 }
00172 
00173                 if ( $key === '' ) {
00174                         wfProfileOut( __METHOD__ );
00175                         return 0;
00176                 }
00177 
00178                 # Some fields heavily used for linking...
00179                 if ( $this->mForUpdate ) {
00180                         $db = wfGetDB( DB_MASTER );
00181                         if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
00182                                 $options = array( 'FOR UPDATE' );
00183                         } else {
00184                                 $options = array();
00185                         }
00186                 } else {
00187                         $db = wfGetDB( DB_SLAVE );
00188                         $options = array();
00189                 }
00190 
00191                 $s = $db->selectRow( 'page',
00192                         array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
00193                         array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
00194                         __METHOD__, $options );
00195                 # Set fields...
00196                 if ( $s !== false ) {
00197                         $this->addGoodLinkObjFromRow( $nt, $s );
00198                         $id = intval( $s->page_id );
00199                 } else {
00200                         $this->addBadLinkObj( $nt );
00201                         $id = 0;
00202                 }
00203 
00204                 wfProfileOut( __METHOD__ );
00205                 return $id;
00206         }
00207 
00211         public function clear() {
00212                 $this->mGoodLinks = array();
00213                 $this->mGoodLinkFields = array();
00214                 $this->mBadLinks = array();
00215         }
00216 }