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
;
25 * Update object handling the cleanup of links tables after a page was deleted.
27 class LinksDeletionUpdate
extends DataUpdate
implements EnqueueableDataUpdate
{
39 * @param WikiPage $page Page we are updating
40 * @param integer|null $pageId ID of the page we are updating [optional]
41 * @param string|null $timestamp TS_MW timestamp of deletion
44 function __construct( WikiPage
$page, $pageId = null, $timestamp = null ) {
47 $this->pageId
= $pageId; // page ID at time of deletion
48 } elseif ( $page->exists() ) {
49 $this->pageId
= $page->getId();
51 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
54 $this->timestamp
= $timestamp ?
: wfTimestampNow();
57 public function doUpdate() {
58 $services = MediaWikiServices
::getInstance();
59 $config = $services->getMainConfig();
60 $lbFactory = $services->getDBLoadBalancerFactory();
61 $batchSize = $config->get( 'UpdateRowsPerQuery' );
63 // Page may already be deleted, so don't just getId()
65 // Make sure all links update threads see the changes of each other.
66 // This handles the case when updates have to batched into several COMMITs.
67 $scopedLock = LinksUpdate
::acquirePageLock( $this->getDB(), $id );
69 $title = $this->page
->getTitle();
70 $dbw = $this->getDB(); // convenience
72 // Delete restrictions for it
73 $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__
);
75 // Fix category table counts
76 $cats = $dbw->selectFieldValues(
82 $catBatches = array_chunk( $cats, $batchSize );
83 foreach ( $catBatches as $catBatch ) {
84 $this->page
->updateCategoryCounts( [], $catBatch, $id );
85 if ( count( $catBatches ) > 1 ) {
86 $lbFactory->commitAndWaitForReplication(
87 __METHOD__
, $this->ticket
, [ 'wiki' => $dbw->getWikiID() ]
92 // Refresh the category table entry if it seems to have no pages. Check
93 // master for the most up-to-date cat_pages count.
94 if ( $title->getNamespace() === NS_CATEGORY
) {
95 $row = $dbw->selectRow(
97 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
98 [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ],
102 Category
::newFromRow( $row, $title )->refreshCounts();
106 // If using cascading deletes, we can skip some explicit deletes
107 if ( !$dbw->cascadingDeletes() ) {
108 // Delete outgoing links
109 $this->batchDeleteByPK(
111 [ 'pl_from' => $id ],
112 [ 'pl_from', 'pl_namespace', 'pl_title' ],
115 $this->batchDeleteByPK(
117 [ 'il_from' => $id ],
118 [ 'il_from', 'il_to' ],
121 $this->batchDeleteByPK(
123 [ 'cl_from' => $id ],
124 [ 'cl_from', 'cl_to' ],
127 $this->batchDeleteByPK(
129 [ 'tl_from' => $id ],
130 [ 'tl_from', 'tl_namespace', 'tl_title' ],
133 $this->batchDeleteByPK(
135 [ 'el_from' => $id ],
139 $this->batchDeleteByPK(
141 [ 'll_from' => $id ],
142 [ 'll_from', 'll_lang' ],
145 $this->batchDeleteByPK(
147 [ 'iwl_from' => $id ],
148 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
151 // Delete any redirect entry or page props entries
152 $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__
);
153 $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__
);
156 // If using cleanup triggers, we can skip some manual deletes
157 if ( !$dbw->cleanupTriggers() ) {
158 // Find recentchanges entries to clean up...
159 $rcIdsForTitle = $dbw->selectFieldValues(
163 'rc_type != ' . RC_LOG
,
164 'rc_namespace' => $title->getNamespace(),
165 'rc_title' => $title->getDBkey(),
167 $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) )
171 $rcIdsForPage = $dbw->selectFieldValues(
174 [ 'rc_type != ' . RC_LOG
, 'rc_cur_id' => $id ],
178 // T98706: delete by PK to avoid lock contention with RC delete log insertions
179 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
180 foreach ( $rcIdBatches as $rcIdBatch ) {
181 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__
);
182 if ( count( $rcIdBatches ) > 1 ) {
183 $lbFactory->commitAndWaitForReplication(
184 __METHOD__
, $this->ticket
, [ 'wiki' => $dbw->getWikiID() ]
190 // Commit and release the lock
191 ScopedCallback
::consume( $scopedLock );
194 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
195 $services = MediaWikiServices
::getInstance();
196 $lbFactory = $services->getDBLoadBalancerFactory();
197 $dbw = $this->getDB(); // convenience
199 $res = $dbw->select( $table, $pk, $conds, __METHOD__
);
202 foreach ( $res as $row ) {
203 $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND
);
204 if ( count( $pkDeleteConds ) >= $bSize ) {
205 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR
), __METHOD__
);
206 $lbFactory->commitAndWaitForReplication(
207 __METHOD__
, $this->ticket
, [ 'wiki' => $dbw->getWikiID() ]
213 if ( $pkDeleteConds ) {
214 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR
), __METHOD__
);
218 protected function getDB() {
220 $this->db
= wfGetDB( DB_MASTER
);
226 public function getAsJobSpecification() {
228 'wiki' => $this->getDB()->getWikiID(),
229 'job' => new JobSpecification(
231 [ 'pageId' => $this->pageId
, 'timestamp' => $this->timestamp
],
232 [ 'removeDuplicates' => true ],
233 $this->page
->getTitle()