MediaWiki  master
LinksDeletionUpdate.php
Go to the documentation of this file.
1 <?php
27  protected $page;
29  protected $pageId;
31  protected $timestamp;
32 
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 
58  // Page may already be deleted, so don't just getId()
59  $id = $this->pageId;
60  // Make sure all links update threads see the changes of each other.
61  // This handles the case when updates have to batched into several COMMITs.
62  $scopedLock = LinksUpdate::acquirePageLock( $this->mDb, $id );
63 
64  $title = $this->page->getTitle();
65 
66  // Delete restrictions for it
67  $this->mDb->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
68 
69  // Fix category table counts
70  $cats = $this->mDb->selectFieldValues(
71  'categorylinks',
72  'cl_to',
73  [ 'cl_from' => $id ],
74  __METHOD__
75  );
76  $catBatches = array_chunk( $cats, $batchSize );
77  foreach ( $catBatches as $catBatch ) {
78  $this->page->updateCategoryCounts( [], $catBatch, $id );
79  if ( count( $catBatches ) > 1 ) {
80  $this->mDb->commit( __METHOD__, 'flush' );
81  wfGetLBFactory()->waitForReplication( [ 'wiki' => $this->mDb->getWikiID() ] );
82  }
83  }
84 
85  // Refresh the category table entry if it seems to have no pages. Check
86  // master for the most up-to-date cat_pages count.
87  if ( $title->getNamespace() === NS_CATEGORY ) {
88  $row = $this->mDb->selectRow(
89  'category',
90  [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
91  [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ],
92  __METHOD__
93  );
94  if ( $row ) {
95  $cat = Category::newFromRow( $row, $title )->refreshCounts();
96  }
97  }
98 
99  // If using cascading deletes, we can skip some explicit deletes
100  if ( !$this->mDb->cascadingDeletes() ) {
101  // Delete outgoing links
102  $this->batchDeleteByPK(
103  'pagelinks',
104  [ 'pl_from' => $id ],
105  [ 'pl_from', 'pl_namespace', 'pl_title' ],
106  $batchSize
107  );
108  $this->batchDeleteByPK(
109  'imagelinks',
110  [ 'il_from' => $id ],
111  [ 'il_from', 'il_to' ],
112  $batchSize
113  );
114  $this->batchDeleteByPK(
115  'categorylinks',
116  [ 'cl_from' => $id ],
117  [ 'cl_from', 'cl_to' ],
118  $batchSize
119  );
120  $this->batchDeleteByPK(
121  'templatelinks',
122  [ 'tl_from' => $id ],
123  [ 'tl_from', 'tl_namespace', 'tl_title' ],
124  $batchSize
125  );
126  $this->batchDeleteByPK(
127  'externallinks',
128  [ 'el_from' => $id ],
129  [ 'el_id' ],
130  $batchSize
131  );
132  $this->batchDeleteByPK(
133  'langlinks',
134  [ 'll_from' => $id ],
135  [ 'll_from', 'll_lang' ],
136  $batchSize
137  );
138  $this->batchDeleteByPK(
139  'iwlinks',
140  [ 'iwl_from' => $id ],
141  [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
142  $batchSize
143  );
144  // Delete any redirect entry or page props entries
145  $this->mDb->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
146  $this->mDb->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
147  }
148 
149  // If using cleanup triggers, we can skip some manual deletes
150  if ( !$this->mDb->cleanupTriggers() ) {
151  // Find recentchanges entries to clean up...
152  $rcIdsForTitle = $this->mDb->selectFieldValues(
153  'recentchanges',
154  'rc_id',
155  [
156  'rc_type != ' . RC_LOG,
157  'rc_namespace' => $title->getNamespace(),
158  'rc_title' => $title->getDBkey(),
159  'rc_timestamp < ' .
160  $this->mDb->addQuotes( $this->mDb->timestamp( $this->timestamp ) )
161  ],
162  __METHOD__
163  );
164  $rcIdsForPage = $this->mDb->selectFieldValues(
165  'recentchanges',
166  'rc_id',
167  [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
168  __METHOD__
169  );
170 
171  // T98706: delete by PK to avoid lock contention with RC delete log insertions
172  $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
173  foreach ( $rcIdBatches as $rcIdBatch ) {
174  $this->mDb->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
175  if ( count( $rcIdBatches ) > 1 ) {
176  $this->mDb->commit( __METHOD__, 'flush' );
177  wfGetLBFactory()->waitForReplication( [ 'wiki' => $this->mDb->getWikiID() ] );
178  }
179  }
180  }
181 
182  // Commit and release the lock
183  ScopedCallback::consume( $scopedLock );
184  }
185 
186  private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
187  $dbw = $this->mDb; // convenience
188  $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
189 
190  $pkDeleteConds = [];
191  foreach ( $res as $row ) {
192  $pkDeleteConds[] = $this->mDb->makeList( (array)$row, LIST_AND );
193  if ( count( $pkDeleteConds ) >= $bSize ) {
194  $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
195  $dbw->commit( __METHOD__, 'flush' );
196  wfGetLBFactory()->waitForReplication( [ 'wiki' => $dbw->getWikiID() ] );
197  $pkDeleteConds = [];
198  }
199  }
200 
201  if ( $pkDeleteConds ) {
202  $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
203  }
204  }
205 
206  public function getAsJobSpecification() {
207  return [
208  'wiki' => $this->mDb->getWikiID(),
209  'job' => new JobSpecification(
210  'deleteLinks',
211  [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
212  [ 'removeDuplicates' => true ],
213  $this->page->getTitle()
214  )
215  ];
216  }
217 }
__construct(WikiPage $page, $pageId=null, $timestamp=null)
the array() calling protocol came about after MediaWiki 1.4rc1.
IDatabase $mDb
Database connection reference.
Interface that marks a DataUpdate as enqueuable via the JobQueue.
Definition: DataUpdate.php:156
static getMain()
Static methods.
const LIST_AND
Definition: Defines.php:194
$res
Definition: database.txt:21
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
const NS_CATEGORY
Definition: Defines.php:83
batchDeleteByPK($table, array $conds, array $pk, $bSize)
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead.&$feedLinks conditions will AND in the final query as a Content object as a Content object $title
Definition: hooks.txt:312
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
Abstract base class for update jobs that put some secondary data extracted from article content into ...
const LIST_OR
Definition: Defines.php:197
Class representing a MediaWiki article and history.
Definition: WikiPage.php:31
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
wfGetLBFactory()
Get the load balancer factory object.
static consume(ScopedCallback &$sc=null)
Trigger a scoped callback and destroy it.
static newFromRow($row, $title=null)
Factory function, for constructing a Category object from a result set.
Definition: Category.php:173
static acquirePageLock(IDatabase $dbw, $pageId, $why= 'atomicity')
Acquire a lock for performing link table updates for a page on a DB.
Job queue task description base code.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk page
Definition: hooks.txt:2376
doUpdate()
Perform the actual work.
Update object handling the cleanup of links tables after a page was deleted.
const RC_LOG
Definition: Defines.php:171