Merge "Fix 'Tags' padding to keep it farther from the edge and document the source...
[lhc/web/wiklou.git] / includes / deferred / LinksDeletionUpdate.php
1 <?php
2 /**
3 * Updater for link tracking tables after a page edit.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22 use MediaWiki\MediaWikiServices;
23 use Wikimedia\ScopedCallback;
24 use Wikimedia\Rdbms\IDatabase;
25
26 /**
27 * Update object handling the cleanup of links tables after a page was deleted.
28 */
29 class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
30 /** @var WikiPage */
31 protected $page;
32 /** @var int */
33 protected $pageId;
34 /** @var string */
35 protected $timestamp;
36
37 /** @var IDatabase */
38 private $db;
39
40 /**
41 * @param WikiPage $page Page we are updating
42 * @param int|null $pageId ID of the page we are updating [optional]
43 * @param string|null $timestamp TS_MW timestamp of deletion
44 * @throws MWException
45 */
46 function __construct( WikiPage $page, $pageId = null, $timestamp = null ) {
47 parent::__construct();
48
49 $this->page = $page;
50 if ( $pageId ) {
51 $this->pageId = $pageId; // page ID at time of deletion
52 } elseif ( $page->exists() ) {
53 $this->pageId = $page->getId();
54 } else {
55 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
56 }
57
58 $this->timestamp = $timestamp ?: wfTimestampNow();
59 }
60
61 public function doUpdate() {
62 $services = MediaWikiServices::getInstance();
63 $config = $services->getMainConfig();
64 $lbFactory = $services->getDBLoadBalancerFactory();
65 $batchSize = $config->get( 'UpdateRowsPerQuery' );
66
67 // Page may already be deleted, so don't just getId()
68 $id = $this->pageId;
69
70 if ( $this->ticket ) {
71 // Make sure all links update threads see the changes of each other.
72 // This handles the case when updates have to batched into several COMMITs.
73 $scopedLock = LinksUpdate::acquirePageLock( $this->getDB(), $id );
74 }
75
76 $title = $this->page->getTitle();
77 $dbw = $this->getDB(); // convenience
78
79 // Delete restrictions for it
80 $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
81
82 // Fix category table counts
83 $cats = $dbw->selectFieldValues(
84 'categorylinks',
85 'cl_to',
86 [ 'cl_from' => $id ],
87 __METHOD__
88 );
89 $catBatches = array_chunk( $cats, $batchSize );
90 foreach ( $catBatches as $catBatch ) {
91 $this->page->updateCategoryCounts( [], $catBatch, $id );
92 if ( count( $catBatches ) > 1 ) {
93 // Only sacrifice atomicity if necessary due to size
94 $lbFactory->commitAndWaitForReplication(
95 __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
96 );
97 }
98 }
99
100 // Refresh counts on categories that should be empty now
101 if ( $title->getNamespace() === NS_CATEGORY ) {
102 // T166757: do the update after the main job DB commit
103 DeferredUpdates::addCallableUpdate( function () use ( $title ) {
104 $this->refreshCategoryIfEmpty( $title );
105 } );
106 }
107
108 $this->batchDeleteByPK(
109 'pagelinks',
110 [ 'pl_from' => $id ],
111 [ 'pl_from', 'pl_namespace', 'pl_title' ],
112 $batchSize
113 );
114 $this->batchDeleteByPK(
115 'imagelinks',
116 [ 'il_from' => $id ],
117 [ 'il_from', 'il_to' ],
118 $batchSize
119 );
120 $this->batchDeleteByPK(
121 'categorylinks',
122 [ 'cl_from' => $id ],
123 [ 'cl_from', 'cl_to' ],
124 $batchSize
125 );
126 $this->batchDeleteByPK(
127 'templatelinks',
128 [ 'tl_from' => $id ],
129 [ 'tl_from', 'tl_namespace', 'tl_title' ],
130 $batchSize
131 );
132 $this->batchDeleteByPK(
133 'externallinks',
134 [ 'el_from' => $id ],
135 [ 'el_id' ],
136 $batchSize
137 );
138 $this->batchDeleteByPK(
139 'langlinks',
140 [ 'll_from' => $id ],
141 [ 'll_from', 'll_lang' ],
142 $batchSize
143 );
144 $this->batchDeleteByPK(
145 'iwlinks',
146 [ 'iwl_from' => $id ],
147 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
148 $batchSize
149 );
150
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__ );
154
155 // Find recentchanges entries to clean up...
156 $rcIdsForTitle = $dbw->selectFieldValues(
157 'recentchanges',
158 'rc_id',
159 [
160 'rc_type != ' . RC_LOG,
161 'rc_namespace' => $title->getNamespace(),
162 'rc_title' => $title->getDBkey(),
163 'rc_timestamp < ' .
164 $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) )
165 ],
166 __METHOD__
167 );
168 $rcIdsForPage = $dbw->selectFieldValues(
169 'recentchanges',
170 'rc_id',
171 [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
172 __METHOD__
173 );
174
175 // T98706: delete by PK to avoid lock contention with RC delete log insertions
176 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
177 foreach ( $rcIdBatches as $rcIdBatch ) {
178 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
179 if ( count( $rcIdBatches ) > 1 ) {
180 $lbFactory->commitAndWaitForReplication(
181 __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
182 );
183 }
184 }
185
186 // Commit and release the lock (if set)
187 ScopedCallback::consume( $scopedLock );
188 }
189
190 /**
191 * @param Title $title
192 */
193 private function refreshCategoryIfEmpty( Title $title ) {
194 $dbw = $this->getDB();
195
196 $row = $dbw->selectRow(
197 'category',
198 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
199 [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 100' ],
200 __METHOD__
201 );
202
203 if ( !$row ) {
204 return; // nothing to delete
205 }
206
207 $cat = Category::newFromRow( $row, $title );
208 $hasLink = $dbw->selectField(
209 'categorylinks',
210 '1',
211 [ 'cl_to' => $title->getDBkey() ],
212 __METHOD__
213 );
214 if ( !$hasLink ) {
215 $cat->refreshCounts(); // delete the category table entry
216 }
217 }
218
219 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
220 $services = MediaWikiServices::getInstance();
221 $lbFactory = $services->getDBLoadBalancerFactory();
222 $dbw = $this->getDB(); // convenience
223
224 $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
225
226 $pkDeleteConds = [];
227 foreach ( $res as $row ) {
228 $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND );
229 if ( count( $pkDeleteConds ) >= $bSize ) {
230 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
231 $lbFactory->commitAndWaitForReplication(
232 __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
233 );
234 $pkDeleteConds = [];
235 }
236 }
237
238 if ( $pkDeleteConds ) {
239 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
240 }
241 }
242
243 protected function getDB() {
244 if ( !$this->db ) {
245 $this->db = wfGetDB( DB_MASTER );
246 }
247
248 return $this->db;
249 }
250
251 public function getAsJobSpecification() {
252 return [
253 'wiki' => WikiMap::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
254 'job' => new JobSpecification(
255 'deleteLinks',
256 [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
257 [ 'removeDuplicates' => true ],
258 $this->page->getTitle()
259 )
260 ];
261 }
262 }