Split out new RefreshSecondaryDataUpdate class
[lhc/web/wiklou.git] / includes / deferred / RefreshSecondaryDataUpdate.php
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 /**
24 * Update object handling the cleanup of secondary data after a page was edited.
25 *
26 * This makes makes it possible for DeferredUpdates to have retry logic using a single
27 * refreshLinks job if any of the bundled updates fail.
28 */
29 class RefreshSecondaryDataUpdate extends DataUpdate implements EnqueueableDataUpdate {
30 /** @var WikiPage */
31 private $page;
32 /** @var DeferrableUpdate[] */
33 private $updates;
34 /** @var bool */
35 private $recursive;
36 /** @var string */
37 private $cacheTimestamp;
38 /** @var string Database domain ID */
39 private $domain;
40
41 /** @var Revision|null */
42 private $revision;
43 /** @var User|null */
44 private $user;
45
46 /**
47 * @param WikiPage $page Page we are updating
48 * @param DeferrableUpdate[] $updates Updates from DerivedPageDataUpdater::getSecondaryUpdates()
49 * @param array $options Options map (causeAction, causeAgent, recursive)
50 * @param string $cacheTime Result of ParserOutput::getCacheTime() for the source output
51 * @param string $domain The database domain ID of the wiki the update is for
52 */
53 function __construct(
54 WikiPage $page,
55 array $updates,
56 array $options,
57 $cacheTime,
58 $domain
59 ) {
60 parent::__construct();
61
62 $this->page = $page;
63 $this->updates = $updates;
64 $this->causeAction = $options['causeAction'] ?? 'unknown';
65 $this->causeAgent = $options['causeAgent'] ?? 'unknown';
66 $this->recursive = !empty( $options['recursive'] );
67 $this->cacheTimestamp = $cacheTime;
68 $this->domain = $domain;
69 }
70
71 public function doUpdate() {
72 foreach ( $this->updates as $update ) {
73 $update->doUpdate();
74 }
75 }
76
77 /**
78 * Set the revision corresponding to this LinksUpdate
79 * @param Revision $revision
80 */
81 public function setRevision( Revision $revision ) {
82 $this->revision = $revision;
83 }
84
85 /**
86 * Set the User who triggered this LinksUpdate
87 * @param User $user
88 */
89 public function setTriggeringUser( User $user ) {
90 $this->user = $user;
91 }
92
93 public function getAsJobSpecification() {
94 return [
95 'wiki' => WikiMap::getWikiIdFromDomain( $this->domain ),
96 'job' => new JobSpecification(
97 'refreshLinksPrioritized',
98 [
99 // Reuse the parser cache if it was saved
100 'rootJobTimestamp' => $this->cacheTimestamp,
101 'useRecursiveLinksUpdate' => $this->recursive,
102 'triggeringUser' => $this->user
103 ? [
104 'userId' => $this->user->getId(),
105 'userName' => $this->user->getName()
106 ]
107 : false,
108 'triggeringRevisionId' => $this->revision ? $this->revision->getId() : false,
109 'causeAction' => $this->getCauseAction(),
110 'causeAgent' => $this->getCauseAgent()
111 ],
112 [ 'removeDuplicates' => true ],
113 $this->page->getTitle()
114 )
115 ];
116 }
117 }