3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
20 use MediaWiki\Category\CategoriesRdf
;
21 use MediaWiki\MainConfigNames
;
22 use MediaWiki\Maintenance\Maintenance
;
23 use Wikimedia\Purtle\RdfWriter
;
24 use Wikimedia\Purtle\RdfWriterFactory
;
25 use Wikimedia\Rdbms\IReadableDatabase
;
27 // @codeCoverageIgnoreStart
28 require_once __DIR__
. '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
32 * Maintenance script to provide RDF representation of the category tree.
34 * @ingroup Maintenance
37 class DumpCategoriesAsRdf
extends Maintenance
{
43 * Categories RDF helper.
46 private $categoriesRdf;
48 public function __construct() {
49 parent
::__construct();
51 $this->addDescription( "Generate RDF dump of categories in a wiki." );
53 $this->setBatchSize( 200 );
54 $this->addOption( 'output', "Output file (default is stdout). Will be overwritten.",
56 $this->addOption( 'format', "Set the dump format.", false, true );
60 * Produce row iterator for categories.
61 * @param IReadableDatabase $dbr
62 * @param string $fname Name of the calling function
63 * @return RecursiveIterator
65 public function getCategoryIterator( IReadableDatabase
$dbr, $fname ) {
66 $it = new BatchRowIterator(
68 [ 'page', 'page_props', 'category' ],
73 'page_namespace' => NS_CATEGORY
,
75 $it->setFetchColumns( [
83 $it->addJoinConditions(
86 'LEFT JOIN', [ 'pp_propname' => 'hiddencat', 'pp_page = page_id' ]
89 'LEFT JOIN', [ 'cat_title = page_title' ]
94 $it->setCaller( $fname );
99 * Get iterator for links for categories.
100 * @param IReadableDatabase $dbr
101 * @param int[] $ids List of page IDs
102 * @param string $fname Name of the calling function
103 * @return Traversable
105 public function getCategoryLinksIterator( IReadableDatabase
$dbr, array $ids, $fname ) {
106 $it = new BatchRowIterator(
109 [ 'cl_from', 'cl_to' ],
110 $this->getBatchSize()
112 $it->addConditions( [
113 'cl_type' => 'subcat',
116 $it->setFetchColumns( [ 'cl_from', 'cl_to' ] );
117 $it->setCaller( $fname );
118 return new RecursiveIteratorIterator( $it );
122 * @param int $timestamp
124 public function addDumpHeader( $timestamp ) {
125 $licenseUrl = $this->getConfig()->get( MainConfigNames
::RightsUrl
);
126 if ( str_starts_with( $licenseUrl, '//' ) ) {
127 $licenseUrl = 'https:' . $licenseUrl;
129 $urlUtils = $this->getServiceContainer()->getUrlUtils();
130 $this->rdfWriter
->about( $this->categoriesRdf
->getDumpURI() )
131 ->a( 'schema', 'Dataset' )
132 ->a( 'owl', 'Ontology' )
133 ->say( 'cc', 'license' )->is( $licenseUrl )
134 ->say( 'schema', 'softwareVersion' )->value( CategoriesRdf
::FORMAT_VERSION
)
135 ->say( 'schema', 'dateModified' )
136 ->value( wfTimestamp( TS_ISO_8601
, $timestamp ), 'xsd', 'dateTime' )
137 ->say( 'schema', 'isPartOf' )->is( (string)$urlUtils->expand( '/', PROTO_CANONICAL
) )
138 ->say( 'owl', 'imports' )->is( CategoriesRdf
::OWL_URL
);
141 public function execute() {
142 $outFile = $this->getOption( 'output', 'php://stdout' );
144 if ( $outFile === '-' ) {
145 $outFile = 'php://stdout';
148 $output = fopen( $outFile, 'w' );
149 $this->rdfWriter
= $this->createRdfWriter( $this->getOption( 'format', 'ttl' ) );
150 $this->categoriesRdf
= new CategoriesRdf( $this->rdfWriter
);
152 $this->categoriesRdf
->setupPrefixes();
153 $this->rdfWriter
->start();
155 $this->addDumpHeader( time() );
156 fwrite( $output, $this->rdfWriter
->drain() );
158 $dbr = $this->getDB( DB_REPLICA
, [ 'vslow' ] );
160 foreach ( $this->getCategoryIterator( $dbr, __METHOD__
) as $batch ) {
162 foreach ( $batch as $row ) {
163 $this->categoriesRdf
->writeCategoryData(
165 $row->pp_propname
=== 'hiddencat',
166 (int)$row->cat_pages
- (int)$row->cat_subcats
- (int)$row->cat_files
,
167 (int)$row->cat_subcats
169 if ( $row->page_id
) {
170 $pages[$row->page_id
] = $row->page_title
;
174 foreach ( $this->getCategoryLinksIterator( $dbr, array_keys( $pages ), __METHOD__
) as $row ) {
175 $this->categoriesRdf
->writeCategoryLinkData( $pages[$row->cl_from
], $row->cl_to
);
177 fwrite( $output, $this->rdfWriter
->drain() );
180 if ( $outFile !== '-' ) {
186 * @param string $format Writer format
189 private function createRdfWriter( $format ) {
190 $factory = new RdfWriterFactory();
191 return $factory->getWriter( $factory->getFormatName( $format ) );
195 // @codeCoverageIgnoreStart
196 $maintClass = DumpCategoriesAsRdf
::class;
197 require_once RUN_MAINTENANCE_IF_MAIN
;
198 // @codeCoverageIgnoreEnd