Completed
Push — master ( a65bfc...74ce99 )
by Maximilian
04:53
created

Peachy::newWiki()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72
Metric Value
dl 0
loc 38
rs 5.3846
ccs 0
cts 21
cp 0
cc 8
eloc 20
nc 10
nop 5
crap 72
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 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
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
	 * Loads a specific plugin into memory
114
	 *
115
	 * @static
116
	 * @access public
117
	 * @param string|array $plugins Name of plugin(s) to load from Plugins directory, minus .php ending
118
	 * @return void
119
	 * @deprecated since 18 June 2013
120
	 */
121
	public static function loadPlugin( $plugins ) {
0 ignored issues
show
Unused Code introduced by
The parameter $plugins is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
		self::deprecatedWarn( null, null, "Warning: Peachy::loadPlugin() is deprecated. Thanks to the wonders of PHP 5, the call can just be removed." );
123
	}
124
125
	/**
126
	 * Loads all available plugins
127
	 *
128
	 * @static
129
	 * @access public
130
	 * @return void
131
	 * @deprecated since 18 June 2013
132
	 */
133
	public static function loadAllPlugins() {
134
		self::deprecatedWarn( null, null, "Warning: Peachy::loadAllPlugins() is deprecated. Thanks to the wonders of PHP 5, the call can just be removed." );
135
136
	}
137
138
	/**
139
	 * Checks for config files, parses them.
140
	 *
141
	 * @access private
142
	 * @static
143
	 * @param string $config_name Name of config file
144
	 * @throws BadEntryError
145
	 * @return array Config params
146
	 */
147
	private static function parse_config( $config_name ) {
148
		global $pgIP;
149
		if( !is_file( $config_name ) ) {
150
			if( !is_file( $pgIP . 'Configs/' . $config_name . '.cfg' ) ) {
151
				throw new BadEntryError( "BadConfig", "A non-existent configuration file was specified." );
152
			} else {
153
				$config_name = $pgIP . 'Configs/' . $config_name . '.cfg';
154
			}
155
		}
156
157
		$config_params = parse_ini_file( $config_name );
158
159
		if( isset( $config_params['useconfig'] ) ) {
160
			$config_params = $config_params + self::parse_config( $config_params['useconfig'] );
161
		}
162
163
		return $config_params;
164
	}
165
166
	/**
167
	 * @param null|string $method
168
	 * @param null|string $newfunction
169
	 * @param string $message
170
	 */
171
	public static function deprecatedWarn( $method, $newfunction, $message = null ) {
172
		if( is_null( $message ) ) {
173
			$message = "Warning: $method is deprecated. Please use $newfunction instead.";
174
		}
175
176
		$message = "[$message|YELLOW_BAR]\n\n";
177
178
		pecho( $message, PECHO_WARN, 'cecho' );
179
	}
180
181
	public static function getSvnInfo() {
182
		global $pgIP;
183
184
		// http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
185
		$entries = $pgIP . '/.svn/entries';
186
187
		if( !file_exists( $entries ) ) {
188
			return false;
189
		}
190
191
		$lines = file( $entries );
192
		if( !count( $lines ) ) {
193
			return false;
194
		}
195
196
		// check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
197
		if( preg_match( '/^<\?xml/', $lines[0] ) ) {
198
			return false;
199
		}
200
201
		// Subversion is release 1.4 or above.
202
		if( count( $lines ) < 11 ) {
203
			return false;
204
		}
205
206
		$info = array(
207
			'checkout-rev'  => intval( trim( $lines[3] ) ),
208
			'url'           => trim( $lines[4] ),
209
			'repo-url'      => trim( $lines[5] ),
210
			'directory-rev' => intval( trim( $lines[10] ) )
211
		);
212
213
		return $info;
214
	}
215
}
216