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