summaryrefslogtreecommitdiff
blob: a5f331df165782763abfbe2fb08e01d2edeb8136 (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
<?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 ConfigException;
use MediaWiki\Linker\LinkRenderer;
use MWException;
use User;
use Wikimedia\Assert\Assert;
use WikiPage;

class CommentStreamsFactory {
	/**
	 * @var \MediaWiki\Config\ServiceOptions|\Config
	 */
	private $options;

	/**
	 * @var CommentStreamsStore
	 */
	private $commentStreamsStore;

	/**
	 * @var CommentStreamsEchoInterface
	 */
	private $echoInterface;

	/**
	 * @var CommentStreamsSMWInterface
	 */
	private $smwInterface;

	/**
	 * @var CommentStreamsSocialProfileInterface
	 */
	private $socialProfileInterface;

	/**
	 * @var LinkRenderer
	 */
	private $linkRenderer;

	/**
	 * @param \MediaWiki\Config\ServiceOptions|\Config $options
	 * @param CommentStreamsStore $commentStreamsStore
	 * @param CommentStreamsEchoInterface $echoInterface
	 * @param CommentStreamsSMWInterface $smwInterface
	 * @param CommentStreamsSocialProfileInterface $socialProfileInterface
	 * @param LinkRenderer $linkRenderer
	 */
	public function __construct(
		$options,
		CommentStreamsStore $commentStreamsStore,
		CommentStreamsEchoInterface $echoInterface,
		CommentStreamsSMWInterface $smwInterface,
		CommentStreamsSocialProfileInterface $socialProfileInterface,
		LinkRenderer $linkRenderer
	) {
		$this->options = $options;
		$this->commentStreamsStore = $commentStreamsStore;
		$this->echoInterface = $echoInterface;
		$this->smwInterface = $smwInterface;
		$this->socialProfileInterface = $socialProfileInterface;
		$this->linkRenderer = $linkRenderer;
	}

	/**
	 * create a new Comment object from existing wiki page
	 *
	 * @param WikiPage $wikipage WikiPage object corresponding to comment page
	 * @return Comment|null the newly created comment or null if there was an
	 * error
	 * @throws MWException
	 * @throws ConfigException
	 */
	public function newFromWikiPage( WikiPage $wikipage ) : ?Comment {
		if ( $wikipage->getTitle()->getNamespace() !== NS_COMMENTSTREAMS || !$wikipage->exists() ) {
			return null;
		}

		$result = $this->commentStreamsStore->getComment( $wikipage->getId() );
		if ( $result === null ) {
			return null;
		}
		$comment_title = $result['comment_title'];
		$wikitext = $this->commentStreamsStore->getWikiText( $wikipage, $comment_title );
		return new Comment(
			$this->options,
			$this->commentStreamsStore,
			$this->echoInterface,
			$this->smwInterface,
			$this->socialProfileInterface,
			$this->linkRenderer,
			$wikipage,
			$result['comment_block_id'],
			$result['assoc_page_id'],
			$result['parent_page_id'],
			$comment_title,
			$wikitext
		);
	}

	/**
	 * create a new Comment object from values and save to database
	 * NOTE: since only head comments can contain a comment title, either
	 * $comment_title or $parent_page_id must be non null, but not both
	 *
	 * @param ?string $comment_block_id unique id to identify comment block in a page
	 * @param int $assoc_page_id page ID for the wiki page this comment is on
	 * @param ?int $parent_page_id page ID for the wiki page this comment is in
	 * reply to or null
	 * @param ?string $comment_title string title of comment
	 * @param string $wikitext the wikitext to add
	 * @param User $user the user
	 * @return Comment|null new comment object or null if there was a problem
	 * creating it
	 * @throws MWException
	 * @throws ConfigException
	 */
	public function newFromValues(
		?string $comment_block_id,
		int $assoc_page_id,
		?int $parent_page_id,
		?string $comment_title,
		string $wikitext,
		User $user
	) : ?Comment {
		Assert::parameter(
			( $comment_title === null && $parent_page_id !== null ) ||
			( $comment_title !== null && $parent_page_id === null ),
			'$comment_title',
			'must be null if parent page ID is non-null or non-null if parent page ID is null'
		);

		$wikipage = $this->commentStreamsStore->insertComment(
			$user,
			$wikitext,
			$comment_block_id,
			$assoc_page_id,
			$parent_page_id,
			$comment_title
		);

		if ( !$wikipage ) {
			return null;
		}

		$comment = new Comment(
			$this->options,
			$this->commentStreamsStore,
			$this->echoInterface,
			$this->smwInterface,
			$this->socialProfileInterface,
			$this->linkRenderer,
			$wikipage,
			$comment_block_id,
			$assoc_page_id,
			$parent_page_id,
			$comment_title,
			$wikitext
		);

		$this->smwInterface->update( $wikipage->getTitle() );

		if ( $parent_page_id === null ) {
			$this->commentStreamsStore->watch( $wikipage->getId(), $user->getId() );
		} else {
			$this->commentStreamsStore->watch( $parent_page_id, $user->getId() );
		}

		return $comment;
	}
}