-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterwikiFromCategorySort_wikibase.php
More file actions
139 lines (129 loc) · 4.63 KB
/
interwikiFromCategorySort_wikibase.php
File metadata and controls
139 lines (129 loc) · 4.63 KB
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
<?php
/**
*/
require_once( dirname( __FILE__ ) . '/Maintenance.php' );
class InterwikiFromCategorySort_wikibase extends Maintenance {
public function __construct() {
parent::__construct();
$this->addOption( 'category', 'Category to check', true, true );
$this->addOption( 'sites', 'Possible foreign sites', true, true );
$this->addOption( 'dry-run', 'Do not really call the linker', false );
$this->addOption( 'regex-replace', 'Do replacement before passing sortkey to interwiki.py', false, true );
$this->addOption( 'regex-replacement', 'Replace with this string, empty by default', false, true );
$this->addOption( 'wikibase-merge', 'Try to merge items on Wikibase Repo', false );
}
public function execute() {
global $IP, $wgLabs, $wgContLang, $wmgUseWikibaseClient, $wgDBname;
if ( !$wmgUseWikibaseClient ) {
$this->output( "Wikibase is not enabled as a client here." );
return;
}
$cattitle = Title::makeTitleSafe( NS_CATEGORY, $this->getOption( 'category' ) );
if ( !$cattitle ) {
$this->output( "Invalid category name.\n" );
return;
}
# To make SGE happy, avoid commas.
$sites = array_map( 'trim', preg_split(
'/,|:/', $this->getOption( 'sites' ), null, PREG_SPLIT_NO_EMPTY
) );
sort( $sites );
$replace = $this->getOption( 'regex-replace', false );
$replacement = $this->getOption( 'regex-replacement', '' );
if ( $replace === false ) {
$cachekey = wfMemcKey( 'InterwikiFromCategorySort_wikibase',
$cattitle->getDBkey(), implode( '!', $sites ) );
} else {
$cachekey = wfMemcKey( 'InterwikiFromCategorySort_wikibase',
$cattitle->getDBkey(), implode( '!', $sites ),
$replace, $replacement );
$replace = "/$replace/u";
}
$cache = ObjectCache::getInstance( CACHE_DB );
$cltscid = $cache->get( $cachekey );
if ( $cltscid ) {
list( $clts, $cid ) = $cltscid;
}
$dbw = wfGetDB( DB_MASTER );
$this->output( "Cache key: $cachekey\n" );
$this->output( 'Looking for pages to add langlinks...' );
if ( $cltscid ) {
$this->output( " (starting from $clts, $cid)" );
}
# Due to replag we must fetch all rows first.
$res = $dbw->select(
array( 'page', 'categorylinks' ),
array( 'page_id', 'page_namespace', 'page_title', 'cl_sortkey_prefix', 'cl_timestamp' ),
array(
'cl_to' => $cattitle->getDBkey(),
( $cltscid ? $dbw->makeList( array(
'cl_timestamp > ' . $dbw->addQuotes( $clts ),
$dbw->makeList( array(
'cl_timestamp' => $clts,
'page_id > ' . $cid,
), LIST_AND ),
), LIST_OR ) : true ),
),
__METHOD__,
array(
'ORDER BY' => array( 'cl_timestamp', 'page_id' ),
),
array(
'categorylinks' => array( 'JOIN', array(
'cl_from = page_id',
) ),
)
);
$this->output( " {$dbw->numRows( $res )} rows.\n" );
$this->output( 'Linking to: ' . implode( ', ', $sites ) . ".\n" );
while ( $row = $dbw->fetchObject( $res ) ) {
$title = Title::newFromRow( $row );
$sortkey = $row->cl_sortkey_prefix;
if ( $replace !== false ) {
$sortkey = preg_replace( $replace, $replacement, $sortkey );
}
$ft = Title::makeTitleSafe( $title->getNamespace(), $sortkey );
if ( !$ft ) {
continue;
}
$fptext = $ft->getText();
if ( $ft->getNamespace() !== 0 ) {
$fptext = MWNamespace::getCanonicalName( $ft->getNamespace() ) . ':' . $fptext;
}
$this->output( "Processing {$title->getPrefixedText()} "
. "(interwiki = {$fptext}, "
. "timestamp = {$row->cl_timestamp}, "
. "curid = {$row->page_id})...\n" );
# Invoke the linker
$args = array(
'--bot', '--wiki', Wikibase\Settings::singleton()->getSetting( 'repoDatabase' ),
'--report', wfMessage( 'ts-iwcatsort-wb-report' )->text(),
'--report-message', wfMessage( 'ts-iwcatsort-wb-report-message' )->params(
$wgDBname, $title->getFullText(),
$cattitle->getText(), $sortkey, $row->cl_sortkey_prefix
)->text(),
$wgDBname, $title->getFullText(),
);
if ( $this->hasOption( 'wikibase-merge' ) ) {
$args[] = '--merge';
}
foreach ( $sites as $site ) {
$args = array_merge( $args, array( $site, $fptext ) );
}
$cmd = wfShellWikiCmd( "$IP/maintenance/wbLinkTitlesLocal.php", $args );
$this->output( "Invoking $cmd\n" );
if ( !$this->hasOption( 'dry-run' ) ) {
$retval = 1;
while ( $retval != 0 ) {
$this->output( wfShellExec( $cmd, $retval, array(), array( 'memory' => 0 ) ) );
$this->output( "Linker exits with return code $retval.\n" );
}
if ( $cache->set( $cachekey, array( $row->cl_timestamp, $row->page_id ) ) ) {
$this->output( "cltscid cache updated.\n" );
}
}
}
}
}
$maintClass = "InterwikiFromCategorySort_wikibase";
require_once( RUN_MAINTENANCE_IF_MAIN );