MovePageNotice::getPageMoveMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Wikibase\Client\Hooks;
4
5
use Html;
6
use MediaWiki\Hook\SpecialMovepageAfterMoveHook;
7
use MovePageForm;
8
use Title;
9
use Wikibase\Client\RepoLinker;
10
use Wikibase\Client\WikibaseClient;
11
use Wikibase\Lib\Store\SiteLinkLookup;
12
13
/**
14
 * Gets a notice about the Wikibase Item belonging to the current page
15
 * after a move (in case there's one).
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Marius Hoch < [email protected] >
19
 */
20
class MovePageNotice implements SpecialMovepageAfterMoveHook {
21
22
	/**
23
	 * @var SiteLinkLookup
24
	 */
25
	private $siteLinkLookup;
26
27
	/**
28
	 * @var string
29
	 */
30
	private $siteId;
31
32
	/**
33
	 * @var RepoLinker
34
	 */
35
	private $repoLinker;
36
37
	/**
38
	 * @param SiteLinkLookup $siteLinkLookup
39
	 * @param string $siteId Global id of the client wiki
40
	 * @param RepoLinker $repoLinker
41
	 */
42
	public function __construct( SiteLinkLookup $siteLinkLookup, $siteId, RepoLinker $repoLinker ) {
43
		$this->siteLinkLookup = $siteLinkLookup;
44
		$this->siteId = $siteId;
45
		$this->repoLinker = $repoLinker;
46
	}
47
48
	public static function factory() {
49
		$wikibaseClient = WikibaseClient::getDefaultInstance();
50
		$siteLinkLookup = $wikibaseClient->getStore()->getSiteLinkLookup();
51
		$repoLinker = $wikibaseClient->newRepoLinker();
52
53
		return new self(
54
			$siteLinkLookup,
55
			$wikibaseClient->getSettings()->getSetting( 'siteGlobalID' ),
56
			$repoLinker
57
		);
58
	}
59
60
	/**
61
	 * Hook for injecting a message on [[Special:MovePage]]
62
	 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialMovepageAfterMove
63
	 *
64
	 * @param MovePageForm $movePage
65
	 * @param Title $oldTitle
66
	 * @param Title $newTitle
67
	 */
68
	public function onSpecialMovepageAfterMove( $movePage, $oldTitle, $newTitle ) {
69
		$out = $movePage->getOutput();
70
		$out->addModules( 'wikibase.client.miscStyles' );
71
		$out->addHTML( $this->getPageMoveNoticeHtml( $oldTitle, $newTitle ) );
72
	}
73
74
	/**
75
	 * Create a repo link directly to the item.
76
	 * We can't use Special:ItemByTitle here as the item might have already been updated.
77
	 *
78
	 * @param Title $title
79
	 *
80
	 * @return string|null
81
	 */
82
	private function getItemUrl( Title $title ) {
83
		$entityId = $this->siteLinkLookup->getItemIdForLink(
84
			$this->siteId,
85
			$title->getPrefixedText()
86
		);
87
88
		if ( !$entityId ) {
89
			return null;
90
		}
91
92
		return $this->repoLinker->getEntityUrl( $entityId );
93
	}
94
95
	/**
96
	 * @param Title $oldTitle Title of the page before the move
97
	 * @param Title $newTitle Title of the page after the move
98
	 *
99
	 * @return string|null
100
	 */
101
	private function getPageMoveNoticeHtml( Title $oldTitle, Title $newTitle ) {
102
		$itemLink = $this->getItemUrl( $oldTitle );
103
104
		if ( !$itemLink ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $itemLink of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
105
			return null;
106
		}
107
108
		$msg = $this->getPageMoveMessage( $newTitle );
109
110
		$html = Html::rawElement(
111
			'div',
112
			[
113
				'id' => 'wbc-after-page-move',
114
				'class' => 'plainlinks'
115
			],
116
			wfMessage( $msg, $itemLink )->parse()
117
		);
118
119
		return $html;
120
	}
121
122
	private function getPageMoveMessage( Title $newTitle ) {
123
		// @phan-suppress-next-line PhanUndeclaredProperty Dynamic property
124
		if ( isset( $newTitle->wikibasePushedMoveToRepo ) ) {
125
			// We're going to update the item using the repo job queue \o/
126
			return 'wikibase-after-page-move-queued';
127
		}
128
129
		// The user has to update the item per hand for some reason
130
		return 'wikibase-after-page-move';
131
	}
132
133
}
134