3 * Updater for link tracking tables after a page edit.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\MediaWikiServices
;
23 use Wikimedia\ScopedCallback
;
26 * Update object handling the cleanup of links tables after a page was deleted.
28 class LinksDeletionUpdate
extends LinksUpdate
implements EnqueueableDataUpdate
{
35 * @param WikiPage $page Page we are updating
36 * @param int|null $pageId ID of the page we are updating [optional]
37 * @param string|null $timestamp TS_MW timestamp of deletion
40 public function __construct( WikiPage
$page, $pageId = null, $timestamp = null ) {
43 $this->mId
= $pageId; // page ID at time of deletion
44 } elseif ( $page->exists() ) {
45 $this->mId
= $page->getId();
47 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
50 $this->timestamp
= $timestamp ?
: wfTimestampNow();
52 $fakePO = new ParserOutput();
53 $fakePO->setCacheTime( $timestamp );
54 parent
::__construct( $page->getTitle(), $fakePO, false );
57 protected function doIncrementalUpdate() {
58 $services = MediaWikiServices
::getInstance();
59 $config = $services->getMainConfig();
60 $lbFactory = $services->getDBLoadBalancerFactory();
61 $batchSize = $config->get( 'UpdateRowsPerQuery' );
64 $title = $this->mTitle
;
66 $dbw = $this->getDB(); // convenience
68 parent
::doIncrementalUpdate();
70 // Typically, a category is empty when deleted, so check that we don't leave
71 // spurious row in the category table.
72 if ( $title->getNamespace() === NS_CATEGORY
) {
73 // T166757: do the update after the main job DB commit
74 DeferredUpdates
::addCallableUpdate( function () use ( $title ) {
75 $cat = Category
::newFromName( $title->getDBkey() );
76 $cat->refreshCountsIfSmall();
80 // Delete restrictions for the deleted page
81 $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__
);
83 // Delete any redirect entry
84 $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__
);
86 // Find recentchanges entries to clean up...
87 $rcIdsForTitle = $dbw->selectFieldValues(
91 'rc_type != ' . RC_LOG
,
92 'rc_namespace' => $title->getNamespace(),
93 'rc_title' => $title->getDBkey(),
95 $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) )
99 $rcIdsForPage = $dbw->selectFieldValues(
102 [ 'rc_type != ' . RC_LOG
, 'rc_cur_id' => $id ],
106 // T98706: delete by PK to avoid lock contention with RC delete log insertions
107 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
108 foreach ( $rcIdBatches as $rcIdBatch ) {
109 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__
);
110 if ( count( $rcIdBatches ) > 1 ) {
111 $lbFactory->commitAndWaitForReplication(
112 __METHOD__
, $this->ticket
, [ 'domain' => $dbw->getDomainID() ]
117 // Commit and release the lock (if set)
118 ScopedCallback
::consume( $scopedLock );
121 public function getAsJobSpecification() {
123 'domain' => $this->getDB()->getDomainID(),
124 'job' => new JobSpecification(
126 [ 'pageId' => $this->mId
, 'timestamp' => $this->timestamp
],
127 [ 'removeDuplicates' => true ],