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( [ 'ar_title' => $condTitles ], LIST_OR ) . " OR ar_title " .
93  $dbr->buildLike( $termDb, $dbr->anyString() )
94  ];
95 
96  return self::listPages( $dbr, $conds );
97  }
98 
107  public static function listPagesByPrefix( $prefix ) {
108  $dbr = wfGetDB( DB_REPLICA );
109 
110  $title = Title::newFromText( $prefix );
111  if ( $title ) {
112  $ns = $title->getNamespace();
113  $prefix = $title->getDBkey();
114  } else {
115  // Prolly won't work too good
116  // @todo handle bare namespace names cleanly?
117  $ns = 0;
118  }
119 
120  $conds = [
121  'ar_namespace' => $ns,
122  'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
123  ];
124 
125  return self::listPages( $dbr, $conds );
126  }
127 
133  protected static function listPages( $dbr, $condition ) {
134  return $dbr->select(
135  [ 'archive' ],
136  [
137  'ar_namespace',
138  'ar_title',
139  'count' => 'COUNT(*)'
140  ],
141  $condition,
142  __METHOD__,
143  [
144  'GROUP BY' => [ 'ar_namespace', 'ar_title' ],
145  'ORDER BY' => [ 'ar_namespace', 'ar_title' ],
146  'LIMIT' => 100,
147  ]
148  );
149  }
150 
158  public function listRevisions() {
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  $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
177  $dbr = $loadBalancer->getConnectionRef( DB_REPLICA );
178  $fileQuery = ArchivedFile::getQueryInfo();
179  return $dbr->select(
180  $fileQuery['tables'],
181  $fileQuery['fields'],
182  [ 'fa_name' => $this->title->getDBkey() ],
183  __METHOD__,
184  [ 'ORDER BY' => 'fa_timestamp DESC' ],
185  $fileQuery['joins']
186  );
187  }
188 
198  public function getRevisionRecordByTimestamp( $timestamp ) {
199  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
200  return $lookup->getRevisionRecordByTimestamp( $this->title, $timestamp );
201  }
202 
212  public function getArchivedRevisionRecord( int $revId ) {
213  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
214  return $lookup->getArchivedRevisionRecord( $this->title, $revId );
215  }
216 
230  public function getPreviousRevisionRecord( string $timestamp ) {
231  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
232  return $lookup->getPreviousRevisionRecord( $this->title, $timestamp );
233  }
234 
241  public function getLastRevisionId() {
242  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
243  return $lookup->getLastRevisionId( $this->title );
244  }
245 
253  public function isDeleted() {
254  $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
255  return $lookup->hasArchivedRevisions( $this->title );
256  }
257 
280  public function undeleteAsUser(
281  $timestamps,
282  UserIdentity $user,
283  $comment = '',
284  $fileVersions = [],
285  $unsuppress = false,
286  $tags = null
287  ) {
288  $services = MediaWikiServices::getInstance();
289  $page = $services->getWikiPageFactory()->newFromTitle( $this->title );
290  $user = $services->getUserFactory()->newFromUserIdentity( $user );
291  $up = $services->getUndeletePageFactory()->newUndeletePage( $page, $user );
292  if ( is_string( $tags ) ) {
293  $tags = [ $tags ];
294  } elseif ( $tags === null ) {
295  $tags = [];
296  }
297  $status = $up
298  ->setUndeleteOnlyTimestamps( $timestamps )
299  ->setUndeleteOnlyFileVersions( $fileVersions ?: [] )
300  ->setUnsuppress( $unsuppress )
301  ->setTags( $tags ?: [] )
302  ->undeleteUnsafe( $comment );
303  // BC with old return format
304  if ( $status->isGood() ) {
305  $restoredRevs = $status->getValue()[UndeletePage::REVISIONS_RESTORED];
306  $restoredFiles = $status->getValue()[UndeletePage::FILES_RESTORED];
307  if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
308  $ret = false;
309  } else {
310  $ret = [ $restoredRevs, $restoredFiles, $comment ];
311  }
312  } else {
313  $ret = false;
314  }
315  $this->fileStatus = $up->getFileStatus();
316  $this->revisionStatus = $up->getRevisionStatus();
317  return $ret;
318  }
319 
324  public function getFileStatus() {
325  wfDeprecated( __METHOD__, '1.38' );
326  return $this->fileStatus;
327  }
328 
333  public function getRevisionStatus() {
334  wfDeprecated( __METHOD__, '1.38' );
335  return $this->revisionStatus;
336  }
337 }
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:49
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1066
getDBkey()
Get the main part with underscores.
Definition: Title.php:1057
getText()
Get the text form (spaces not underscores) of the main part.
Definition: Title.php:1039
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:370
Interface for objects representing user identity.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:39
Result wrapper for grabbing data queried from an IDatabase object.
const DB_REPLICA
Definition: defines.php:26