summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/maintenance/updateEntityCollation.php
blob: 2328cf91ca78909992e5aa08d98170e34c0a76fa (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

namespace SMW\Maintenance;

use SMW\ApplicationFactory;
use SMW\SQLStore\SQLStore;
use SMW\SQLStore\TableFieldUpdater;
use SMW\DIWikiPage;
use SMW\DIProperty;
use SMWDataItem as DataItem;
use SMW\Exception\PredefinedPropertyLabelMismatchException;
use SMW\Setup;

$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv('MW_INSTALL_PATH' ) : __DIR__ . '/../../..';

require_once $basePath . '/maintenance/Maintenance.php';

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class UpdateEntityCollation extends \Maintenance {

	public function __construct() {
		$this->mDescription = 'Update the smw_sort field (relying on the $smwgEntityCollation setting)';
		$this->addOption( 's', 'ID starting point', false, true );

		parent::__construct();
	}

	/**
	 * @see Maintenance::addDefaultParams
	 *
	 * @since 3.0
	 */
	protected function addDefaultParams() {

		parent::addDefaultParams();
	}

	/**
	 * @see Maintenance::execute
	 */
	public function execute() {

		if ( !Setup::isEnabled() ) {
			$this->reportMessage( "\nYou need to have SMW enabled in order to run the maintenance script!\n" );
			exit;
		}

		if ( !Setup::isValid( true ) ) {
			$this->reportMessage( "\nYou need to run `update.php` or `setupStore.php` first before continuing\nwith any maintenance tasks!\n" );
			exit;
		}

		$applicationFactory = ApplicationFactory::getInstance();
		$store = $applicationFactory->getStore( 'SMW\SQLStore\SQLStore' );

		$connection = $store->getConnection( 'mw.db' );
		$tableFieldUpdater = new TableFieldUpdater( $store );

		$condition = " smw_iw!=" . $connection->addQuotes( SMW_SQL3_SMWIW_OUTDATED ) . " AND smw_iw!=" . $connection->addQuotes( SMW_SQL3_SMWDELETEIW );
		$i = 1;

		if ( $this->hasOption( 's' ) ) {
			$i = $this->getOption( 's' );
			$condition .= ' AND smw_id > ' . $connection->addQuotes( $this->getOption( 's' ) );
		}

		$res = $connection->select(
			SQLStore::ID_TABLE,
			[
				'smw_id',
				'smw_title',
				'smw_sortkey'
			],
			$condition,
			__METHOD__
		);

		$expected = $res->numRows() + $i;

		if ( $applicationFactory->getSettings()->get( 'smwgEntityCollation' ) !== $GLOBALS['wgCategoryCollation'] ) {
			$this->reportMessage(
				"\n" . 'The setting of $smwgEntityCollation and $wgCategoryCollation are different' . "\n" .
				'and may result in an inconsitent sorting display for entities.' . "\n"
			);

			$this->reportMessage( "\n" . '$smwgEntityCollation: ' . $applicationFactory->getSettings()->get( 'smwgEntityCollation' ) );
			$this->reportMessage( "\n" . '$wgCategoryCollation: ' . $GLOBALS['wgCategoryCollation'] . "\n" );
		}

		$this->reportMessage(
			"\nPerforming the update ..."
		);

		$this->reportMessage( "\n   ... selecting $expected rows ..." );
		$this->reportMessage( "\n" );

		$this->doUpdate( $store, $tableFieldUpdater, $res, $i, $expected );
		$this->reportMessage( "\n" );
	}

	private function doUpdate( $store, $tableFieldUpdater, $res, $i, $expected ) {
		$property = new DIProperty( '_SKEY' );

		foreach ( $res as $row ) {

			if ( $row->smw_title === '' ) {
				continue;
			}

			$i++;

			$dataItem = $store->getObjectIds()->getDataItemById( $row->smw_id );
			$pv = $store->getPropertyValues( $dataItem, $property );

			$search = $this->getSortKey( $row, $pv );

			if ( $search === '' && $row->smw_title !== '' ) {
				$search = str_replace( '_', ' ', $row->smw_title );
			}

			$this->reportMessage(
				"\r". sprintf( "%-35s%s", "   ... updating document no.", sprintf( "%4.0f%% (%s/%s)", ( $i / $expected ) * 100, $i, $expected ) )
			);

			$tableFieldUpdater->updateSortField( $row->smw_id, $search );
		}
	}

	private function getSortKey( $row, $pv ) {

		if ( $pv !== [] ) {
			return end( $pv )->getString();
		}

		if ( $row->smw_title{0} !== '_' ) {
			return $row->smw_sortkey;
		}

		try {
			$property = new DIProperty( $row->smw_title );
		} catch ( PredefinedPropertyLabelMismatchException $e ) {
			return $row->smw_sortkey;
		}

		return $property->getCanonicalLabel();
	}

	/**
	 * @see Maintenance::reportMessage
	 *
	 * @since 1.9
	 *
	 * @param string $message
	 */
	public function reportMessage( $message ) {
		$this->output( $message );
	}

}

$maintClass = 'SMW\Maintenance\UpdateEntityCollation';
require_once( RUN_MAINTENANCE_IF_MAIN );