MediaWiki  REL1_20
LinkCache.php
Go to the documentation of this file.
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 
00044         static function &singleton() {
00045                 static $instance;
00046                 if ( !isset( $instance ) ) {
00047                         $instance = new LinkCache;
00048                 }
00049                 return $instance;
00050         }
00051 
00057         public function forUpdate( $update = null ) {
00058                 return wfSetVar( $this->mForUpdate, $update );
00059         }
00060 
00065         public function getGoodLinkID( $title ) {
00066                 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
00067                         return $this->mGoodLinks[$title];
00068                 } else {
00069                         return 0;
00070                 }
00071         }
00072 
00080         public function getGoodLinkFieldObj( $title, $field ) {
00081                 $dbkey = $title->getPrefixedDbKey();
00082                 if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
00083                         return $this->mGoodLinkFields[$dbkey][$field];
00084                 } else {
00085                         return null;
00086                 }
00087         }
00088 
00093         public function isBadLink( $title ) {
00094                 return array_key_exists( $title, $this->mBadLinks );
00095         }
00096 
00106         public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
00107                 $dbkey = $title->getPrefixedDbKey();
00108                 $this->mGoodLinks[$dbkey] = intval( $id );
00109                 $this->mGoodLinkFields[$dbkey] = array(
00110                         'length' => intval( $len ),
00111                         'redirect' => intval( $redir ),
00112                         'revision' => intval( $revision ) );
00113         }
00114 
00122         public function addGoodLinkObjFromRow( $title, $row ) {
00123                 $dbkey = $title->getPrefixedDbKey();
00124                 $this->mGoodLinks[$dbkey] = intval( $row->page_id );
00125                 $this->mGoodLinkFields[$dbkey] = array(
00126                         'length' => intval( $row->page_len ),
00127                         'redirect' => intval( $row->page_is_redirect ),
00128                         'revision' => intval( $row->page_latest ),
00129                 );
00130         }
00131 
00135         public function addBadLinkObj( $title ) {
00136                 $dbkey = $title->getPrefixedDbKey();
00137                 if ( !$this->isBadLink( $dbkey ) ) {
00138                         $this->mBadLinks[$dbkey] = 1;
00139                 }
00140         }
00141 
00142         public function clearBadLink( $title ) {
00143                 unset( $this->mBadLinks[$title] );
00144         }
00145 
00149         public function clearLink( $title ) {
00150                 $dbkey = $title->getPrefixedDbKey();
00151                 unset( $this->mBadLinks[$dbkey] );
00152                 unset( $this->mGoodLinks[$dbkey] );
00153                 unset( $this->mGoodLinkFields[$dbkey] );
00154         }
00155 
00156         public function getGoodLinks() { return $this->mGoodLinks; }
00157         public function getBadLinks() { return array_keys( $this->mBadLinks ); }
00158 
00165         public function addLink( $title ) {
00166                 $nt = Title::newFromDBkey( $title );
00167                 if( $nt ) {
00168                         return $this->addLinkObj( $nt );
00169                 } else {
00170                         return 0;
00171                 }
00172         }
00173 
00180         public function addLinkObj( $nt ) {
00181                 global $wgAntiLockFlags;
00182                 wfProfileIn( __METHOD__ );
00183 
00184                 $key = $nt->getPrefixedDBkey();
00185                 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
00186                         wfProfileOut( __METHOD__ );
00187                         return 0;
00188                 }
00189                 $id = $this->getGoodLinkID( $key );
00190                 if ( $id != 0 ) {
00191                         wfProfileOut( __METHOD__ );
00192                         return $id;
00193                 }
00194 
00195                 if ( $key === '' ) {
00196                         wfProfileOut( __METHOD__ );
00197                         return 0;
00198                 }
00199 
00200                 # Some fields heavily used for linking...
00201                 if ( $this->mForUpdate ) {
00202                         $db = wfGetDB( DB_MASTER );
00203                         if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
00204                                 $options = array( 'FOR UPDATE' );
00205                         } else {
00206                                 $options = array();
00207                         }
00208                 } else {
00209                         $db = wfGetDB( DB_SLAVE );
00210                         $options = array();
00211                 }
00212 
00213                 $s = $db->selectRow( 'page',
00214                         array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
00215                         array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
00216                         __METHOD__, $options );
00217                 # Set fields...
00218                 if ( $s !== false ) {
00219                         $this->addGoodLinkObjFromRow( $nt, $s );
00220                         $id = intval( $s->page_id );
00221                 } else {
00222                         $this->addBadLinkObj( $nt );
00223                         $id = 0;
00224                 }
00225 
00226                 wfProfileOut( __METHOD__ );
00227                 return $id;
00228         }
00229 
00233         public function clear() {
00234                 $this->mGoodLinks = array();
00235                 $this->mGoodLinkFields = array();
00236                 $this->mBadLinks = array();
00237         }
00238 }