Completed
Push — master ( ea11bc...4da539 )
by mw
39:14 queued 04:01
created

DataRepairSection::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 null|Job
45
	 */
46
	private $refreshjob = null;
47
48
	/**
49
	 * @since 2.5
50
	 *
51
	 * @param Database $connection
52
	 * @param HtmlFormRenderer $htmlFormRenderer
53
	 * @param OutputFormatter $outputFormatter
54
	 */
55 4
	public function __construct( Database $connection, HtmlFormRenderer $htmlFormRenderer, OutputFormatter $outputFormatter ) {
56 4
		$this->connection = $connection;
57 4
		$this->htmlFormRenderer = $htmlFormRenderer;
58 4
		$this->outputFormatter = $outputFormatter;
59 4
	}
60
61
	/**
62
	 * @since 2.5
63
	 *
64
	 * @param boolean $enabledRefreshStore
65
	 */
66 2
	public function enabledRefreshStore( $enabledRefreshStore ) {
67 2
		$this->enabledRefreshStore = (bool)$enabledRefreshStore;
68 2
	}
69
70
	/**
71
	 * @since 2.5
72
	 *
73
	 * @return string
74
	 */
75 1
	public function getForm() {
76
77 1
		$refreshjob = $this->getRefreshJob();
78
79 1
		$this->htmlFormRenderer
80 1
			->setName( 'refreshwiki' )
81 1
			->setMethod( 'post' )
82 1
			->addHiddenField( 'action', 'refreshstore' )
83 1
			->addHeader( 'h2', $this->getMessage( 'smw_smwadmin_datarefresh' ) );
84 1
85
		if ( !$this->enabledRefreshStore ) {
86 1
			$this->htmlFormRenderer->addParagraph( $this->getMessage( 'smw-smwadmin-datarefresh-disabled' ) );
87
		} else {
88
			$this->htmlFormRenderer->addParagraph( $this->getMessage( 'smw_smwadmin_datarefreshdocu' ) );
89
		}
90
91
		if ( $refreshjob !== null ) {
92
			$prog = $refreshjob->getProgress();
93
94
			$progressBar = Html::rawElement(
95
				'div',
96
				array( 'style' => 'float: left; background: #DDDDDD; border: 1px solid grey; width: 300px;' ),
97
				Html::rawElement( 'div', array( 'style' => 'background: #AAF; width: ' . round( $prog * 300 ) . 'px; height: 20px; ' ), '' )
98
			);
99
100
			$this->htmlFormRenderer
101
				->addParagraph( $this->getMessage( 'smw_smwadmin_datarefreshprogress' ) )
102
				->addParagraph( $progressBar . '&#160;' . round( $prog * 100, 4 ) . '%' )
103
				->addLineBreak();
104
105
			if ( $this->enabledRefreshStore ) {
106
				$this->htmlFormRenderer
107
					->addSubmitButton( $this->getMessage( 'smw_smwadmin_datarefreshstop' ) )
108
					->addCheckbox(
109
						$this->getMessage( 'smw_smwadmin_datarefreshstopconfirm' ),
110 1
						'rfsure',
111
						'stop'
112
					);
113
			}
114
115
		} elseif ( $this->enabledRefreshStore ) {
116 1
			$this->htmlFormRenderer
117
				->addHiddenField( 'rfsure', 'yes' )
118
				->addSubmitButton( $this->getMessage( 'smw_smwadmin_datarefreshbutton' ) );
119
		}
120
121
		return $this->htmlFormRenderer->getForm() . Html::element( 'p', array(), '' );
122
	}
123
124 2
	/**
125
	 * @since 2.5
126 2
	 *
127 2
	 * @param WebRequest $webRequest
128
	 */
129 2
	public function doRefresh( WebRequest $webRequest ) {
130
131
		$this->outputFormatter->setPageTitle( $this->getMessage( 'smw_smwadmin_datarefresh' ) );
132
		$this->outputFormatter->addParentLink();
133 2
134 2
		if ( !$this->enabledRefreshStore ) {
135
			return $this->outputMessage( 'smw_smwadmin_return' );
136 2
		}
137 1
138
		$refreshjob = $this->getRefreshJob();
139 1
		$sure = $webRequest->getText( 'rfsure' );
140 1
141 1
		if ( $sure == 'yes' ) {
142 1
			if ( $refreshjob === null ) { // careful, there might be race conditions here
143
144
				$newjob = ApplicationFactory::getInstance()->newJobFactory()->newByType(
145 1
					'SMW\RefreshJob',
146 1
					\SpecialPage::getTitleFor( 'SMWAdmin' ),
147
					array( 'spos' => 1, 'prog' => 0, 'rc' => 2 )
148 1
				);
149
150
				$newjob->insert();
151 1
				$this->outputMessage( 'smw_smwadmin_updatestarted' );
152
			} else {
153
				$this->outputMessage( 'smw_smwadmin_updatenotstarted' );
154 1
			}
155 1
156 1
		} elseif ( $sure == 'stop' ) {
157 1
158
			// delete (all) existing iteration jobs
159
			$this->connection->delete(
160 1
				'job',
161
				array( 'job_cmd' => 'SMW\RefreshJob' ),
162
				__METHOD__
163
			);
164 2
165
			$this->outputMessage( 'smw_smwadmin_updatestopped' );
166 2
		} else {
167 2
			$this->outputMessage( 'smw_smwadmin_updatenotstopped' );
168 2
		}
169
	}
170 3
171
	private function getMessage( $key, $type = Message::TEXT ) {
172 3
		return Message::get( $key, $type, Message::USER_LANGUAGE );
173 1
	}
174
175
	private function outputMessage( $message ) {
176 2
		$this->outputFormatter->addHTML( '<p>' . $this->getMessage( $message ) . '</p>' );
177
	}
178
179
	private function getRefreshJob() {
180 2
181
		if ( !$this->enabledRefreshStore ) {
182 2
			return null;
183 2
		}
184
185 2
		if ( $this->refreshjob !== null ) {
186
			return $this->refreshjob;
187
		}
188
189
		$this->refreshjob = null;
190
191 2
		$jobQueueLookup = ApplicationFactory::getInstance()->create( 'JobQueueLookup', $this->connection );
192
		$row = $jobQueueLookup->selectJobRowFor( 'SMW\RefreshJob' );
193
194
		if ( $row !== null && $row !== false ) { // similar to Job::pop_type, but without deleting the job
195
			$title = Title::makeTitleSafe( $row->job_namespace, $row->job_title );
196
			$blob = (string)$row->job_params !== '' ? unserialize( $row->job_params ) : false;
197
			$this->refreshjob = Job::factory( $row->job_cmd, $title, $blob, $row->job_id );
198
		}
199
200
		return $this->refreshjob;
201
	}
202
203
}
204