Debian Codegraph @

maintenance/updateSearchIndex.php

raw symbols SCIP
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
31 use MediaWiki\Deferred\SearchUpdate;
32 use MediaWiki\Revision\SlotRecord;
33 use MediaWiki\Title\Title;
34 use MediaWiki\WikiMap\WikiMap;
35 use Wikimedia\Rdbms\IDBAccessObject;
36
37 // @codeCoverageIgnoreStart
38 require_once __DIR__ . '/Maintenance.php';
39 // @codeCoverageIgnoreEnd
40
41 /**
42 * Maintenance script for periodic off-peak updating of the search index.
43 *
44 * @ingroup Maintenance
45 */
46 class UpdateSearchIndex extends Maintenance {
47
48 public function __construct() {
49 parent::__construct();
50 $this->addDescription( 'Script for periodic off-peak updating of the search index' );
51 $this->addOption( 's', 'Starting timestamp', false, true );
52 $this->addOption( 'e', 'Ending timestamp', false, true );
53 $this->addOption(
54 'p',
55 'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default',
56 false,
57 true
58 );
59 $this->addOption(
60 'l',
61 'Deprecated, has no effect (formerly lock time)',
62 false,
63 true
64 );
65 }
66
67 public function getDbType() {
68 return Maintenance::DB_ADMIN;
69 }
70
71 public function execute() {
72 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
73 $posFile = $this->getOption( 'p', 'searchUpdate.' . rawurlencode( $dbDomain ) . '.pos' );
74 $end = $this->getOption( 'e', wfTimestampNow() );
75 if ( $this->hasOption( 's' ) ) {
76 $start = $this->getOption( 's' );
77 } elseif ( is_readable( $posFile ) ) {
78 $start = file_get_contents( $posFile );
79 } else {
80 $start = wfTimestamp( TS_MW, time() - 86400 );
81 }
82
83 $this->doUpdateSearchIndex( $start, $end );
84 $file = fopen( $posFile, 'w' );
85 if ( $file !== false ) {
86 fwrite( $file, $end );
87 fclose( $file );
88 } else {
89 $this->error( "*** Couldn't write to the $posFile!\n" );
90 }
91 }
92
93 private function doUpdateSearchIndex( $start, $end ) {
94 global $wgDisableSearchUpdate;
95
96 $wgDisableSearchUpdate = false;
97
98 $dbw = $this->getPrimaryDB();
99
100 $this->output( "Updating searchindex between $start and $end\n" );
101
102 # Select entries from recentchanges which are on top and between the specified times
103 $start = $dbw->timestamp( $start );
104 $end = $dbw->timestamp( $end );
105
106 $res = $dbw->newSelectQueryBuilder()
107 ->select( 'rc_cur_id' )
108 ->from( 'recentchanges' )
109 ->join( 'page', null, 'rc_cur_id=page_id AND rc_this_oldid=page_latest' )
110 ->where( [
111 $dbw->expr( 'rc_type', '!=', RC_LOG ),
112 $dbw->expr( 'rc_timestamp', '>=', $start ),
113 $dbw->expr( 'rc_timestamp', '<=', $end ),
114 ] )
115 ->caller( __METHOD__ )->fetchResultSet();
116
117 foreach ( $res as $row ) {
118 $this->updateSearchIndexForPage( (int)$row->rc_cur_id );
119 }
120 $this->output( "Done\n" );
121 }
122
123 /**
124 * Update the searchindex table for a given pageid
125 * @param int $pageId The page ID to update.
126 * @return null|string
127 */
128 private function updateSearchIndexForPage( int $pageId ) {
129 // Get current revision
130 $rev = $this->getServiceContainer()
131 ->getRevisionLookup()
132 ->getRevisionByPageId( $pageId, 0, IDBAccessObject::READ_LATEST );
133 $title = null;
134 if ( $rev ) {
135 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
136 $title = $titleObj->getPrefixedDBkey();
137 $this->output( "$title..." );
138 # Update searchindex
139 $u = new SearchUpdate( $pageId, $titleObj, $rev->getContent( SlotRecord::MAIN ) );
140 $u->doUpdate();
141 $this->output( "\n" );
142 }
143
144 return $title;
145 }
146 }
147
148 // @codeCoverageIgnoreStart
149 $maintClass = UpdateSearchIndex::class;
150 require_once RUN_MAINTENANCE_IF_MAIN;
151 // @codeCoverageIgnoreEnd