| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\FileRepo\File\FileSelectQueryBuilder; |
| 22 |
use MediaWiki\MediaWikiServices; |
| 23 |
use MediaWiki\Page\UndeletePage; |
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use MediaWiki\User\UserIdentity; |
| 26 |
use Wikimedia\Rdbms\IExpression; |
| 27 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 28 |
use Wikimedia\Rdbms\IResultWrapper; |
| 29 |
use Wikimedia\Rdbms\LikeValue; |
| 30 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class PageArchive { |
| 36 |
|
| 37 |
protected Title $title; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function __construct( Title $title ) { |
| 43 |
$this->title = $title; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 62 |
|
| 63 |
$ns = 0; |
| 64 |
$termMain = $termDb = $term; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 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 |
|
| 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 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 113 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 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 |
|
| 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 |
} |