MediaWiki  master
LinksDeletionUpdate.php
Go to the documentation of this file.
1 <?php
24 
25 use Category;
26 use DeferredUpdates;
28 use InvalidArgumentException;
32 use MWException;
33 use ParserOutput;
34 use WikiPage;
35 
41  protected $page;
43  protected $timestamp;
44 
51  public function __construct( WikiPage $page, $pageId = null, $timestamp = null ) {
52  $this->page = $page;
53  if ( $pageId ) {
54  $this->mId = $pageId; // page ID at time of deletion
55  } elseif ( $page->exists() ) {
56  $this->mId = $page->getId();
57  } else {
58  throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
59  }
60 
61  $this->timestamp = $timestamp ?: wfTimestampNow();
62 
63  $fakePO = new ParserOutput();
64  $fakePO->setCacheTime( $timestamp );
65  // Use immutable page identity to keep reference to the page id at time of deletion - T299244
66  $immutablePageIdentity = $page->getTitle()->toPageIdentity();
67  parent::__construct( $immutablePageIdentity, $fakePO, false );
68  }
69 
70  protected function doIncrementalUpdate() {
71  $services = MediaWikiServices::getInstance();
72  $config = $services->getMainConfig();
73  $lbFactory = $services->getDBLoadBalancerFactory();
74  $batchSize = $config->get( MainConfigNames::UpdateRowsPerQuery );
75 
76  $id = $this->mId;
78 
79  $dbw = $this->getDB(); // convenience
80 
81  parent::doIncrementalUpdate();
82 
83  // Typically, a category is empty when deleted, so check that we don't leave
84  // spurious row in the category table.
85  if ( $title->getNamespace() === NS_CATEGORY ) {
86  // T166757: do the update after the main job DB commit
87  DeferredUpdates::addCallableUpdate( static function () use ( $title ) {
88  $cat = Category::newFromName( $title->getDBkey() );
89  $cat->refreshCountsIfSmall();
90  } );
91  }
92 
93  // Delete restrictions for the deleted page
94  $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
95 
96  // Delete any redirect entry
97  $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
98 
99  // Find recentchanges entries to clean up...
100  // Select RC IDs just by curid, and not by title (see T307865 and T140960)
101  $rcIdsForPage = $dbw->selectFieldValues(
102  'recentchanges',
103  'rc_id',
104  [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
105  __METHOD__
106  );
107 
108  // T98706: delete by PK to avoid lock contention with RC delete log insertions
109  $rcIdBatches = array_chunk( $rcIdsForPage, $batchSize );
110  foreach ( $rcIdBatches as $rcIdBatch ) {
111  $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
112  if ( count( $rcIdBatches ) > 1 ) {
113  $lbFactory->commitAndWaitForReplication(
114  __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
115  );
116  }
117  }
118  }
119 
120  public function getAsJobSpecification() {
121  return [
122  'domain' => $this->getDB()->getDomainID(),
123  'job' => new JobSpecification(
124  'deleteLinks',
125  [ 'pageId' => $this->mId, 'timestamp' => $this->timestamp ],
126  [ 'removeDuplicates' => true ],
127  $this->mTitle
128  )
129  ];
130  }
131 }
132 
134 class_alias( LinksDeletionUpdate::class, 'LinksDeletionUpdate' );
const RC_LOG
Definition: Defines.php:118
const NS_CATEGORY
Definition: Defines.php:78
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Category objects are immutable, strictly speaking.
Definition: Category.php:34
static newFromName( $name)
Factory function.
Definition: Category.php:149
Class for managing the deferral of updates within the scope of a PHP script invocation.
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add an update to the pending update queue that invokes the specified callback when run.
Job queue task description base code.
MediaWiki exception.
Definition: MWException.php:30
Update object handling the cleanup of links tables after a page was deleted.
__construct(WikiPage $page, $pageId=null, $timestamp=null)
Class the manages updates of *_link tables as well as similar extension-managed tables.
Definition: LinksUpdate.php:55
Title $mTitle
Title object of the article linked from.
Definition: LinksUpdate.php:65
int $mId
Page ID of the article linked from.
Definition: LinksUpdate.php:62
A class containing constants representing the names of configuration variables.
const UpdateRowsPerQuery
Name constant for the UpdateRowsPerQuery setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Base representation for an editable wiki page.
Definition: WikiPage.php:72
getId( $wikiId=self::LOCAL)
Definition: WikiPage.php:585
getTitle()
Get the title object of the article.
Definition: WikiPage.php:315
Interface that marks a DataUpdate as enqueuable via the JobQueue.