MediaWiki  master
PageArchive.php
Go to the documentation of this file.
1 <?php
27 
31 class PageArchive {
32 
34  protected $title;
35 
37  protected $fileStatus;
38 
40  protected $revisionStatus;
41 
45  public function __construct( Title $title ) {
46  $this->title = $title;
47  }
48 
57  public static function listPagesBySearch( $term ) {
58  $title = Title::newFromText( $term );
59  if ( $title ) {
60  $ns = $title->getNamespace();
61  $termMain = $title->getText();
62  $termDb = $title->getDBkey();
63  } else {
64  // Prolly won't work too good
65  // @todo handle bare namespace names cleanly?
66  $ns = 0;
67  $termMain = $termDb = $term;
68  }
69 
70  // Try search engine first
71  $engine = MediaWikiServices::getInstance()->newSearchEngine();
72  $engine->setLimitOffset( 100 );
73  $engine->setNamespaces( [ $ns ] );
74  $results = $engine->searchArchiveTitle( $termMain );
75  if ( !$results->isOK() ) {
76  $results = [];
77  } else {
78  $results = $results->getValue();
79  }
80 
81  if ( !$results ) {
82  // Fall back to regular prefix search
83  return self::listPagesByPrefix( $term );
84  }
85 
86  $dbr = wfGetDB( DB_REPLICA );
87  $condTitles = array_unique( array_map( static function ( Title $t ) {
88  return $t->getDBkey();
89  }, $results ) );
90  $conds = [
91  'ar_namespace' => $ns,
92  $dbr->makeList( [
93  'ar_title' => $condTitles,
94  'ar_title' . $dbr->buildLike( $termDb, $dbr->anyString() ),
95  ], LIST_OR ),
96  ];
97 
98  return self::listPages( $dbr, $conds );
99  }
100 
109  public static function listPagesByPrefix( $prefix ) {
110  $dbr = wfGetDB( DB_REPLICA );
111 
112  $title = Title::newFromText( $prefix );
113  if ( $title ) {
114  $ns = $title->getNamespace();
115  $prefix = $title->getDBkey();
116  } else {
117  // Prolly won't work too good
118  // @todo handle bare namespace names cleanly?
119  $ns = 0;
120  }
121 
122  $conds = [
123  'ar_namespace' => $ns,
124  'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
125  ];
126 
127  return self::listPages( $dbr, $conds );
128  }
129 
135  protected static function listPages( $dbr, $condition ) {
136  return $dbr->select(
137  [ 'archive' ],
138  [
139  'ar_namespace',
140  'ar_title',
141  'count' => 'COUNT(*)'
142  ],
143  $condition,
144  __METHOD__,
145  [
146  'GROUP BY' => [ 'ar_namespace', 'ar_title' ],
147  'ORDER BY' => [ 'ar_namespace', 'ar_title' ],
148  'LIMIT' => 100,
149  ]
150  );
151  }
152 
160  public function listRevisions() {
161  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
162  return $lookup->listRevisions( $this->title );
163  }
164 
173  public function listFiles() {
174  if ( $this->title->getNamespace() !== NS_FILE ) {
175  return null;
176  }
177 
178  $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
179  $dbr = $loadBalancer->getConnectionRef( DB_REPLICA );
180  $fileQuery = ArchivedFile::getQueryInfo();
181  return $dbr->select(
182  $fileQuery['tables'],
183  $fileQuery['fields'],
184  [ 'fa_name' => $this->title->getDBkey() ],
185  __METHOD__,
186  [ 'ORDER BY' => 'fa_timestamp DESC' ],
187  $fileQuery['joins']
188  );
189  }
190 
200  public function getRevisionRecordByTimestamp( $timestamp ) {
201  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
202  return $lookup->getRevisionRecordByTimestamp( $this->title, $timestamp );
203  }
204 
214  public function getArchivedRevisionRecord( int $revId ) {
215  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
216  return $lookup->getArchivedRevisionRecord( $this->title, $revId );
217  }
218 
232  public function getPreviousRevisionRecord( string $timestamp ) {
233  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
234  return $lookup->getPreviousRevisionRecord( $this->title, $timestamp );
235  }
236 
243  public function getLastRevisionId() {
244  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
245  return $lookup->getLastRevisionId( $this->title );
246  }
247 
255  public function isDeleted() {
256  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
257  return $lookup->hasArchivedRevisions( $this->title );
258  }
259 
282  public function undeleteAsUser(
283  $timestamps,
284  UserIdentity $user,
285  $comment = '',
286  $fileVersions = [],
287  $unsuppress = false,
288  $tags = null
289  ) {
290  $services = MediaWikiServices::getInstance();
291  $page = $services->getWikiPageFactory()->newFromTitle( $this->title );
292  $user = $services->getUserFactory()->newFromUserIdentity( $user );
293  $up = $services->getUndeletePageFactory()->newUndeletePage( $page, $user );
294  if ( is_string( $tags ) ) {
295  $tags = [ $tags ];
296  } elseif ( $tags === null ) {
297  $tags = [];
298  }
299  $status = $up
300  ->setUndeleteOnlyTimestamps( $timestamps )
301  ->setUndeleteOnlyFileVersions( $fileVersions ?: [] )
302  ->setUnsuppress( $unsuppress )
303  ->setTags( $tags ?: [] )
304  ->undeleteUnsafe( $comment );
305  // BC with old return format
306  if ( $status->isGood() ) {
307  $restoredRevs = $status->getValue()[UndeletePage::REVISIONS_RESTORED];
308  $restoredFiles = $status->getValue()[UndeletePage::FILES_RESTORED];
309  if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
310  $ret = false;
311  } else {
312  $ret = [ $restoredRevs, $restoredFiles, $comment ];
313  }
314  } else {
315  $ret = false;
316  }
317  $this->fileStatus = $up->getFileStatus();
318  $this->revisionStatus = $up->getRevisionStatus();
319  return $ret;
320  }
321 
326  public function getFileStatus() {
327  wfDeprecated( __METHOD__, '1.38' );
328  return $this->fileStatus;
329  }
330 
335  public function getRevisionStatus() {
336  wfDeprecated( __METHOD__, '1.38' );
337  return $this->revisionStatus;
338  }
339 }
const NS_FILE
Definition: Defines.php:70
const LIST_OR
Definition: Defines.php:46
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object.
Service locator for MediaWiki core services.
Backend logic for performing a page undelete action.
Page revision base class.
Used to show archived pages and eventually restore them.
Definition: PageArchive.php:31
Status null $revisionStatus
Definition: PageArchive.php:40
listFiles()
List the deleted file revisions for this page, if it's a file page.
Title $title
Definition: PageArchive.php:34
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 listPages( $dbr, $condition)
static listPagesBySearch( $term)
List deleted pages recorded in the archive matching the given term, using search engine archive.
Definition: PageArchive.php:57
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:45
Status null $fileStatus
Definition: PageArchive.php:37
listRevisions()
List the revisions of the given page.
static listPagesByPrefix( $prefix)
List deleted pages recorded in the archive table matching the given title prefix.
isDeleted()
Quick check if any archived revisions are present for the page.
Represents a title within MediaWiki.
Definition: Title.php:51
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1068
getDBkey()
Get the main part with underscores.
Definition: Title.php:1059
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:1041
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:372
Interface for objects representing user identity.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
Result wrapper for grabbing data queried from an IDatabase object.
const DB_REPLICA
Definition: defines.php:26