summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/includes/CampaignHooks.php
blob: eb98c31818619270447195b127f24620ff3cd039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
 * Hooks for managing JSON Schema namespace and content model.
 *
 * @file
 * @ingroup Extensions
 * @ingroup UploadWizard
 *
 * @author Ori Livneh <ori@wikimedia.org>
 */

class CampaignHooks {

	/**
	 * 'Campaign' content model must be used in, and only in, the 'Campaign' namespace.
	 *
	 * @param string $contentModel
	 * @param Title $title
	 * @param bool &$ok
	 * @return bool
	 */
	public static function onContentModelCanBeUsedOn( $contentModel, Title $title, &$ok ) {
		$isCampaignModel = $contentModel === 'Campaign';
		$isCampaignNamespace = $title->inNamespace( NS_CAMPAIGN );
		if ( $isCampaignModel !== $isCampaignNamespace ) {
			$ok = false;
			return false;
		}
		return true;
	}

	/**
	 * FIXME: This should be done as a DataUpdate
	 *
	 * Sets up appropriate entries in the uc_campaigns table for each Campaign
	 * Acts everytime a page in the NS_CAMPAIGN namespace is saved
	 *
	 * @param WikiPage $wikiPage
	 * @param User $user
	 * @param Content $content
	 * @param string $summary
	 * @param bool $isMinor
	 * @param bool $isWatch
	 * @param string $section
	 * @param int $flags
	 * @param Revision $revision
	 * @param Status $status
	 * @param int $baseRevId
	 *
	 * @return bool
	 */
	public static function onPageContentSaveComplete(
		WikiPage $wikiPage, $user, $content, $summary, $isMinor, $isWatch,
		$section, $flags, $revision, $status, $baseRevId
	) {
		if ( !$content instanceof CampaignContent ) {
			return true;
		}

		$dbw = wfGetDB( DB_MASTER );

		$campaignData = $content->getJsonData();
		$insertData = [
			'campaign_enabled' => $campaignData['enabled'] ? 1 : 0
		];
		$success = $dbw->upsert(
			'uw_campaigns',
			array_merge( [
				'campaign_name' => $wikiPage->getTitle()->getDBkey()
			], $insertData ),
			[ 'campaign_name' ],
			$insertData
		);

		$campaign = new UploadWizardCampaign( $wikiPage->getTitle(), $content->getJsonData() );
		$dbw->onTransactionPreCommitOrIdle( function () use ( $campaign ) {
			$campaign->invalidateCache();
		} );

		return $success;
	}

	/**
	 * Invalidates the cache for a campaign when any of its dependents are edited. The 'dependents'
	 * are tracked by entries in the templatelinks table, which are inserted by using the
	 * PageContentSaveComplete hook.
	 *
	 * This is usually run via the Job Queue mechanism.
	 * @param LinksUpdate &$linksupdate
	 * @return bool
	 */
	public static function onLinksUpdateComplete( LinksUpdate &$linksupdate ) {
		if ( !$linksupdate->getTitle()->inNamespace( NS_CAMPAIGN ) ) {
			return true;
		}

		$campaign = new UploadWizardCampaign( $linksupdate->getTitle() );
		$campaign->invalidateCache();

		return true;
	}
	/**
	 * Deletes entries from uc_campaigns table when a Campaign is deleted
	 * @param Article $article
	 * @param User $user
	 * @param string $reason
	 * @param int $id
	 * @param Content $content
	 * @param ManualLogEntry $logEntry
	 * @return bool
	 */
	public static function onArticleDeleteComplete(
		$article, $user, $reason, $id, $content, $logEntry
	) {
		if ( !$article->getTitle()->inNamespace( NS_CAMPAIGN ) ) {
			return true;
		}

		$dbw = wfGetDB( DB_MASTER );
		$dbw->onTransactionPreCommitOrIdle( function () use ( $dbw, $article ) {
			$dbw->delete(
				'uw_campaigns',
				[ 'campaign_name' => $article->getTitle()->getDBKey() ]
			);
		} );

		return true;
	}

	/**
	 * Update campaign names when the Campaign page moves
	 * @param Title $oldTitle
	 * @param Title $newTitle
	 * @param User $user
	 * @param int $pageid
	 * @param int $redirid
	 * @return bool
	 */
	public static function onTitleMoveComplete(
		Title $oldTitle, Title $newTitle, $user, $pageid, $redirid
	) {
		if ( !$oldTitle->inNamespace( NS_CAMPAIGN ) ) {
			return true;
		}

		$dbw = wfGetDB( DB_MASTER );
		$success = $dbw->update(
			'uw_campaigns',
			[ 'campaign_name' => $newTitle->getDBKey() ],
			[ 'campaign_name' => $oldTitle->getDBKey() ]
		);

		return $success;
	}

	/**
	 * Declares JSON as the code editor language for Campaign: pages.
	 * This hook only runs if the CodeEditor extension is enabled.
	 * @param Title $title
	 * @param string &$lang Page language.
	 * @return bool
	 */
	public static function onCodeEditorGetPageLanguage( $title, &$lang ) {
		if ( $title->inNamespace( NS_CAMPAIGN ) ) {
			$lang = 'json';
		}
		return true;
	}

	/**
	 * Validates that the revised contents are valid JSON.
	 * If not valid, rejects edit with error message.
	 * @param IContextSource $context
	 * @param Content $content
	 * @param Status $status
	 * @param string $summary
	 * @param User $user
	 * @param bool $minoredit
	 * @return true
	 */
	public static function onEditFilterMergedContent( $context, $content, $status, $summary,
		$user, $minoredit
	) {
		if ( !$context->getTitle()->inNamespace( NS_CAMPAIGN )
			|| !$content instanceof CampaignContent
		) {
			return true;
		}

		try {
			$content->validate();
		} catch ( JsonSchemaException $e ) {
			$status->fatal( $context->msg( $e->getCode(), $e->args ) );
		}

		return true;
	}
}