Moved LinksDeletionUpdate to a separate file
[lhc/web/wiklou.git] / includes / deferred / LinksDeletionUpdate.php
1 <?php
2 /**
3 * Updater for link tracking tables after a page edit.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Update object handling the cleanup of links tables after a page was deleted.
25 **/
26 class LinksDeletionUpdate extends SqlDataUpdate {
27 /** @var WikiPage The WikiPage that was deleted */
28 protected $mPage;
29
30 /**
31 * Constructor
32 *
33 * @param WikiPage $page Page we are updating
34 * @throws MWException
35 */
36 function __construct( WikiPage $page ) {
37 parent::__construct( false ); // no implicit transaction
38
39 $this->mPage = $page;
40
41 if ( !$page->exists() ) {
42 throw new MWException( "Page ID not known, perhaps the page doesn't exist?" );
43 }
44 }
45
46 /**
47 * Do some database updates after deletion
48 */
49 public function doUpdate() {
50 $title = $this->mPage->getTitle();
51 $id = $this->mPage->getId();
52
53 # Delete restrictions for it
54 $this->mDb->delete( 'page_restrictions', array( 'pr_page' => $id ), __METHOD__ );
55
56 # Fix category table counts
57 $cats = array();
58 $res = $this->mDb->select( 'categorylinks', 'cl_to', array( 'cl_from' => $id ), __METHOD__ );
59
60 foreach ( $res as $row ) {
61 $cats[] = $row->cl_to;
62 }
63
64 $this->mPage->updateCategoryCounts( array(), $cats );
65
66 # If using cascading deletes, we can skip some explicit deletes
67 if ( !$this->mDb->cascadingDeletes() ) {
68 # Delete outgoing links
69 $this->mDb->delete( 'pagelinks', array( 'pl_from' => $id ), __METHOD__ );
70 $this->mDb->delete( 'imagelinks', array( 'il_from' => $id ), __METHOD__ );
71 $this->mDb->delete( 'categorylinks', array( 'cl_from' => $id ), __METHOD__ );
72 $this->mDb->delete( 'templatelinks', array( 'tl_from' => $id ), __METHOD__ );
73 $this->mDb->delete( 'externallinks', array( 'el_from' => $id ), __METHOD__ );
74 $this->mDb->delete( 'langlinks', array( 'll_from' => $id ), __METHOD__ );
75 $this->mDb->delete( 'iwlinks', array( 'iwl_from' => $id ), __METHOD__ );
76 $this->mDb->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ );
77 $this->mDb->delete( 'page_props', array( 'pp_page' => $id ), __METHOD__ );
78 }
79
80 # If using cleanup triggers, we can skip some manual deletes
81 if ( !$this->mDb->cleanupTriggers() ) {
82 # Find recentchanges entries to clean up...
83 $rcIdsForTitle = $this->mDb->selectFieldValues( 'recentchanges',
84 'rc_id',
85 array(
86 'rc_type != ' . RC_LOG,
87 'rc_namespace' => $title->getNamespace(),
88 'rc_title' => $title->getDBkey()
89 ),
90 __METHOD__
91 );
92 $rcIdsForPage = $this->mDb->selectFieldValues( 'recentchanges',
93 'rc_id',
94 array( 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ),
95 __METHOD__
96 );
97
98 # T98706: delete PK to avoid lock contention with RC delete log insertions
99 $rcIds = array_merge( $rcIdsForTitle, $rcIdsForPage );
100 if ( $rcIds ) {
101 $this->mDb->delete( 'recentchanges', array( 'rc_id' => $rcIds ), __METHOD__ );
102 }
103 }
104 }
105 }