Debian Codegraph @

includes/deferred/RefreshSecondaryDataUpdate.php

raw symbols SCIP
1 <?php
2 /**
3 * Updater for secondary data 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 namespace MediaWiki\Deferred;
24
25 use Exception;
26 use JobSpecification;
27 use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
28 use MediaWiki\Page\PageIdentity;
29 use MediaWiki\Revision\RevisionRecord;
30 use MediaWiki\Storage\DerivedPageDataUpdater;
31 use MediaWiki\User\UserIdentity;
32 use MWExceptionHandler;
33 use Wikimedia\Rdbms\ILBFactory;
34
35 /**
36 * Update object handling the cleanup of secondary data after a page was edited.
37 *
38 * This makes it possible for DeferredUpdates to have retry logic using a
39 * single refreshLinks job if any of the bundled updates fail.
40 *
41 * @since 1.34
42 */
43 class RefreshSecondaryDataUpdate extends DataUpdate
44 implements TransactionRoundAwareUpdate, EnqueueableDataUpdate
45 {
46 /** @var ILBFactory */
47 private $lbFactory;
48 /** @var PageIdentity */
49 private $page;
50 /** @var DerivedPageDataUpdater */
51 private $updater;
52 /** @var bool */
53 private $recursive;
54 /** @var string|false TS_MW */
55 private $freshness;
56
57 /** @var RevisionRecord */
58 private $revisionRecord;
59 /** @var UserIdentity */
60 private $user;
61
62 /**
63 * @param ILBFactory $lbFactory
64 * @param UserIdentity $user
65 * @param PageIdentity $page Page we are updating
66 * @param RevisionRecord $revisionRecord
67 * @param DerivedPageDataUpdater $updater
68 * @param array $options Options map; supports "recursive" (bool) and "freshness" (string|false, TS_MW)
69 */
70 public function __construct(
71 ILBFactory $lbFactory,
72 UserIdentity $user,
73 PageIdentity $page,
74 RevisionRecord $revisionRecord,
75 DerivedPageDataUpdater $updater,
76 array $options
77 ) {
78 parent::__construct();
79
80 $this->lbFactory = $lbFactory;
81 $this->user = $user;
82 $this->page = $page;
83 $this->revisionRecord = $revisionRecord;
84 $this->updater = $updater;
85 $this->recursive = !empty( $options['recursive'] );
86 $this->freshness = $options['freshness'] ?? false;
87 }
88
89 public function getTransactionRoundRequirement() {
90 return self::TRX_ROUND_ABSENT;
91 }
92
93 public function doUpdate() {
94 $updates = $this->updater->getSecondaryDataUpdates( $this->recursive );
95 foreach ( $updates as $update ) {
96 if ( $update instanceof LinksUpdate ) {
97 $update->setRevisionRecord( $this->revisionRecord );
98 $update->setTriggeringUser( $this->user );
99 }
100 if ( $update instanceof DataUpdate ) {
101 $update->setCause( $this->causeAction, $this->causeAgent );
102 }
103 }
104
105 // T221577, T248003: flush any transaction; each update needs outer transaction scope
106 // and the code above may have implicitly started one.
107 $this->lbFactory->commitPrimaryChanges( __METHOD__ );
108
109 $e = null;
110 foreach ( $updates as $update ) {
111 try {
112 DeferredUpdates::attemptUpdate( $update );
113 } catch ( Exception $e ) {
114 // Try as many updates as possible on the first pass
115 MWExceptionHandler::rollbackPrimaryChangesAndLog( $e );
116 }
117 }
118
119 if ( $e instanceof Exception ) {
120 throw $e; // trigger RefreshLinksJob enqueue via getAsJobSpecification()
121 }
122 }
123
124 public function getAsJobSpecification() {
125 return [
126 'domain' => $this->lbFactory->getLocalDomainID(),
127 'job' => new JobSpecification(
128 'refreshLinksPrioritized',
129 [
130 'namespace' => $this->page->getNamespace(),
131 'title' => $this->page->getDBkey(),
132 // Ensure fresh data are used, for normal data reuse the parser cache if it was saved
133 'rootJobTimestamp' => $this->freshness ?: $this->revisionRecord->getTimestamp(),
134 'useRecursiveLinksUpdate' => $this->recursive,
135 'triggeringUser' => [
136 'userId' => $this->user->getId(),
137 'userName' => $this->user->getName()
138 ],
139 'triggeringRevisionId' => $this->revisionRecord->getId(),
140 'causeAction' => $this->getCauseAction(),
141 'causeAgent' => $this->getCauseAgent()
142 ],
143 [ 'removeDuplicates' => true ]
144 )
145 ];
146 }
147 }
148 /** @deprecated class alias since 1.42 */
149 class_alias( RefreshSecondaryDataUpdate::class, 'RefreshSecondaryDataUpdate' );