MediaWiki  1.29.1
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, [ 'wiki' => $dbw->getWikiID() ]
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  Category::newFromRow( $row, $title )->refreshCounts();
110  }
111  }
112 
113  $this->batchDeleteByPK(
114  'pagelinks',
115  [ 'pl_from' => $id ],
116  [ 'pl_from', 'pl_namespace', 'pl_title' ],
117  $batchSize
118  );
119  $this->batchDeleteByPK(
120  'imagelinks',
121  [ 'il_from' => $id ],
122  [ 'il_from', 'il_to' ],
123  $batchSize
124  );
125  $this->batchDeleteByPK(
126  'categorylinks',
127  [ 'cl_from' => $id ],
128  [ 'cl_from', 'cl_to' ],
129  $batchSize
130  );
131  $this->batchDeleteByPK(
132  'templatelinks',
133  [ 'tl_from' => $id ],
134  [ 'tl_from', 'tl_namespace', 'tl_title' ],
135  $batchSize
136  );
137  $this->batchDeleteByPK(
138  'externallinks',
139  [ 'el_from' => $id ],
140  [ 'el_id' ],
141  $batchSize
142  );
143  $this->batchDeleteByPK(
144  'langlinks',
145  [ 'll_from' => $id ],
146  [ 'll_from', 'll_lang' ],
147  $batchSize
148  );
149  $this->batchDeleteByPK(
150  'iwlinks',
151  [ 'iwl_from' => $id ],
152  [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
153  $batchSize
154  );
155 
156  // Delete any redirect entry or page props entries
157  $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
158  $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
159 
160  // Find recentchanges entries to clean up...
161  $rcIdsForTitle = $dbw->selectFieldValues(
162  'recentchanges',
163  'rc_id',
164  [
165  'rc_type != ' . RC_LOG,
166  'rc_namespace' => $title->getNamespace(),
167  'rc_title' => $title->getDBkey(),
168  'rc_timestamp < ' .
169  $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) )
170  ],
171  __METHOD__
172  );
173  $rcIdsForPage = $dbw->selectFieldValues(
174  'recentchanges',
175  'rc_id',
176  [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ],
177  __METHOD__
178  );
179 
180  // T98706: delete by PK to avoid lock contention with RC delete log insertions
181  $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
182  foreach ( $rcIdBatches as $rcIdBatch ) {
183  $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
184  if ( count( $rcIdBatches ) > 1 ) {
185  $lbFactory->commitAndWaitForReplication(
186  __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ]
187  );
188  }
189  }
190 
191  // Commit and release the lock (if set)
192  ScopedCallback::consume( $scopedLock );
193  }
194 
195  private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
196  $services = MediaWikiServices::getInstance();
197  $lbFactory = $services->getDBLoadBalancerFactory();
198  $dbw = $this->getDB(); // convenience
199 
200  $res = $dbw->select( $table, $pk, $conds, __METHOD__ );
201 
202  $pkDeleteConds = [];
203  foreach ( $res as $row ) {
204  $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND );
205  if ( count( $pkDeleteConds ) >= $bSize ) {
206  $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
207  $lbFactory->commitAndWaitForReplication(
208  __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ]
209  );
210  $pkDeleteConds = [];
211  }
212  }
213 
214  if ( $pkDeleteConds ) {
215  $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
216  }
217  }
218 
219  protected function getDB() {
220  if ( !$this->db ) {
221  $this->db = wfGetDB( DB_MASTER );
222  }
223 
224  return $this->db;
225  }
226 
227  public function getAsJobSpecification() {
228  return [
229  'wiki' => $this->getDB()->getWikiID(),
230  'job' => new JobSpecification(
231  'deleteLinks',
232  [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
233  [ 'removeDuplicates' => true ],
234  $this->page->getTitle()
235  )
236  ];
237  }
238 }
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:203
captcha-old.count
count
Definition: captcha-old.py:225
RC_LOG
const RC_LOG
Definition: Defines.php:142
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:36
LinksDeletionUpdate\getDB
getDB()
Definition: LinksDeletionUpdate.php:219
LinksDeletionUpdate\$pageId
integer $pageId
Definition: LinksDeletionUpdate.php:33
$res
$res
Definition: database.txt:21
$lbFactory
$lbFactory
Definition: doMaintenance.php:117
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:41
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
LIST_OR
const LIST_OR
Definition: Defines.php:44
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
LinksDeletionUpdate\getAsJobSpecification
getAsJobSpecification()
Definition: LinksDeletionUpdate.php:227
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3060
WikiPage\getId
getId()
Definition: WikiPage.php:455
WikiPage\exists
exists()
Definition: WikiPage.php:465
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:2023
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:76
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$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:2179
Category\newFromRow
static newFromRow( $row, $title=null)
Factory function, for constructing a Category object from a result set.
Definition: Category.php:173
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
page
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 my talk page
Definition: hooks.txt:2536
LinksDeletionUpdate\batchDeleteByPK
batchDeleteByPK( $table, array $conds, array $pk, $bSize)
Definition: LinksDeletionUpdate.php:195
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.