Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
fallbacks-graph.php
Go to the documentation of this file.
1<?php
11use MediaWiki\MediaWikiServices;
12
13// Standard boilerplate to define $IP
14if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
15 $IP = getenv( 'MW_INSTALL_PATH' );
16} else {
17 $dir = __DIR__;
18 $IP = "$dir/../../..";
19}
20require_once "$IP/maintenance/Maintenance.php";
21
23class FallbacksCompare extends Maintenance {
24 public function __construct() {
25 parent::__construct();
26 $this->addDescription( 'Creates graphml xml file of language fallbacks.' );
27 }
28
29 public function execute() {
30 $template = <<<XML
31<?xml version="1.0" encoding="UTF-8"?>
32<graphml
33 xmlns="http://graphml.graphdrawing.org/xmlns"
34 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
35 xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
36 http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"
37 xmlns:y="http://www.yworks.com/xml/graphml">
38
39 <key id="code" for="node" yfiles.type="nodegraphics"/>
40 <graph id="G" edgedefault="directed">
41$1
42 </graph>
43</graphml>
44
45XML;
46
47 $langs = MediaWikiServices::getInstance()
48 ->getLanguageNameUtils()
49 ->getLanguageNames();
50 $nodes = $edges = [];
51 foreach ( $langs as $code => $name ) {
52 $fallbacks = Language::getFallbacksFor( $code, Language::STRICT_FALLBACKS );
53 if ( $fallbacks === [] ) {
54 continue;
55 }
56
57 $nodes[$code] = $this->createNode( $code );
58
59 $prev = $code;
60 foreach ( $fallbacks as $fb ) {
61 $nodes[$fb] = $this->createNode( $fb );
62 $edges[$fb . $prev] = Xml::element( 'edge', [ 'source' => $prev, 'target' => $fb ] );
63 $prev = $fb;
64 }
65 }
66
67 $output = array_merge( $nodes, $edges );
68 $output = "\t\t" . implode( "\n\t\t", $output );
69 echo str_replace( '$1', $output, $template );
70 }
71
72 protected function createNode( $code ) {
73 return Xml::openElement( 'node', [ 'id' => $code ] )
74 . Xml::openElement( 'data', [ 'key' => 'code' ] )
75 . Xml::openElement( 'y:Shapenode' )
76 . Xml::element(
77 'y:Geometry',
78 [ 'height' => 40, 'width' => max( 40, 20 * strlen( $code ) ) ],
79 ''
80 )
81 . Xml::element( 'y:NodeLabel', [ 'fontSize' => '24' ], $code )
82 . Xml::element( 'y:BorderStyle', [ 'hasColor' => 'false' ], '' )
83 . Xml::element( 'y:Fill', [ 'hasColor' => 'false' ], '' )
84 . Xml::closeElement( 'y:Shapenode' )
85 . Xml::closeElement( 'data' )
86 . Xml::closeElement( 'node' );
87 }
88}
89
90$maintClass = FallbacksCompare::class;
91require_once RUN_MAINTENANCE_IF_MAIN;
Creates graphml xml file of language fallbacks.