2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2012-05-08 12:51:21 +00:00
|
|
|
/**
|
|
|
|
|
* Page existence cache.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Cache
|
|
|
|
|
*/
|
|
|
|
|
|
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.
|
2011-04-18 19:03:14 +00:00
|
|
|
private $mClassVer = 4;
|
2006-05-11 22:40:38 +00:00
|
|
|
|
2011-12-21 18:53:00 +00:00
|
|
|
private $mGoodLinks = array();
|
|
|
|
|
private $mGoodLinkFields = array();
|
|
|
|
|
private $mBadLinks = array();
|
|
|
|
|
private $mForUpdate = false;
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2006-01-05 02:05:53 +00:00
|
|
|
/**
|
|
|
|
|
* Get an instance of this class
|
2011-04-25 22:41:54 +00:00
|
|
|
*
|
|
|
|
|
* @return LinkCache
|
2006-01-05 02:05:53 +00:00
|
|
|
*/
|
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
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* General accessor to get/set whether SELECT FOR UPDATE should be used
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
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
|
|
|
|
2011-05-28 18:58:51 +00:00
|
|
|
/**
|
|
|
|
|
* @param $title
|
|
|
|
|
* @return array|int
|
|
|
|
|
*/
|
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.
|
2010-03-15 21:51:12 +00:00
|
|
|
* @param $title Title
|
2010-07-17 20:13:49 +00:00
|
|
|
* @param $field String: ('length','redirect','revision')
|
2008-04-09 05:21:00 +00:00
|
|
|
* @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
|
|
|
|
2011-05-28 18:58:51 +00:00
|
|
|
/**
|
|
|
|
|
* @param $title
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
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
|
2010-07-17 20:13:49 +00:00
|
|
|
*
|
|
|
|
|
* @param $id Integer: page's ID
|
|
|
|
|
* @param $title Title object
|
|
|
|
|
* @param $len Integer: text's length
|
|
|
|
|
* @param $redir Integer: whether the page is a redirect
|
|
|
|
|
* @param $revision Integer: latest revision's ID
|
2008-04-09 05:21:00 +00:00
|
|
|
*/
|
2010-06-15 12:14:54 +00:00
|
|
|
public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
|
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 ),
|
2010-06-15 12:14:54 +00:00
|
|
|
'redirect' => intval( $redir ),
|
|
|
|
|
'revision' => intval( $revision ) );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-20 15:19:18 +00:00
|
|
|
/**
|
|
|
|
|
* Same as above with better interface.
|
|
|
|
|
* @since 1.19
|
|
|
|
|
* @param $title Title
|
|
|
|
|
* @param $row object which has the fields page_id, page_is_redirect,
|
|
|
|
|
* page_latest
|
|
|
|
|
*/
|
|
|
|
|
public function addGoodLinkObjFromRow( $title, $row ) {
|
|
|
|
|
$dbkey = $title->getPrefixedDbKey();
|
|
|
|
|
$this->mGoodLinks[$dbkey] = intval( $row->page_id );
|
|
|
|
|
$this->mGoodLinkFields[$dbkey] = array(
|
|
|
|
|
'length' => intval( $row->page_len ),
|
|
|
|
|
'redirect' => intval( $row->page_is_redirect ),
|
|
|
|
|
'revision' => intval( $row->page_latest ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-26 19:21:50 +00:00
|
|
|
/**
|
|
|
|
|
* @param $title Title
|
|
|
|
|
*/
|
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
|
|
|
|
2011-05-26 19:21:50 +00:00
|
|
|
/**
|
|
|
|
|
* @param $title Title
|
|
|
|
|
*/
|
2008-09-07 08:24:06 +00:00
|
|
|
public function clearLink( $title ) {
|
|
|
|
|
$dbkey = $title->getPrefixedDbKey();
|
2011-09-20 13:52:47 +00:00
|
|
|
unset( $this->mBadLinks[$dbkey] );
|
|
|
|
|
unset( $this->mGoodLinks[$dbkey] );
|
|
|
|
|
unset( $this->mGoodLinkFields[$dbkey] );
|
2008-09-07 08:24:06 +00:00
|
|
|
}
|
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
|
2010-07-17 20:13:49 +00:00
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $title String: title to add
|
2010-07-17 20:13:49 +00:00
|
|
|
* @return Integer
|
2005-12-30 09:33:11 +00:00
|
|
|
*/
|
2010-07-17 20:13:49 +00:00
|
|
|
public function addLink( $title ) {
|
2003-11-04 08:59:28 +00:00
|
|
|
$nt = Title::newFromDBkey( $title );
|
|
|
|
|
if( $nt ) {
|
2010-07-17 20:13:49 +00:00
|
|
|
return $this->addLinkObj( $nt );
|
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
|
2010-07-17 20:13:49 +00:00
|
|
|
*
|
|
|
|
|
* @param $nt Title object to add
|
|
|
|
|
* @return Integer
|
2005-12-30 09:33:11 +00:00
|
|
|
*/
|
2010-07-17 20:13:49 +00:00
|
|
|
public function addLinkObj( $nt ) {
|
2010-07-24 19:11:52 +00:00
|
|
|
global $wgAntiLockFlags;
|
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();
|
2010-12-22 15:54:27 +00:00
|
|
|
if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
|
2008-08-29 19:12:56 +00:00
|
|
|
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
|
|
|
}
|
2011-02-12 04:06:22 +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
|
|
|
|
2011-02-12 04:06:22 +00:00
|
|
|
$s = $db->selectRow( 'page',
|
2010-06-15 12:14:54 +00:00
|
|
|
array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
|
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 ) {
|
2011-09-20 15:19:18 +00:00
|
|
|
$this->addGoodLinkObjFromRow( $nt, $s );
|
2011-09-20 16:26:39 +00:00
|
|
|
$id = intval( $s->page_id );
|
2008-08-29 19:12:56 +00:00
|
|
|
} else {
|
2005-05-26 10:23:36 +00:00
|
|
|
$this->addBadLinkObj( $nt );
|
2011-09-20 16:26:39 +00:00
|
|
|
$id = 0;
|
2005-05-26 10:23:36 +00:00
|
|
|
}
|
2011-09-20 15:19:18 +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
|
|
|
}
|