+ /**
+ * Do the actual link refreshing.
+ * @param $start int Page_id to start from
+ * @param $newOnly bool Only do pages with 1 edit
+ * @param $maxLag int Max DB replication lag
+ * @param $end int Page_id to stop at
+ * @param $redirectsOnly bool Only fix redirects
+ * @param $oldRedirectsOnly bool Only fix redirects without redirect entries
+ */
+ private function doRefreshLinks( $start, $newOnly = false, $maxLag = false,
+ $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
+ global $wgUser, $wgParser, $wgUseTidy;
+
+ $reportingInterval = 100;
+ $dbr = wfGetDB( DB_SLAVE );
+ $start = intval( $start );
+
+ # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
+ $wgUser->setOption('math', MW_MATH_SOURCE);
+
+ # Don't generate extension images (e.g. Timeline)
+ if( method_exists( $wgParser, "clearTagHooks" ) ) {
+ $wgParser->clearTagHooks();
+ }
+
+ # Don't use HTML tidy
+ $wgUseTidy = false;
+
+ $what = $redirectsOnly ? "redirects" : "links";
+
+ if( $oldRedirectsOnly ) {
+ # This entire code path is cut-and-pasted from below. Hurrah.
+ $res = $dbr->query(
+ "SELECT page_id ".
+ "FROM page ".
+ "LEFT JOIN redirect ON page_id=rd_from ".
+ "WHERE page_is_redirect=1 AND rd_from IS NULL AND ".
+ ($end == 0 ? "page_id >= $start"
+ : "page_id BETWEEN $start AND $end"),
+ __METHOD__
+ );
+ $num = $dbr->numRows( $res );
+ $this->output( "Refreshing $num old redirects from $start...\n" );
+
+ foreach( $res as $row ) {
+ if ( !( ++$i % $reportingInterval ) ) {
+ $this->output( "$i\n" );
+ wfWaitForSlaves( $maxLag );
+ }
+ $this->fixRedirect( $row->page_id );
+ }
+ } elseif( $newOnly ) {
+ $this->output( "Refreshing $what from " );
+ $res = $dbr->select( 'page',
+ array( 'page_id' ),
+ array(
+ 'page_is_new' => 1,
+ "page_id >= $start" ),
+ __METHOD__
+ );
+ $num = $dbr->numRows( $res );
+ $this->output( "$num new articles...\n" );
+
+ $i = 0;
+ foreach ( $res as $row ) {
+ if ( !( ++$i % $reportingInterval ) ) {
+ $this->output( "$i\n" );
+ wfWaitForSlaves( $maxLag );
+ }
+ if($redirectsOnly)
+ $this->fixRedirect( $row->page_id );
+ else
+ $this->fixLinksFromArticle( $row->page_id );
+ }
+ } else {
+ if ( !$end ) {
+ $end = $dbr->selectField( 'page', 'max(page_id)', false );
+ }
+ $this->output( "Refreshing redirects table.\n" );
+ $this->output( "Starting from page_id $start of $end.\n" );
+
+ for ($id = $start; $id <= $end; $id++) {
+
+ if ( !($id % $reportingInterval) ) {
+ $this->output( "$id\n" );
+ wfWaitForSlaves( $maxLag );
+ }
+ $this->fixRedirect( $id );
+ }
+
+ if(!$redirectsOnly) {
+ $this->output( "Refreshing links table.\n" );
+ $this->output( "Starting from page_id $start of $end.\n" );
+
+ for ($id = $start; $id <= $end; $id++) {
+
+ if ( !($id % $reportingInterval) ) {
+ $this->output( "$id\n" );
+ wfWaitForSlaves( $maxLag );
+ }
+ $this->fixLinksFromArticle( $id );
+ }
+ }
+ }
+ }
+
+ /**
+ * Update the redirect entry for a given page
+ * @param $id int The page_id of the redirect
+ */
+ private function fixRedirect( $id ){
+ global $wgTitle, $wgArticle;
+
+ $wgTitle = Title::newFromID( $id );
+ $dbw = wfGetDB( DB_MASTER );
+
+ if ( is_null( $wgTitle ) ) {
+ return;
+ }
+ $wgArticle = new Article($wgTitle);
+
+ $rt = $wgArticle->followRedirect();
+
+ if($rt == false || !is_object($rt))
+ return;
+
+ $wgArticle->updateRedirectOn($dbw,$rt);
+ }
+
+ /**
+ * Run LinksUpdate for all links on a given page_id
+ * @param $id int The page_id
+ */
+ private function fixLinksFromArticle( $id ) {
+ global $wgTitle, $wgParser;