Add a message to localize strings like "2×" and use it in enhanced RC
[lhc/web/wiklou.git] / includes / page / PageArchive.php
index 388e693..11e1a30 100644 (file)
@@ -20,6 +20,7 @@
 
 use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\ResultWrapper;
+use Wikimedia\Rdbms\IDatabase;
 
 /**
  * Used to show archived pages and eventually restore them.
@@ -66,6 +67,56 @@ class PageArchive {
                return self::listPages( $dbr, '' );
        }
 
+       /**
+        * List deleted pages recorded in the archive matching the
+        * given term, using search engine archive.
+        * Returns result wrapper with (ar_namespace, ar_title, count) fields.
+        *
+        * @param string $term Search term
+        * @return ResultWrapper
+        */
+       public static function listPagesBySearch( $term ) {
+               $title = Title::newFromText( $term );
+               if ( $title ) {
+                       $ns = $title->getNamespace();
+                       $termMain = $title->getText();
+                       $termDb = $title->getDBkey();
+               } else {
+                       // Prolly won't work too good
+                       // @todo handle bare namespace names cleanly?
+                       $ns = 0;
+                       $termMain = $termDb = $term;
+               }
+
+               // Try search engine first
+               $engine = MediaWikiServices::getInstance()->newSearchEngine();
+               $engine->setLimitOffset( 100 );
+               $engine->setNamespaces( [ $ns ] );
+               $results = $engine->searchArchiveTitle( $termMain );
+               if ( !$results->isOK() ) {
+                       $results = [];
+               } else {
+                       $results = $results->getValue();
+               }
+
+               if ( !$results ) {
+                       // Fall back to regular prefix search
+                       return self::listPagesByPrefix( $term );
+               }
+
+               $dbr = wfGetDB( DB_REPLICA );
+               $condTitles = array_unique( array_map( function ( Title $t ) {
+                       return $t->getDBkey();
+               }, $results ) );
+               $conds = [
+                       'ar_namespace' => $ns,
+                       $dbr->makeList( [ 'ar_title' => $condTitles ], LIST_OR ) . " OR ar_title " .
+                       $dbr->buildLike( $termDb, $dbr->anyString() )
+               ];
+
+               return self::listPages( $dbr, $conds );
+       }
+
        /**
         * List deleted pages recorded in the archive table matching the
         * given title prefix.
@@ -132,6 +183,7 @@ class PageArchive {
                $fields = [
                        'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
                        'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
+                       'ar_page_id'
                ];
 
                if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
@@ -401,25 +453,12 @@ class PageArchive {
 
                // Touch the log!
 
-               if ( $textRestored && $filesRestored ) {
-                       $reason = wfMessage( 'undeletedrevisions-files' )
-                               ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
-               } elseif ( $textRestored ) {
-                       $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
-                               ->inContentLanguage()->text();
-               } elseif ( $filesRestored ) {
-                       $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
-                               ->inContentLanguage()->text();
-               } else {
+               if ( !$textRestored && !$filesRestored ) {
                        wfDebug( "Undelete: nothing undeleted...\n" );
 
                        return false;
                }
 
-               if ( trim( $comment ) != '' ) {
-                       $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
-               }
-
                if ( $user === null ) {
                        global $wgUser;
                        $user = $wgUser;
@@ -428,15 +467,21 @@ class PageArchive {
                $logEntry = new ManualLogEntry( 'delete', 'restore' );
                $logEntry->setPerformer( $user );
                $logEntry->setTarget( $this->title );
-               $logEntry->setComment( $reason );
+               $logEntry->setComment( $comment );
                $logEntry->setTags( $tags );
+               $logEntry->setParameters( [
+                       ':assoc:count' => [
+                               'revisions' => $textRestored,
+                               'files' => $filesRestored,
+                       ],
+               ] );
 
                Hooks::run( 'ArticleUndeleteLogEntry', [ $this, &$logEntry, $user ] );
 
                $logid = $logEntry->insert();
                $logEntry->publish( $logid );
 
-               return [ $textRestored, $filesRestored, $reason ];
+               return [ $textRestored, $filesRestored, $comment ];
        }
 
        /**
@@ -619,7 +664,7 @@ class PageArchive {
                $restored = 0; // number of revisions restored
                /** @var Revision $revision */
                $revision = null;
-
+               $restoredPages = [];
                // If there are no restorable revisions, we can skip most of the steps.
                if ( $latestRestorableRow === null ) {
                        $failedRevisionCount = $rev_count;
@@ -676,6 +721,7 @@ class PageArchive {
 
                                Hooks::run( 'ArticleRevisionUndeleted',
                                        [ &$this->title, $revision, $row->ar_page_id ] );
+                               $restoredPages[$row->ar_page_id] = true;
                        }
 
                        // Now that it's safely stored, take it out of the archive
@@ -716,7 +762,8 @@ class PageArchive {
                                );
                        }
 
-                       Hooks::run( 'ArticleUndelete', [ &$this->title, $created, $comment, $oldPageId ] );
+                       Hooks::run( 'ArticleUndelete',
+                               [ &$this->title, $created, $comment, $oldPageId, $restoredPages ] );
                        if ( $this->title->getNamespace() == NS_FILE ) {
                                DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->title, 'imagelinks' ) );
                        }