1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
use MediaWiki\FileRepo\File\FileSelectQueryBuilder;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\UndeletePage;
use MediaWiki\Title\Title;
use MediaWiki\User\UserIdentity;
use Wikimedia\Rdbms\IExpression;
use Wikimedia\Rdbms\IReadableDatabase;
use Wikimedia\Rdbms\IResultWrapper;
use Wikimedia\Rdbms\LikeValue;
use Wikimedia\Rdbms\SelectQueryBuilder;
/**
* Used to show archived pages and eventually restore them.
*/
class PageArchive {
protected Title $title;
/**
* @param Title $title
*/
public function __construct( Title $title ) {
$this->title = $title;
}
/**
* List deleted pages recorded in the archive matching the
* given term, using search engine archive.
* Returns result wrapper with (ar_namespace, ar_title, count) fields.
*
* @param string $term Search term
* @return IResultWrapper|bool
*/
public static function listPagesBySearch( $term ) {
$title = Title::newFromText( $term );
if ( $title ) {
$ns = $title->getNamespace();
$termMain = $title->getText();
$termDb = $title->getDBkey();
} else {
// Prolly won't work too good
// @todo handle bare namespace names cleanly?
$ns = 0;
$termMain = $termDb = $term;
}
// Try search engine first
$engine = MediaWikiServices::getInstance()->newSearchEngine();
$engine->setLimitOffset( 100 );
$engine->setNamespaces( [ $ns ] );
$results = $engine->searchArchiveTitle( $termMain );
if ( !$results->isOK() ) {
$results = [];
} else {
$results = $results->getValue();
}
if ( !$results ) {
// Fall back to regular prefix search
return self::listPagesByPrefix( $term );
}
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
$condTitles = array_values( array_unique( array_map( static function ( Title $t ) {
return $t->getDBkey();
}, $results ) ) );
$conds = [
'ar_namespace' => $ns,
$dbr->expr( 'ar_title', '=', $condTitles )
->or( 'ar_title', IExpression::LIKE, new LikeValue( $termDb, $dbr->anyString() ) ),
];
return self::listPages( $dbr, $conds );
}
/**
* List deleted pages recorded in the archive table matching the
* given title prefix.
* Returns result wrapper with (ar_namespace, ar_title, count) fields.
*
* @param string $prefix Title prefix
* @return IResultWrapper|bool
*/
public static function listPagesByPrefix( $prefix ) {
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
$title = Title::newFromText( $prefix );
if ( $title ) {
$ns = $title->getNamespace();
$prefix = $title->getDBkey();
} else {
// Prolly won't work too good
// @todo handle bare namespace names cleanly?
$ns = 0;
}
$conds = [
'ar_namespace' => $ns,
$dbr->expr( 'ar_title', IExpression::LIKE, new LikeValue( $prefix, $dbr->anyString() ) ),
];
return self::listPages( $dbr, $conds );
}
/**
* @param IReadableDatabase $dbr
* @param string|array $condition
* @return IResultWrapper
*/
protected static function listPages( IReadableDatabase $dbr, $condition ) {
return $dbr->newSelectQueryBuilder()
->select( [ 'ar_namespace', 'ar_title', 'count' => 'COUNT(*)' ] )
->from( 'archive' )
->where( $condition )
->groupBy( [ 'ar_namespace', 'ar_title' ] )
->orderBy( [ 'ar_namespace', 'ar_title' ] )
->limit( 100 )
->caller( __METHOD__ )->fetchResultSet();
}
/**
* List the deleted file revisions for this page, if it's a file page.
* Returns a result wrapper with various filearchive fields, or null
* if not a file page.
*
* @return IResultWrapper|null
* @todo Does this belong in Image for fuller encapsulation?
*/
public function listFiles() {
if ( $this->title->getNamespace() !== NS_FILE ) {
return null;
}
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
$queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbr );
$queryBuilder->where( [ 'fa_name' => $this->title->getDBkey() ] )
->orderBy( 'fa_timestamp', SelectQueryBuilder::SORT_DESC );
return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
}
/**
* Restore the given (or all) text and file revisions for the page.
* Once restored, the items will be removed from the archive tables.
* The deletion log will be updated with an undeletion notice.
*
* @since 1.35
* @deprecated since 1.38, use UndeletePage instead, hard-deprecated since 1.43
*
* @param array $timestamps Pass an empty array to restore all revisions,
* otherwise list the ones to undelete.
* @param UserIdentity $user
* @param string $comment
* @param array $fileVersions
* @param bool $unsuppress
* @param string|string[]|null $tags Change tags to add to log entry
* ($user should be able to add the specified tags before this is called)
* @return array|false [ number of file revisions restored, number of image revisions
* restored, log message ] on success, false on failure.
*/
public function undeleteAsUser(
$timestamps,
UserIdentity $user,
$comment = '',
$fileVersions = [],
$unsuppress = false,
$tags = null
) {
wfDeprecated( __METHOD__, '1.43' );
$services = MediaWikiServices::getInstance();
$page = $services->getWikiPageFactory()->newFromTitle( $this->title );
$user = $services->getUserFactory()->newFromUserIdentity( $user );
$up = $services->getUndeletePageFactory()->newUndeletePage( $page, $user );
if ( is_string( $tags ) ) {
$tags = [ $tags ];
} elseif ( $tags === null ) {
$tags = [];
}
$status = $up
->setUndeleteOnlyTimestamps( $timestamps )
->setUndeleteOnlyFileVersions( $fileVersions ?: [] )
->setUnsuppress( $unsuppress )
->setTags( $tags ?: [] )
->undeleteUnsafe( $comment );
// BC with old return format
if ( $status->isGood() ) {
$restoredRevs = $status->getValue()[UndeletePage::REVISIONS_RESTORED];
$restoredFiles = $status->getValue()[UndeletePage::FILES_RESTORED];
if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
$ret = false;
} else {
$ret = [ $restoredRevs, $restoredFiles, $comment ];
}
} else {
$ret = false;
}
return $ret;
}
}
|