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
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2005-04-12 01:29:21 +00:00
|
|
|
* @subpackage Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2003-07-06 11:42:42 +00:00
|
|
|
# These are used in incrementalSetup()
|
|
|
|
|
define ('LINKCACHE_GOOD', 0);
|
|
|
|
|
define ('LINKCACHE_BAD', 1);
|
|
|
|
|
define ('LINKCACHE_IMAGE', 2);
|
2005-05-26 10:23:36 +00:00
|
|
|
define ('LINKCACHE_PAGE', 3);
|
2003-07-06 11:42:42 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2005-05-10 08:28:33 +00:00
|
|
|
* @subpackage 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.
|
2005-05-26 10:23:36 +00:00
|
|
|
/* private */ var $mClassVer = 3;
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2005-05-26 10:23:36 +00:00
|
|
|
/* private */ var $mPageLinks;
|
2003-04-14 23:10:40 +00:00
|
|
|
/* private */ var $mGoodLinks, $mBadLinks, $mActive;
|
2004-05-15 00:29:39 +00:00
|
|
|
/* private */ var $mImageLinks, $mCategoryLinks;
|
2003-07-06 11:42:42 +00:00
|
|
|
/* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
|
2004-07-18 08:48:43 +00:00
|
|
|
/* private */ var $mForUpdate;
|
|
|
|
|
|
2003-11-04 08:59:28 +00:00
|
|
|
/* private */ function getKey( $title ) {
|
|
|
|
|
global $wgDBname;
|
2004-08-22 17:24:50 +00:00
|
|
|
return $wgDBname.':lc:title:'.$title;
|
2003-11-04 08:59:28 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function LinkCache() {
|
2003-04-14 23:10:40 +00:00
|
|
|
$this->mActive = true;
|
2003-07-06 11:42:42 +00:00
|
|
|
$this->mPreFilled = false;
|
2004-07-18 08:48:43 +00:00
|
|
|
$this->mForUpdate = false;
|
2005-05-26 10:23:36 +00:00
|
|
|
$this->mPageLinks = array();
|
2003-04-14 23:10:40 +00:00
|
|
|
$this->mGoodLinks = array();
|
|
|
|
|
$this->mBadLinks = array();
|
|
|
|
|
$this->mImageLinks = array();
|
2004-05-15 00:29:39 +00:00
|
|
|
$this->mCategoryLinks = array();
|
2003-07-06 11:42:42 +00:00
|
|
|
$this->mOldGoodLinks = array();
|
|
|
|
|
$this->mOldBadLinks = array();
|
2005-05-29 10:17:44 +00:00
|
|
|
$this->mOldPageLinks = array();
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* General accessor to get/set whether SELECT FOR UPDATE should be used
|
|
|
|
|
*/
|
2005-08-02 13:35:19 +00:00
|
|
|
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
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2005-05-26 10:23:36 +00:00
|
|
|
function addGoodLinkObj( $id, $title ) {
|
2003-04-14 23:10:40 +00:00
|
|
|
if ( $this->mActive ) {
|
2005-05-26 10:23:36 +00:00
|
|
|
$dbkey = $title->getPrefixedDbKey();
|
|
|
|
|
$this->mGoodLinks[$dbkey] = $id;
|
|
|
|
|
$this->mPageLinks[$dbkey] = $title;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-26 10:23:36 +00:00
|
|
|
function addBadLinkObj( $title ) {
|
|
|
|
|
$dbkey = $title->getPrefixedDbKey();
|
|
|
|
|
if ( $this->mActive && ( ! $this->isBadLink( $dbkey ) ) ) {
|
|
|
|
|
$this->mBadLinks[$dbkey] = 1;
|
|
|
|
|
$this->mPageLinks[$dbkey] = $title;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function addImageLink( $title ) {
|
2003-04-14 23:10:40 +00:00
|
|
|
if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function addImageLinkObj( $nt ) {
|
2003-11-04 08:59:28 +00:00
|
|
|
if ( $this->mActive ) { $this->mImageLinks[$nt->getDBkey()] = 1; }
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-05-15 00:29:39 +00:00
|
|
|
function addCategoryLink( $title, $sortkey ) {
|
|
|
|
|
if ( $this->mActive ) { $this->mCategoryLinks[$title] = $sortkey; }
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-05-15 00:29:39 +00:00
|
|
|
function addCategoryLinkObj( &$nt, $sortkey ) {
|
|
|
|
|
$this->addCategoryLink( $nt->getDBkey(), $sortkey );
|
|
|
|
|
}
|
2003-11-04 08:59:28 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function clearBadLink( $title ) {
|
2003-11-24 19:49:32 +00:00
|
|
|
unset( $this->mBadLinks[$title] );
|
2003-11-04 08:59:28 +00:00
|
|
|
$this->clearLink( $title );
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function clearLink( $title ) {
|
2003-11-12 10:21:28 +00:00
|
|
|
global $wgMemc, $wgLinkCacheMemcached;
|
|
|
|
|
if( $wgLinkCacheMemcached )
|
|
|
|
|
$wgMemc->delete( $this->getKey( $title ) );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2005-12-30 09:33:11 +00:00
|
|
|
/** @deprecated */
|
2003-04-14 23:10:40 +00:00
|
|
|
function suspend() { $this->mActive = false; }
|
2005-12-30 09:33:11 +00:00
|
|
|
/** @deprecated */
|
2003-04-14 23:10:40 +00:00
|
|
|
function resume() { $this->mActive = true; }
|
2005-12-30 09:33:11 +00:00
|
|
|
|
2005-05-26 10:23:36 +00:00
|
|
|
function getPageLinks() { return $this->mPageLinks; }
|
2003-04-14 23:10:40 +00:00
|
|
|
function getGoodLinks() { return $this->mGoodLinks; }
|
2003-11-24 19:49:32 +00:00
|
|
|
function getBadLinks() { return array_keys( $this->mBadLinks ); }
|
2003-04-14 23:10:40 +00:00
|
|
|
function getImageLinks() { return $this->mImageLinks; }
|
2004-05-15 00:29:39 +00:00
|
|
|
function getCategoryLinks() { return $this->mCategoryLinks; }
|
2003-04-14 23:10:40 +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
|
|
|
|
|
* @param string $title Title to add
|
|
|
|
|
* @return integer
|
|
|
|
|
*/
|
2004-09-02 23:28:24 +00:00
|
|
|
function addLink( $title ) {
|
2003-11-04 08:59:28 +00:00
|
|
|
$nt = Title::newFromDBkey( $title );
|
|
|
|
|
if( $nt ) {
|
|
|
|
|
return $this->addLinkObj( $nt );
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2003-10-22 23:56:49 +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
|
|
|
|
|
* @param Title $nt Title to add
|
|
|
|
|
* @return integer
|
|
|
|
|
*/
|
2004-09-02 23:28:24 +00:00
|
|
|
function addLinkObj( &$nt ) {
|
2005-05-29 05:54:04 +00:00
|
|
|
global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
|
2003-11-04 08:59:28 +00:00
|
|
|
$title = $nt->getPrefixedDBkey();
|
2005-08-02 13:35:19 +00:00
|
|
|
if ( $this->isBadLink( $title ) ) { return 0; }
|
2003-04-14 23:10:40 +00:00
|
|
|
$id = $this->getGoodLinkID( $title );
|
|
|
|
|
if ( 0 != $id ) { return $id; }
|
|
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
$fname = 'LinkCache::addLinkObj';
|
2005-12-26 22:35:47 +00:00
|
|
|
global $wgProfiling, $wgProfiler;
|
|
|
|
|
if ( $wgProfiling && isset( $wgProfiler ) ) {
|
2005-10-22 20:52:30 +00:00
|
|
|
$fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-16 13:30:45 +00:00
|
|
|
wfProfileIn( $fname );
|
2003-05-16 13:37:02 +00:00
|
|
|
|
2003-04-14 23:10:40 +00:00
|
|
|
$ns = $nt->getNamespace();
|
2003-11-04 08:59:28 +00:00
|
|
|
$t = $nt->getDBkey();
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
if ( '' == $title ) {
|
2003-10-22 23:56:49 +00:00
|
|
|
wfProfileOut( $fname );
|
2005-08-02 13:35:19 +00:00
|
|
|
return 0;
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-01-31 01:47:54 +00:00
|
|
|
$id = NULL;
|
2003-11-12 10:21:28 +00:00
|
|
|
if( $wgLinkCacheMemcached )
|
|
|
|
|
$id = $wgMemc->get( $key = $this->getKey( $title ) );
|
2004-01-31 01:47:54 +00:00
|
|
|
if( ! is_integer( $id ) ) {
|
2004-07-18 08:48:43 +00:00
|
|
|
if ( $this->mForUpdate ) {
|
|
|
|
|
$db =& wfGetDB( DB_MASTER );
|
2005-05-29 05:54:04 +00:00
|
|
|
if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
|
|
|
|
|
$options = array( 'FOR UPDATE' );
|
2005-08-20 01:30:22 +00:00
|
|
|
} else {
|
|
|
|
|
$options = array();
|
2005-05-29 05:54:04 +00:00
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
} else {
|
|
|
|
|
$db =& wfGetDB( DB_SLAVE );
|
|
|
|
|
$options = array();
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
$id = $db->selectField( 'page', 'page_id',
|
|
|
|
|
array( 'page_namespace' => $ns, 'page_title' => $t ),
|
|
|
|
|
$fname, $options );
|
2004-07-10 03:09:26 +00:00
|
|
|
if ( !$id ) {
|
2003-08-11 13:53:20 +00:00
|
|
|
$id = 0;
|
|
|
|
|
}
|
2003-11-12 10:21:28 +00:00
|
|
|
if( $wgLinkCacheMemcached )
|
2004-01-31 01:47:54 +00:00
|
|
|
$wgMemc->add( $key, $id, 3600*24 );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-05-26 10:23:36 +00:00
|
|
|
if( 0 == $id ) {
|
|
|
|
|
$this->addBadLinkObj( $nt );
|
|
|
|
|
} else {
|
|
|
|
|
$this->addGoodLinkObj( $id, $nt );
|
|
|
|
|
}
|
2003-10-16 13:30:45 +00:00
|
|
|
wfProfileOut( $fname );
|
2003-04-14 23:10:40 +00:00
|
|
|
return $id;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-26 10:23:36 +00:00
|
|
|
/**
|
|
|
|
|
* Bulk-check the pagelinks and page arrays for existence info.
|
|
|
|
|
* @param Title $fromtitle
|
2005-12-30 09:33:11 +00:00
|
|
|
* @deprecated
|
2005-05-26 10:23:36 +00:00
|
|
|
*/
|
2004-09-02 23:28:24 +00:00
|
|
|
function preFill( &$fromtitle ) {
|
2005-06-26 08:25:18 +00:00
|
|
|
global $wgAntiLockFlags;
|
2004-08-22 17:24:50 +00:00
|
|
|
$fname = 'LinkCache::preFill';
|
2003-10-16 13:30:45 +00:00
|
|
|
wfProfileIn( $fname );
|
2003-11-08 15:12:34 +00:00
|
|
|
|
2004-03-11 09:06:13 +00:00
|
|
|
$this->suspend();
|
|
|
|
|
$id = $fromtitle->getArticleID();
|
|
|
|
|
$this->resume();
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-03-11 09:06:13 +00:00
|
|
|
if( $id == 0 ) {
|
|
|
|
|
wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
if ( $this->mForUpdate ) {
|
|
|
|
|
$db =& wfGetDB( DB_MASTER );
|
2005-06-26 08:25:18 +00:00
|
|
|
if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
|
|
|
|
|
$options = 'FOR UPDATE';
|
|
|
|
|
} else {
|
|
|
|
|
$options = '';
|
|
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
} else {
|
|
|
|
|
$db =& wfGetDB( DB_SLAVE );
|
|
|
|
|
$options = '';
|
|
|
|
|
}
|
2003-11-08 15:12:34 +00:00
|
|
|
|
2004-12-19 08:00:50 +00:00
|
|
|
$page = $db->tableName( 'page' );
|
2005-05-26 10:23:36 +00:00
|
|
|
$pagelinks = $db->tableName( 'pagelinks' );
|
2004-07-10 03:09:26 +00:00
|
|
|
|
2005-05-26 10:23:36 +00:00
|
|
|
$sql = "SELECT page_id,pl_namespace,pl_title
|
|
|
|
|
FROM $pagelinks
|
|
|
|
|
LEFT JOIN $page
|
|
|
|
|
ON pl_namespace=page_namespace AND pl_title=page_title
|
|
|
|
|
WHERE pl_from=$id $options";
|
2004-07-18 08:48:43 +00:00
|
|
|
$res = $db->query( $sql, $fname );
|
|
|
|
|
while( $s = $db->fetchObject( $res ) ) {
|
2005-05-26 10:23:36 +00:00
|
|
|
$title = Title::makeTitle( $s->pl_namespace, $s->pl_title );
|
|
|
|
|
if( $s->page_id ) {
|
|
|
|
|
$this->addGoodLinkObj( $s->page_id, $title );
|
|
|
|
|
} else {
|
|
|
|
|
$this->addBadLinkObj( $title );
|
|
|
|
|
}
|
2003-05-16 13:37:02 +00:00
|
|
|
}
|
2003-07-06 11:42:42 +00:00
|
|
|
$this->mPreFilled = true;
|
2003-11-08 15:12:34 +00:00
|
|
|
|
2003-10-16 13:30:45 +00:00
|
|
|
wfProfileOut( $fname );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
function clear() {
|
2005-05-26 10:23:36 +00:00
|
|
|
$this->mPageLinks = array();
|
2003-07-06 11:42:42 +00:00
|
|
|
$this->mGoodLinks = array();
|
|
|
|
|
$this->mBadLinks = array();
|
|
|
|
|
$this->mImageLinks = array();
|
2005-05-29 10:17:44 +00:00
|
|
|
$this->mCategoryLinks = array();
|
|
|
|
|
$this->mOldGoodLinks = array();
|
|
|
|
|
$this->mOldBadLinks = array();
|
|
|
|
|
$this->mOldPageLinks = array();
|
2003-07-06 11:42:42 +00:00
|
|
|
}
|
2003-11-27 19:53:40 +00:00
|
|
|
|
2005-05-29 10:17:44 +00:00
|
|
|
/**
|
|
|
|
|
* Swaps old and current link registers
|
2005-12-30 09:33:11 +00:00
|
|
|
* @deprecated
|
2005-05-29 10:17:44 +00:00
|
|
|
*/
|
|
|
|
|
function swapRegisters() {
|
|
|
|
|
swap( $this->mGoodLinks, $this->mOldGoodLinks );
|
|
|
|
|
swap( $this->mBadLinks, $this->mOldBadLinks );
|
|
|
|
|
swap( $this->mImageLinks, $this->mOldImageLinks );
|
2005-06-25 20:54:49 +00:00
|
|
|
swap( $this->mPageLinks, $this->mOldPageLinks );
|
2005-05-29 10:17:44 +00:00
|
|
|
}
|
2003-07-06 11:42:42 +00:00
|
|
|
}
|
2003-04-14 23:10:40 +00:00
|
|
|
?>
|