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

IdHandlerSection::getIdInfoAsJson()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 2
nop 2
dl 0
loc 21
ccs 1
cts 1
cp 1
crap 3
rs 9.3142
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 SMW\MediaWiki\ManualEntryLogger;
8
use SMW\MediaWiki\Database;
9
use SMW\Message;
10
use SMW\Store;
11
use Html;
12
use WebRequest;
13
use User;
14
15
/**
16
 * @license GNU GPL v2+
17
 * @since   2.5
18
 *
19
 * @author mwjames
20
 */
21
class IdHandlerSection {
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 $enabledIdDisposal = false;
42
43
	/**
44
	 * @since 2.5
45 3
	 *
46 3
	 * @param Database $connection
47 3
	 * @param HtmlFormRenderer $htmlFormRenderer
48 3
	 * @param OutputFormatter $outputFormatter
49 3
	 */
50
	public function __construct( Database $connection, HtmlFormRenderer $htmlFormRenderer, OutputFormatter $outputFormatter ) {
51
		$this->connection = $connection;
52
		$this->htmlFormRenderer = $htmlFormRenderer;
53
		$this->outputFormatter = $outputFormatter;
54
	}
55
56
	/**
57 2
	 * @since 2.5
58
	 *
59 2
	 * @param boolean $enabledIdDisposal
60 2
	 */
61
	public function enabledIdDisposal( $enabledIdDisposal ) {
62 2
		$this->enabledIdDisposal = (bool)$enabledIdDisposal;
63
	}
64 2
65 1
	/**
66
	 * @since 2.5
67
	 *
68 2
	 * @param WebRequest $webRequest
69 2
	 * @param User|null $user
70
	 */
71
	public function outputActionForm( WebRequest $webRequest, User $user = null ) {
72
73
		$this->outputFormatter->setPageTitle( $this->getMessage( 'smw-smwadmin-idlookup-title' ) );
74
		$this->outputFormatter->addParentLink();
75 1
76
		$id = (int)$webRequest->getText( 'id' );
77 1
78 1
		if ( $this->enabledIdDisposal && $id > 0 && $webRequest->getText( 'dispose' ) === 'yes' ) {
79
			$this->doDispose( $id, $user );
80
		}
81 1
82
		$this->outputFormatter->addHtml( $this->getForm( $webRequest, $id ) );
83 1
	}
84 1
85 1
	/**
86 1
	 * @param integer $id
87
	 * @param User|null $use
0 ignored issues
show
Bug introduced by
There is no parameter named $use. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
88 2
	 */
89
	private function doDispose( $id, $user = null ) {
90 2
91
		$entityIdDisposerJob = ApplicationFactory::getInstance()->newJobFactory()->newEntityIdDisposerJob(
92 2
			\Title::newFromText( __METHOD__ )
93 1
		);
94
95
		$entityIdDisposerJob->executeWith( $id );
96 2
97 2
		$manualEntryLogger = ApplicationFactory::getInstance()->create( 'ManualEntryLogger' );
98 2
		$manualEntryLogger->registerLoggableEventType( 'admin' );
99 2
		$manualEntryLogger->log( 'admin', $user, 'Special:SMWAdmin', 'Forced removal of ID '. $id );
100 2
	}
101 2
102 2
	private function getForm( $webRequest, $id ) {
103 2
104 2
		$message = $this->getIdInfoAsJson( $webRequest, $id );
105
106
		if ( $id < 1 ) {
107 2
			$id = null;
108 2
		}
109 2
110 2
		$html = $this->htmlFormRenderer
111
			->setName( 'idlookup' )
112 2
			->setMethod( 'get' )
113
			->addHiddenField( 'action', 'idlookup' )
114 2
			->addHiddenField( 'id', $id )
115
			->addParagraph( $this->getMessage( 'smw-sp-admin-idlookup-docu' ) )
116
			->addInputField(
117
				$this->getMessage( 'smw-sp-admin-objectid' ),
118
				'id',
119 2
				$id
120 2
			)
121 2
			->addNonBreakingSpace()
122 2
			->addSubmitButton( $this->getMessage( 'allpagessubmit' ) )
123 2
			->addParagraph( $message )
124 2
			->getForm();
125 2
126 2
		$html .= Html::element( 'p', array(), '' );
127 2
128 2
		if ( $id > 0 && $webRequest->getText( 'dispose' ) == 'yes' ) {
129
			$message = $this->getMessage( array ('smw-sp-admin-iddispose-done', $id ) );
0 ignored issues
show
Unused Code introduced by
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130 2
			$id = null;
131 2
		}
132 2
133 2
		if ( !$this->enabledIdDisposal ) {
134
			return $html;
135 2
		}
136 2
137 2
		$html .= $this->htmlFormRenderer
138 2
			->setName( 'iddispose' )
139 2
			->setMethod( 'get' )
140 2
			->addHiddenField( 'action', 'idlookup' )
141
			->addHiddenField( 'id', $id )
142 2
			->addHeader( 'h3', $this->getMessage( 'smw-sp-admin-iddispose-title' ) )
143
			->addParagraph( $this->getMessage( 'smw-sp-admin-iddispose-docu', Message::PARSE ) )
144 2
			->addInputField(
145
				$this->getMessage( 'smw-sp-admin-objectid' ),
146
				'id',
147 2
				$id,
148
				null,
149 2
				20,
150 1
				'',
151
				true
152
			)
153 1
			->addNonBreakingSpace()
154 1
			->addSubmitButton( $this->getMessage( 'allpagessubmit' ) )
155
			->addCheckbox(
156 1
				$this->getMessage( 'smw_smwadmin_datarefreshstopconfirm', Message::ESCAPED ),
157
				'dispose',
158
				'yes'
159
			)
160
			->getForm();
161
162 1
		return $html . Html::element( 'p', array(), '' );
163 1
	}
164
165
	private function getIdInfoAsJson( $webRequest, $id ) {
166 1
167
		if ( $id < 1 || $webRequest->getText( 'action' ) !== 'idlookup' ) {
168
			return '';
169
		}
170
171
		$row = $this->connection->selectRow(
172
				\SMWSql3SmwIds::TABLE_NAME,
173
				array(
174
					'smw_title',
175
					'smw_namespace',
176
					'smw_iw',
177
					'smw_subobject',
178
					'smw_sortkey'
179
				),
180
				'smw_id=' . $id,
181
				__METHOD__
182
		);
183
184
		return '<pre>' . $this->outputFormatter->encodeAsJson( array( $id, $row ) ) . '</pre>';
185
	}
186
187
	private function getMessage( $key, $type = Message::TEXT ) {
188
		return Message::get( $key, $type, Message::USER_LANGUAGE );
189
	}
190
191
}
192