Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GenerateDescription
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3declare( strict_types=1 );
4use MediaWiki\Extension\WikiSEO\DeferredDescriptionUpdate;
5
6$IP = getenv( 'MW_INSTALL_PATH' );
7if ( $IP === false ) {
8    $IP = __DIR__ . '/../../..';
9}
10require_once "$IP/maintenance/Maintenance.php";
11
12class GenerateDescription extends Maintenance {
13    public function __construct() {
14        parent::__construct();
15
16        $this->addDescription( 'Creates SEO descriptions for wiki pages.' );
17        $this->addOption( 'force', 'Force description generation, even for pages that already have one' );
18        $this->addOption( 'cleanSentence', 'Cut off dangling sentences' );
19        $this->addArg( 'namespaces', 'Comma separated list of namespace ids to work on' );
20        $this->setBatchSize( 100 );
21
22        $this->requireExtension( 'TextExtracts' );
23        $this->requireExtension( 'WikiSEO' );
24    }
25
26    public function execute() {
27        $pageQuery = WikiPage::getQueryInfo();
28        $it = new BatchRowIterator(
29            $this->getDB( DB_REPLICA ),
30            $pageQuery['tables'],
31            'page_id', $this->getBatchSize()
32        );
33
34        $validNamespaces = array_map( static function ( $ns ) {
35            return (int)( $ns );
36        }, explode( ',', $this->getArg( 0, '' ) ) );
37
38        $pageProps = $this->getServiceContainer()->getPageProps();
39        $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
40
41        foreach ( $it as $batch ) {
42            foreach ( $batch as $page ) {
43                $wikiPage = $wikiPageFactory->newFromID( $page->page_id );
44
45                if ( $wikiPage === null ) {
46                    $this->error( sprintf( "Page with id %s is null", $page->page_id ) );
47                    continue;
48                }
49
50                if ( $wikiPage->isRedirect() || !$wikiPage->getTitle()->inNamespaces( $validNamespaces ) ) {
51                    continue;
52                }
53
54                $properties = $pageProps->getProperties(
55                    $wikiPage->getTitle(),
56                    [
57                        'manualDescription',
58                        'description'
59                    ]
60                );
61
62                $properties = array_shift( $properties );
63
64                if ( isset( $properties['manualDescription'] ) ) {
65                    continue;
66                }
67
68                if ( isset( $properties['description'] ) && !$this->getOption( 'force' ) ) {
69                    $this->output(
70                        sprintf(
71                            "Page '%s' already has a description, skipping.\r\n",
72                            $wikiPage->getTitle()->getPrefixedText()
73                        )
74                    );
75                    continue;
76                }
77
78                $this->output(
79                    sprintf(
80                        "Generating description for '%s'\r\n",
81                        $wikiPage->getTitle()->getPrefixedText()
82                    )
83                );
84
85                ( new DeferredDescriptionUpdate(
86                    $wikiPage->getTitle(),
87                    null,
88                    $this->getOption( 'cleanSentence', false )
89                ) )->doUpdate();
90            }
91        }
92    }
93
94}
95
96$maintClass = GenerateDescription::class;
97require_once RUN_MAINTENANCE_IF_MAIN;