summaryrefslogtreecommitdiff
blob: 7ab2f70ef28399d02e91546ee03c1e1bfc194c10 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/*
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

namespace MediaWiki\Extension\CommentStreams;

use Content;
use FatalError;
use File;
use IContextSource;
use IDBAccessObject;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserIdentity;
use MWException;
use MWNamespace;
use OutputPage;
use Parser;
use Status;
use Title;
use User;
use WikiPage;

// @phan-file-suppress PhanUndeclaredMethod
// @phan-file-suppress PhanTypeMismatchArgumentReal
// @phan-file-suppress UnusedPluginFileSuppression
class CommentStreamsUtils {
	/**
	 * @param string $action
	 * @param User $user
	 * @param Title $title
	 * @return bool
	 */
	public static function userCan( string $action, User $user, Title $title ) : bool {
		if ( class_exists( '\MediaWiki\Permissions\PermissionManager' ) ) {
			// MW 1.33+
			return MediaWikiServices::getInstance()->getPermissionManager()->
				userCan( $action, $user, $title );
		}
		return $title->userCan( $action, $user );
	}

	/**
	 * @param User $user
	 * @param string $right
	 * @return bool
	 */
	public static function userHasRight( User $user, string $right ) : bool {
		if ( class_exists( '\MediaWiki\Permissions\PermissionManager' ) &&
			method_exists( '\MediaWiki\Permissions\PermissionManager', 'userHasRight' ) ) {
			// MW 1.34+
			$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
			if ( !$permissionManager->userHasRight( $user, $right ) ||
				$user->getBlock() !== null ) {
				// This is not quite right, since it will prevent a user from commenting if they
				// are blocked from any action, which may be overly broad
				return false;
			}
		} elseif ( !in_array( $right, $user->getRights() ) || $user->isBlocked() ) {
			return false;
		}
		return true;
	}

	/**
	 * @param int $id Article ID to load
	 * @param string|int $from One of the following values:
	 *        - "fromdb" or WikiPage::READ_NORMAL to select from a replica DB
	 *        - "fromdbmaster" or WikiPage::READ_LATEST to select from the master database
	 * @return WikiPage|null
	 */
	public static function newWikiPageFromId( int $id, $from = 'fromdb' ) : ?WikiPage {
		if ( class_exists( '\MediaWiki\Page\WikiPageFactory' ) ) {
			// MW 1.36+
			return MediaWikiServices::getInstance()->getWikiPageFactory()->newFromID( $id, $from );
		}
		return WikiPage::newFromId( $id, $from );
	}

	/**
	 * @param string $wikitext
	 * @param OutputPage $outputPage
	 * @throws MWException
	 */
	public static function addWikiTextToOutputPage( string $wikitext, OutputPage $outputPage ) {
		if ( method_exists( 'OutputPage', 'addWikiTextAsInterface' ) ) {
			// MW 1.32+
			$outputPage->addWikiTextAsInterface( $wikitext );
		} else {
			$outputPage->addWikiText( $wikitext );
		}
	}

	/**
	 * @param Title $title
	 * @return bool
	 */
	public static function hasDeletedEdits( Title $title ) : bool {
		if ( method_exists( $title, 'hasDeletedEdits' ) ) {
			// MW 1.36+
			return $title->hasDeletedEdits();
		}
		return $title->isDeletedQuick();
	}

	/**
	 * @param string $filename
	 * @return bool|File
	 */
	public static function findFile( string $filename ) {
		if ( method_exists( MediaWikiServices::class, 'getRepoGroup' ) ) {
			// MediaWiki 1.34+
			return MediaWikiServices::getInstance()->getRepoGroup()->findFile( $filename );
		} else {
			return wfFindFile( $filename, [ 'latest' => true ] );
		}
	}

	/**
	 * @param int $id
	 * @param Title $title
	 * @return bool|string
	 */
	public static function getTimestampFromId( int $id, Title $title ) {
		$revStore = MediaWikiServices::getInstance()->getRevisionStore();
		if ( version_compare( MW_VERSION, '1.34', '<' ) ) {
			// @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal
			return $revStore->getTimestampFromId( $title, $id );
		} else {
			return $revStore->getTimestampFromId( $id );
		}
	}

	/**
	 * @param Title $title
	 * @return ?string
	 */
	public static function getCreationTimestamp( Title $title ) : ?string {
		if ( class_exists( '\MediaWiki\Revision\RevisionStore' ) &&
			method_exists( '\MediaWiki\Revision\RevisionStore', 'getFirstRevision' ) ) {
			// MW 1.35+
			return MediaWikiServices::getInstance()->getRevisionStore()->
				getFirstRevision( $title )->getTimestamp();
		}
		return $title->getEarliestRevTime();
	}

	/**
	 * @param Title $title
	 * @return ?int
	 */
	public static function getFirstRevisionId( Title $title ) : ?int {
		if ( class_exists( '\MediaWiki\Revision\RevisionStore' ) &&
			method_exists( '\MediaWiki\Revision\RevisionStore', 'getFirstRevision' ) ) {
			// MW 1.35+
			$revisionRecord =
				MediaWikiServices::getInstance()->getRevisionStore()->getFirstRevision( $title );
			if ( $revisionRecord !== null ) {
				return $revisionRecord->getId();
			}
		} else {
			$revision = $title->getFirstRevision();
			if ( $revision !== null ) {
				return $revision->getId();
			}
		}
		return null;
	}

	/**
	 * @param Title $title
	 * @return ?User
	 */
	public static function getAuthor( Title $title ) : ?User {
		if ( class_exists( '\MediaWiki\Revision\RevisionStore' ) &&
			method_exists( '\MediaWiki\Revision\RevisionStore', 'getFirstRevision' ) ) {
			// MW 1.35+
			$revisionRecord =
				MediaWikiServices::getInstance()->getRevisionStore()->getFirstRevision( $title );
			if ( $revisionRecord !== null ) {
				return User::newFromId( $revisionRecord->getUser(
					\MediaWiki\Revision\RevisionRecord::RAW )->getId() );
			}
		} else {
			$revision = $title->getFirstRevision( Title::GAID_FOR_UPDATE );
			if ( $revision !== null ) {
				return User::newFromId( $revision->getUser() );
			}
		}
		return null;
	}

	/**
	 * @param WikiPage $wikipage
	 * @return ?UserIdentity
	 */
	public static function getLastEditor( WikiPage $wikipage ) : ?UserIdentity {
		$revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
		$latestRevision = $revisionStore->getRevisionByTitle( $wikipage->getTitle(),
			0, IDBAccessObject::READ_LATEST );
		if ( $latestRevision !== null ) {
			if ( class_exists( '\MediaWiki\Revision\RevisionRecord' ) ) {
				return $latestRevision->getUser( \MediaWiki\Revision\RevisionRecord::RAW );
			} else {
				return $latestRevision->getUser( \MediaWiki\Storage\RevisionRecord::RAW );
			}
		}
		return null;
	}

	/**
	 * @param WikiPage $wikipage
	 * @param Content $content
	 * @param User $user
	 * @param int $flags
	 * @return Status
	 * @throws MWException
	 */
	public static function doEditContent(
		WikiPage $wikipage,
		Content $content,
		User $user,
		int $flags
	) : Status {
		return $wikipage->doEditContent(
			$content,
			'',
			$flags,
			false,
			$user,
			null );
	}

	/**
	 * @param WikiPage $wikipage
	 * @param string $reason
	 * @param User $deleter
	 * @return Status
	 * @throws FatalError
	 * @throws MWException
	 */
	public static function deDeleteArticle(
		WikiPage $wikipage,
		string $reason,
		User $deleter
	) : Status {
		if ( version_compare( MW_VERSION, '1.35', '<' ) ) {
			return $wikipage->doDeleteArticleReal( $reason, true );
		}
		return $wikipage->doDeleteArticleReal( $reason, $deleter, true );
	}

	/**
	 * @param int $namespace
	 * @return int
	 */
	public static function getSubjectNamespace( int $namespace ) : int {
		if ( class_exists( 'NamespaceInfo' ) ) {
			// MW 1.34+
			return MediaWikiServices::getInstance()->getNamespaceInfo()->getSubject( $namespace );
		}
		return MWNamespace::getSubject( $namespace );
	}

	/**
	 * @param string $wikitext
	 * @param WikiPage $wikipage
	 * @param IContextSource $context
	 * @return string
	 */
	public static function parse(
		string $wikitext,
		WikiPage $wikipage,
		IContextSource $context
	) : string {
		if ( class_exists( '\ParserFactory' ) ) {
			// MW 1.32+
			$parser = MediaWikiServices::getInstance()->getParserFactory()->create();
		} else {
			// @phan-suppress-next-line PhanParamTooFew
			$parser = new Parser();
		}
		$parserOptions = $wikipage->makeParserOptions( $context );
		$parserOptions->setOption( 'enableLimitReport', false );
		return $parser
			->parse( $wikitext, $wikipage->getTitle(), $parserOptions )
			->getText( [ 'wrapperDivClass' => '' ] );
	}

	/**
	 * @param WikiPage $wikipage
	 * @return Content
	 */
	public static function getContent( WikiPage $wikipage ) : Content {
		if ( class_exists( '\MediaWiki\Revision\RevisionRecord' ) ) {
			return $wikipage->getContent( \MediaWiki\Revision\RevisionRecord::RAW );
		}
		return $wikipage->getContent( \MediaWiki\Storage\RevisionRecord::RAW );
	}
}