- /**
- * Get an associative array of information about a given path, from its .svn
- * subdirectory. Returns false on error, such as if the directory was not
- * checked out with subversion.
- *
- * Returned keys are:
- * Required:
- * checkout-rev The revision which was checked out
- * Optional:
- * directory-rev The revision when the directory was last modified
- * url The subversion URL of the directory
- * repo-url The base URL of the repository
- * viewvc-url A ViewVC URL pointing to the checked-out revision
- * @param string $dir
- * @return array|bool
- */
- public static function getSvnInfo( $dir ) {
- // http://svnbook.red-bean.com/nightly/en/svn.developer.insidewc.html
- $entries = $dir . '/.svn/entries';
-
- if ( !file_exists( $entries ) ) {
- return false;
- }
-
- $lines = file( $entries );
- if ( !count( $lines ) ) {
- return false;
- }
-
- // check if file is xml (subversion release <= 1.3) or not (subversion release = 1.4)
- if ( preg_match( '/^<\?xml/', $lines[0] ) ) {
- // subversion is release <= 1.3
- if ( !function_exists( 'simplexml_load_file' ) ) {
- // We could fall back to expat... YUCK
- return false;
- }
-
- // SimpleXml whines about the xmlns...
- MediaWiki\suppressWarnings();
- $xml = simplexml_load_file( $entries );
- MediaWiki\restoreWarnings();
-
- if ( $xml ) {
- foreach ( $xml->entry as $entry ) {
- if ( $xml->entry[0]['name'] == '' ) {
- // The directory entry should always have a revision marker.
- if ( $entry['revision'] ) {
- return array( 'checkout-rev' => intval( $entry['revision'] ) );
- }
- }
- }
- }
-
- return false;
- }
-
- // Subversion is release 1.4 or above.
- if ( count( $lines ) < 11 ) {
- return false;
- }
-
- $info = array(
- 'checkout-rev' => intval( trim( $lines[3] ) ),
- 'url' => trim( $lines[4] ),
- 'repo-url' => trim( $lines[5] ),
- 'directory-rev' => intval( trim( $lines[10] ) )
- );
-
- if ( isset( self::$viewvcUrls[$info['repo-url']] ) ) {
- $viewvc = str_replace(
- $info['repo-url'],
- self::$viewvcUrls[$info['repo-url']],
- $info['url']
- );
-
- $viewvc .= '/?pathrev=';
- $viewvc .= urlencode( $info['checkout-rev'] );
- $info['viewvc-url'] = $viewvc;
- }
-
- return $info;
- }
-
- /**
- * Retrieve the revision number of a Subversion working directory.
- *
- * @param string $dir Directory of the svn checkout
- *
- * @return int Revision number
- */
- public static function getSvnRevision( $dir ) {
- $info = self::getSvnInfo( $dir );
-
- if ( $info === false ) {
- return false;
- } elseif ( isset( $info['checkout-rev'] ) ) {
- return $info['checkout-rev'];
- } else {
- return false;
- }
- }
-