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