summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Deferred/ChangeTitleUpdate.php
blob: 73603655c076efdd5e96fa396d8caeb9bc3be2d0 (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
<?php

namespace SMW\MediaWiki\Deferred;

use DeferrableUpdate;
use DeferredUpdates;
use Title;
use SMW\ApplicationFactory;
use SMW\MediaWiki\Jobs\UpdateJob;
use SMW\Site;
use SMW\Enum;

/**
 * Run a deferred update job for a changed title instance to re-parse the content
 * of those associated titles and make sure that its content (incl. any
 * self-reference) is correctly represented.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class ChangeTitleUpdate implements DeferrableUpdate {

	/**
	 * @var Title|null
	 */
	private $oldTitle;

	/**
	 * @var Title|null
	 */
	private $newTitle;

	/**
	 * @since 3.0
	 *
	 * @param Title|null $oldTitle
	 * @param Title|null $newTitle
	 */
	public function __construct( Title $oldTitle = null, Title $newTitle = null ) {
		$this->oldTitle = $oldTitle;
		$this->newTitle = $newTitle;
	}

	/**
	 * @since 3.0
	 *
	 * @param Title|null $oldTitle
	 * @param Title|null $newTitle
	 */
	public static function addUpdate( Title $oldTitle = null, Title $newTitle = null ) {

		// Avoid deferring the update on CLI (and the DeferredUpdates::tryOpportunisticExecute)
		// since we use a Job instance to carry out the change
		if ( Site::isCommandLineMode() ) {
			$changeTitleUpdate = new self( $oldTitle, $newTitle );
			$changeTitleUpdate->doUpdate();
		} else {
			DeferredUpdates::addUpdate( new self( $oldTitle, $newTitle ) );
		}
	}

	/**
	 * @see DeferrableUpdate::doUpdate
	 *
	 * @since 3.0
	 */
	public function doUpdate() {

		$applicationFactory = ApplicationFactory::getInstance();
		$jobFactory = $applicationFactory->newJobFactory();

		$parameters = [
			UpdateJob::FORCED_UPDATE => true,

			// Run purge job after the change has happened since no post-edit event
			// will be triggered on a changed/redirect title
			Enum::PURGE_ASSOC_PARSERCACHE => true,

			'origin' => 'ChangeTitleUpdate'
		];

		if ( $this->oldTitle !== null ) {
			$jobFactory->newUpdateJob( $this->oldTitle, $parameters )->run();
		}

		if ( $this->newTitle !== null ) {
			$jobFactory->newUpdateJob( $this->newTitle, $parameters )->run();
		}
	}

}