MediaWiki  1.30.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  }
75 
76  $title = $this->page->getTitle();
77  $dbw = $this->getDB(); // convenience
78 
79  // Delete restrictions for it
80  $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
81 
82  // Fix category table counts
83  $cats = $dbw->selectFieldValues(
84  'categorylinks',
85  'cl_to',
86  [ 'cl_from' => $id ],
87  __METHOD__
88  );
89  $catBatches = array_chunk( $cats, $batchSize );
90  foreach ( $catBatches as $catBatch ) {
91  $this->page->updateCategoryCounts( [], $catBatch, $id );
92  if ( count( $catBatches ) > 1 ) {
93  $lbFactory->commitAndWaitForReplication(
94  __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
95  );
96  }
97  }
98 
99  // Refresh the category table entry if it seems to have no pages. Check
100  // master for the most up-to-date cat_pages count.
101  if ( $title->getNamespace() === NS_CATEGORY ) {
102  $row = $dbw->selectRow(
103  'category',
104  [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
105  [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ],
106  __METHOD__
107  );
108  if ( $row ) {
109  $cat = Category::newFromRow( $row, $title );
110  // T166757: do the update after the main job DB commit
111  DeferredUpdates::addCallableUpdate( function () use ( $cat ) {
112  $cat->refreshCounts();
113  } );
114  }
115  }
116 
117  $this->batchDeleteByPK(
118  'pagelinks',
119  [ 'pl_from' => $id ],
120  [ 'pl_from', 'pl_namespace', 'pl_title' ],
121  $batchSize
122  );
123  $this->batchDeleteByPK(
124  'imagelinks',
125  [ 'il_from' => $id ],
126  [ 'il_from', 'il_to' ],
127  $batchSize
128  );
129  $this->batchDeleteByPK(
130  'categorylinks',
131  [ 'cl_from' => $id ],
132  [ 'cl_from', 'cl_to' ],
133  $batchSize
134  );
135  $this->batchDeleteByPK(
136  'templatelinks',
137  [ 'tl_from' => $id ],
138  [ 'tl_from', 'tl_namespace', 'tl_title' ],
139  $batchSize
140  );
141  $this->batchDeleteByPK(
142  'externallinks',
143  [ 'el_from' => $id ],
144  [ 'el_id' ],
145  $batchSize
146  );
147  $this->batchDeleteByPK(
148  'langlinks',
149  [ 'll_from' => $id ],
150  [ 'll_from', 'll_lang' ],
151  $batchSize
152  );
153  $this->batchDeleteByPK(
154  'iwlinks',
155  [ 'iwl_from' => $id ],
156  [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
157  $batchSize
158  );
159 
160  // Delete any redirect entry or page props entries
161  $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
162  $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
163 
164  // Find recentchanges entries to clean up...
165  $rcIdsForTitle = $dbw->selectFieldValues(
166  'recentchanges',
167  'rc_id',
168  [
169  'rc_type != ' . RC_LOG,
170  'rc_namespace' => $title->getNamespace(),
171  'rc_title' => $title->getDBkey(),
172  'rc_timestamp < ' .
173  $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) )
174  ],
175  __METHOD__
176  );
177  $rcIdsForPage = $dbw->selectFieldValues(
178  'recentchanges',
179  'rc_id',
180  [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
181  __METHOD__
182  );
183 
184  // T98706: delete by PK to avoid lock contention with RC delete log insertions
185  $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
186  foreach ( $rcIdBatches as $rcIdBatch ) {
187  $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
188  if ( count( $rcIdBatches ) > 1 ) {
189  $lbFactory->commitAndWaitForReplication(
190  __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
191  );
192  }
193  }
194 
195  // Commit and release the lock (if set)
196  ScopedCallback::consume( $scopedLock );
197  }
198 
199  private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
200  $services = MediaWikiServices::getInstance();
201  $lbFactory = $services->getDBLoadBalancerFactory();
202  $dbw = $this->getDB(); // convenience
203 
204  $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
205 
206  $pkDeleteConds = [];
207  foreach ( $res as $row ) {
208  $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND );
209  if ( count( $pkDeleteConds ) >= $bSize ) {
210  $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
211  $lbFactory->commitAndWaitForReplication(
212  __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
213  );
214  $pkDeleteConds = [];
215  }
216  }
217 
218  if ( $pkDeleteConds ) {
219  $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
220  }
221  }
222 
223  protected function getDB() {
224  if ( !$this->db ) {
225  $this->db = wfGetDB( DB_MASTER );
226  }
227 
228  return $this->db;
229  }
230 
231  public function getAsJobSpecification() {
232  return [
233  'wiki' => WikiMap::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
234  'job' => new JobSpecification(
235  'deleteLinks',
236  [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
237  [ 'removeDuplicates' => true ],
238  $this->page->getTitle()
239  )
240  ];
241  }
242 }
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:201
captcha-old.count
count
Definition: captcha-old.py:249
RC_LOG
const RC_LOG
Definition: Defines.php:145
LinksDeletionUpdate\$timestamp
string $timestamp
Definition: LinksDeletionUpdate.php:35
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
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:37
LinksDeletionUpdate\getDB
getDB()
Definition: LinksDeletionUpdate.php:223
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:44
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
EnqueueableDataUpdate
Interface that marks a DataUpdate as enqueuable via the JobQueue.
Definition: EnqueueableDataUpdate.php:10
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, IDatabase $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:111
LIST_OR
const LIST_OR
Definition: Defines.php:47
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
LinksDeletionUpdate\getAsJobSpecification
getAsJobSpecification()
Definition: LinksDeletionUpdate.php:231
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
WikiPage\getId
getId()
Definition: WikiPage.php:458
WikiPage\exists
exists()
Definition: WikiPage.php:468
LinksDeletionUpdate\__construct
__construct(WikiPage $page, $pageId=null, $timestamp=null)
Definition: LinksDeletionUpdate.php:46
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:2069
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:79
DB_MASTER
const DB_MASTER
Definition: defines.php:26
WikiMap\getWikiIdFromDomain
static getWikiIdFromDomain( $domain)
Get the wiki ID of a database domain.
Definition: WikiMap.php:252
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title 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:2198
Category\newFromRow
static newFromRow( $row, $title=null)
Factory function, for constructing a Category object from a result set.
Definition: Category.php:179
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:199
JobSpecification
Job queue task description base code.
Definition: JobSpecification.php:104
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
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
array
the array() calling protocol came about after MediaWiki 1.4rc1.