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
|
|
|
/**
|
2013-05-20 10:37:15 +00:00
|
|
|
* @var LinkCache
|
|
|
|
|
*/
|
|
|
|
|
protected static $instance;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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() {
|
2013-05-20 10:37:15 +00:00
|
|
|
if ( self::$instance ) {
|
|
|
|
|
return self::$instance;
|
2006-01-05 02:05:53 +00:00
|
|
|
}
|
2013-05-20 10:37:15 +00:00
|
|
|
self::$instance = new LinkCache;
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2013-05-20 10:37:15 +00:00
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroy the singleton instance, a new one will be created next time
|
|
|
|
|
* singleton() is called.
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
static function destroySingleton() {
|
|
|
|
|
self::$instance = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the singleton instance to a given object.
|
|
|
|
|
* Since we do not have an interface for LinkCache, you have to be sure the
|
|
|
|
|
* given object implements all the LinkCache public methods.
|
|
|
|
|
* @param LinkCache $instance
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
static function setSingleton( LinkCache $instance ) {
|
|
|
|
|
self::$instance = $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
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param bool $update
|
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
|
|
|
/**
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $title
|
2014-05-11 16:19:00 +00:00
|
|
|
* @return int
|
2011-05-28 18:58:51 +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.
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param Title $title
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $field ('length','redirect','revision','model')
|
2014-04-18 23:19:46 +00:00
|
|
|
* @return string|null
|
2008-04-09 05:21:00 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function getGoodLinkFieldObj( $title, $field ) {
|
2013-03-09 20:14:22 +00:00
|
|
|
$dbkey = $title->getPrefixedDBkey();
|
2008-04-09 05:21:00 +00:00
|
|
|
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
|
|
|
/**
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $title
|
2011-05-28 18:58:51 +00:00
|
|
|
* @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
|
|
|
*
|
2013-11-17 20:42:23 +00:00
|
|
|
* @param int $id Page's ID
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param int $len Text's length
|
|
|
|
|
* @param int $redir Whether the page is a redirect
|
|
|
|
|
* @param int $revision Latest revision's ID
|
|
|
|
|
* @param int $model Latest revision's content model ID
|
2008-04-09 05:21:00 +00:00
|
|
|
*/
|
2013-11-17 20:42:23 +00:00
|
|
|
public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
|
|
|
|
|
$revision = 0, $model = 0
|
|
|
|
|
) {
|
2013-03-09 20:14:22 +00:00
|
|
|
$dbkey = $title->getPrefixedDBkey();
|
2013-11-17 20:42:23 +00:00
|
|
|
$this->mGoodLinks[$dbkey] = (int)$id;
|
2009-08-21 18:11:13 +00:00
|
|
|
$this->mGoodLinkFields[$dbkey] = array(
|
2013-11-17 20:42:23 +00:00
|
|
|
'length' => (int)$len,
|
|
|
|
|
'redirect' => (int)$redir,
|
|
|
|
|
'revision' => (int)$revision,
|
|
|
|
|
'model' => (int)$model
|
|
|
|
|
);
|
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
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param Title $title
|
|
|
|
|
* @param stdClass $row Object which has the fields page_id, page_is_redirect,
|
2012-06-11 13:36:52 +00:00
|
|
|
* page_latest and page_content_model
|
2011-09-20 15:19:18 +00:00
|
|
|
*/
|
|
|
|
|
public function addGoodLinkObjFromRow( $title, $row ) {
|
2013-03-09 20:14:22 +00:00
|
|
|
$dbkey = $title->getPrefixedDBkey();
|
2011-09-20 15:19:18 +00:00
|
|
|
$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 ),
|
2012-06-25 21:30:51 +00:00
|
|
|
'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
|
2011-09-20 15:19:18 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-26 19:21:50 +00:00
|
|
|
/**
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param Title $title
|
2011-05-26 19:21:50 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function addBadLinkObj( $title ) {
|
2013-03-09 20:14:22 +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
|
|
|
/**
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param Title $title
|
2011-05-26 19:21:50 +00:00
|
|
|
*/
|
2008-09-07 08:24:06 +00:00
|
|
|
public function clearLink( $title ) {
|
2013-03-09 20:14:22 +00:00
|
|
|
$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
|
|
|
|
2013-04-20 17:18:13 +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
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $title Title to add
|
|
|
|
|
* @return int
|
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 );
|
2013-04-20 17:18:13 +00:00
|
|
|
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
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param Title $nt Title object to add
|
|
|
|
|
* @return int
|
2005-12-30 09:33:11 +00:00
|
|
|
*/
|
2010-07-17 20:13:49 +00:00
|
|
|
public function addLinkObj( $nt ) {
|
2012-06-11 13:36:52 +00:00
|
|
|
global $wgAntiLockFlags, $wgContentHandlerUseDB;
|
|
|
|
|
|
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__ );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2008-08-29 19:12:56 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$id = $this->getGoodLinkID( $key );
|
|
|
|
|
if ( $id != 0 ) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2008-08-29 19:12:56 +00:00
|
|
|
return $id;
|
2005-10-22 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-29 19:12:56 +00:00
|
|
|
if ( $key === '' ) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
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
|
|
|
|
2012-06-11 13:36:52 +00:00
|
|
|
$f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( $wgContentHandlerUseDB ) {
|
|
|
|
|
$f[] = 'page_content_model';
|
|
|
|
|
}
|
2012-06-11 13:36:52 +00:00
|
|
|
|
|
|
|
|
$s = $db->selectRow( 'page', $f,
|
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__ );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
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
|
|
|
}
|