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