RebuildTextIndex::getDbType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Rebuild search index table from scratch.  This may take several
4
 * hours, depending on the database size and server configuration.
5
 *
6
 * Postgres is trigger-based and should never need rebuilding.
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program; if not, write to the Free Software Foundation, Inc.,
20
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 * http://www.gnu.org/copyleft/gpl.html
22
 *
23
 * @file
24
 * @ingroup Maintenance
25
 * @todo document
26
 */
27
28
require_once __DIR__ . '/Maintenance.php';
29
30
/**
31
 * Maintenance script that rebuilds search index table from scratch.
32
 *
33
 * @ingroup Maintenance
34
 */
35
class RebuildTextIndex extends Maintenance {
36
	const RTI_CHUNK_SIZE = 500;
37
38
	/**
39
	 * @var Database
40
	 */
41
	private $db;
42
43
	public function __construct() {
44
		parent::__construct();
45
		$this->addDescription( 'Rebuild search index table from scratch' );
46
	}
47
48
	public function getDbType() {
49
		return Maintenance::DB_ADMIN;
50
	}
51
52
	public function execute() {
53
		// Shouldn't be needed for Postgres
54
		$this->db = $this->getDB( DB_MASTER );
55
		if ( $this->db->getType() == 'postgres' ) {
56
			$this->error( "This script is not needed when using Postgres.\n", true );
57
		}
58
59
		if ( $this->db->getType() == 'sqlite' ) {
60
			if ( !DatabaseSqlite::getFulltextSearchModule() ) {
61
				$this->error( "Your version of SQLite module for PHP doesn't "
62
					. "support full-text search (FTS3).\n", true );
63
			}
64
			if ( !$this->db->checkForEnabledSearch() ) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Database as the method checkForEnabledSearch() does only exist in the following sub-classes of Database: DatabaseSqlite. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
65
				$this->error( "Your database schema is not configured for "
66
					. "full-text search support. Run update.php.\n", true );
67
			}
68
		}
69
70
		if ( $this->db->getType() == 'mysql' ) {
71
			$this->dropMysqlTextIndex();
72
			$this->clearSearchIndex();
73
			$this->populateSearchIndex();
74
			$this->createMysqlTextIndex();
75
		} else {
76
			$this->clearSearchIndex();
77
			$this->populateSearchIndex();
78
		}
79
80
		$this->output( "Done.\n" );
81
	}
82
83
	/**
84
	 * Populates the search index with content from all pages
85
	 */
86
	protected function populateSearchIndex() {
87
		$res = $this->db->select( 'page', 'MAX(page_id) AS count' );
88
		$s = $this->db->fetchObject( $res );
0 ignored issues
show
Bug introduced by
It seems like $res defined by $this->db->select('page'...MAX(page_id) AS count') on line 87 can also be of type boolean; however, IDatabase::fetchObject() does only seem to accept object<ResultWrapper>|object<stdClass>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
89
		$count = $s->count;
90
		$this->output( "Rebuilding index fields for {$count} pages...\n" );
91
		$n = 0;
92
93
		$fields = array_merge(
94
			Revision::selectPageFields(),
95
			Revision::selectFields(),
96
			Revision::selectTextFields()
97
		);
98
99
		while ( $n < $count ) {
100
			if ( $n ) {
101
				$this->output( $n . "\n" );
102
			}
103
			$end = $n + self::RTI_CHUNK_SIZE - 1;
104
105
			$res = $this->db->select( [ 'page', 'revision', 'text' ], $fields,
106
				[ "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ],
107
				__METHOD__
108
			);
109
110
			foreach ( $res as $s ) {
0 ignored issues
show
Bug introduced by
The expression $res of type boolean|object<ResultWrapper> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
111
				try {
112
					$title = Title::makeTitle( $s->page_namespace, $s->page_title );
113
114
					$rev = new Revision( $s );
115
					$content = $rev->getContent();
116
117
					$u = new SearchUpdate( $s->page_id, $title, $content );
0 ignored issues
show
Bug introduced by
It seems like $content defined by $rev->getContent() on line 115 can be null; however, SearchUpdate::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
118
					$u->doUpdate();
119
				} catch ( MWContentSerializationException $ex ) {
120
					$this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
121
						. "`" . $title->getPrefixedDBkey() . "`!\n" );
122
				}
123
			}
124
			$n += self::RTI_CHUNK_SIZE;
125
		}
126
	}
127
128
	/**
129
	 * (MySQL only) Drops fulltext index before populating the table.
130
	 */
131 View Code Duplication
	private function dropMysqlTextIndex() {
132
		$searchindex = $this->db->tableName( 'searchindex' );
133
		if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
134
			$this->output( "Dropping index...\n" );
135
			$sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
136
			$this->db->query( $sql, __METHOD__ );
137
		}
138
	}
139
140
	/**
141
	 * (MySQL only) Adds back fulltext index after populating the table.
142
	 */
143 View Code Duplication
	private function createMysqlTextIndex() {
144
		$searchindex = $this->db->tableName( 'searchindex' );
145
		$this->output( "\nRebuild the index...\n" );
146
		$sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
147
			"ADD FULLTEXT si_text (si_text)";
148
		$this->db->query( $sql, __METHOD__ );
149
	}
150
151
	/**
152
	 * Deletes everything from search index.
153
	 */
154
	private function clearSearchIndex() {
155
		$this->output( 'Clearing searchindex table...' );
156
		$this->db->delete( 'searchindex', '*', __METHOD__ );
157
		$this->output( "Done\n" );
158
	}
159
}
160
161
$maintClass = "RebuildTextIndex";
162
require_once RUN_MAINTENANCE_IF_MAIN;
163