Merge "ImageHistoryList: Remove 'wpEditToken' parameter from the "revert" link for...
[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
24 /**
25 * Update object handling the cleanup of links tables after a page was deleted.
26 **/
27 class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
28 /** @var WikiPage */
29 protected $page;
30 /** @var integer */
31 protected $pageId;
32 /** @var string */
33 protected $timestamp;
34
35 /** @var IDatabase */
36 private $db;
37
38 /**
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
42 * @throws MWException
43 */
44 function __construct( WikiPage $page, $pageId = null, $timestamp = null ) {
45 parent::__construct();
46
47 $this->page = $page;
48 if ( $pageId ) {
49 $this->pageId = $pageId; // page ID at time of deletion
50 } elseif ( $page->exists() ) {
51 $this->pageId = $page->getId();
52 } else {
53 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
54 }
55
56 $this->timestamp = $timestamp ?: wfTimestampNow();
57 }
58
59 public function doUpdate() {
60 $services = MediaWikiServices::getInstance();
61 $config = $services->getMainConfig();
62 $lbFactory = $services->getDBLoadBalancerFactory();
63 $batchSize = $config->get( 'UpdateRowsPerQuery' );
64
65 // Page may already be deleted, so don't just getId()
66 $id = $this->pageId;
67 // Make sure all links update threads see the changes of each other.
68 // This handles the case when updates have to batched into several COMMITs.
69 $scopedLock = LinksUpdate::acquirePageLock( $this->getDB(), $id );
70
71 $title = $this->page->getTitle();
72 $dbw = $this->getDB(); // convenience
73
74 // Delete restrictions for it
75 $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
76
77 // Fix category table counts
78 $cats = $dbw->selectFieldValues(
79 'categorylinks',
80 'cl_to',
81 [ 'cl_from' => $id ],
82 __METHOD__
83 );
84 $catBatches = array_chunk( $cats, $batchSize );
85 foreach ( $catBatches as $catBatch ) {
86 $this->page->updateCategoryCounts( [], $catBatch, $id );
87 if ( count( $catBatches ) > 1 ) {
88 $lbFactory->commitAndWaitForReplication(
89 __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ]
90 );
91 }
92 }
93
94 // Refresh the category table entry if it seems to have no pages. Check
95 // master for the most up-to-date cat_pages count.
96 if ( $title->getNamespace() === NS_CATEGORY ) {
97 $row = $dbw->selectRow(
98 'category',
99 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
100 [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ],
101 __METHOD__
102 );
103 if ( $row ) {
104 Category::newFromRow( $row, $title )->refreshCounts();
105 }
106 }
107
108 // If using cascading deletes, we can skip some explicit deletes
109 if ( !$dbw->cascadingDeletes() ) {
110 // Delete outgoing links
111 $this->batchDeleteByPK(
112 'pagelinks',
113 [ 'pl_from' => $id ],
114 [ 'pl_from', 'pl_namespace', 'pl_title' ],
115 $batchSize
116 );
117 $this->batchDeleteByPK(
118 'imagelinks',
119 [ 'il_from' => $id ],
120 [ 'il_from', 'il_to' ],
121 $batchSize
122 );
123 $this->batchDeleteByPK(
124 'categorylinks',
125 [ 'cl_from' => $id ],
126 [ 'cl_from', 'cl_to' ],
127 $batchSize
128 );
129 $this->batchDeleteByPK(
130 'templatelinks',
131 [ 'tl_from' => $id ],
132 [ 'tl_from', 'tl_namespace', 'tl_title' ],
133 $batchSize
134 );
135 $this->batchDeleteByPK(
136 'externallinks',
137 [ 'el_from' => $id ],
138 [ 'el_id' ],
139 $batchSize
140 );
141 $this->batchDeleteByPK(
142 'langlinks',
143 [ 'll_from' => $id ],
144 [ 'll_from', 'll_lang' ],
145 $batchSize
146 );
147 $this->batchDeleteByPK(
148 'iwlinks',
149 [ 'iwl_from' => $id ],
150 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
151 $batchSize
152 );
153 // Delete any redirect entry or page props entries
154 $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
155 $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
156 }
157
158 // If using cleanup triggers, we can skip some manual deletes
159 if ( !$dbw->cleanupTriggers() ) {
160 // Find recentchanges entries to clean up...
161 $rcIdsForTitle = $dbw->selectFieldValues(
162 'recentchanges',
163 'rc_id',
164 [
165 'rc_type != ' . RC_LOG,
166 'rc_namespace' => $title->getNamespace(),
167 'rc_title' => $title->getDBkey(),
168 'rc_timestamp < ' .
169 $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) )
170 ],
171 __METHOD__
172 );
173 $rcIdsForPage = $dbw->selectFieldValues(
174 'recentchanges',
175 'rc_id',
176 [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
177 __METHOD__
178 );
179
180 // T98706: delete by PK to avoid lock contention with RC delete log insertions
181 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
182 foreach ( $rcIdBatches as $rcIdBatch ) {
183 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
184 if ( count( $rcIdBatches ) > 1 ) {
185 $lbFactory->commitAndWaitForReplication(
186 __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ]
187 );
188 }
189 }
190 }
191
192 // Commit and release the lock
193 ScopedCallback::consume( $scopedLock );
194 }
195
196 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
197 $services = MediaWikiServices::getInstance();
198 $lbFactory = $services->getDBLoadBalancerFactory();
199 $dbw = $this->getDB(); // convenience
200
201 $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
202
203 $pkDeleteConds = [];
204 foreach ( $res as $row ) {
205 $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND );
206 if ( count( $pkDeleteConds ) >= $bSize ) {
207 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
208 $lbFactory->commitAndWaitForReplication(
209 __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ]
210 );
211 $pkDeleteConds = [];
212 }
213 }
214
215 if ( $pkDeleteConds ) {
216 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
217 }
218 }
219
220 protected function getDB() {
221 if ( !$this->db ) {
222 $this->db = wfGetDB( DB_MASTER );
223 }
224
225 return $this->db;
226 }
227
228 public function getAsJobSpecification() {
229 return [
230 'wiki' => $this->getDB()->getWikiID(),
231 'job' => new JobSpecification(
232 'deleteLinks',
233 [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
234 [ 'removeDuplicates' => true ],
235 $this->page->getTitle()
236 )
237 ];
238 }
239 }