Completed
Push — master ( d856bd...46e2a4 )
by Maximilian
11:35 queued 07:50
created

Peachy::loadPlugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
			);
38
39
		}
40
41
		if( is_null( $config_params['baseurl'] ) || !isset( $config_params['baseurl'] ) ) {
42
			throw new LoginError( array( "MissingParam", "The baseurl parameter was not set." ) );
43
		}
44
45
		if( !isset( $config_params['username'] ) ) {
46
			$config_params['nologin'] = true;
47
		}
48
		if( !isset( $config_params['password'] ) && isset( $config_params['method'] ) && $config_params['method'] == "legacy" ) {
49
			$config_params['nologin'] = true;
50
		}
51
52
		list( $version, $extensions ) = self::wikiChecks( $config_params['baseurl'] );
53
54
		Hooks::runHook( 'StartLogin', array( &$config_params, &$extensions ) );
55
56
		$w = new $classname( $config_params, $extensions, false, null );
57
		$w->mwversion = $version;
58
59
		return $w;
60
	}
61
62
	/**
63
	 * Performs various checks and settings
64
	 * Checks if MW version is at least {@link MINMW}
65
	 *
66
	 * @static
67
	 * @access    public
68
	 * @param    string $base_url URL to api.php
69
	 * @throws    DependencyError|string
70
	 * @return    array                                    Installed extensions
71
	 */
72
	public static function wikiChecks( $base_url ) {
73
		$http = HTTP::getDefaultInstance();
74
75
		$siteInfo = unserialize(
76
			$http->get(
77
				$base_url,
78
				array(
79
					'action' => 'query',
80
					'meta'   => 'siteinfo',
81
					'format' => 'php',
82
					'siprop' => 'extensions|general',
83
				)
84
			)
85
		);
86
87
		if (isset($siteInfo['error']) && $siteInfo['error']['code'] == 'readapidenied') {
88
			global $pgHooks;
89
			$pgHooks['PostLogin'][] = array( 'Peachy::wikiChecks', $base_url );
90
			return array( MINMW, array() );
91
		}
92
93
		$version = preg_replace('/[^0-9\.]/', '', $siteInfo['query']['general']['generator']);
94
95
		if( version_compare( $version, MINMW ) < 0 ) {
96
			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...
97
		}
98
99
		$extensions = array();
100
101
		foreach ($siteInfo['query']['extensions'] as $ext) {
102
			if( isset( $ext['version'] ) ) {
103
				$extensions[$ext['name']] = $ext['version'];
104
			} else {
105
				$extensions[$ext['name']] = '';
106
			}
107
		}
108
109
		return array( $version, $extensions );
110
	}
111
112
	/**
113
	 * Checks for config files, parses them.
114
	 *
115
	 * @access    private
116
	 * @static
117
	 * @param    string $config_name Name of config file
118
	 * @throws    BadEntryError
119
	 * @return    array                            Config params
120
	 */
121
	private static function parse_config( $config_name ) {
122
		global $pgIP;
123
		if( !is_file( $config_name ) ) {
124
			if( !is_file( $pgIP . 'Configs/' . $config_name . '.cfg' ) ) {
125
				throw new BadEntryError( "BadConfig", "A non-existent configuration file was specified." );
126
			} else {
127
				$config_name = $pgIP . 'Configs/' . $config_name . '.cfg';
128
			}
129
		}
130
131
		$config_params = parse_ini_file( $config_name );
132
133
		if( isset( $config_params['useconfig'] ) ) {
134
			$config_params = $config_params + self::parse_config( $config_params['useconfig'] );
135
		}
136
137
		return $config_params;
138
	}
139
140
	/**
141
	 * Function that displays an error message when an End-User attempts to use a function no longer included in Peachy
142
	 *
143
	 * @param null|string $method
144
	 * @param null|string $newFunction
145
	 * @param string $message
146
	 */
147
	public static function deprecatedWarn($method, $newFunction, $message = null)
148
	{
149
		if( is_null( $message ) ) {
150
			$message = "Warning: $method is deprecated. Please use $newFunction instead.";
151
		}
152
153
		$message = "[$message|YELLOW_BAR]\n\n";
154
155
		pecho( $message, PECHO_WARN, 'cecho' );
156
	}
157
158
	/**
159
	 * Checks for and returns an SVN repository version.
160
	 *
161
	 * @return array|bool
162
	 */
163
	public static function getSvnInfo() {
164
		global $pgIP;
165
166
		// http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
167
		$entries = $pgIP . '/.svn/entries';
168
169
		if( !file_exists( $entries ) ) {
170
			return false;
171
		}
172
173
		$lines = file( $entries );
174
		if( !count( $lines ) ) {
175
			return false;
176
		}
177
178
		// check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
179
		if( preg_match( '/^<\?xml/', $lines[0] ) ) {
180
			return false;
181
		}
182
183
		// Subversion is release 1.4 or above.
184
		if( count( $lines ) < 11 ) {
185
			return false;
186
		}
187
188
		$info = array(
189
			'checkout-rev'  => intval( trim( $lines[3] ) ),
190
			'url'           => trim( $lines[4] ),
191
			'repo-url'      => trim( $lines[5] ),
192
			'directory-rev' => intval( trim( $lines[10] ) )
193
		);
194
195
		return $info;
196
	}
197
}
198