MediaWiki  REL1_21
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 
00107         public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false, $model = false ) {
00108                 $dbkey = $title->getPrefixedDBkey();
00109                 $this->mGoodLinks[$dbkey] = intval( $id );
00110                 $this->mGoodLinkFields[$dbkey] = array(
00111                         'length' => intval( $len ),
00112                         'redirect' => intval( $redir ),
00113                         'revision' => intval( $revision ),
00114                         'model' => intval( $model ) );
00115         }
00116 
00124         public function addGoodLinkObjFromRow( $title, $row ) {
00125                 $dbkey = $title->getPrefixedDBkey();
00126                 $this->mGoodLinks[$dbkey] = intval( $row->page_id );
00127                 $this->mGoodLinkFields[$dbkey] = array(
00128                         'length' => intval( $row->page_len ),
00129                         'redirect' => intval( $row->page_is_redirect ),
00130                         'revision' => intval( $row->page_latest ),
00131                         'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
00132                 );
00133         }
00134 
00138         public function addBadLinkObj( $title ) {
00139                 $dbkey = $title->getPrefixedDBkey();
00140                 if ( !$this->isBadLink( $dbkey ) ) {
00141                         $this->mBadLinks[$dbkey] = 1;
00142                 }
00143         }
00144 
00145         public function clearBadLink( $title ) {
00146                 unset( $this->mBadLinks[$title] );
00147         }
00148 
00152         public function clearLink( $title ) {
00153                 $dbkey = $title->getPrefixedDBkey();
00154                 unset( $this->mBadLinks[$dbkey] );
00155                 unset( $this->mGoodLinks[$dbkey] );
00156                 unset( $this->mGoodLinkFields[$dbkey] );
00157         }
00158 
00159         public function getGoodLinks() { return $this->mGoodLinks; }
00160         public function getBadLinks() { return array_keys( $this->mBadLinks ); }
00161 
00168         public function addLink( $title ) {
00169                 $nt = Title::newFromDBkey( $title );
00170                 if( $nt ) {
00171                         return $this->addLinkObj( $nt );
00172                 } else {
00173                         return 0;
00174                 }
00175         }
00176 
00183         public function addLinkObj( $nt ) {
00184                 global $wgAntiLockFlags, $wgContentHandlerUseDB;
00185 
00186                 wfProfileIn( __METHOD__ );
00187 
00188                 $key = $nt->getPrefixedDBkey();
00189                 if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
00190                         wfProfileOut( __METHOD__ );
00191                         return 0;
00192                 }
00193                 $id = $this->getGoodLinkID( $key );
00194                 if ( $id != 0 ) {
00195                         wfProfileOut( __METHOD__ );
00196                         return $id;
00197                 }
00198 
00199                 if ( $key === '' ) {
00200                         wfProfileOut( __METHOD__ );
00201                         return 0;
00202                 }
00203 
00204                 # Some fields heavily used for linking...
00205                 if ( $this->mForUpdate ) {
00206                         $db = wfGetDB( DB_MASTER );
00207                         if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
00208                                 $options = array( 'FOR UPDATE' );
00209                         } else {
00210                                 $options = array();
00211                         }
00212                 } else {
00213                         $db = wfGetDB( DB_SLAVE );
00214                         $options = array();
00215                 }
00216 
00217                 $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
00218                 if ( $wgContentHandlerUseDB ) $f[] = 'page_content_model';
00219 
00220                 $s = $db->selectRow( 'page', $f,
00221                         array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
00222                         __METHOD__, $options );
00223                 # Set fields...
00224                 if ( $s !== false ) {
00225                         $this->addGoodLinkObjFromRow( $nt, $s );
00226                         $id = intval( $s->page_id );
00227                 } else {
00228                         $this->addBadLinkObj( $nt );
00229                         $id = 0;
00230                 }
00231 
00232                 wfProfileOut( __METHOD__ );
00233                 return $id;
00234         }
00235 
00239         public function clear() {
00240                 $this->mGoodLinks = array();
00241                 $this->mGoodLinkFields = array();
00242                 $this->mBadLinks = array();
00243         }
00244 }