Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 83
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigrateZ16K1StringsToZ61s
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 2
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2
3/**
4 * WikiLambda maintenance script to migrate Z16K1s from strings to Z61 references
5 *
6 * Updates all
7 *
8 * @file
9 * @ingroup Extensions
10 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
11 * @license MIT
12 */
13
14namespace MediaWiki\Extensions\WikiLambda\Maintenance;
15
16use Maintenance;
17use MediaWiki\Extension\WikiLambda\ZObjects\ZReference;
18use MediaWiki\Extension\WikiLambda\ZObjects\ZString;
19use MediaWiki\Extension\WikiLambda\ZObjectStore;
20use MediaWiki\Logger\LoggerFactory;
21use MediaWiki\MediaWikiServices;
22
23$IP = getenv( 'MW_INSTALL_PATH' );
24if ( $IP === false ) {
25    $IP = __DIR__ . '/../../..';
26}
27require_once "$IP/maintenance/Maintenance.php";
28
29class MigrateZ16K1StringsToZ61s extends Maintenance {
30
31    /**
32     * @var ZObjectStore
33     */
34    private $zObjectStore = null;
35
36    /**
37     * @inheritDoc
38     */
39    public function __construct() {
40        parent::__construct();
41        $this->requireExtension( 'WikiLambda' );
42        $this->addDescription( 'Migrates saved ZImplementations to reference a Z61 rather than hard-code a string' );
43
44        $this->addOption(
45            'implement',
46            'Whether to actually make the changes or (default) just dry-run',
47            false,
48            false
49        );
50    }
51
52    /**
53     * @inheritDoc
54     */
55    public function execute() {
56        // Construct the ZObjectStore, because ServiceWiring hasn't run
57        $services = MediaWikiServices::getInstance();
58        $this->zObjectStore = new ZObjectStore(
59            $services->getDBLoadBalancerFactory(),
60            $services->getTitleFactory(),
61            $services->getWikiPageFactory(),
62            $services->getRevisionStore(),
63            $services->getUserGroupManager(),
64            LoggerFactory::getInstance( 'WikiLambda' )
65        );
66
67        $implement = $this->getOption( 'implement' );
68
69        $queryLimit = 10;
70
71        $targets = $this->zObjectStore->fetchZidsOfType( 'Z14' );
72
73        $this->output( "Found " . count( $targets ) . " Z14s\n" );
74
75        $offset = 0;
76
77        $updateComment = wfMessage( 'wikilambda-migration-edit-comment' )->inLanguage( 'en' )->text();
78
79        do {
80            $contents = $this->zObjectStore->fetchBatchZObjects( array_slice( $targets, $offset, $queryLimit ) );
81            $offset += $queryLimit;
82
83            foreach ( $contents as $zid => $persistentObject ) {
84                $newValue = null;
85
86                $this->output( "Reviewing $zid" );
87
88                $zCode = $persistentObject->getInnerZObject()->getValueByKey( 'Z14K3' );
89
90                if ( $zCode === null ) {
91                    $this->output( "… not using a ZCode!\n" );
92                    continue;
93                }
94
95                $reference = $zCode->getValueByKey( 'Z16K1' );
96
97                if ( $reference instanceof ZReference ) {
98                    $this->output( "… already a ZReference, success!\n" );
99                    continue;
100                }
101
102                $programmingLanguage = $reference->getValueByKey( 'Z61K1' );
103
104                if ( $programmingLanguage instanceof ZString ) {
105                    $this->output( "… a hard-coded string " );
106                    switch ( $programmingLanguage->getSerialized() ) {
107                        case 'javascript':
108                            $this->output( "for JS" );
109                            $newValue = 'Z600';
110                            break;
111
112                        case 'python':
113                        case 'python-3':
114                            $this->output( "for Py" );
115                            $newValue = 'Z610';
116                            break;
117
118                        default:
119                            $this->output( "but not understood: " . $programmingLanguage );
120                            continue 2;
121                    }
122                } else {
123                    $this->output(
124                        '… but  \'' . var_export( $programmingLanguage, true )
125                        . "', not a reference or a simple string? Skipping.\n"
126                    );
127                    continue;
128                }
129
130                if ( !$newValue ) {
131                    $this->output( "… nothing to do?\n" );
132                    continue;
133                }
134
135                if ( !$implement ) {
136                    $this->output( ".\n\tWould have updated $zid, changing '$programmingLanguage' to '$newValue'.\n" );
137                    continue;
138
139                }
140
141                $persistentObject->getInnerZObject()->getValueByKey( 'Z14K3' )
142                    ->setValueByKey( 'Z16K1', new ZReference( $newValue ) );
143                $data = json_encode( $persistentObject->getSerialized() );
144
145                $response = $this->zObjectStore->updateZObjectAsSystemUser(
146                    /* String ZID */ $zid,
147                    /* String content */ $data,
148                    /* Edit summary */ $updateComment,
149                    /* Update flags */ EDIT_UPDATE
150                );
151
152                if ( $response->isOK() ) {
153                    $this->output( ", now updated!\n" );
154                } else {
155                    $this->output( ", but there was a problem updating:\n" );
156                    $this->output( "\t" . $response->getErrors() . "\n" );
157                }
158            }
159
160        } while ( count( $targets ) - $offset > 0 );
161    }
162}
163
164$maintClass = MigrateZ16K1StringsToZ61s::class;
165require_once RUN_MAINTENANCE_IF_MAIN;