Apply $wgMaxArticleSize more exactly
[mediawiki.git] / includes / deferred / LinksDeletionUpdate.php
bloba7c39ca625acf0662d1afa48fee5fa5a37f166ca
1 <?php
2 /**
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
20 * @file
22 /**
23 * Update object handling the cleanup of links tables after a page was deleted.
24 **/
25 class LinksDeletionUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
26 /** @var WikiPage */
27 protected $page;
28 /** @var integer */
29 protected $pageId;
30 /** @var string */
31 protected $timestamp;
33 /**
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
37 * @throws MWException
39 function __construct( WikiPage $page, $pageId = null, $timestamp = null ) {
40 parent::__construct( false ); // no implicit transaction
42 $this->page = $page;
43 if ( $pageId ) {
44 $this->pageId = $pageId; // page ID at time of deletion
45 } elseif ( $page->exists() ) {
46 $this->pageId = $page->getId();
47 } else {
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' );
58 // Page may already be deleted, so don't just getId()
59 $id = $this->pageId;
60 // Make sure all links update threads see the changes of each other.
61 // This handles the case when updates have to batched into several COMMITs.
62 $scopedLock = LinksUpdate::acquirePageLock( $this->mDb, $id );
64 // Delete restrictions for it
65 $this->mDb->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
67 // Fix category table counts
68 $cats = $this->mDb->selectFieldValues(
69 'categorylinks',
70 'cl_to',
71 [ 'cl_from' => $id ],
72 __METHOD__
74 $catBatches = array_chunk( $cats, $batchSize );
75 foreach ( $catBatches as $catBatch ) {
76 $this->page->updateCategoryCounts( [], $catBatch, $id );
77 if ( count( $catBatches ) > 1 ) {
78 $this->mDb->commit( __METHOD__, 'flush' );
79 wfGetLBFactory()->waitForReplication( [ 'wiki' => $this->mDb->getWikiID() ] );
83 // If using cascading deletes, we can skip some explicit deletes
84 if ( !$this->mDb->cascadingDeletes() ) {
85 // Delete outgoing links
86 $this->batchDeleteByPK(
87 'pagelinks',
88 [ 'pl_from' => $id ],
89 [ 'pl_from', 'pl_namespace', 'pl_title' ],
90 $batchSize
92 $this->batchDeleteByPK(
93 'imagelinks',
94 [ 'il_from' => $id ],
95 [ 'il_from', 'il_to' ],
96 $batchSize
98 $this->batchDeleteByPK(
99 'categorylinks',
100 [ 'cl_from' => $id ],
101 [ 'cl_from', 'cl_to' ],
102 $batchSize
104 $this->batchDeleteByPK(
105 'templatelinks',
106 [ 'tl_from' => $id ],
107 [ 'tl_from', 'tl_namespace', 'tl_title' ],
108 $batchSize
110 $this->batchDeleteByPK(
111 'externallinks',
112 [ 'el_from' => $id ],
113 [ 'el_id' ],
114 $batchSize
116 $this->batchDeleteByPK(
117 'langlinks',
118 [ 'll_from' => $id ],
119 [ 'll_from', 'll_lang' ],
120 $batchSize
122 $this->batchDeleteByPK(
123 'iwlinks',
124 [ 'iwl_from' => $id ],
125 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
126 $batchSize
128 // Delete any redirect entry or page props entries
129 $this->mDb->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
130 $this->mDb->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
133 // If using cleanup triggers, we can skip some manual deletes
134 if ( !$this->mDb->cleanupTriggers() ) {
135 $title = $this->page->getTitle();
136 // Find recentchanges entries to clean up...
137 $rcIdsForTitle = $this->mDb->selectFieldValues(
138 'recentchanges',
139 'rc_id',
141 'rc_type != ' . RC_LOG,
142 'rc_namespace' => $title->getNamespace(),
143 'rc_title' => $title->getDBkey(),
144 'rc_timestamp < ' .
145 $this->mDb->addQuotes( $this->mDb->timestamp( $this->timestamp ) )
147 __METHOD__
149 $rcIdsForPage = $this->mDb->selectFieldValues(
150 'recentchanges',
151 'rc_id',
152 [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
153 __METHOD__
156 // T98706: delete by PK to avoid lock contention with RC delete log insertions
157 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
158 foreach ( $rcIdBatches as $rcIdBatch ) {
159 $this->mDb->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
160 if ( count( $rcIdBatches ) > 1 ) {
161 $this->mDb->commit( __METHOD__, 'flush' );
162 wfGetLBFactory()->waitForReplication( [ 'wiki' => $this->mDb->getWikiID() ] );
167 $this->mDb->onTransactionIdle( function() use ( &$scopedLock ) {
168 // Release the lock *after* the final COMMIT for correctness
169 ScopedCallback::consume( $scopedLock );
170 } );
173 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
174 $dbw = $this->mDb; // convenience
175 $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
177 $pkDeleteConds = [];
178 foreach ( $res as $row ) {
179 $pkDeleteConds[] = $this->mDb->makeList( (array)$row, LIST_AND );
180 if ( count( $pkDeleteConds ) >= $bSize ) {
181 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
182 $dbw->commit( __METHOD__, 'flush' );
183 wfGetLBFactory()->waitForReplication( [ 'wiki' => $dbw->getWikiID() ] );
184 $pkDeleteConds = [];
188 if ( $pkDeleteConds ) {
189 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
193 public function getAsJobSpecification() {
194 return [
195 'wiki' => $this->mDb->getWikiID(),
196 'job' => new JobSpecification(
197 'deleteLinks',
198 [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
199 [ 'removeDuplicates' => true ],
200 $this->page->getTitle()