2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Cache for article titles (prefixed DB keys) and ids linked from one source
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2005-08-02 13:35:19 +00:00
|
|
|
class LinkCache {
|
2003-11-27 19:53:40 +00:00
|
|
|
// Increment $mClassVer whenever old serialized versions of this class
|
|
|
|
|
// becomes incompatible with the new version.
|
2008-04-09 05:21:00 +00:00
|
|
|
/* private */ var $mClassVer = 4;
|
2006-05-11 22:40:38 +00:00
|
|
|
|
|
|
|
|
/* private */ var $mGoodLinks, $mBadLinks;
|
|
|
|
|
/* private */ var $mForUpdate;
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2006-01-05 02:05:53 +00:00
|
|
|
/**
|
|
|
|
|
* Get an instance of this class
|
|
|
|
|
*/
|
2006-07-10 15:41:30 +00:00
|
|
|
static function &singleton() {
|
2006-01-05 02:05:53 +00:00
|
|
|
static $instance;
|
|
|
|
|
if ( !isset( $instance ) ) {
|
|
|
|
|
$instance = new LinkCache;
|
|
|
|
|
}
|
|
|
|
|
return $instance;
|
2003-11-04 08:59:28 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2007-01-20 13:34:31 +00:00
|
|
|
function __construct() {
|
2004-07-18 08:48:43 +00:00
|
|
|
$this->mForUpdate = false;
|
2003-04-14 23:10:40 +00:00
|
|
|
$this->mGoodLinks = array();
|
2008-04-09 05:21:00 +00:00
|
|
|
$this->mGoodLinkFields = array();
|
2003-04-14 23:10:40 +00:00
|
|
|
$this->mBadLinks = array();
|
2006-01-05 02:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* General accessor to get/set whether SELECT FOR UPDATE should be used
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
public function forUpdate( $update = null ) {
|
2004-07-18 08:48:43 +00:00
|
|
|
return wfSetVar( $this->mForUpdate, $update );
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-04-09 13:02:34 +00:00
|
|
|
public function getGoodLinkID( $title ) {
|
2003-04-14 23:10:40 +00:00
|
|
|
if ( array_key_exists( $title, $this->mGoodLinks ) ) {
|
|
|
|
|
return $this->mGoodLinks[$title];
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-04-09 05:21:00 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Get a field of a title object from cache.
|
2008-04-09 05:21:00 +00:00
|
|
|
* If this link is not good, it will return NULL.
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param string $field ('length','redirect')
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function getGoodLinkFieldObj( $title, $field ) {
|
2008-04-09 05:21:00 +00:00
|
|
|
$dbkey = $title->getPrefixedDbKey();
|
|
|
|
|
if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
|
|
|
|
|
return $this->mGoodLinkFields[$dbkey][$field];
|
|
|
|
|
} else {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2008-04-09 05:21:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2008-04-09 13:02:34 +00:00
|
|
|
public function isBadLink( $title ) {
|
2005-08-02 13:35:19 +00:00
|
|
|
return array_key_exists( $title, $this->mBadLinks );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2008-04-09 05:21:00 +00:00
|
|
|
/**
|
|
|
|
|
* Add a link for the title to the link cache
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param int $len
|
|
|
|
|
* @param int $redir
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
public function addGoodLinkObj( $id, $title, $len = -1, $redir = null ) {
|
2006-01-05 02:05:53 +00:00
|
|
|
$dbkey = $title->getPrefixedDbKey();
|
2009-08-21 18:11:13 +00:00
|
|
|
$this->mGoodLinks[$dbkey] = intval( $id );
|
|
|
|
|
$this->mGoodLinkFields[$dbkey] = array(
|
|
|
|
|
'length' => intval( $len ),
|
|
|
|
|
'redirect' => intval( $redir ) );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2008-04-09 13:02:34 +00:00
|
|
|
public function addBadLinkObj( $title ) {
|
2005-05-26 10:23:36 +00:00
|
|
|
$dbkey = $title->getPrefixedDbKey();
|
2008-08-29 19:12:56 +00:00
|
|
|
if ( !$this->isBadLink( $dbkey ) ) {
|
2005-05-26 10:23:36 +00:00
|
|
|
$this->mBadLinks[$dbkey] = 1;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-09 13:02:34 +00:00
|
|
|
public function clearBadLink( $title ) {
|
2003-11-24 19:49:32 +00:00
|
|
|
unset( $this->mBadLinks[$title] );
|
2003-11-04 08:59:28 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-09-07 08:24:06 +00:00
|
|
|
public function clearLink( $title ) {
|
|
|
|
|
$dbkey = $title->getPrefixedDbKey();
|
|
|
|
|
if( isset($this->mBadLinks[$dbkey]) ) {
|
|
|
|
|
unset($this->mBadLinks[$dbkey]);
|
|
|
|
|
}
|
|
|
|
|
if( isset($this->mGoodLinks[$dbkey]) ) {
|
|
|
|
|
unset($this->mGoodLinks[$dbkey]);
|
|
|
|
|
}
|
|
|
|
|
if( isset($this->mGoodLinkFields[$dbkey]) ) {
|
|
|
|
|
unset($this->mGoodLinkFields[$dbkey]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2008-04-09 13:02:34 +00:00
|
|
|
public function getGoodLinks() { return $this->mGoodLinks; }
|
|
|
|
|
public function getBadLinks() { return array_keys( $this->mBadLinks ); }
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-12-30 09:33:11 +00:00
|
|
|
/**
|
|
|
|
|
* Add a title to the link cache, return the page_id or zero if non-existent
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $title String: title to add
|
2008-04-09 12:43:48 +00:00
|
|
|
* @param $len int, page size
|
|
|
|
|
* @param $redir bool, is redirect?
|
2005-12-30 09:33:11 +00:00
|
|
|
* @return integer
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
public function addLink( $title, $len = -1, $redir = null ) {
|
2003-11-04 08:59:28 +00:00
|
|
|
$nt = Title::newFromDBkey( $title );
|
|
|
|
|
if( $nt ) {
|
2008-04-09 12:43:48 +00:00
|
|
|
return $this->addLinkObj( $nt, $len, $redir );
|
2003-11-04 08:59:28 +00:00
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2003-10-22 23:56:49 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-12-30 09:33:11 +00:00
|
|
|
/**
|
|
|
|
|
* Add a title to the link cache, return the page_id or zero if non-existent
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $nt Title to add.
|
2008-04-09 12:43:48 +00:00
|
|
|
* @param $len int, page size
|
|
|
|
|
* @param $redir bool, is redirect?
|
2005-12-30 09:33:11 +00:00
|
|
|
* @return integer
|
|
|
|
|
*/
|
2009-12-11 21:07:27 +00:00
|
|
|
public function addLinkObj( &$nt, $len = -1, $redirect = null ) {
|
2008-04-14 16:41:54 +00:00
|
|
|
global $wgAntiLockFlags, $wgProfiler;
|
2008-08-29 19:12:56 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2008-04-14 16:41:54 +00:00
|
|
|
|
2008-08-29 19:12:56 +00:00
|
|
|
$key = $nt->getPrefixedDBkey();
|
|
|
|
|
if ( $this->isBadLink( $key ) ) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$id = $this->getGoodLinkID( $key );
|
|
|
|
|
if ( $id != 0 ) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $id;
|
2005-10-22 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-29 19:12:56 +00:00
|
|
|
if ( $key === '' ) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
2005-08-02 13:35:19 +00:00
|
|
|
return 0;
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
2008-08-29 19:12:56 +00:00
|
|
|
|
2008-04-09 05:21:00 +00:00
|
|
|
# Some fields heavily used for linking...
|
2008-04-14 16:41:54 +00:00
|
|
|
if ( $this->mForUpdate ) {
|
|
|
|
|
$db = wfGetDB( DB_MASTER );
|
|
|
|
|
if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
|
|
|
|
|
$options = array( 'FOR UPDATE' );
|
2004-07-18 08:48:43 +00:00
|
|
|
} else {
|
|
|
|
|
$options = array();
|
|
|
|
|
}
|
2008-04-14 16:41:54 +00:00
|
|
|
} else {
|
|
|
|
|
$db = wfGetDB( DB_SLAVE );
|
|
|
|
|
$options = array();
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2008-04-14 16:41:54 +00:00
|
|
|
$s = $db->selectRow( 'page',
|
|
|
|
|
array( 'page_id', 'page_len', 'page_is_redirect' ),
|
2008-08-29 19:12:56 +00:00
|
|
|
array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
|
|
|
|
|
__METHOD__, $options );
|
2008-04-14 16:41:54 +00:00
|
|
|
# Set fields...
|
2008-08-29 19:12:56 +00:00
|
|
|
if ( $s !== false ) {
|
2009-08-21 18:11:13 +00:00
|
|
|
$id = intval( $s->page_id );
|
|
|
|
|
$len = intval( $s->page_len );
|
|
|
|
|
$redirect = intval( $s->page_is_redirect );
|
2008-08-29 19:12:56 +00:00
|
|
|
} else {
|
|
|
|
|
$len = -1;
|
|
|
|
|
$redirect = 0;
|
|
|
|
|
}
|
2008-04-14 16:41:54 +00:00
|
|
|
|
2008-08-29 19:12:56 +00:00
|
|
|
if ( $id == 0 ) {
|
2005-05-26 10:23:36 +00:00
|
|
|
$this->addBadLinkObj( $nt );
|
|
|
|
|
} else {
|
2008-04-09 05:21:00 +00:00
|
|
|
$this->addGoodLinkObj( $id, $nt, $len, $redirect );
|
2005-05-26 10:23:36 +00:00
|
|
|
}
|
2008-08-29 19:12:56 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2003-04-14 23:10:40 +00:00
|
|
|
return $id;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2005-08-02 13:35:19 +00:00
|
|
|
* Clears cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function clear() {
|
2003-07-06 11:42:42 +00:00
|
|
|
$this->mGoodLinks = array();
|
2008-04-09 05:21:00 +00:00
|
|
|
$this->mGoodLinkFields = array();
|
2003-07-06 11:42:42 +00:00
|
|
|
$this->mBadLinks = array();
|
2005-05-29 10:17:44 +00:00
|
|
|
}
|
2003-07-06 11:42:42 +00:00
|
|
|
}
|