Debian Codegraph @

includes/deferred/LinksUpdate/LinksDeletionUpdate.php

raw symbols SCIP
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 namespace MediaWiki\Deferred\LinksUpdate;
24
25 use InvalidArgumentException;
26 use JobSpecification;
27 use MediaWiki\Category\Category;
28 use MediaWiki\Deferred\DeferredUpdates;
29 use MediaWiki\Deferred\EnqueueableDataUpdate;
30 use MediaWiki\MainConfigNames;
31 use MediaWiki\MediaWikiServices;
32 use MediaWiki\Page\PageIdentity;
33 use MediaWiki\Page\PageIdentityValue;
34 use MediaWiki\Parser\ParserOutput;
35
36 /**
37 * Update object handling the cleanup of links tables after a page was deleted.
38 */
39 class LinksDeletionUpdate extends LinksUpdate implements EnqueueableDataUpdate {
40 /** @var string */
41 protected $timestamp;
42
43 /**
44 * @param PageIdentity $page Page we are updating
45 * @param int|null $pageId ID of the page we are updating [optional]
46 * @param string|null $timestamp TS_MW timestamp of deletion
47 */
48 public function __construct( PageIdentity $page, $pageId = null, $timestamp = null ) {
49 if ( $pageId ) {
50 $this->mId = $pageId; // page ID at time of deletion
51 } elseif ( $page->exists() ) {
52 $this->mId = $page->getId();
53 } else {
54 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
55 }
56
57 $this->timestamp = $timestamp ?: wfTimestampNow();
58
59 $fakePO = new ParserOutput();
60 $fakePO->setCacheTime( $timestamp );
61 // Use an immutable page identity to keep reference to the page id at time of deletion - T299244
62 $immutablePageIdentity = new PageIdentityValue(
63 $page->getId(),
64 $page->getNamespace(),
65 $page->getDBkey(),
66 $page->getWikiId()
67 );
68 parent::__construct( $immutablePageIdentity, $fakePO, false );
69 }
70
71 protected function doIncrementalUpdate() {
72 $services = MediaWikiServices::getInstance();
73 $config = $services->getMainConfig();
74 $dbProvider = $services->getConnectionProvider();
75 $batchSize = $config->get( MainConfigNames::UpdateRowsPerQuery );
76
77 $id = $this->mId;
78 $title = $this->mTitle;
79
80 $dbw = $this->getDB(); // convenience
81
82 parent::doIncrementalUpdate();
83
84 // Typically, a category is empty when deleted, so check that we don't leave
85 // spurious row in the category table.
86 if ( $title->getNamespace() === NS_CATEGORY ) {
87 // T166757: do the update after the main job DB commit
88 DeferredUpdates::addCallableUpdate( static function () use ( $title ) {
89 $cat = Category::newFromName( $title->getDBkey() );
90 $cat->refreshCountsIfSmall();
91 } );
92 }
93
94 // Delete restrictions for the deleted page
95 $dbw->newDeleteQueryBuilder()
96 ->deleteFrom( 'page_restrictions' )
97 ->where( [ 'pr_page' => $id ] )
98 ->caller( __METHOD__ )->execute();
99
100 // Delete any redirect entry
101 $dbw->newDeleteQueryBuilder()
102 ->deleteFrom( 'redirect' )
103 ->where( [ 'rd_from' => $id ] )
104 ->caller( __METHOD__ )->execute();
105
106 // Find recentchanges entries to clean up...
107 // Select RC IDs just by curid, and not by title (see T307865 and T140960)
108 $rcIdsForPage = $dbw->newSelectQueryBuilder()
109 ->select( 'rc_id' )
110 ->from( 'recentchanges' )
111 ->where( [ $dbw->expr( 'rc_type', '!=', RC_LOG ), 'rc_cur_id' => $id ] )
112 ->caller( __METHOD__ )->fetchFieldValues();
113
114 // T98706: delete by PK to avoid lock contention with RC delete log insertions
115 $rcIdBatches = array_chunk( $rcIdsForPage, $batchSize );
116 foreach ( $rcIdBatches as $rcIdBatch ) {
117 $dbw->newDeleteQueryBuilder()
118 ->deleteFrom( 'recentchanges' )
119 ->where( [ 'rc_id' => $rcIdBatch ] )
120 ->caller( __METHOD__ )->execute();
121 if ( count( $rcIdBatches ) > 1 ) {
122 $dbProvider->commitAndWaitForReplication(
123 __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
124 );
125 }
126 }
127 }
128
129 public function getAsJobSpecification() {
130 return [
131 'domain' => $this->getDB()->getDomainID(),
132 'job' => new JobSpecification(
133 'deleteLinks',
134 [ 'pageId' => $this->mId, 'timestamp' => $this->timestamp ],
135 [ 'removeDuplicates' => true ],
136 $this->mTitle
137 )
138 ];
139 }
140 }