Merge "Http::getProxy() method to get proxy configuration"
[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 * Update object handling the cleanup of links tables after a page was deleted.
24 **/
25 class LinksDeletionUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
26 /** @var WikiPage */
27 protected $page;
28 /** @var integer */
29 protected $pageId;
30
31 /**
32 * @param WikiPage $page Page we are updating
33 * @param integer|null $pageId ID of the page we are updating [optional]
34 * @throws MWException
35 */
36 function __construct( WikiPage $page, $pageId = null ) {
37 parent::__construct( false ); // no implicit transaction
38
39 $this->page = $page;
40 if ( $page->exists() ) {
41 $this->pageId = $page->getId();
42 } elseif ( $pageId ) {
43 $this->pageId = $pageId;
44 } else {
45 throw new MWException( "Page ID not known, perhaps the page doesn't exist?" );
46 }
47 }
48
49 public function doUpdate() {
50 # Page may already be deleted, so don't just getId()
51 $id = $this->pageId;
52
53 # Delete restrictions for it
54 $this->mDb->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ );
55
56 # Fix category table counts
57 $cats = $this->mDb->selectFieldValues(
58 'categorylinks',
59 'cl_to',
60 [ 'cl_from' => $id ],
61 __METHOD__
62 );
63 $this->page->updateCategoryCounts( [], $cats );
64
65 # If using cascading deletes, we can skip some explicit deletes
66 if ( !$this->mDb->cascadingDeletes() ) {
67 # Delete outgoing links
68 $this->mDb->delete( 'pagelinks', [ 'pl_from' => $id ], __METHOD__ );
69 $this->mDb->delete( 'imagelinks', [ 'il_from' => $id ], __METHOD__ );
70 $this->mDb->delete( 'categorylinks', [ 'cl_from' => $id ], __METHOD__ );
71 $this->mDb->delete( 'templatelinks', [ 'tl_from' => $id ], __METHOD__ );
72 $this->mDb->delete( 'externallinks', [ 'el_from' => $id ], __METHOD__ );
73 $this->mDb->delete( 'langlinks', [ 'll_from' => $id ], __METHOD__ );
74 $this->mDb->delete( 'iwlinks', [ 'iwl_from' => $id ], __METHOD__ );
75 $this->mDb->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ );
76 $this->mDb->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ );
77 }
78
79 # If using cleanup triggers, we can skip some manual deletes
80 if ( !$this->mDb->cleanupTriggers() ) {
81 $title = $this->page->getTitle();
82 # Find recentchanges entries to clean up...
83 $rcIdsForTitle = $this->mDb->selectFieldValues( 'recentchanges',
84 'rc_id',
85 [
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 [ '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', [ 'rc_id' => $rcIds ], __METHOD__ );
102 }
103 }
104 }
105
106 public function getAsJobSpecification() {
107 return [
108 'wiki' => $this->mDb->getWikiID(),
109 'job' => new JobSpecification(
110 'deleteLinks',
111 [ 'pageId' => $this->pageId ],
112 [ 'removeDuplicates' => true ],
113 $this->page->getTitle()
114 )
115 ];
116 }
117 }