LinksDeletionUpdate: Fix typos in column names
[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 /**
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
31 /**
32 * @param WikiPage $page Page we are updating
33 * @param integer|null $pageId ID of the page we are updating [optional]
34 * @throws MWException
35 */
36 function __construct( WikiPage $page, $pageId = null ) {
37 parent::__construct( false ); // no implicit transaction
38
39 $this->page = $page;
40 if ( $page->exists() ) {
41 $this->pageId = $page->getId();
42 } elseif ( $pageId ) {
43 $this->pageId = $pageId;
44 } else {
45 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
46 }
47 }
48
49 public function doUpdate() {
50 $config = RequestContext::getMain()->getConfig();
51 $batchSize = $config->get( 'UpdateRowsPerQuery' );
52
53 // Page may already be deleted, so don't just getId()
54 $id = $this->pageId;
55 // Make sure all links update threads see the changes of each other.
56 // This handles the case when updates have to batched into several COMMITs.
57 $scopedLock = LinksUpdate::acquirePageLock( $this->mDb, $id );
58
59 // Delete restrictions for it
60 $this->mDb->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
61
62 // Fix category table counts
63 $cats = $this->mDb->selectFieldValues(
64 'categorylinks',
65 'cl_to',
66 [ 'cl_from' => $id ],
67 __METHOD__
68 );
69 $catBatches = array_chunk( $cats, $batchSize );
70 foreach ( $catBatches as $catBatch ) {
71 $this->page->updateCategoryCounts( [], $catBatch );
72 if ( count( $catBatches ) > 1 ) {
73 $this->mDb->commit( __METHOD__, 'flush' );
74 wfGetLBFactory()->waitForReplication( [ 'wiki' => $this->mDb->getWikiID() ] );
75 }
76 }
77
78 // If using cascading deletes, we can skip some explicit deletes
79 if ( !$this->mDb->cascadingDeletes() ) {
80 // Delete outgoing links
81 $this->batchDeleteByPK(
82 'pagelinks',
83 [ 'pl_from' => $id ],
84 [ 'pl_from', 'pl_namespace', 'pl_title' ],
85 $batchSize
86 );
87 $this->batchDeleteByPK(
88 'imagelinks',
89 [ 'il_from' => $id ],
90 [ 'il_from', 'il_to' ],
91 $batchSize
92 );
93 $this->batchDeleteByPK(
94 'categorylinks',
95 [ 'cl_from' => $id ],
96 [ 'cl_from', 'cl_to' ],
97 $batchSize
98 );
99 $this->batchDeleteByPK(
100 'templatelinks',
101 [ 'tl_from' => $id ],
102 [ 'tl_from', 'tl_namespace', 'tl_title' ],
103 $batchSize
104 );
105 $this->batchDeleteByPK(
106 'externallinks',
107 [ 'el_from' => $id ],
108 [ 'el_id' ],
109 $batchSize
110 );
111 $this->batchDeleteByPK(
112 'langlinks',
113 [ 'll_from' => $id ],
114 [ 'll_from', 'll_lang' ],
115 $batchSize
116 );
117 $this->batchDeleteByPK(
118 'iwlinks',
119 [ 'iwl_from' => $id ],
120 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
121 $batchSize
122 );
123 // Delete any redirect entry or page props entries
124 $this->mDb->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
125 $this->mDb->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
126 }
127
128 // If using cleanup triggers, we can skip some manual deletes
129 if ( !$this->mDb->cleanupTriggers() ) {
130 $title = $this->page->getTitle();
131 // Find recentchanges entries to clean up...
132 $rcIdsForTitle = $this->mDb->selectFieldValues(
133 'recentchanges',
134 'rc_id',
135 [
136 'rc_type != ' . RC_LOG,
137 'rc_namespace' => $title->getNamespace(),
138 'rc_title' => $title->getDBkey()
139 ],
140 __METHOD__
141 );
142 $rcIdsForPage = $this->mDb->selectFieldValues(
143 'recentchanges',
144 'rc_id',
145 [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
146 __METHOD__
147 );
148
149 // T98706: delete by PK to avoid lock contention with RC delete log insertions
150 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
151 foreach ( $rcIdBatches as $rcIdBatch ) {
152 $this->mDb->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
153 if ( count( $rcIdBatches ) > 1 ) {
154 $this->mDb->commit( __METHOD__, 'flush' );
155 wfGetLBFactory()->waitForReplication( [ 'wiki' => $this->mDb->getWikiID() ] );
156 }
157 }
158 }
159
160 $this->mDb->onTransactionIdle( function() use ( &$scopedLock ) {
161 // Release the lock *after* the final COMMIT for correctness
162 ScopedCallback::consume( $scopedLock );
163 } );
164 }
165
166 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
167 $dbw = $this->mDb; // convenience
168 $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
169
170 $pkDeleteConds = [];
171 foreach ( $res as $row ) {
172 $pkDeleteConds[] = $this->mDb->makeList( (array)$row, LIST_AND );
173 if ( count( $pkDeleteConds ) >= $bSize ) {
174 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
175 $dbw->commit( __METHOD__, 'flush' );
176 wfGetLBFactory()->waitForReplication( [ 'wiki' => $dbw->getWikiID() ] );
177 $pkDeleteConds = [];
178 }
179 }
180
181 if ( $pkDeleteConds ) {
182 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
183 }
184 }
185
186 public function getAsJobSpecification() {
187 return [
188 'wiki' => $this->mDb->getWikiID(),
189 'job' => new JobSpecification(
190 'deleteLinks',
191 [ 'pageId' => $this->pageId ],
192 [ 'removeDuplicates' => true ],
193 $this->page->getTitle()
194 )
195 ];
196 }
197 }