MediaWiki master
PageArchive.php
Go to the documentation of this file.
1<?php
31
36
37 protected Title $title;
38
42 public function __construct( Title $title ) {
43 $this->title = $title;
44 }
45
54 public static function listPagesBySearch( $term ) {
55 $title = Title::newFromText( $term );
56 if ( $title ) {
57 $ns = $title->getNamespace();
58 $termMain = $title->getText();
59 $termDb = $title->getDBkey();
60 } else {
61 // Prolly won't work too good
62 // @todo handle bare namespace names cleanly?
63 $ns = 0;
64 $termMain = $termDb = $term;
65 }
66
67 // Try search engine first
68 $engine = MediaWikiServices::getInstance()->newSearchEngine();
69 $engine->setLimitOffset( 100 );
70 $engine->setNamespaces( [ $ns ] );
71 $results = $engine->searchArchiveTitle( $termMain );
72 if ( !$results->isOK() ) {
73 $results = [];
74 } else {
75 $results = $results->getValue();
76 }
77
78 if ( !$results ) {
79 // Fall back to regular prefix search
80 return self::listPagesByPrefix( $term );
81 }
82
83 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
84 $condTitles = array_values( array_unique( array_map( static function ( Title $t ) {
85 return $t->getDBkey();
86 }, $results ) ) );
87 $conds = [
88 'ar_namespace' => $ns,
89 $dbr->expr( 'ar_title', '=', $condTitles )
90 ->or( 'ar_title', IExpression::LIKE, new LikeValue( $termDb, $dbr->anyString() ) ),
91 ];
92
93 return self::listPages( $dbr, $conds );
94 }
95
104 public static function listPagesByPrefix( $prefix ) {
105 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
106
107 $title = Title::newFromText( $prefix );
108 if ( $title ) {
109 $ns = $title->getNamespace();
110 $prefix = $title->getDBkey();
111 } else {
112 // Prolly won't work too good
113 // @todo handle bare namespace names cleanly?
114 $ns = 0;
115 }
116
117 $conds = [
118 'ar_namespace' => $ns,
119 $dbr->expr( 'ar_title', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ),
120 ];
121
122 return self::listPages( $dbr, $conds );
123 }
124
130 protected static function listPages( IReadableDatabase $dbr, $condition ) {
131 return $dbr->newSelectQueryBuilder()
132 ->select( [ 'ar_namespace', 'ar_title', 'count' => 'COUNT(*)' ] )
133 ->from( 'archive' )
134 ->where( $condition )
135 ->groupBy( [ 'ar_namespace', 'ar_title' ] )
136 ->orderBy( [ 'ar_namespace', 'ar_title' ] )
137 ->limit( 100 )
138 ->caller( __METHOD__ )->fetchResultSet();
139 }
140
149 public function listFiles() {
150 if ( $this->title->getNamespace() !== NS_FILE ) {
151 return null;
152 }
153
154 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
155 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbr );
156 $queryBuilder->where( [ 'fa_name' => $this->title->getDBkey() ] )
157 ->orderBy( 'fa_timestamp', SelectQueryBuilder::SORT_DESC );
158 return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
159 }
160
180 public function undeleteAsUser(
181 $timestamps,
182 UserIdentity $user,
183 $comment = '',
184 $fileVersions = [],
185 $unsuppress = false,
186 $tags = null
187 ) {
188 wfDeprecated( __METHOD__, '1.43' );
189 $services = MediaWikiServices::getInstance();
190 $page = $services->getWikiPageFactory()->newFromTitle( $this->title );
191 $user = $services->getUserFactory()->newFromUserIdentity( $user );
192 $up = $services->getUndeletePageFactory()->newUndeletePage( $page, $user );
193 if ( is_string( $tags ) ) {
194 $tags = [ $tags ];
195 } elseif ( $tags === null ) {
196 $tags = [];
197 }
198 $status = $up
199 ->setUndeleteOnlyTimestamps( $timestamps )
200 ->setUndeleteOnlyFileVersions( $fileVersions ?: [] )
201 ->setUnsuppress( $unsuppress )
202 ->setTags( $tags ?: [] )
203 ->undeleteUnsafe( $comment );
204 // BC with old return format
205 if ( $status->isGood() ) {
206 $restoredRevs = $status->getValue()[UndeletePage::REVISIONS_RESTORED];
207 $restoredFiles = $status->getValue()[UndeletePage::FILES_RESTORED];
208 if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
209 $ret = false;
210 } else {
211 $ret = [ $restoredRevs, $restoredFiles, $comment ];
212 }
213 } else {
214 $ret = false;
215 }
216 return $ret;
217 }
218
219}
const NS_FILE
Definition Defines.php:71
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.
Represents a title within MediaWiki.
Definition Title.php:79
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1045
getDBkey()
Get the main part with underscores.
Definition Title.php:1036
getText()
Get the text form (spaces not underscores) of the main part.
Definition Title.php:1018
Used to show archived pages and eventually restore them.
listFiles()
List the deleted file revisions for this page, if it's a file page.
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.
__construct(Title $title)
static listPagesByPrefix( $prefix)
List deleted pages recorded in the archive table matching the given title prefix.
static listPages(IReadableDatabase $dbr, $condition)
Content of like value.
Definition LikeValue.php:14
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.