Peachy::wikiChecks()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 5
nop 1
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
ccs 0
cts 25
cp 0
crap 42
1
<?php
2
3
/**
4
 * Base Peachy class, used to generate all other classes
5
 */
6
class Peachy {
7
8
	/**
9
	 * Initializes Peachy, logs in with a either the configuration file or a given username and password
10
	 *
11
	 * @static
12
	 * @access public
13
	 *
14
	 * @param string $config_name Name of the config file stored in the Configs directory, minus the .cfg extension. Default null
15
	 * @param string $pgUsername Username to log in if no config file specified. Default null
16
	 * @param string $password Password to log in with if no config file specified. Default null
17
	 * @param string $base_url URL to api.php if no config file specified. Defaults to English Wikipedia's API.
18
	 * @param string $classname
19
	 *
20
	 * @throws LoginError
21
	 * @return Wiki                Instance of the Wiki class, where most functions are stored
22
	 */
23
	public static function newWiki( $config_name = null, $pgUsername = null, $password = null, $base_url = 'http://en.wikipedia.org/w/api.php', $classname = 'Wiki' ) {
24
		pecho( "Loading Peachy (version " . PEACHYVERSION . ")...\n\n", PECHO_NORMAL );
25
		/*$updater = new AutoUpdate();
26
		$Uptodate = $updater->Checkforupdate();
27
		if( !$Uptodate ) $updater->updatePeachy();*/
28
29
		if( !is_null( $config_name ) ) {
30
			$config_params = self::parse_config( $config_name );
31
32
		} else {
33
			$config_params = array(
34
				'username' => $pgUsername,
35
				'password' => $password,
36
				'baseurl'  => $base_url,
37
				'method'   => 'legacy'
38
			);
39
40
		}
41
42
		if( is_null( $config_params['baseurl'] ) || !isset( $config_params['baseurl'] ) ) {
43
			throw new LoginError( array( "MissingParam", "The baseurl parameter was not set." ) );
44
		}
45
46
		if( !isset( $config_params['username'] ) ) {
47
			$config_params['nologin'] = true;
48
		}
49
		if( !isset( $config_params['password'] ) && isset( $config_params['method'] ) && $config_params['method'] == "legacy" ) {
50
			$config_params['nologin'] = true;
51
		}
52
53
		list( $version, $extensions ) = self::wikiChecks( $config_params['baseurl'] );
54
55
		Hooks::runHook( 'StartLogin', array( &$config_params, &$extensions ) );
56
57
		$w = new $classname( $config_params, $extensions, false, null );
58
		$w->mwversion = $version;
59
60
		return $w;
61
	}
62
63
	/**
64
	 * Performs various checks and settings
65
	 * Checks if MW version is at least {@link MINMW}
66
	 *
67
	 * @static
68
	 * @access    public
69
	 * @param    string $base_url URL to api.php
70
	 * @throws    DependencyError|string
71
	 * @return    array                                    Installed extensions
72
	 */
73
	public static function wikiChecks( $base_url ) {
74
		$http = HTTP::getDefaultInstance();
75
76
		$siteInfo = unserialize(
77
			$http->get(
78
				$base_url,
79
				array(
80
					'action' => 'query',
81
					'meta'   => 'siteinfo',
82
					'format' => 'php',
83
					'siprop' => 'extensions|general',
84
				)
85
			)
86
		);
87
88
		if (isset($siteInfo['error']) && $siteInfo['error']['code'] == 'readapidenied') {
89
			global $pgHooks;
90
			$pgHooks['PostLogin'][] = array( 'Peachy::wikiChecks', $base_url );
91
			return array( MINMW, array() );
92
		}
93
94
		$version = preg_replace('/[^0-9\.]/', '', $siteInfo['query']['general']['generator']);
95
96
		if( version_compare( $version, MINMW ) < 0 ) {
97
			throw new DependencyError( "MediaWiki " . MINMW, "http://mediawiki.org" );
0 ignored issues
show
Documentation introduced by
'http://mediawiki.org' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
		}
99
100
		$extensions = array();
101
102
		foreach ($siteInfo['query']['extensions'] as $ext) {
103
			if( isset( $ext['version'] ) ) {
104
				$extensions[$ext['name']] = $ext['version'];
105
			} else {
106
				$extensions[$ext['name']] = '';
107
			}
108
		}
109
110
		return array( $version, $extensions );
111
	}
112
113
	/**
114
	 * Checks for config files, parses them.
115
	 *
116
	 * @access    private
117
	 * @static
118
	 * @param    string $config_name Name of config file
119
	 * @throws    BadEntryError
120
	 * @return    array                            Config params
121
	 */
122
	private static function parse_config( $config_name ) {
123
		global $pgIP;
124
		if( !is_file( $config_name ) ) {
125
			if( !is_file( $pgIP . 'Configs/' . $config_name . '.cfg' ) ) {
126
				throw new BadEntryError( "BadConfig", "A non-existent configuration file was specified." );
127
			} else {
128
				$config_name = $pgIP . 'Configs/' . $config_name . '.cfg';
129
			}
130
		}
131
132
		$config_params = parse_ini_file( $config_name );
133
134
		if( isset( $config_params['useconfig'] ) ) {
135
			$config_params = $config_params + self::parse_config( $config_params['useconfig'] );
136
		}
137
138
		return $config_params;
139
	}
140
141
	/**
142
	 * Function that displays an error message when an End-User attempts to use a function no longer included in Peachy
143
	 *
144
	 * @param null|string $method
145
	 * @param null|string $newFunction
146
	 * @param string $message
147
	 */
148
	public static function deprecatedWarn($method, $newFunction, $message = null)
149
	{
150
		if( is_null( $message ) ) {
151
			$message = "Warning: $method is deprecated. Please use $newFunction instead.";
152
		}
153
154
		$message = "[$message|YELLOW_BAR]\n\n";
155
156
		pecho( $message, PECHO_WARN, 'cecho' );
157
	}
158
159
	/**
160
	 * Checks for and returns an SVN repository version.
161
	 *
162
	 * @return array|bool
163
	 */
164
	public static function getSvnInfo() {
165
		global $pgIP;
166
167
		// http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
168
		$entries = $pgIP . '/.svn/entries';
169
170
		if( !file_exists( $entries ) ) {
171
			return false;
172
		}
173
174
		$lines = file( $entries );
175
		if( !count( $lines ) ) {
176
			return false;
177
		}
178
179
		// check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
180
		if( preg_match( '/^<\?xml/', $lines[0] ) ) {
181
			return false;
182
		}
183
184
		// Subversion is release 1.4 or above.
185
		if( count( $lines ) < 11 ) {
186
			return false;
187
		}
188
189
		$info = array(
190
			'checkout-rev'  => intval( trim( $lines[3] ) ),
191
			'url'           => trim( $lines[4] ),
192
			'repo-url'      => trim( $lines[5] ),
193
			'directory-rev' => intval( trim( $lines[10] ) )
194
		);
195
196
		return $info;
197
	}
198
}
199