Merge "Always set DBO_DEFAULT by default for LBFactory classes for consistency"
[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 /** @var string */
31 protected $timestamp;
32
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
38 */
39 function __construct( WikiPage $page, $pageId = null, $timestamp = null ) {
40 parent::__construct( false ); // no implicit transaction
41
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?" );
49 }
50
51 $this->timestamp = $timestamp ?: wfTimestampNow();
52 }
53
54 public function doUpdate() {
55 $config = RequestContext::getMain()->getConfig();
56 $batchSize = $config->get( 'UpdateRowsPerQuery' );
57 $factory = wfGetLBFactory();
58
59 // Page may already be deleted, so don't just getId()
60 $id = $this->pageId;
61 // Make sure all links update threads see the changes of each other.
62 // This handles the case when updates have to batched into several COMMITs.
63 $scopedLock = LinksUpdate::acquirePageLock( $this->mDb, $id );
64
65 $title = $this->page->getTitle();
66
67 // Delete restrictions for it
68 $this->mDb->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
69
70 // Fix category table counts
71 $cats = $this->mDb->selectFieldValues(
72 'categorylinks',
73 'cl_to',
74 [ 'cl_from' => $id ],
75 __METHOD__
76 );
77 $catBatches = array_chunk( $cats, $batchSize );
78 foreach ( $catBatches as $catBatch ) {
79 $this->page->updateCategoryCounts( [], $catBatch, $id );
80 if ( count( $catBatches ) > 1 ) {
81 $factory->commitMasterChanges( __METHOD__ );
82 $factory->waitForReplication( [ 'wiki' => $this->mDb->getWikiID() ] );
83 }
84 }
85
86 // Refresh the category table entry if it seems to have no pages. Check
87 // master for the most up-to-date cat_pages count.
88 if ( $title->getNamespace() === NS_CATEGORY ) {
89 $row = $this->mDb->selectRow(
90 'category',
91 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
92 [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ],
93 __METHOD__
94 );
95 if ( $row ) {
96 $cat = Category::newFromRow( $row, $title )->refreshCounts();
97 }
98 }
99
100 // If using cascading deletes, we can skip some explicit deletes
101 if ( !$this->mDb->cascadingDeletes() ) {
102 // Delete outgoing links
103 $this->batchDeleteByPK(
104 'pagelinks',
105 [ 'pl_from' => $id ],
106 [ 'pl_from', 'pl_namespace', 'pl_title' ],
107 $batchSize
108 );
109 $this->batchDeleteByPK(
110 'imagelinks',
111 [ 'il_from' => $id ],
112 [ 'il_from', 'il_to' ],
113 $batchSize
114 );
115 $this->batchDeleteByPK(
116 'categorylinks',
117 [ 'cl_from' => $id ],
118 [ 'cl_from', 'cl_to' ],
119 $batchSize
120 );
121 $this->batchDeleteByPK(
122 'templatelinks',
123 [ 'tl_from' => $id ],
124 [ 'tl_from', 'tl_namespace', 'tl_title' ],
125 $batchSize
126 );
127 $this->batchDeleteByPK(
128 'externallinks',
129 [ 'el_from' => $id ],
130 [ 'el_id' ],
131 $batchSize
132 );
133 $this->batchDeleteByPK(
134 'langlinks',
135 [ 'll_from' => $id ],
136 [ 'll_from', 'll_lang' ],
137 $batchSize
138 );
139 $this->batchDeleteByPK(
140 'iwlinks',
141 [ 'iwl_from' => $id ],
142 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
143 $batchSize
144 );
145 // Delete any redirect entry or page props entries
146 $this->mDb->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
147 $this->mDb->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
148 }
149
150 // If using cleanup triggers, we can skip some manual deletes
151 if ( !$this->mDb->cleanupTriggers() ) {
152 // Find recentchanges entries to clean up...
153 $rcIdsForTitle = $this->mDb->selectFieldValues(
154 'recentchanges',
155 'rc_id',
156 [
157 'rc_type != ' . RC_LOG,
158 'rc_namespace' => $title->getNamespace(),
159 'rc_title' => $title->getDBkey(),
160 'rc_timestamp < ' .
161 $this->mDb->addQuotes( $this->mDb->timestamp( $this->timestamp ) )
162 ],
163 __METHOD__
164 );
165 $rcIdsForPage = $this->mDb->selectFieldValues(
166 'recentchanges',
167 'rc_id',
168 [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
169 __METHOD__
170 );
171
172 // T98706: delete by PK to avoid lock contention with RC delete log insertions
173 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
174 foreach ( $rcIdBatches as $rcIdBatch ) {
175 $this->mDb->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
176 if ( count( $rcIdBatches ) > 1 ) {
177 $factory->commitMasterChanges( __METHOD__ );
178 $factory->waitForReplication( [ 'wiki' => $this->mDb->getWikiID() ] );
179 }
180 }
181 }
182
183 // Commit and release the lock
184 ScopedCallback::consume( $scopedLock );
185 }
186
187 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
188 $dbw = $this->mDb; // convenience
189 $factory = wfGetLBFactory();
190 $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
191
192 $pkDeleteConds = [];
193 foreach ( $res as $row ) {
194 $pkDeleteConds[] = $this->mDb->makeList( (array)$row, LIST_AND );
195 if ( count( $pkDeleteConds ) >= $bSize ) {
196 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
197 $factory->commitMasterChanges( __METHOD__ );
198 $factory->waitForReplication( [ 'wiki' => $dbw->getWikiID() ] );
199 $pkDeleteConds = [];
200 }
201 }
202
203 if ( $pkDeleteConds ) {
204 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
205 }
206 }
207
208 public function getAsJobSpecification() {
209 return [
210 'wiki' => $this->mDb->getWikiID(),
211 'job' => new JobSpecification(
212 'deleteLinks',
213 [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
214 [ 'removeDuplicates' => true ],
215 $this->page->getTitle()
216 )
217 ];
218 }
219 }