summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Specials/Admin/DataRefreshJobTaskHandler.php
blob: 82d059cfd8d582a6232c3ba28b6a526768784213 (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
<?php

namespace SMW\MediaWiki\Specials\Admin;

use Html;
use SMW\ApplicationFactory;
use SMW\MediaWiki\Renderer\HtmlFormRenderer;
use Title;
use WebRequest;
use SMW\MediaWiki\Job;

/**
 * @license GNU GPL v2+
 * @since   2.5
 *
 * @author mwjames
 */
class DataRefreshJobTaskHandler extends TaskHandler {

	/**
	 * @var HtmlFormRenderer
	 */
	private $htmlFormRenderer;

	/**
	 * @var OutputFormatter
	 */
	private $outputFormatter;

	/**
	 * @var null|Job
	 */
	private $refreshjob = null;

	/**
	 * @since 2.5
	 *
	 * @param HtmlFormRenderer $htmlFormRenderer
	 * @param OutputFormatter $outputFormatter
	 */
	public function __construct( HtmlFormRenderer $htmlFormRenderer, OutputFormatter $outputFormatter ) {
		$this->htmlFormRenderer = $htmlFormRenderer;
		$this->outputFormatter = $outputFormatter;
	}

	/**
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function getSection() {
		return self::SECTION_DATAREPAIR;
	}

	/**
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function hasAction() {
		return true;
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function isTaskFor( $task ) {
		return $task === 'refreshstore';
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getHtml() {

		$this->htmlFormRenderer
			->addHeader( 'h4', $this->msg( 'smw_smwadmin_datarefresh' ) )
			->addParagraph( $this->msg( 'smw_smwadmin_datarefreshdocu' ) );

		if ( !$this->isEnabledFeature( SMW_ADM_REFRESH ) ) {
			$this->htmlFormRenderer->addParagraph( $this->msg( 'smw-admin-feature-disabled' ) );
		} elseif ( $this->getRefreshJob() !== null ) {
			var_dump('expression');
			$this->htmlFormRenderer
				->setMethod( 'post' )
				->addHiddenField( 'action', 'refreshstore' )
				->addParagraph( $this->msg( 'smw_smwadmin_datarefreshprogress' ) )
				->addParagraph( $this->getProgressBar( $this->getRefreshJob()->getProgress() ) )
				->addLineBreak()
				->addSubmitButton(
					$this->msg( 'smw_smwadmin_datarefreshstop' ),
					[
						'class' => ''
					]
				)
				->addCheckbox(
					$this->msg( 'smw_smwadmin_datarefreshstopconfirm' ),
					'rfsure',
					'stop'
				);
		} elseif ( $this->getRefreshJob() === null ) {
			$this->htmlFormRenderer
				->setMethod( 'post' )
				->addHiddenField( 'action', 'refreshstore' )
				->addHiddenField( 'rfsure', 'yes' )
				->addSubmitButton(
					$this->msg( 'smw_smwadmin_datarefreshbutton' ),
					[
						'class' => ''
					]
				);
		}

		return Html::rawElement( 'div', [], $this->htmlFormRenderer->getForm() );
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function handleRequest( WebRequest $webRequest ) {

		if ( !$this->isEnabledFeature( SMW_ADM_REFRESH ) ) {
			return '';
		}

		$sure = $webRequest->getText( 'rfsure' );
		$applicationFactory = ApplicationFactory::getInstance();

		if ( $sure == 'yes' ) {
			$refreshjob = $this->getRefreshJob();

			if ( $refreshjob === null ) { // careful, there might be race conditions here

				$newjob = $applicationFactory->newJobFactory()->newByType(
					'SMW\RefreshJob',
					\SpecialPage::getTitleFor( 'SMWAdmin' ),
					[ 'spos' => 1, 'prog' => 0, 'rc' => 2 ]
				);

				$newjob->insert();
			}

		} elseif ( $sure == 'stop' ) {
			$jobQueue = $applicationFactory->getJobQueue();
			$jobQueue->disableCache();
			$jobQueue->delete( 'SMW\RefreshJob' );
		}

		$this->outputFormatter->redirectToRootPage( '', [ 'tab' => 'rebuild' ] );
	}

	private function getProgressBar( $prog ) {
		return Html::rawElement(
			'div',
			[ 'style' => 'float: left; background: #DDDDDD; border: 1px solid grey; width: 300px;' ],
			Html::rawElement( 'div', [ 'style' => 'background: #AAF; width: ' . round( $prog * 300 ) . 'px; height: 20px; ' ], '' )
		) . '&#160;' . round( $prog * 100, 4 ) . '%';
	}

	private function getRefreshJob() {

		if ( !$this->isEnabledFeature( SMW_ADM_REFRESH ) ) {
			return null;
		}

		if ( $this->refreshjob !== null ) {
			return $this->refreshjob;
		}

		$jobQueue = ApplicationFactory::getInstance()->getJobQueue();

		if ( !$jobQueue->hasPendingJob( 'SMW\RefreshJob' ) ) {
			return null;
		}

		// Pop and acknowledge the job to fetch progress details
		// from itself
		$refreshJob = $jobQueue->pop( 'SMW\RefreshJob' );

		if ( $refreshJob instanceof Job ) {
			$refreshJob->run();
			$jobQueue->ack( $refreshJob );
			$this->refreshjob = $refreshJob;
		}

		return $this->refreshjob;
	}

}