Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateSearchIndex
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 getDbType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 doUpdateSearchIndex
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
 updateSearchIndexForPage
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Periodic off-peak updating of the search index.
4 *
5 * Usage: php updateSearchIndex.php [-s START] [-e END] [-p POSFILE] [-l LOCKTIME] [-q]
6 * Where START is the starting timestamp
7 * END is the ending timestamp
8 * POSFILE is a file to load timestamps from and save them to, searchUpdate.WIKI_ID.pos by default
9 * LOCKTIME is how long the searchindex and revision tables will be locked for
10 * -q means quiet
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup Maintenance
29 */
30
31use MediaWiki\Deferred\SearchUpdate;
32use MediaWiki\Revision\SlotRecord;
33use MediaWiki\Title\Title;
34use MediaWiki\WikiMap\WikiMap;
35
36require_once __DIR__ . '/Maintenance.php';
37
38/**
39 * Maintenance script for periodic off-peak updating of the search index.
40 *
41 * @ingroup Maintenance
42 */
43class UpdateSearchIndex extends Maintenance {
44
45    public function __construct() {
46        parent::__construct();
47        $this->addDescription( 'Script for periodic off-peak updating of the search index' );
48        $this->addOption( 's', 'Starting timestamp', false, true );
49        $this->addOption( 'e', 'Ending timestamp', false, true );
50        $this->addOption(
51            'p',
52            'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default',
53            false,
54            true
55        );
56        $this->addOption(
57            'l',
58            'Deprecated, has no effect (formerly lock time)',
59            false,
60            true
61        );
62    }
63
64    public function getDbType() {
65        return Maintenance::DB_ADMIN;
66    }
67
68    public function execute() {
69        $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
70        $posFile = $this->getOption( 'p', 'searchUpdate.' . rawurlencode( $dbDomain ) . '.pos' );
71        $end = $this->getOption( 'e', wfTimestampNow() );
72        if ( $this->hasOption( 's' ) ) {
73            $start = $this->getOption( 's' );
74        } elseif ( is_readable( $posFile ) ) {
75            $start = file_get_contents( $posFile );
76        } else {
77            $start = wfTimestamp( TS_MW, time() - 86400 );
78        }
79
80        $this->doUpdateSearchIndex( $start, $end );
81        $file = fopen( $posFile, 'w' );
82        if ( $file !== false ) {
83            fwrite( $file, $end );
84            fclose( $file );
85        } else {
86            $this->error( "*** Couldn't write to the $posFile!\n" );
87        }
88    }
89
90    private function doUpdateSearchIndex( $start, $end ) {
91        global $wgDisableSearchUpdate;
92
93        $wgDisableSearchUpdate = false;
94
95        $dbw = $this->getPrimaryDB();
96
97        $this->output( "Updating searchindex between $start and $end\n" );
98
99        # Select entries from recentchanges which are on top and between the specified times
100        $start = $dbw->timestamp( $start );
101        $end = $dbw->timestamp( $end );
102
103        $res = $dbw->newSelectQueryBuilder()
104            ->select( 'rc_cur_id' )
105            ->from( 'recentchanges' )
106            ->join( 'page', null, 'rc_cur_id=page_id AND rc_this_oldid=page_latest' )
107            ->where( [
108                $dbw->expr( 'rc_type', '!=', RC_LOG ),
109                'rc_timestamp BETWEEN ' . $dbw->addQuotes( $start ) . ' AND ' . $dbw->addQuotes( $end )
110            ] )
111            ->caller( __METHOD__ )->fetchResultSet();
112
113        foreach ( $res as $row ) {
114            $this->updateSearchIndexForPage( (int)$row->rc_cur_id );
115        }
116        $this->output( "Done\n" );
117    }
118
119    /**
120     * Update the searchindex table for a given pageid
121     * @param int $pageId The page ID to update.
122     * @return null|string
123     */
124    private function updateSearchIndexForPage( int $pageId ) {
125        // Get current revision
126        $rev = $this->getServiceContainer()
127            ->getRevisionLookup()
128            ->getRevisionByPageId( $pageId, 0, IDBAccessObject::READ_LATEST );
129        $title = null;
130        if ( $rev ) {
131            $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
132            $title = $titleObj->getPrefixedDBkey();
133            $this->output( "$title..." );
134            # Update searchindex
135            $u = new SearchUpdate( $pageId, $titleObj, $rev->getContent( SlotRecord::MAIN ) );
136            $u->doUpdate();
137            $this->output( "\n" );
138        }
139
140        return $title;
141    }
142}
143
144$maintClass = UpdateSearchIndex::class;
145require_once RUN_MAINTENANCE_IF_MAIN;