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 {
|
2015-07-26 23:07:59 +00:00
|
|
|
/**
|
2015-11-09 23:17:39 +00:00
|
|
|
* @var HashBagOStuff
|
2015-07-26 23:07:59 +00:00
|
|
|
*/
|
|
|
|
|
private $mGoodLinks;
|
|
|
|
|
/**
|
2015-11-09 23:17:39 +00:00
|
|
|
* @var HashBagOStuff
|
2015-07-26 23:07:59 +00:00
|
|
|
*/
|
|
|
|
|
private $mBadLinks;
|
2011-12-21 18:53:00 +00:00
|
|
|
private $mForUpdate = false;
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2015-07-26 23:07:59 +00:00
|
|
|
/**
|
|
|
|
|
* How many Titles to store. There are two caches, so the amount actually
|
|
|
|
|
* stored in memory can be up to twice this.
|
|
|
|
|
*/
|
|
|
|
|
const MAX_SIZE = 10000;
|
|
|
|
|
|
2006-01-05 02:05:53 +00:00
|
|
|
/**
|
2013-05-20 10:37:15 +00:00
|
|
|
* @var LinkCache
|
|
|
|
|
*/
|
|
|
|
|
protected static $instance;
|
|
|
|
|
|
2015-07-26 23:07:59 +00:00
|
|
|
public function __construct() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mGoodLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
|
|
|
|
|
$this->mBadLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
|
2015-07-26 23:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-20 10:37:15 +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
|
|
|
*/
|
2015-11-09 23:14:42 +00:00
|
|
|
public static function &singleton() {
|
|
|
|
|
if ( !self::$instance ) {
|
|
|
|
|
self::$instance = new LinkCache;
|
2006-01-05 02:05:53 +00:00
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2013-05-20 10:37:15 +00:00
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-11-09 23:14:42 +00:00
|
|
|
* Destroy the singleton instance
|
|
|
|
|
*
|
|
|
|
|
* A new one will be created next time singleton() is called.
|
|
|
|
|
*
|
2013-05-20 10:37:15 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
2015-11-09 23:14:42 +00:00
|
|
|
public static function destroySingleton() {
|
2013-05-20 10:37:15 +00:00
|
|
|
self::$instance = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the singleton instance to a given object.
|
2015-11-09 23:14:42 +00:00
|
|
|
*
|
2013-05-20 10:37:15 +00:00
|
|
|
* Since we do not have an interface for LinkCache, you have to be sure the
|
|
|
|
|
* given object implements all the LinkCache public methods.
|
2015-11-09 23:14:42 +00:00
|
|
|
*
|
2013-05-20 10:37:15 +00:00
|
|
|
* @param LinkCache $instance
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
2015-11-09 23:14:42 +00:00
|
|
|
public static function setSingleton( LinkCache $instance ) {
|
2013-05-20 10:37:15 +00:00
|
|
|
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
|
|
|
/**
|
2015-10-30 09:29:12 +00:00
|
|
|
* General accessor to get/set whether the master DB should be used
|
|
|
|
|
*
|
|
|
|
|
* This used to also set the FOR UPDATE option (locking the rows read
|
|
|
|
|
* in order to avoid link table inconsistency), which was later removed
|
|
|
|
|
* for performance on wikis with a high edit rate.
|
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
|
2015-11-09 23:14:42 +00:00
|
|
|
* @return int Page ID or zero
|
2011-05-28 18:58:51 +00:00
|
|
|
*/
|
2008-04-09 13:02:34 +00:00
|
|
|
public function getGoodLinkID( $title ) {
|
2015-11-09 23:17:39 +00:00
|
|
|
$info = $this->mGoodLinks->get( $title );
|
|
|
|
|
if ( !$info ) {
|
2003-04-14 23:10:40 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2015-11-09 23:14:42 +00:00
|
|
|
return $info['id'];
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
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.
|
2015-11-09 23:14:42 +00:00
|
|
|
* If this link is not a cached good title, 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')
|
2015-11-09 23:14:42 +00:00
|
|
|
* @return string|int|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();
|
2015-11-09 23:17:39 +00:00
|
|
|
$info = $this->mGoodLinks->get( $dbkey );
|
|
|
|
|
if ( !$info ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2008-04-09 05:21:00 +00:00
|
|
|
}
|
2015-11-09 23:14:42 +00:00
|
|
|
return $info[$field];
|
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 ) {
|
2015-11-09 23:14:42 +00:00
|
|
|
// Use get() to ensure it records as used for LRU.
|
2015-11-09 23:17:39 +00:00
|
|
|
return $this->mBadLinks->get( $title ) !== false;
|
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
|
2014-08-20 21:46:11 +00:00
|
|
|
* @param string|null $model Latest revision's content model ID
|
2015-12-19 15:25:45 +00:00
|
|
|
* @param string|null $lang Language code of the page, if not the content language
|
2008-04-09 05:21:00 +00:00
|
|
|
*/
|
2015-11-09 23:14:42 +00:00
|
|
|
public function addGoodLinkObj( $id, Title $title, $len = -1, $redir = null,
|
2015-12-19 15:25:45 +00:00
|
|
|
$revision = 0, $model = null, $lang = null
|
2013-11-17 20:42:23 +00:00
|
|
|
) {
|
2013-03-09 20:14:22 +00:00
|
|
|
$dbkey = $title->getPrefixedDBkey();
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mGoodLinks->set( $dbkey, [
|
2015-07-26 23:07:59 +00:00
|
|
|
'id' => (int)$id,
|
2013-11-17 20:42:23 +00:00
|
|
|
'length' => (int)$len,
|
|
|
|
|
'redirect' => (int)$redir,
|
|
|
|
|
'revision' => (int)$revision,
|
2014-08-20 21:46:11 +00:00
|
|
|
'model' => $model ? (string)$model : null,
|
2015-12-19 15:25:45 +00:00
|
|
|
'lang' => $lang ? (string)$lang : null,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
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
|
|
|
*/
|
2015-11-09 23:14:42 +00:00
|
|
|
public function addGoodLinkObjFromRow( Title $title, $row ) {
|
2013-03-09 20:14:22 +00:00
|
|
|
$dbkey = $title->getPrefixedDBkey();
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mGoodLinks->set( $dbkey, [
|
2015-07-26 23:07:59 +00:00
|
|
|
'id' => intval( $row->page_id ),
|
2011-09-20 15:19:18 +00:00
|
|
|
'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,
|
2015-12-19 15:25:45 +00:00
|
|
|
'lang' => !empty( $row->page_lang ) ? strval( $row->page_lang ) : null,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
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
|
|
|
*/
|
2015-11-09 23:14:42 +00:00
|
|
|
public function addBadLinkObj( Title $title ) {
|
2013-03-09 20:14:22 +00:00
|
|
|
$dbkey = $title->getPrefixedDBkey();
|
2008-08-29 19:12:56 +00:00
|
|
|
if ( !$this->isBadLink( $dbkey ) ) {
|
2015-07-26 23:07:59 +00:00
|
|
|
$this->mBadLinks->set( $dbkey, 1 );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-09 13:02:34 +00:00
|
|
|
public function clearBadLink( $title ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mBadLinks->clear( [ $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();
|
2015-11-09 23:17:39 +00:00
|
|
|
$this->mBadLinks->delete( $dbkey );
|
|
|
|
|
$this->mGoodLinks->delete( $dbkey );
|
2008-09-07 08:24:06 +00:00
|
|
|
}
|
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
|
2010-07-17 20:13:49 +00:00
|
|
|
*
|
2014-04-18 23:19:46 +00:00
|
|
|
* @param string $title Title to add
|
2015-11-09 23:14:42 +00:00
|
|
|
* @return int Page ID or zero
|
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 );
|
2015-11-09 23:14:42 +00:00
|
|
|
if ( !$nt ) {
|
2003-11-04 08:59:28 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2015-11-09 23:14:42 +00:00
|
|
|
return $this->addLinkObj( $nt );
|
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
|
2015-11-09 23:14:42 +00:00
|
|
|
* @return int Page ID or zero
|
2005-12-30 09:33:11 +00:00
|
|
|
*/
|
2015-11-09 23:14:42 +00:00
|
|
|
public function addLinkObj( Title $nt ) {
|
2015-12-19 15:25:45 +00:00
|
|
|
global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
|
2012-06-11 13:36:52 +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
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$id = $this->getGoodLinkID( $key );
|
|
|
|
|
if ( $id != 0 ) {
|
|
|
|
|
return $id;
|
2005-10-22 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-29 19:12:56 +00:00
|
|
|
if ( $key === '' ) {
|
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
|
|
|
|
2015-11-09 23:14:42 +00:00
|
|
|
// Some fields heavily used for linking...
|
|
|
|
|
$db = $this->mForUpdate ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( $wgContentHandlerUseDB ) {
|
2015-11-09 23:14:42 +00:00
|
|
|
$fields[] = 'page_content_model';
|
2013-04-20 17:18:13 +00:00
|
|
|
}
|
2015-12-19 15:25:45 +00:00
|
|
|
if ( $wgPageLanguageUseDB ) {
|
|
|
|
|
$fields[] = 'page_lang';
|
|
|
|
|
}
|
2012-06-11 13:36:52 +00:00
|
|
|
|
2015-11-09 23:14:42 +00:00
|
|
|
$row = $db->selectRow( 'page', $fields,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
|
2015-11-09 23:14:42 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $row !== false ) {
|
|
|
|
|
$this->addGoodLinkObjFromRow( $nt, $row );
|
|
|
|
|
$id = intval( $row->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
|
|
|
|
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() {
|
2015-07-26 23:07:59 +00:00
|
|
|
$this->mGoodLinks->clear();
|
|
|
|
|
$this->mBadLinks->clear();
|
2005-05-29 10:17:44 +00:00
|
|
|
}
|
2003-07-06 11:42:42 +00:00
|
|
|
}
|