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