Skip to content

Commit dde40a5

Browse files
authored
Merge pull request filbertkm#21 from lucaswerkmeister/stdin
Add option to read entity IDs from standard input
2 parents a70c166 + 85abcc0 commit dde40a5

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ Import a range of entities:
6363
```
6464
php maintenance/importEntities.php --range Q1:Q20
6565
```
66+
67+
Import a list of entities printed by another program:
68+
69+
```
70+
printf 'Q%s\n' {1..20} {100..120} | php maintenance/importEntities.php --stdin
71+
```

maintenance/importEntities.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private function addOptions() {
3939
$this->addOption( 'entity', 'ID of entity to import', false, true );
4040
$this->addOption( 'query', 'Import items with property and entity id value', false, true );
4141
$this->addOption( 'range', 'Range of ids to import', false, true );
42+
$this->addOption( 'stdin', 'Read entity IDs to import from standard input', false, false );
4243
$this->addOption( 'all-properties', 'Import all properties', false, false );
4344
}
4445

@@ -98,7 +99,7 @@ private function extractOptions() {
9899
}
99100

100101
private function getValidOptions() {
101-
return [ 'entity', 'file', 'all-properties', 'query', 'range' ];
102+
return [ 'entity', 'file', 'all-properties', 'query', 'range', 'stdin' ];
102103
}
103104

104105
private function newEntityIdListBuilderFactory() {

src/EntityId/EntityIdListBuilderFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public function newEntityIdListBuilder( $mode ) {
6565
return $this->newRangeEntityIdListBuilder();
6666
case 'query':
6767
return $this->newQueryEntityIdListBuilder();
68+
case 'stdin':
69+
return $this->newStdinEntityIdListBuilder();
6870
default:
6971
throw new InvalidArgumentException( 'Unknown import mode: ' . $mode );
7072
}
@@ -96,4 +98,8 @@ private function newRangeEntityIdListBuilder() {
9698
return new RangeEntityIdListBuilder( $this->idParser );
9799
}
98100

101+
private function newStdinEntityIdListBuilder() {
102+
return new StdinEntityIdListBuilder();
103+
}
104+
99105
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Wikibase\Import\EntityId;
4+
5+
use RuntimeException;
6+
7+
/**
8+
* @licence GNU GPL v2+
9+
* @author Lucas Werkmeister < lucas.werkmeister@wikimedia.de >
10+
*/
11+
class StdinEntityIdListBuilder implements EntityIdListBuilder {
12+
13+
/**
14+
* @param string $input (ignored)
15+
*
16+
* @throws RuntimeException
17+
* @return string[]
18+
*/
19+
public function getEntityIds( $input ) {
20+
$entityIds = [];
21+
while ( $line = fgets( STDIN ) ) {
22+
$entityIds[] = trim( $line );
23+
}
24+
return $entityIds;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)