MediaWiki master
PageArchive.php
Go to the documentation of this file.
1<?php
31
36
38 protected $title;
39
43 public function __construct( Title $title ) {
44 $this->title = $title;
45 }
46
55 public static function listPagesBySearch( $term ) {
56 $title = Title::newFromText( $term );
57 if ( $title ) {
58 $ns = $title->getNamespace();
59 $termMain = $title->getText();
60 $termDb = $title->getDBkey();
61 } else {
62 // Prolly won't work too good
63 // @todo handle bare namespace names cleanly?
64 $ns = 0;
65 $termMain = $termDb = $term;
66 }
67
68 // Try search engine first
69 $engine = MediaWikiServices::getInstance()->newSearchEngine();
70 $engine->setLimitOffset( 100 );
71 $engine->setNamespaces( [ $ns ] );
72 $results = $engine->searchArchiveTitle( $termMain );
73 if ( !$results->isOK() ) {
74 $results = [];
75 } else {
76 $results = $results->getValue();
77 }
78
79 if ( !$results ) {
80 // Fall back to regular prefix search
81 return self::listPagesByPrefix( $term );
82 }
83
84 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
85 $condTitles = array_values( array_unique( array_map( static function ( Title $t ) {
86 return $t->getDBkey();
87 }, $results ) ) );
88 $conds = [
89 'ar_namespace' => $ns,
90 $dbr->expr( 'ar_title', '=', $condTitles )
91 ->or( 'ar_title', IExpression::LIKE, new LikeValue( $termDb, $dbr->anyString() ) ),
92 ];
93
94 return self::listPages( $dbr, $conds );
95 }
96
105 public static function listPagesByPrefix( $prefix ) {
106 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
107
108 $title = Title::newFromText( $prefix );
109 if ( $title ) {
110 $ns = $title->getNamespace();
111 $prefix = $title->getDBkey();
112 } else {
113 // Prolly won't work too good
114 // @todo handle bare namespace names cleanly?
115 $ns = 0;
116 }
117
118 $conds = [
119 'ar_namespace' => $ns,
120 $dbr->expr( 'ar_title', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ),
121 ];
122
123 return self::listPages( $dbr, $conds );
124 }
125
131 protected static function listPages( IReadableDatabase $dbr, $condition ) {
132 return $dbr->newSelectQueryBuilder()
133 ->select( [ 'ar_namespace', 'ar_title', 'count' => 'COUNT(*)' ] )
134 ->from( 'archive' )
135 ->where( $condition )
136 ->groupBy( [ 'ar_namespace', 'ar_title' ] )
137 ->orderBy( [ 'ar_namespace', 'ar_title' ] )
138 ->limit( 100 )
139 ->caller( __METHOD__ )->fetchResultSet();
140 }
141
150 public function listFiles() {
151 if ( $this->title->getNamespace() !== NS_FILE ) {
152 return null;
153 }
154
155 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
156 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbr );
157 $queryBuilder->where( [ 'fa_name' => $this->title->getDBkey() ] )
158 ->orderBy( 'fa_timestamp', SelectQueryBuilder::SORT_DESC );
159 return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
160 }
161
181 public function undeleteAsUser(
182 $timestamps,
183 UserIdentity $user,
184 $comment = '',
185 $fileVersions = [],
186 $unsuppress = false,
187 $tags = null
188 ) {
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:70
Service locator for MediaWiki core services.
Backend logic for performing a page undelete action.
Represents a title within MediaWiki.
Definition Title.php:78
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1044
getDBkey()
Get the main part with underscores.
Definition Title.php:1035
getText()
Get the text form (spaces not underscores) of the main part.
Definition Title.php:1017
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.