+ /**
+ * Check and repair the destination fields in a link table
+ * @param string $table The link table name
+ * @param string $fieldPrefix The field prefix in the link table
+ * @param int $ns Destination namespace id
+ * @param string $name
+ * @param array $options Associative array of validated command-line options
+ * @param array $extraConds Extra conditions for the SQL query
+ */
+ private function checkLinkTable( $table, $fieldPrefix, $ns, $name, $options,
+ $extraConds = array()
+ ) {
+ $batchConds = array();
+ $fromField = "{$fieldPrefix}_from";
+ $namespaceField = "{$fieldPrefix}_namespace";
+ $titleField = "{$fieldPrefix}_title";
+ $batchSize = 500;
+ while ( true ) {
+ $res = $this->db->select(
+ $table,
+ array( $fromField, $namespaceField, $titleField ),
+ array_merge( $batchConds, $extraConds, array(
+ $namespaceField => 0,
+ $titleField . $this->db->buildLike( "$name:", $this->db->anyString() )
+ ) ),
+ __METHOD__,
+ array(
+ 'ORDER BY' => array( $titleField, $fromField ),
+ 'LIMIT' => $batchSize
+ )
+ );
+
+ if ( $res->numRows() == 0 ) {
+ break;
+ }
+ foreach ( $res as $row ) {
+ $logTitle = "from={$row->$fromField} ns={$row->$namespaceField} " .
+ "dbk={$row->$titleField}";
+ $destTitle = $this->getDestinationTitle( $ns, $name,
+ $row->$namespaceField, $row->$titleField, $options );
+ $this->totalLinks++;
+ if ( !$destTitle ) {
+ $this->output( "$table $logTitle *** INVALID\n" );
+ continue;
+ }
+ $this->resolvableLinks++;
+ if ( !$options['fix'] ) {
+ $this->output( "$table $logTitle -> " .
+ $destTitle->getPrefixedDBkey() . " DRY RUN\n" );
+ continue;
+ }
+
+ $this->db->update( $table,
+ // SET
+ array(
+ $namespaceField => $destTitle->getNamespace(),
+ $titleField => $destTitle->getDBkey()
+ ),
+ // WHERE
+ array(
+ $namespaceField => 0,
+ $titleField => $row->$titleField,
+ $fromField => $row->$fromField
+ ),
+ __METHOD__
+ );
+ $this->output( "$table $logTitle -> " .
+ $destTitle->getPrefixedDBkey() . "\n" );
+ }
+ $encLastTitle = $this->db->addQuotes( $row->$titleField );
+ $encLastFrom = $this->db->addQuotes( $row->$fromField );
+
+ $batchConds = array(
+ "$titleField > $encLastTitle " .
+ "OR ($titleField = $encLastTitle AND $fromField > $encLastFrom)" );
+
+ wfWaitForSlaves();
+ }
+ }
+