Instead of checking if the resulting $matches array is complete, we can safely assume it is, as long as the preg_match() call returned a non-false value. Note that some of these used empty() before and are actually bogus because of this! empty() considers the string "0" to be empty. In case of a ==0== headline that's an actual bug. I'm also removing the `= []` initialization before the preg_match. I understand why it was added: to make it a little more obvious that the variable is guaranteed to be initialized. But: * This is guaranteed by the preg_match anyway. * Neither initializing it with null or an empty array makes much sense because the code below assumes so much more, e.g. that specific elements exist, and are arrays. Again, these guarantees are all given by the preg_match. I find the additional initialization more distracting than helpful. Change-Id: I22b192b59038d9fa51a7e6f04d8d76634ae3de73
27 lines
663 B
PHP
27 lines
663 B
PHP
<?php
|
|
|
|
/**
|
|
* Provides access to MediaWiki's version without requiring MediaWiki (or anything else)
|
|
* being loaded first.
|
|
*
|
|
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
|
|
*/
|
|
class MediaWikiVersionFetcher {
|
|
|
|
/**
|
|
* Get the MediaWiki version, extracted from the PHP source file where it is defined.
|
|
*
|
|
* @return string
|
|
* @throws RuntimeException
|
|
*/
|
|
public function fetchVersion() {
|
|
$code = file_get_contents( __DIR__ . '/Defines.php' );
|
|
|
|
if ( !preg_match( "/define\( 'MW_VERSION', '([^']+)'/", $code, $matches ) ) {
|
|
throw new RuntimeException( 'Could not extract the MediaWiki version from Defines.php' );
|
|
}
|
|
|
|
return $matches[1];
|
|
}
|
|
|
|
}
|