summaryrefslogtreecommitdiff
blob: 2af3bfcb2206303025776f10150ddafa81cf6534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
 * Script for creating graphml xml file of language fallbacks.
 *
 * @author Niklas Laxström
 *
 * @copyright Copyright © 2012-2013, Niklas Laxström
 * @license GPL-2.0+
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

/// Creates graphml xml file of language fallbacks.
class FallbacksCompare extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Creates graphml xml file of language fallbacks.';
	}

	public function execute() {
		$template = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<graphml
	xmlns="http://graphml.graphdrawing.org/xmlns"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
		http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"
	xmlns:y="http://www.yworks.com/xml/graphml">

	<key id="code" for="node" yfiles.type="nodegraphics"/>
	<graph id="G" edgedefault="directed">
$1
	</graph>
</graphml>

XML;

		$langs = Language::fetchLanguageNames( null, 'mw' );
		$nodes = $edges = array();
		foreach ( $langs as $code => $name ) {

			$fallbacks = Language::getFallbacksFor( $code );
			if ( $fallbacks === array( 'en' ) ) {
				continue;
			}

			$nodes[$code] = $this->createNode( $code );

			$prev = $code;
			foreach ( $fallbacks as $fb ) {
				$nodes[$fb] = $this->createNode( $fb );
				$edges[$fb . $prev] = Xml::element( 'edge', array( 'source' => $prev, 'target' => $fb ) );
				$prev = $fb;
			}
		}

		$output = array_merge( $nodes, $edges );
		$output = "\t\t" . implode( "\n\t\t", $output );
		echo str_replace( '$1', $output, $template );
	}

	protected function createNode( $code ) {
		return
			Xml::openElement( 'node', array( 'id' => $code ) )
			. Xml::openElement( 'data', array( 'key' => "code" ) )
			. Xml::openElement( 'y:Shpapenode' )
			. Xml::element( 'y:NodeLabel', array(), $code )
			. Xml::closeElement( 'y:Shpapenode' )
			. Xml::closeElement( 'data' )
			. Xml::closeElement( 'node' );
	}
}

$maintClass = 'FallbacksCompare';
require_once DO_MAINTENANCE;