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