Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeferredDescriptionUpdate
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doUpdate
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
110
 loadDescriptionFromApi
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
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.
7 *
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.
12 *
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 *
17 * @file
18 */
19
20declare( strict_types=1 );
21
22namespace MediaWiki\Extension\WikiSEO;
23
24use DeferrableUpdate;
25use Exception;
26use ExtensionDependencyError;
27use MediaWiki\MediaWikiServices;
28use MediaWiki\Title\Title;
29use MWException;
30
31/**
32 * This runs through the onRevisionDataUpdates hook but only if $wgWikiSeoEnableAutoDescription is enabled
33 * and no manual description was set
34 *
35 * The goal of this class is to automatically set a description for each page after if has been edited.
36 * Currently, only TextExtracts is available
37 */
38class DeferredDescriptionUpdate implements DeferrableUpdate {
39
40    private readonly string $currentDescription;
41
42    /**
43     * Do a deferred update to the specified title.
44     * Usually runs when RevisionDataUpdates occurs
45     *
46     * @param Title $title The title to work on
47     * @param string|null $currentDescription Current description property from ParserOutput
48     * @param bool $clean Whether to cut of dangling sentences
49     */
50    public function __construct(
51        private readonly Title $title,
52        ?string $currentDescription,
53        private readonly bool $clean = false,
54    ) {
55        $this->currentDescription = $currentDescription ?? '';
56    }
57
58    /**
59     * We do have to manually set the page properties, as we have no way of getting the parser or outputpage
60     * in a deferred update
61     */
62    public function doUpdate(): void {
63        try {
64            $apiDescription = $this->loadDescriptionFromApi();
65        } catch ( Exception ) {
66            return;
67        }
68
69        $apiDescription = trim( $apiDescription ?? '' );
70        $emptyLikeDescriptions = [ '', '…', '\u2026' ];
71
72        // If API response is empty like, or current description is equal to api description, exit early
73        if ( in_array( $apiDescription, $emptyLikeDescriptions, true ) ||
74            strcmp( $this->currentDescription, $apiDescription ) === 0 ) {
75            return;
76        }
77
78        $propertyDescriptions = MediaWikiServices::getInstance()->getPageProps()
79            ->getProperties( $this->title, 'description' );
80
81        $dbl = MediaWikiServices::getInstance()->getDBLoadBalancer();
82        $db = $dbl->getConnection( DB_PRIMARY );
83
84        // Flag indicating if an insert or update should happen
85        $shouldInsert = false;
86        switch ( true ) {
87            case count( $propertyDescriptions ) > 1:
88                // There are multiple page props with the name 'description' present
89                // This shouldn't happen, but we'll try to clean it here
90                $db->delete(
91                    'page_props',
92                    [
93                        'pp_page' => $this->title->getArticleID(),
94                        'pp_propname' => 'description',
95                    ],
96                    __METHOD__
97                );
98            // Intentional fall-through, as deleting all 'description' props requires inserting a new row
99            case empty( $propertyDescriptions ):
100                $shouldInsert = true;
101                break;
102
103            default:
104                break;
105        }
106
107        if ( count( $propertyDescriptions ) === 1 ) {
108            $prop = array_shift( $propertyDescriptions );
109            // Sanity check
110            $descriptionEqual = strcmp( $prop ?? '', $apiDescription ) === 0;
111            if ( $descriptionEqual ) {
112                return;
113            }
114        }
115
116        if ( $shouldInsert ) {
117            $db->insert(
118                'page_props',
119                [
120                    'pp_page' => $this->title->getArticleID(),
121                    'pp_propname' => 'description',
122                    'pp_value' => $apiDescription,
123                    'pp_sortkey' => null,
124                ],
125                __METHOD__
126            );
127        } else {
128            $db->update(
129                'page_props',
130                [
131                    'pp_value' => $apiDescription,
132                ],
133                [
134                    'pp_page' => $this->title->getArticleID(),
135                    'pp_propname' => 'description',
136                ],
137                __METHOD__
138            );
139        }
140    }
141
142    /**
143     * @return string|null
144     * @throws ExtensionDependencyError
145     * @throws MWException
146     */
147    private function loadDescriptionFromApi(): ?string {
148        $descriptor = new ApiDescription(
149            $this->title,
150            $this->clean
151        );
152
153        return $descriptor->getDescription();
154    }
155}