MediaWiki  master
LinksDeletionUpdate.php
Go to the documentation of this file.
1 <?php
24 
25 use DeferredUpdates;
27 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;
77  $title = $this->mTitle;
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->newDeleteQueryBuilder()
95  ->deleteFrom( 'page_restrictions' )
96  ->where( [ 'pr_page' => $id ] )
97  ->caller( __METHOD__ )->execute();
98 
99  // Delete any redirect entry
100  $dbw->newDeleteQueryBuilder()
101  ->deleteFrom( 'redirect' )
102  ->where( [ 'rd_from' => $id ] )
103  ->caller( __METHOD__ )->execute();
104 
105  // Find recentchanges entries to clean up...
106  // Select RC IDs just by curid, and not by title (see T307865 and T140960)
107  $rcIdsForPage = $dbw->newSelectQueryBuilder()
108  ->select( 'rc_id' )
109  ->from( 'recentchanges' )
110  ->where( [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ] )
111  ->caller( __METHOD__ )->fetchFieldValues();
112 
113  // T98706: delete by PK to avoid lock contention with RC delete log insertions
114  $rcIdBatches = array_chunk( $rcIdsForPage, $batchSize );
115  foreach ( $rcIdBatches as $rcIdBatch ) {
116  $dbw->newDeleteQueryBuilder()
117  ->deleteFrom( 'recentchanges' )
118  ->where( [ 'rc_id' => $rcIdBatch ] )
119  ->caller( __METHOD__ )->execute();
120  if ( count( $rcIdBatches ) > 1 ) {
121  $lbFactory->commitAndWaitForReplication(
122  __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
123  );
124  }
125  }
126  }
127 
128  public function getAsJobSpecification() {
129  return [
130  'domain' => $this->getDB()->getDomainID(),
131  'job' => new JobSpecification(
132  'deleteLinks',
133  [ 'pageId' => $this->mId, 'timestamp' => $this->timestamp ],
134  [ 'removeDuplicates' => true ],
135  $this->mTitle
136  )
137  ];
138  }
139 }
140 
142 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.
Defer callable updates to run later in the PHP process.
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:33
Category objects are immutable, strictly speaking.
Definition: Category.php:41
static newFromName( $name)
Factory function.
Definition: Category.php:156
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:54
Title $mTitle
Title object of the article linked from.
Definition: LinksUpdate.php:61
int $mId
Page ID of the article linked from.
Definition: LinksUpdate.php:58
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:77
getId( $wikiId=self::LOCAL)
Definition: WikiPage.php:528
getTitle()
Get the title object of the article.
Definition: WikiPage.php:258
Interface that marks a DataUpdate as enqueuable via the JobQueue.