MediaWiki REL1_40
PageArchive.php
Go to the documentation of this file.
1<?php
28
33
35 protected $title;
36
38 protected $fileStatus;
39
41 protected $revisionStatus;
42
46 public function __construct( Title $title ) {
47 $this->title = $title;
48 }
49
58 public static function listPagesBySearch( $term ) {
59 $title = Title::newFromText( $term );
60 if ( $title ) {
61 $ns = $title->getNamespace();
62 $termMain = $title->getText();
63 $termDb = $title->getDBkey();
64 } else {
65 // Prolly won't work too good
66 // @todo handle bare namespace names cleanly?
67 $ns = 0;
68 $termMain = $termDb = $term;
69 }
70
71 // Try search engine first
72 $engine = MediaWikiServices::getInstance()->newSearchEngine();
73 $engine->setLimitOffset( 100 );
74 $engine->setNamespaces( [ $ns ] );
75 $results = $engine->searchArchiveTitle( $termMain );
76 if ( !$results->isOK() ) {
77 $results = [];
78 } else {
79 $results = $results->getValue();
80 }
81
82 if ( !$results ) {
83 // Fall back to regular prefix search
84 return self::listPagesByPrefix( $term );
85 }
86
88 $condTitles = array_unique( array_map( static function ( Title $t ) {
89 return $t->getDBkey();
90 }, $results ) );
91 $conds = [
92 'ar_namespace' => $ns,
93 $dbr->makeList( [
94 'ar_title' => $condTitles,
95 'ar_title' . $dbr->buildLike( $termDb, $dbr->anyString() ),
96 ], LIST_OR ),
97 ];
98
99 return self::listPages( $dbr, $conds );
100 }
101
110 public static function listPagesByPrefix( $prefix ) {
112
113 $title = Title::newFromText( $prefix );
114 if ( $title ) {
115 $ns = $title->getNamespace();
116 $prefix = $title->getDBkey();
117 } else {
118 // Prolly won't work too good
119 // @todo handle bare namespace names cleanly?
120 $ns = 0;
121 }
122
123 $conds = [
124 'ar_namespace' => $ns,
125 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
126 ];
127
128 return self::listPages( $dbr, $conds );
129 }
130
136 protected static function listPages( $dbr, $condition ) {
137 return $dbr->select(
138 [ 'archive' ],
139 [
140 'ar_namespace',
141 'ar_title',
142 'count' => 'COUNT(*)'
143 ],
144 $condition,
145 __METHOD__,
146 [
147 'GROUP BY' => [ 'ar_namespace', 'ar_title' ],
148 'ORDER BY' => [ 'ar_namespace', 'ar_title' ],
149 'LIMIT' => 100,
150 ]
151 );
152 }
153
161 public function listRevisions() {
162 $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
163 return $lookup->listRevisions( $this->title );
164 }
165
174 public function listFiles() {
175 if ( $this->title->getNamespace() !== NS_FILE ) {
176 return null;
177 }
178
179 $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
180 $dbr = $loadBalancer->getConnectionRef( DB_REPLICA );
181 $fileQuery = ArchivedFile::getQueryInfo();
182 return $dbr->select(
183 $fileQuery['tables'],
184 $fileQuery['fields'],
185 [ 'fa_name' => $this->title->getDBkey() ],
186 __METHOD__,
187 [ 'ORDER BY' => 'fa_timestamp DESC' ],
188 $fileQuery['joins']
189 );
190 }
191
201 public function getRevisionRecordByTimestamp( $timestamp ) {
202 $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
203 return $lookup->getRevisionRecordByTimestamp( $this->title, $timestamp );
204 }
205
215 public function getArchivedRevisionRecord( int $revId ) {
216 $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
217 return $lookup->getArchivedRevisionRecord( $this->title, $revId );
218 }
219
233 public function getPreviousRevisionRecord( string $timestamp ) {
234 $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
235 return $lookup->getPreviousRevisionRecord( $this->title, $timestamp );
236 }
237
244 public function getLastRevisionId() {
245 $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
246 return $lookup->getLastRevisionId( $this->title );
247 }
248
256 public function isDeleted() {
257 $lookup = MediaWikiServices::getInstance()->getArchivedRevisionLookup();
258 return $lookup->hasArchivedRevisions( $this->title );
259 }
260
283 public function undeleteAsUser(
284 $timestamps,
285 UserIdentity $user,
286 $comment = '',
287 $fileVersions = [],
288 $unsuppress = false,
289 $tags = null
290 ) {
291 $services = MediaWikiServices::getInstance();
292 $page = $services->getWikiPageFactory()->newFromTitle( $this->title );
293 $user = $services->getUserFactory()->newFromUserIdentity( $user );
294 $up = $services->getUndeletePageFactory()->newUndeletePage( $page, $user );
295 if ( is_string( $tags ) ) {
296 $tags = [ $tags ];
297 } elseif ( $tags === null ) {
298 $tags = [];
299 }
300 $status = $up
301 ->setUndeleteOnlyTimestamps( $timestamps )
302 ->setUndeleteOnlyFileVersions( $fileVersions ?: [] )
303 ->setUnsuppress( $unsuppress )
304 ->setTags( $tags ?: [] )
305 ->undeleteUnsafe( $comment );
306 // BC with old return format
307 if ( $status->isGood() ) {
308 $restoredRevs = $status->getValue()[UndeletePage::REVISIONS_RESTORED];
309 $restoredFiles = $status->getValue()[UndeletePage::FILES_RESTORED];
310 if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
311 $ret = false;
312 } else {
313 $ret = [ $restoredRevs, $restoredFiles, $comment ];
314 }
315 } else {
316 $ret = false;
317 }
318 $this->fileStatus = $up->getFileStatus();
319 $this->revisionStatus = $up->getRevisionStatus();
320 return $ret;
321 }
322
327 public function getFileStatus() {
328 wfDeprecated( __METHOD__, '1.38' );
329 return $this->fileStatus;
330 }
331
336 public function getRevisionStatus() {
337 wfDeprecated( __METHOD__, '1.38' );
338 return $this->revisionStatus;
339 }
340}
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.
Represents a title within MediaWiki.
Definition Title.php:82
Used to show archived pages and eventually restore them.
Status null $revisionStatus
listFiles()
List the deleted file revisions for this page, if it's a file page.
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.
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)
Status null $fileStatus
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.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:46
Interface for objects representing user identity.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
Result wrapper for grabbing data queried from an IDatabase object.
const DB_REPLICA
Definition defines.php:26