Completed
Push — master ( 294bd1...ef297c )
by mw
39:06
created

DataRepairSection::getProgressBar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\MediaWiki\Specials\Admin;
4
5
use SMW\ApplicationFactory;
6
use SMW\MediaWiki\Renderer\HtmlFormRenderer;
7
use Onoi\MessageReporter\MessageReporterFactory;
8
use SMW\Message;
9
use SMW\MediaWiki\Database;
10
use Html;
11
use WebRequest;
12
use Job;
13
use Title;
14
15
/**
16
 * @license GNU GPL v2+
17
 * @since   2.5
18
 *
19
 * @author mwjames
20
 */
21
class DataRepairSection {
22
23
	/**
24
	 * @var Database
25
	 */
26
	private $connection;
27
28
	/**
29
	 * @var HtmlFormRenderer
30
	 */
31
	private $htmlFormRenderer;
32
33
	/**
34
	 * @var OutputFormatter
35
	 */
36
	private $outputFormatter;
37
38
	/**
39
	 * @var boolean
40
	 */
41
	private $enabledRefreshStore = false;
42
43
	/**
44
	 * @var boolean
45
	 */
46
	private $enabledIdDisposal = false;
47
48
	/**
49
	 * @var null|Job
50
	 */
51
	private $refreshjob = null;
52
53
	/**
54
	 * @since 2.5
55 4
	 *
56 4
	 * @param Database $connection
57 4
	 * @param HtmlFormRenderer $htmlFormRenderer
58 4
	 * @param OutputFormatter $outputFormatter
59 4
	 */
60
	public function __construct( Database $connection, HtmlFormRenderer $htmlFormRenderer, OutputFormatter $outputFormatter ) {
61
		$this->connection = $connection;
62
		$this->htmlFormRenderer = $htmlFormRenderer;
63
		$this->outputFormatter = $outputFormatter;
64
	}
65
66 2
	/**
67 2
	 * @since 2.5
68 2
	 *
69
	 * @param boolean $enabledRefreshStore
70
	 */
71
	public function enabledRefreshStore( $enabledRefreshStore ) {
72
		$this->enabledRefreshStore = (bool)$enabledRefreshStore;
73
	}
74
75 1
	/**
76
	 * @since 2.5
77 1
	 *
78
	 * @param boolean $enabledIdDisposal
79 1
	 */
80 1
	public function enabledIdDisposal( $enabledIdDisposal ) {
81 1
		$this->enabledIdDisposal = (bool)$enabledIdDisposal;
82 1
	}
83 1
84
	/**
85 1
	 * @since 2.5
86 1
	 *
87
	 * @return string
88
	 */
89
	public function getForm() {
90
91 1
		$refreshjob = $this->getRefreshJob();
92
93
		$this->htmlFormRenderer
94
			->setName( 'refreshwiki' )
95
			->addHeader( 'h2', $this->getMessage( 'smw-smwadmin-refresh-title' ) )
96
			->addHeader( 'h3', $this->getMessage( 'smw_smwadmin_datarefresh' ) )
97
			->addParagraph( $this->getMessage( 'smw_smwadmin_datarefreshdocu' ) );
98
99
		if ( !$this->enabledRefreshStore ) {
100
			$this->htmlFormRenderer->addParagraph( $this->getMessage( 'smw-smwadmin-datarefresh-disabled' ) );
101
		} elseif ( $refreshjob !== null ) {
102
			$this->htmlFormRenderer
103
				->setMethod( 'post' )
104
				->addHiddenField( 'action', 'refreshstore' )
105
				->addParagraph( $this->getMessage( 'smw_smwadmin_datarefreshprogress' ) )
106
				->addParagraph( $this->getProgressBar(  $refreshjob->getProgress() ) )
107
				->addLineBreak()
108
				->addSubmitButton( $this->getMessage( 'smw_smwadmin_datarefreshstop' ) )
109
				->addCheckbox(
110
					$this->getMessage( 'smw_smwadmin_datarefreshstopconfirm' ),
111
					'rfsure',
112
					'stop'
113
				);
114
		} elseif ( $refreshjob === null ) {
115 1
			$this->htmlFormRenderer
116
				->setMethod( 'post' )
117
				->addHiddenField( 'action', 'refreshstore' )
118
				->addHiddenField( 'rfsure', 'yes' )
119
				->addSubmitButton( $this->getMessage( 'smw_smwadmin_datarefreshbutton' ) );
120
		}
121 1
122
		$html = $this->htmlFormRenderer->getForm() . Html::element( 'p', array(), '' );
123
124
		// smw-smwadmin-outdateddisposal
125
		$this->htmlFormRenderer
126
				->addHeader( 'h3', $this->getMessage( 'smw-smwadmin-outdateddisposal-title' ) )
127
				->addParagraph( $this->getMessage( 'smw-smwadmin-outdateddisposal-intro', Message::PARSE ) );
128
129 2
		if ( $this->enabledIdDisposal && !$this->hasEntityIdDisposerJob() ) {
130
			$this->htmlFormRenderer
131 2
				->setMethod( 'post' )
132 2
				->addHiddenField( 'action', 'dispose' )
133
				->addSubmitButton( $this->getMessage( 'smw-smwadmin-outdateddisposal-button' ) );
134 2
		} elseif ( $this->enabledIdDisposal ) {
135
			$this->htmlFormRenderer
136
				->addParagraph( $this->getMessage( 'smw-smwadmin-outdateddisposal-active' ), array( 'style' => 'font-style:italic;' ) );
137
		} else {
138 2
			$this->htmlFormRenderer
139 2
				->addParagraph( $this->getMessage( 'smw-smwadmin-outdateddisposal-disabled' ) );
140
		}
141 2
142 1
		$html .= $this->htmlFormRenderer->getForm();
143
144 1
		return $html;
145 1
	}
146 1
147 1
	/**
148
	 * @since 2.5
149
	 *
150 1
	 * @param WebRequest $webRequest
151 1
	 */
152
	public function doRefresh( WebRequest $webRequest ) {
153 1
154
		$this->outputFormatter->setPageTitle( $this->getMessage( 'smw_smwadmin_datarefresh' ) );
155
		$this->outputFormatter->addParentLink();
156 1
157
		if ( !$this->enabledRefreshStore ) {
158
			return $this->outputMessage( 'smw-smwadmin-datarefresh-disabled' );
159 1
		}
160 1
161 1
		$refreshjob = $this->getRefreshJob();
162 1
		$sure = $webRequest->getText( 'rfsure' );
163
164
		if ( $sure == 'yes' ) {
165 1
			if ( $refreshjob === null ) { // careful, there might be race conditions here
166
167
				$newjob = ApplicationFactory::getInstance()->newJobFactory()->newByType(
168
					'SMW\RefreshJob',
169 2
					\SpecialPage::getTitleFor( 'SMWAdmin' ),
170
					array( 'spos' => 1, 'prog' => 0, 'rc' => 2 )
171 3
				);
172 3
173
				$newjob->insert();
174
				$this->outputMessage( 'smw_smwadmin_updatestarted' );
175 2
			} else {
176 2
				$this->outputMessage( 'smw_smwadmin_updatenotstarted' );
177 2
			}
178
179 3
		} elseif ( $sure == 'stop' ) {
180
181 3
			// delete (all) existing iteration jobs
182 1
			$this->connection->delete(
183
				'job',
184
				array( 'job_cmd' => 'SMW\RefreshJob' ),
185 2
				__METHOD__
186
			);
187
188
			$this->outputMessage( 'smw_smwadmin_updatestopped' );
189 2
		} else {
190
			$this->outputMessage( 'smw_smwadmin_updatenotstopped' );
191 2
		}
192 2
	}
193
194 2
	/**
195
	 * @since 2.5
196
	 *
197
	 * @param WebRequest $webRequest
198
	 */
199
	public function doDispose( WebRequest $webRequest ) {
0 ignored issues
show
Unused Code introduced by
The parameter $webRequest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
200 2
201
		$this->outputFormatter->setPageTitle( $this->getMessage( 'smw-smwadmin-outdateddisposal-title' ) );
202
		$this->outputFormatter->addParentLink();
203
204
		if ( !$this->enabledIdDisposal ) {
205
			return $this->outputMessage( 'smw-smwadmin-outdateddisposal-disabled' );
206
		}
207
208
		if ( !$this->hasEntityIdDisposerJob() ) {
209
			$newjob = ApplicationFactory::getInstance()->newJobFactory()->newByType(
210
				'SMW\EntityIdDisposerJob',
211
				\SpecialPage::getTitleFor( 'SMWAdmin' )
212
			);
213
214
			$newjob->insert();
215
		}
216
217
		$this->outputMessage( 'smw-smwadmin-outdateddisposal-active' );
218
	}
219
220
	private function getMessage( $key, $type = Message::TEXT ) {
221
		return Message::get( $key, $type, Message::USER_LANGUAGE );
222
	}
223
224
	private function outputMessage( $message ) {
225
		$this->outputFormatter->addHTML( '<p>' . $this->getMessage( $message ) . '</p>' );
226
	}
227
228
	private function getProgressBar( $prog ) {
229
		return Html::rawElement(
230
			'div',
231
			array( 'style' => 'float: left; background: #DDDDDD; border: 1px solid grey; width: 300px;' ),
232
			Html::rawElement( 'div', array( 'style' => 'background: #AAF; width: ' . round( $prog * 300 ) . 'px; height: 20px; ' ), '' )
233
		) . '&#160;' . round( $prog * 100, 4 ) . '%';
234
	}
235
236
	private function getRefreshJob() {
237
238
		if ( !$this->enabledRefreshStore ) {
239
			return null;
240
		}
241
242
		if ( $this->refreshjob !== null ) {
243
			return $this->refreshjob;
244
		}
245
246
		$this->refreshjob = null;
247
248
		$jobQueueLookup = ApplicationFactory::getInstance()->create( 'JobQueueLookup', $this->connection );
249
		$row = $jobQueueLookup->selectJobRowBy( 'SMW\RefreshJob' );
250
251
		if ( $row !== null && $row !== false ) { // similar to Job::pop_type, but without deleting the job
252
			$title = Title::makeTitleSafe( $row->job_namespace, $row->job_title );
253
			$blob = (string)$row->job_params !== '' ? unserialize( $row->job_params ) : false;
254
			$this->refreshjob = Job::factory( $row->job_cmd, $title, $blob, $row->job_id );
255
		}
256
257
		return $this->refreshjob;
258
	}
259
260
	private function hasEntityIdDisposerJob() {
261
262
		if ( !$this->enabledIdDisposal ) {
263
			return false;
264
		}
265
266
		$jobQueueLookup = ApplicationFactory::getInstance()->create( 'JobQueueLookup', $this->connection );
267
268
		$row = $jobQueueLookup->selectJobRowBy(
269
			'SMW\EntityIdDisposerJob'
270
		);
271
272
		return $row !== null && $row !== false;
273
	}
274
275
}
276