Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[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 $lbFactory->commitAndWaitForReplication(
94 __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
95 );
96 }
97 }
98
99 // Refresh counts on categories that should be empty now
100 if ( $title->getNamespace() === NS_CATEGORY ) {
101 $row = $dbw->selectRow(
102 'category',
103 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
104 [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 100' ],
105 __METHOD__
106 );
107 if ( $row ) {
108 $cat = Category::newFromRow( $row, $title );
109 // T166757: do the update after the main job DB commit
110 DeferredUpdates::addCallableUpdate( function () use ( $cat ) {
111 $cat->refreshCounts();
112 } );
113 }
114 }
115
116 $this->batchDeleteByPK(
117 'pagelinks',
118 [ 'pl_from' => $id ],
119 [ 'pl_from', 'pl_namespace', 'pl_title' ],
120 $batchSize
121 );
122 $this->batchDeleteByPK(
123 'imagelinks',
124 [ 'il_from' => $id ],
125 [ 'il_from', 'il_to' ],
126 $batchSize
127 );
128 $this->batchDeleteByPK(
129 'categorylinks',
130 [ 'cl_from' => $id ],
131 [ 'cl_from', 'cl_to' ],
132 $batchSize
133 );
134 $this->batchDeleteByPK(
135 'templatelinks',
136 [ 'tl_from' => $id ],
137 [ 'tl_from', 'tl_namespace', 'tl_title' ],
138 $batchSize
139 );
140 $this->batchDeleteByPK(
141 'externallinks',
142 [ 'el_from' => $id ],
143 [ 'el_id' ],
144 $batchSize
145 );
146 $this->batchDeleteByPK(
147 'langlinks',
148 [ 'll_from' => $id ],
149 [ 'll_from', 'll_lang' ],
150 $batchSize
151 );
152 $this->batchDeleteByPK(
153 'iwlinks',
154 [ 'iwl_from' => $id ],
155 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
156 $batchSize
157 );
158
159 // Delete any redirect entry or page props entries
160 $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
161 $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
162
163 // Find recentchanges entries to clean up...
164 $rcIdsForTitle = $dbw->selectFieldValues(
165 'recentchanges',
166 'rc_id',
167 [
168 'rc_type != ' . RC_LOG,
169 'rc_namespace' => $title->getNamespace(),
170 'rc_title' => $title->getDBkey(),
171 'rc_timestamp < ' .
172 $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) )
173 ],
174 __METHOD__
175 );
176 $rcIdsForPage = $dbw->selectFieldValues(
177 'recentchanges',
178 'rc_id',
179 [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
180 __METHOD__
181 );
182
183 // T98706: delete by PK to avoid lock contention with RC delete log insertions
184 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
185 foreach ( $rcIdBatches as $rcIdBatch ) {
186 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
187 if ( count( $rcIdBatches ) > 1 ) {
188 $lbFactory->commitAndWaitForReplication(
189 __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
190 );
191 }
192 }
193
194 // Commit and release the lock (if set)
195 ScopedCallback::consume( $scopedLock );
196 }
197
198 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
199 $services = MediaWikiServices::getInstance();
200 $lbFactory = $services->getDBLoadBalancerFactory();
201 $dbw = $this->getDB(); // convenience
202
203 $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
204
205 $pkDeleteConds = [];
206 foreach ( $res as $row ) {
207 $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND );
208 if ( count( $pkDeleteConds ) >= $bSize ) {
209 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
210 $lbFactory->commitAndWaitForReplication(
211 __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
212 );
213 $pkDeleteConds = [];
214 }
215 }
216
217 if ( $pkDeleteConds ) {
218 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
219 }
220 }
221
222 protected function getDB() {
223 if ( !$this->db ) {
224 $this->db = wfGetDB( DB_MASTER );
225 }
226
227 return $this->db;
228 }
229
230 public function getAsJobSpecification() {
231 return [
232 'wiki' => WikiMap::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
233 'job' => new JobSpecification(
234 'deleteLinks',
235 [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
236 [ 'removeDuplicates' => true ],
237 $this->page->getTitle()
238 )
239 ];
240 }
241 }