RPED::insert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 8
cp 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of Peachy MediaWiki Bot API
5
 *
6
 * Peachy is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
class RPED {
21
22
	/**
23
	 * Wiki class
24
	 *
25
	 * @var Wiki
26
	 * @access private
27
	 */
28
	private $wiki;
29
30
	/**
31
	 * maxURLLength
32
	 * Default maximum length of the URL to be posted
33
	 *
34
	 * @var int
35
	 * @access private
36
	 */
37
	private $defaultMaxURLLength;
38
39
	/**
40
	 * Construction method for the RPED class
41
	 *
42
	 * @access public
43
	 * @param Wiki &$wikiClass The Wiki class object
44
	 */
45
	function __construct( Wiki &$wikiClass ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __construct.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
46
		$this->wiki = $wikiClass;
47
		$defaultMaxURLLength = 2000;
0 ignored issues
show
Unused Code introduced by
$defaultMaxURLLength 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...
48
		return;
0 ignored issues
show
Coding Style introduced by
Empty return statement not required here
Loading history...
49
	}
50
51
	/**
52
	 * Insert a page title into the rped_page table
53
	 *
54
	 * @static
55
	 * @access public
56
	 * @param string $page
57
	 * @return void
58
	 */
59
	public function insert( $page ) {
60
		$this->wiki->apiQuery(
61
			array(
62
				'action' => 'rped',
63
				'insert' => $page,
64
			), true
65
		);
66
	}
67
68
	/**
69
	 * Delete a page title from the rped_page table
70
	 *
71
	 * @static
72
	 * @access public
73
	 * @param string $page
74
	 * @return void
75
	 */
76
	public function delete( $page ) {
77
		$this->wiki->apiQuery(
78
			array(
79
				'action' => 'rped',
80
				'delete' => $page,
81
			), true
82
		);
83
	}
84
85
	/**
86
	 * Insert/delete an array of page titles into/from the rped_page table
87
	 *
88
	 * @static
89
	 * @access public
90
	 * @param string $command Either 'insert' or 'delete'
91
	 * @param array $pageArray The array of page title to insert
92
	 * @param int $maxURLLength The maximum length of the url to be POSTed
93
	 * @return void
94
	 */
95
	public function insertOrDeleteArray( $command, $pageArray, $maxURLLength = 0 ) {
96
		if( $command != 'insert' && $command != 'delete' ) {
97
			die( 'Something tried to call insertOrDeleteArray without'
0 ignored issues
show
Coding Style Compatibility introduced by
The method insertOrDeleteArray() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
98
				 . 'specifying an insert or delete command.' );
99
		}
100
		if( $maxURLLength == 0 ) {
101
			$maxURLLength = $this->defaultMaxURLLength;
102
		}
103
		$line = '';
104
		foreach( $pageArray as $page ){
105
			if( $line != '' ) {
106
				$line .= '|';
107
			}
108
			if( strlen( $line ) + strlen( $page ) > $maxURLLength ) {
109
				if( $command == 'delete' ) {
110
					$this->delete( $line );
111
				} else {
112
					$this->insert( $line );
113
				}
114
				$line = '';
115
			}
116
			$line .= $page;
117
		}
118
		if( $command == 'delete' ) {
119
			$this->delete( $line );
120
		} else {
121
			$this->insert( $line );
122
		}
123
	}
124
125
	/**
126
	 * Insert an array of page titles into/from the rped_page table
127
	 *
128
	 * @static
129
	 * @access public
130
	 * @param array $pageArray The array of page title to insert
131
	 * @param int $maxURLLength The maximum length of the url to be POSTed
132
	 * @return void
133
	 */
134
	public function insertArray( $pageArray, $maxURLLength = 0 ) {
135
		$this->insertOrDeleteArray( 'insert', $pageArray, $maxURLLength );
136
	}
137
138
	/**
139
	 * Delete an array of page titles from the rped_page table
140
	 *
141
	 * @static
142
	 * @access public
143
	 * @param array $pageArray The array of page title to insert
144
	 * @param int $maxURLLength The maximum length of the url to be POSTed
145
	 * @return void
146
	 */
147
	public function deleteArray( $pageArray, $maxURLLength = 0 ) {
148
		$this->insertOrDeleteArray( 'delete', $pageArray, $maxURLLength );
149
	}
150
}
151