summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Jobs/RefreshJob.php
blob: eda691703bc0934852ddff28fb7d57cadb4f4126 (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
<?php

namespace SMW\MediaWiki\Jobs;

use SMW\MediaWiki\Job;
use SMW\ApplicationFactory;

/**
 * RefreshJob iterates over all page ids of the wiki, to perform an update
 * action for all of them in sequence. This corresponds to the in-wiki version
 * of the SMW_refreshData.php script for updating the whole wiki.
 *
 * @note This class ignores $smwgEnableUpdateJobs and always creates updates.
 * In fact, it might be needed specifically on wikis that do not use update
 * jobs in normal operation.
 *
 * @ingroup SMW
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class RefreshJob extends Job {

	/**
	 * Constructor. The parameters optionally specified in the second
	 * argument of this constructor use the following array keys:
	 *
	 * - 'spos' : (start index, default 1),
	 * - 'prog' : (progress indicator, default 0),
	 * - 'rc' : (number of runs to be done, default 1)
	 *
	 * If more than one run is done, then the first run will restrict to properties
	 * and types. The progress indication refers to the current run, not to the
	 * overall job.
	 *
	 * @param Title $title
	 * @param array $params
	 */
	public function __construct( $title, $params = [ 'spos' => 1, 'prog' => 0, 'rc' => 1 ] ) {
		parent::__construct( 'smw.refresh', $title, $params );
	}

	/**
	 * @see Job::run
	 *
	 * @return boolean
	 */
	public function run() {

		if ( $this->hasParameter( 'spos' ) ) {
			$this->refreshData( $this->getParameter( 'spos' ) );
		}

		return true;
	}

	/**
	 * Report the estimated progress status of this job as a number between
	 * 0 and 1 (0% to 100%). The progress refers to the state before
	 * processing this job.
	 *
	 * @return double
	 */
	public function getProgress() {

		$prog = $this->hasParameter( 'prog' ) ? $this->getParameter( 'prog' ) : 0;
		$run  = $this->hasParameter( 'run' ) ? $this->getParameter( 'run' ): 1;
		$rc   = $this->hasParameter( 'rc' ) ? $this->getParameter( 'rc' ) : 1;

		return round( ( $run - 1 + $prog ) / $rc, 1 );
	}

	/**
	 * @param $spos start index
	 */
	protected function refreshData( $spos ) {

		$run  = $this->hasParameter( 'run' ) ? $this->getParameter( 'run' ) : 1;

		$entityRebuildDispatcher = ApplicationFactory::getInstance()->getStore()->refreshData(
			$spos,
			20,
			$this->getNamespace( $run )
		);

		$entityRebuildDispatcher->rebuild( $spos );
		$prog = $entityRebuildDispatcher->getEstimatedProgress();

		if ( $spos > 0 ) {

			$this->createNextJob( [
				'spos' => $spos,
				'prog' => $prog,
				'rc'   => $this->getParameter( 'rc' ),
				'run'  => $run
			] );

		} elseif ( $this->hasParameter( 'rc' ) && $this->getParameter( 'rc' ) > $run ) { // do another run from the beginning

			$this->createNextJob( [
				'spos' => 1,
				'prog' => 0,
				'rc'   => $this->getParameter( 'rc' ),
				'run'  => $run + 1
			] );

		}

		return true;
	}

	protected function createNextJob( array $parameters ) {

		$job = new self(
			$this->getTitle(),
			$parameters
		);

		$job->isEnabledJobQueue( $this->isEnabledJobQueue );
		$job->insert();
	}

	protected function getNamespace( $run ) {

		if ( !$this->hasParameter( 'rc' ) ) {
			return false;
		}

		return ( ( $this->getParameter( 'rc' ) > 1 ) && ( $run == 1 ) ) ? [ SMW_NS_PROPERTY ] : false;
	}

}