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
23 * Update object handling the cleanup of links tables after a page was deleted.
25 class LinksDeletionUpdate
extends SqlDataUpdate
implements EnqueueableDataUpdate
{
34 * @param WikiPage $page Page we are updating
35 * @param integer|null $pageId ID of the page we are updating [optional]
36 * @param string|null $timestamp TS_MW timestamp of deletion
39 function __construct( WikiPage
$page, $pageId = null, $timestamp = null ) {
40 parent
::__construct( false ); // no implicit transaction
44 $this->pageId
= $pageId; // page ID at time of deletion
45 } elseif ( $page->exists() ) {
46 $this->pageId
= $page->getId();
48 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
51 $this->timestamp
= $timestamp ?
: wfTimestampNow();
54 public function doUpdate() {
55 $config = RequestContext
::getMain()->getConfig();
56 $batchSize = $config->get( 'UpdateRowsPerQuery' );
57 $factory = wfGetLBFactory();
59 // Page may already be deleted, so don't just getId()
61 // Make sure all links update threads see the changes of each other.
62 // This handles the case when updates have to batched into several COMMITs.
63 $scopedLock = LinksUpdate
::acquirePageLock( $this->mDb
, $id );
65 $title = $this->page
->getTitle();
67 // Delete restrictions for it
68 $this->mDb
->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__
);
70 // Fix category table counts
71 $cats = $this->mDb
->selectFieldValues(
77 $catBatches = array_chunk( $cats, $batchSize );
78 foreach ( $catBatches as $catBatch ) {
79 $this->page
->updateCategoryCounts( [], $catBatch, $id );
80 if ( count( $catBatches ) > 1 ) {
81 $factory->commitAndWaitForReplication(
82 __METHOD__
, $this->ticket
, [ 'wiki' => $this->mDb
->getWikiID() ]
87 // Refresh the category table entry if it seems to have no pages. Check
88 // master for the most up-to-date cat_pages count.
89 if ( $title->getNamespace() === NS_CATEGORY
) {
90 $row = $this->mDb
->selectRow(
92 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
93 [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ],
97 $cat = Category
::newFromRow( $row, $title )->refreshCounts();
101 // If using cascading deletes, we can skip some explicit deletes
102 if ( !$this->mDb
->cascadingDeletes() ) {
103 // Delete outgoing links
104 $this->batchDeleteByPK(
106 [ 'pl_from' => $id ],
107 [ 'pl_from', 'pl_namespace', 'pl_title' ],
110 $this->batchDeleteByPK(
112 [ 'il_from' => $id ],
113 [ 'il_from', 'il_to' ],
116 $this->batchDeleteByPK(
118 [ 'cl_from' => $id ],
119 [ 'cl_from', 'cl_to' ],
122 $this->batchDeleteByPK(
124 [ 'tl_from' => $id ],
125 [ 'tl_from', 'tl_namespace', 'tl_title' ],
128 $this->batchDeleteByPK(
130 [ 'el_from' => $id ],
134 $this->batchDeleteByPK(
136 [ 'll_from' => $id ],
137 [ 'll_from', 'll_lang' ],
140 $this->batchDeleteByPK(
142 [ 'iwl_from' => $id ],
143 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
146 // Delete any redirect entry or page props entries
147 $this->mDb
->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__
);
148 $this->mDb
->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__
);
151 // If using cleanup triggers, we can skip some manual deletes
152 if ( !$this->mDb
->cleanupTriggers() ) {
153 // Find recentchanges entries to clean up...
154 $rcIdsForTitle = $this->mDb
->selectFieldValues(
158 'rc_type != ' . RC_LOG
,
159 'rc_namespace' => $title->getNamespace(),
160 'rc_title' => $title->getDBkey(),
162 $this->mDb
->addQuotes( $this->mDb
->timestamp( $this->timestamp
) )
166 $rcIdsForPage = $this->mDb
->selectFieldValues(
169 [ 'rc_type != ' . RC_LOG
, 'rc_cur_id' => $id ],
173 // T98706: delete by PK to avoid lock contention with RC delete log insertions
174 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
175 foreach ( $rcIdBatches as $rcIdBatch ) {
176 $this->mDb
->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__
);
177 if ( count( $rcIdBatches ) > 1 ) {
178 $factory->commitAndWaitForReplication(
179 __METHOD__
, $this->ticket
, [ 'wiki' => $this->mDb
->getWikiID() ]
185 // Commit and release the lock
186 ScopedCallback
::consume( $scopedLock );
189 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
190 $dbw = $this->mDb
; // convenience
191 $factory = wfGetLBFactory();
192 $res = $dbw->select( $table, $pk, $conds, __METHOD__
);
195 foreach ( $res as $row ) {
196 $pkDeleteConds[] = $this->mDb
->makeList( (array)$row, LIST_AND
);
197 if ( count( $pkDeleteConds ) >= $bSize ) {
198 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR
), __METHOD__
);
199 $factory->commitAndWaitForReplication(
200 __METHOD__
, $this->ticket
, [ 'wiki' => $this->mDb
->getWikiID() ]
206 if ( $pkDeleteConds ) {
207 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR
), __METHOD__
);
211 public function getAsJobSpecification() {
213 'wiki' => $this->mDb
->getWikiID(),
214 'job' => new JobSpecification(
216 [ 'pageId' => $this->pageId
, 'timestamp' => $this->timestamp
],
217 [ 'removeDuplicates' => true ],
218 $this->page
->getTitle()