MediaWiki  master
PageArchive.php
Go to the documentation of this file.
1 <?php
31 
35 class PageArchive {
36 
38  protected $title;
39 
41  protected $fileStatus;
42 
44  protected $revisionStatus;
45 
49  public function __construct( Title $title ) {
50  $this->title = $title;
51  }
52 
61  public static function listPagesBySearch( $term ) {
62  $title = Title::newFromText( $term );
63  if ( $title ) {
64  $ns = $title->getNamespace();
65  $termMain = $title->getText();
66  $termDb = $title->getDBkey();
67  } else {
68  // Prolly won't work too good
69  // @todo handle bare namespace names cleanly?
70  $ns = 0;
71  $termMain = $termDb = $term;
72  }
73 
74  // Try search engine first
75  $engine = MediaWikiServices::getInstance()->newSearchEngine();
76  $engine->setLimitOffset( 100 );
77  $engine->setNamespaces( [ $ns ] );
78  $results = $engine->searchArchiveTitle( $termMain );
79  if ( !$results->isOK() ) {
80  $results = [];
81  } else {
82  $results = $results->getValue();
83  }
84 
85  if ( !$results ) {
86  // Fall back to regular prefix search
87  return self::listPagesByPrefix( $term );
88  }
89 
90  $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
91  $condTitles = array_unique( array_map( static function ( Title $t ) {
92  return $t->getDBkey();
93  }, $results ) );
94  $conds = [
95  'ar_namespace' => $ns,
96  $dbr->makeList( [
97  'ar_title' => $condTitles,
98  'ar_title' . $dbr->buildLike( $termDb, $dbr->anyString() ),
99  ], LIST_OR ),
100  ];
101 
102  return self::listPages( $dbr, $conds );
103  }
104 
113  public static function listPagesByPrefix( $prefix ) {
114  $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
115 
116  $title = Title::newFromText( $prefix );
117  if ( $title ) {
118  $ns = $title->getNamespace();
119  $prefix = $title->getDBkey();
120  } else {
121  // Prolly won't work too good
122  // @todo handle bare namespace names cleanly?
123  $ns = 0;
124  }
125 
126  $conds = [
127  'ar_namespace' => $ns,
128  'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
129  ];
130 
131  return self::listPages( $dbr, $conds );
132  }
133 
139  protected static function listPages( IReadableDatabase $dbr, $condition ) {
140  return $dbr->newSelectQueryBuilder()
141  ->select( [ 'ar_namespace', 'ar_title', 'count' => 'COUNT(*)' ] )
142  ->from( 'archive' )
143  ->where( $condition )
144  ->groupBy( [ 'ar_namespace', 'ar_title' ] )
145  ->orderBy( [ 'ar_namespace', 'ar_title' ] )
146  ->limit( 100 )
147  ->caller( __METHOD__ )->fetchResultSet();
148  }
149 
157  public function listRevisions() {
158  wfDeprecated( __METHOD__, '1.38' );
159  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
160  return $lookup->listRevisions( $this->title );
161  }
162 
171  public function listFiles() {
172  if ( $this->title->getNamespace() !== NS_FILE ) {
173  return null;
174  }
175 
176  $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
177  $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbr );
178  $queryBuilder->where( [ 'fa_name' => $this->title->getDBkey() ] )
179  ->orderBy( 'fa_timestamp', SelectQueryBuilder::SORT_DESC );
180  return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
181  }
182 
192  public function getRevisionRecordByTimestamp( $timestamp ) {
193  wfDeprecated( __METHOD__, '1.38' );
194  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
195  return $lookup->getRevisionRecordByTimestamp( $this->title, $timestamp );
196  }
197 
207  public function getArchivedRevisionRecord( int $revId ) {
208  wfDeprecated( __METHOD__, '1.38' );
209  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
210  return $lookup->getArchivedRevisionRecord( $this->title, $revId );
211  }
212 
226  public function getPreviousRevisionRecord( string $timestamp ) {
227  wfDeprecated( __METHOD__, '1.38' );
228  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
229  return $lookup->getPreviousRevisionRecord( $this->title, $timestamp );
230  }
231 
238  public function getLastRevisionId() {
239  wfDeprecated( __METHOD__, '1.38' );
240  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
241  return $lookup->getLastRevisionId( $this->title );
242  }
243 
251  public function isDeleted() {
252  wfDeprecated( __METHOD__, '1.38' );
253  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
254  return $lookup->hasArchivedRevisions( $this->title );
255  }
256 
279  public function undeleteAsUser(
280  $timestamps,
281  UserIdentity $user,
282  $comment = '',
283  $fileVersions = [],
284  $unsuppress = false,
285  $tags = null
286  ) {
287  $services = MediaWikiServices::getInstance();
288  $page = $services->getWikiPageFactory()->newFromTitle( $this->title );
289  $user = $services->getUserFactory()->newFromUserIdentity( $user );
290  $up = $services->getUndeletePageFactory()->newUndeletePage( $page, $user );
291  if ( is_string( $tags ) ) {
292  $tags = [ $tags ];
293  } elseif ( $tags === null ) {
294  $tags = [];
295  }
296  $status = $up
297  ->setUndeleteOnlyTimestamps( $timestamps )
298  ->setUndeleteOnlyFileVersions( $fileVersions ?: [] )
299  ->setUnsuppress( $unsuppress )
300  ->setTags( $tags ?: [] )
301  ->undeleteUnsafe( $comment );
302  // BC with old return format
303  if ( $status->isGood() ) {
304  $restoredRevs = $status->getValue()[UndeletePage::REVISIONS_RESTORED];
305  $restoredFiles = $status->getValue()[UndeletePage::FILES_RESTORED];
306  if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
307  $ret = false;
308  } else {
309  $ret = [ $restoredRevs, $restoredFiles, $comment ];
310  }
311  } else {
312  $ret = false;
313  }
314  $this->fileStatus = $up->getFileStatus();
315  $this->revisionStatus = $up->getRevisionStatus();
316  return $ret;
317  }
318 
323  public function getFileStatus() {
324  wfDeprecated( __METHOD__, '1.38' );
325  return $this->fileStatus;
326  }
327 
332  public function getRevisionStatus() {
333  wfDeprecated( __METHOD__, '1.38' );
334  return $this->revisionStatus;
335  }
336 }
const NS_FILE
Definition: Defines.php:70
const LIST_OR
Definition: Defines.php:46
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Service locator for MediaWiki core services.
Backend logic for performing a page undelete action.
Page revision base class.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:58
Represents a title within MediaWiki.
Definition: Title.php:76
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1058
getDBkey()
Get the main part with underscores.
Definition: Title.php:1049
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:1031
Used to show archived pages and eventually restore them.
Definition: PageArchive.php:35
Status null $revisionStatus
Definition: PageArchive.php:44
listFiles()
List the deleted file revisions for this page, if it's a file page.
Title $title
Definition: PageArchive.php:38
getPreviousRevisionRecord(string $timestamp)
Return the most-previous revision, either live or deleted, against the deleted revision given by time...
undeleteAsUser( $timestamps, UserIdentity $user, $comment='', $fileVersions=[], $unsuppress=false, $tags=null)
Restore the given (or all) text and file revisions for the page.
static listPagesBySearch( $term)
List deleted pages recorded in the archive matching the given term, using search engine archive.
Definition: PageArchive.php:61
getArchivedRevisionRecord(int $revId)
Return the archived revision with the given ID.
getRevisionRecordByTimestamp( $timestamp)
Return a RevisionRecord object containing data for the deleted revision.
getLastRevisionId()
Returns the ID of the latest deleted revision.
__construct(Title $title)
Definition: PageArchive.php:49
Status null $fileStatus
Definition: PageArchive.php:41
listRevisions()
List the revisions of the given page.
static listPagesByPrefix( $prefix)
List deleted pages recorded in the archive table matching the given title prefix.
static listPages(IReadableDatabase $dbr, $condition)
isDeleted()
Quick check if any archived revisions are present for the page.
Build SELECT queries with a fluent interface.
Interface for objects representing user identity.
A database connection without write operations.
newSelectQueryBuilder()
Create an empty SelectQueryBuilder which can be used to run queries against this connection.
Result wrapper for grabbing data queried from an IDatabase object.