MediaWiki  1.32.0
LinksDeletionUpdate.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\ScopedCallback;
25 
31  protected $page;
33  protected $pageId;
35  protected $timestamp;
36 
38  private $db;
39 
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  if ( !$scopedLock ) {
75  throw new RuntimeException( "Could not acquire lock for page ID '{$id}'." );
76  }
77  }
78 
79  $title = $this->page->getTitle();
80  $dbw = $this->getDB(); // convenience
81 
82  // Delete restrictions for it
83  $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
84 
85  // Fix category table counts
86  $cats = $dbw->selectFieldValues(
87  'categorylinks',
88  'cl_to',
89  [ 'cl_from' => $id ],
90  __METHOD__
91  );
92  $catBatches = array_chunk( $cats, $batchSize );
93  foreach ( $catBatches as $catBatch ) {
94  $this->page->updateCategoryCounts( [], $catBatch, $id );
95  if ( count( $catBatches ) > 1 ) {
96  // Only sacrifice atomicity if necessary due to size
97  $lbFactory->commitAndWaitForReplication(
98  __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
99  );
100  }
101  }
102 
103  // Refresh counts on categories that should be empty now
104  if ( $title->getNamespace() === NS_CATEGORY ) {
105  // T166757: do the update after the main job DB commit
107  $cat = Category::newFromName( $title->getDBkey() );
108  $cat->refreshCountsIfEmpty();
109  } );
110  }
111 
112  $this->batchDeleteByPK(
113  'pagelinks',
114  [ 'pl_from' => $id ],
115  [ 'pl_from', 'pl_namespace', 'pl_title' ],
116  $batchSize
117  );
118  $this->batchDeleteByPK(
119  'imagelinks',
120  [ 'il_from' => $id ],
121  [ 'il_from', 'il_to' ],
122  $batchSize
123  );
124  $this->batchDeleteByPK(
125  'categorylinks',
126  [ 'cl_from' => $id ],
127  [ 'cl_from', 'cl_to' ],
128  $batchSize
129  );
130  $this->batchDeleteByPK(
131  'templatelinks',
132  [ 'tl_from' => $id ],
133  [ 'tl_from', 'tl_namespace', 'tl_title' ],
134  $batchSize
135  );
136  $this->batchDeleteByPK(
137  'externallinks',
138  [ 'el_from' => $id ],
139  [ 'el_id' ],
140  $batchSize
141  );
142  $this->batchDeleteByPK(
143  'langlinks',
144  [ 'll_from' => $id ],
145  [ 'll_from', 'll_lang' ],
146  $batchSize
147  );
148  $this->batchDeleteByPK(
149  'iwlinks',
150  [ 'iwl_from' => $id ],
151  [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
152  $batchSize
153  );
154 
155  // Delete any redirect entry or page props entries
156  $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
157  $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
158 
159  // Find recentchanges entries to clean up...
160  $rcIdsForTitle = $dbw->selectFieldValues(
161  'recentchanges',
162  'rc_id',
163  [
164  'rc_type != ' . RC_LOG,
165  'rc_namespace' => $title->getNamespace(),
166  'rc_title' => $title->getDBkey(),
167  'rc_timestamp < ' .
168  $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) )
169  ],
170  __METHOD__
171  );
172  $rcIdsForPage = $dbw->selectFieldValues(
173  'recentchanges',
174  'rc_id',
175  [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
176  __METHOD__
177  );
178 
179  // T98706: delete by PK to avoid lock contention with RC delete log insertions
180  $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
181  foreach ( $rcIdBatches as $rcIdBatch ) {
182  $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
183  if ( count( $rcIdBatches ) > 1 ) {
184  $lbFactory->commitAndWaitForReplication(
185  __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
186  );
187  }
188  }
189 
190  // Commit and release the lock (if set)
191  ScopedCallback::consume( $scopedLock );
192  }
193 
194  private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
195  $services = MediaWikiServices::getInstance();
196  $lbFactory = $services->getDBLoadBalancerFactory();
197  $dbw = $this->getDB(); // convenience
198 
199  $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
200 
201  $pkDeleteConds = [];
202  foreach ( $res as $row ) {
203  $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND );
204  if ( count( $pkDeleteConds ) >= $bSize ) {
205  $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
206  $lbFactory->commitAndWaitForReplication(
207  __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
208  );
209  $pkDeleteConds = [];
210  }
211  }
212 
213  if ( $pkDeleteConds ) {
214  $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
215  }
216  }
217 
218  protected function getDB() {
219  if ( !$this->db ) {
220  $this->db = wfGetDB( DB_MASTER );
221  }
222 
223  return $this->db;
224  }
225 
226  public function getAsJobSpecification() {
227  return [
228  'wiki' => WikiMap::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
229  'job' => new JobSpecification(
230  'deleteLinks',
231  [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
232  [ 'removeDuplicates' => true ],
233  $this->page->getTitle()
234  )
235  ];
236  }
237 }
LinksUpdate\acquirePageLock
static acquirePageLock(IDatabase $dbw, $pageId, $why='atomicity')
Acquire a lock for performing link table updates for a page on a DB.
Definition: LinksUpdate.php:200
captcha-old.count
count
Definition: captcha-old.py:249
RC_LOG
const RC_LOG
Definition: Defines.php:144
LinksDeletionUpdate\$timestamp
string $timestamp
Definition: LinksDeletionUpdate.php:35
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:44
LinksDeletionUpdate\getDB
getDB()
Definition: LinksDeletionUpdate.php:218
page
target page
Definition: All_system_messages.txt:1267
$res
$res
Definition: database.txt:21
LinksDeletionUpdate\$page
WikiPage $page
Definition: LinksDeletionUpdate.php:31
DataUpdate
Abstract base class for update jobs that do something with some secondary data extracted from article...
Definition: DataUpdate.php:28
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
LIST_AND
const LIST_AND
Definition: Defines.php:43
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
EnqueueableDataUpdate
Interface that marks a DataUpdate as enqueuable via the JobQueue.
Definition: EnqueueableDataUpdate.php:10
LIST_OR
const LIST_OR
Definition: Defines.php:46
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
LinksDeletionUpdate\getAsJobSpecification
getAsJobSpecification()
Definition: LinksDeletionUpdate.php:226
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2693
WikiPage\getId
getId()
Definition: WikiPage.php:578
WikiPage\exists
exists()
Definition: WikiPage.php:588
LinksDeletionUpdate\__construct
__construct(WikiPage $page, $pageId=null, $timestamp=null)
Definition: LinksDeletionUpdate.php:46
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1983
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
WikiMap\getWikiIdFromDomain
static getWikiIdFromDomain( $domain)
Get the wiki ID of a database domain.
Definition: WikiMap.php:252
LinksDeletionUpdate\doUpdate
doUpdate()
Perform the actual work.
Definition: LinksDeletionUpdate.php:61
LinksDeletionUpdate\$db
IDatabase $db
Definition: LinksDeletionUpdate.php:38
LinksDeletionUpdate
Update object handling the cleanup of links tables after a page was deleted.
Definition: LinksDeletionUpdate.php:29
LinksDeletionUpdate\$pageId
int $pageId
Definition: LinksDeletionUpdate.php:33
LinksDeletionUpdate\batchDeleteByPK
batchDeleteByPK( $table, array $conds, array $pk, $bSize)
Definition: LinksDeletionUpdate.php:194
JobSpecification
Job queue task description base code.
Definition: JobSpecification.php:103
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Category\newFromName
static newFromName( $name)
Factory function.
Definition: Category.php:126
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2270
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:118