-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulateCite.php
More file actions
192 lines (185 loc) · 6.11 KB
/
populateCite.php
File metadata and controls
192 lines (185 loc) · 6.11 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
*/
require_once( dirname( __FILE__ ) . '/Maintenance.php' );
class PopulateCite extends Maintenance {
public function __construct() {
parent::__construct();
$this->addOption( 'category', 'Category to check', false, true );
$this->addOption( 'template', 'Template name', true, true );
$this->addOption( 'source', 'URL to trigger when a cite does not exist in source, $1 for placeholder', false, false );
$this->addOption( 'dry-run', 'Only list what to do', false );
$this->addOption( 'wikibase', 'Link cite pages on Wikibase repo', false );
}
public function execute() {
global $wgParser;
$process = array();
$articles = array();
if ( $this->hasOption( 'category' ) ) {
$cat = Category::newFromName( $this->getOption( 'category' ) );
if ( !$cat ) {
$this->output( "Invalid category name.\n" );
return;
}
$templateRe = strtr( preg_quote( $this->getOption( 'template' ) ), array(
' ' => '[ _]',
'_' => '[ _]',
) );
$citeRe = '/\{\{\s*' . $templateRe . '\s*\|\s*([^|}]*?)\s*[|}]/i';
foreach ( $cat->getMembers() as $title ) {
$this->output( $title->getPrefixedText() );
$text = Revision::newFromTitle( $title )->getText();
if ( $text === false ) {
continue;
}
# Search for refs to work on and add them to $process.
$matches = array();
preg_match_all( $citeRe, $text, $matches, PREG_PATTERN_ORDER );
$count = 0;
foreach ( $matches[1] as $cite ) {
$anchor = (string)substr( $wgParser->guessSectionNameFromWikiText( $cite ), 1 );
if ( !isset( $process[$anchor] ) ) {
$process[$anchor] = array( $cite, $title );
$count++;
}
}
$this->output( " ... $count more cites extracted.\n" );
$articles[] = $title;
}
} else { # Redirect-fix mode:
$prefixtitle = Title::makeTitleSafe( NS_TEMPLATE, $this->getOption( 'template' ) . '/' );
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array(
'source' => 'page',
'target' => 'page',
'redirect',
),
array(
'source.page_namespace source_namespace',
'source.page_title source_title',
'redirect.rd_namespace target_namespace',
'redirect.rd_title target_title',
),
array(
'target.page_id' => null,
# Already filtered out interwikis.
'rd_namespace' => $prefixtitle->getNamespace(),
'rd_title' . $dbr->buildLike( $prefixtitle->getDBkey(), $dbr->anyString() ),
),
__METHOD__,
array(),
array(
'target' => array(
'LEFT JOIN',
array(
'target.page_namespace = rd_namespace',
'target.page_title = rd_title',
),
),
'source' => array(
'JOIN',
'source.page_id = rd_from',
),
)
);
while ( $row = $dbr->fetchObject( $res ) ) {
$anchor = substr( $row->target_title, strlen( $prefixtitle->getDBkey() ) );
$localtitle = Title::makeTitleSafe( $row->target_namespace, $row->target_title );
$rdtitle = Title::makeTitleSafe( $row->source_namespace, $row->source_title );
if ( !isset( $process[$anchor] ) ) {
$this->output(
"{$rdtitle->getPrefixedText()} -> $anchor ({$localtitle->getPrefixedText()})\n"
);
$process[$anchor] = $rdtitle;
}
}
}
foreach ( $process as $anchor => $citetitle ) {
if ( is_array( $citetitle ) ) {
list( $cite, $title ) = $citetitle;
$rdtitle = null;
$this->output( "$anchor: $cite in [[{$title->getPrefixedText()}]] ..." );
} else {
$rdtitle = $citetitle;
$this->output( "$anchor: <- [[{$rdtitle->getPrefixedText()}]] ..." );
}
$titletext = $this->getOption( 'template' ) . '/' . $anchor;
$sourcetitle = MWNamespace::getCanonicalName( NS_TEMPLATE ) . ':' . $titletext;
$localtitle = Title::makeTitleSafe( NS_TEMPLATE, $titletext );
if ( !$localtitle ) {
$this->output( " invalid title.\n" );
continue;
}
if ( $localtitle->exists() ) {
$this->output( " exists locally.\n" );
continue;
}
$source = Http::get( 'https://en.wikipedia.org/w/index.php?title='
. wfUrlencode( $sourcetitle ) . '&action=raw' );
if ( $source === false ) {
$this->output( " does not exist in source.\n" );
if ( $this->hasOption( 'source' ) && !$rdtitle ) {
$url = $this->getOption( 'source' );
$url = str_replace( '$1', wfUrlencode( $cite ), $url );
$this->output( " triggering source: $url ..." );
# We ignore the result and just wait for our next run.
Http::get( $url );
$this->output( "\n" );
}
continue;
}
if ( $this->hasOption( 'dry-run' ) ) {
$this->output( " content:\n" );
$this->output( $source );
$this->output( "\n" );
continue;
}
$this->output( " creating ..." );
$page = WikiPage::factory( $localtitle );
if ( $rdtitle ) {
$summary = wfMessage( 'ts-populate-cite-redirect' )
->params( $rdtitle->getPrefixedText(), $rdtitle->getLatestRevId() )
->text();
} else {
$summary = wfMessage( 'ts-populate-cite' )
->params(
$this->getOption( 'template' ), $cite,
$title->getPrefixedText(), $title->getLatestRevId()
)
->text();
}
$status = $page->doEdit( $source, $summary, EDIT_NEW | EDIT_SUPPRESS_RC );
if ( $status->isOK() ) {
$this->output( ' ok.' );
if ( $this->hasOption( 'wikibase' ) ) {
$this->output( " wikibase ..." );
global $IP, $wgDBname;
$cmd = wfShellWikiCmd( "$IP/maintenance/wbLinkTitlesLocal.php", array(
'--bot', '--wiki', Wikibase\Settings::singleton()->getSetting( 'repoDatabase' ),
$wgDBname, $localtitle->getFullText(), 'enwiki', $sourcetitle
) );
$retVal = 1;
$data = trim( wfShellExec( $cmd, $retVal, array(), array( 'memory' => 0 ) ) );
if ( $data ) {
$this->output( " $data" );
} else {
$this->output( ' ERROR' );
}
}
$this->output( "\n" );
} else {
$this->output( " FAILED.\n" );
}
}
$parserOutput = new ParserOutput();
foreach ( $articles as $article ) {
$this->output( "[[{$article->getPrefixedText()}]] linksupdate ..." );
$linksUpdate = new LinksUpdate( $article, $parserOutput );
$linksUpdate->doUpdate();
$this->output( "\n" );
}
}
}
$maintClass = "PopulateCite";
require_once( RUN_MAINTENANCE_IF_MAIN );