LocalFile.php 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515
  1. <?php
  2. /**
  3. * Local file in the wiki's own database.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup FileAbstraction
  22. */
  23. use MediaWiki\Logger\LoggerFactory;
  24. use Wikimedia\Rdbms\Database;
  25. use Wikimedia\Rdbms\IDatabase;
  26. use MediaWiki\MediaWikiServices;
  27. /**
  28. * Class to represent a local file in the wiki's own database
  29. *
  30. * Provides methods to retrieve paths (physical, logical, URL),
  31. * to generate image thumbnails or for uploading.
  32. *
  33. * Note that only the repo object knows what its file class is called. You should
  34. * never name a file class explictly outside of the repo class. Instead use the
  35. * repo's factory functions to generate file objects, for example:
  36. *
  37. * RepoGroup::singleton()->getLocalRepo()->newFile( $title );
  38. *
  39. * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
  40. * in most cases.
  41. *
  42. * @ingroup FileAbstraction
  43. */
  44. class LocalFile extends File {
  45. const VERSION = 11; // cache version
  46. const CACHE_FIELD_MAX_LEN = 1000;
  47. /** @var bool Does the file exist on disk? (loadFromXxx) */
  48. protected $fileExists;
  49. /** @var int Image width */
  50. protected $width;
  51. /** @var int Image height */
  52. protected $height;
  53. /** @var int Returned by getimagesize (loadFromXxx) */
  54. protected $bits;
  55. /** @var string MEDIATYPE_xxx (bitmap, drawing, audio...) */
  56. protected $media_type;
  57. /** @var string MIME type, determined by MimeAnalyzer::guessMimeType */
  58. protected $mime;
  59. /** @var int Size in bytes (loadFromXxx) */
  60. protected $size;
  61. /** @var string Handler-specific metadata */
  62. protected $metadata;
  63. /** @var string SHA-1 base 36 content hash */
  64. protected $sha1;
  65. /** @var bool Whether or not core data has been loaded from the database (loadFromXxx) */
  66. protected $dataLoaded;
  67. /** @var bool Whether or not lazy-loaded data has been loaded from the database */
  68. protected $extraDataLoaded;
  69. /** @var int Bitfield akin to rev_deleted */
  70. protected $deleted;
  71. /** @var string */
  72. protected $repoClass = LocalRepo::class;
  73. /** @var int Number of line to return by nextHistoryLine() (constructor) */
  74. private $historyLine;
  75. /** @var int Result of the query for the file's history (nextHistoryLine) */
  76. private $historyRes;
  77. /** @var string Major MIME type */
  78. private $major_mime;
  79. /** @var string Minor MIME type */
  80. private $minor_mime;
  81. /** @var string Upload timestamp */
  82. private $timestamp;
  83. /** @var User Uploader */
  84. private $user;
  85. /** @var string Description of current revision of the file */
  86. private $description;
  87. /** @var string TS_MW timestamp of the last change of the file description */
  88. private $descriptionTouched;
  89. /** @var bool Whether the row was upgraded on load */
  90. private $upgraded;
  91. /** @var bool Whether the row was scheduled to upgrade on load */
  92. private $upgrading;
  93. /** @var bool True if the image row is locked */
  94. private $locked;
  95. /** @var bool True if the image row is locked with a lock initiated transaction */
  96. private $lockedOwnTrx;
  97. /** @var bool True if file is not present in file system. Not to be cached in memcached */
  98. private $missing;
  99. // @note: higher than IDBAccessObject constants
  100. const LOAD_ALL = 16; // integer; load all the lazy fields too (like metadata)
  101. const ATOMIC_SECTION_LOCK = 'LocalFile::lockingTransaction';
  102. /**
  103. * Create a LocalFile from a title
  104. * Do not call this except from inside a repo class.
  105. *
  106. * Note: $unused param is only here to avoid an E_STRICT
  107. *
  108. * @param Title $title
  109. * @param FileRepo $repo
  110. * @param null $unused
  111. *
  112. * @return self
  113. */
  114. static function newFromTitle( $title, $repo, $unused = null ) {
  115. return new self( $title, $repo );
  116. }
  117. /**
  118. * Create a LocalFile from a title
  119. * Do not call this except from inside a repo class.
  120. *
  121. * @param stdClass $row
  122. * @param FileRepo $repo
  123. *
  124. * @return self
  125. */
  126. static function newFromRow( $row, $repo ) {
  127. $title = Title::makeTitle( NS_FILE, $row->img_name );
  128. $file = new self( $title, $repo );
  129. $file->loadFromRow( $row );
  130. return $file;
  131. }
  132. /**
  133. * Create a LocalFile from a SHA-1 key
  134. * Do not call this except from inside a repo class.
  135. *
  136. * @param string $sha1 Base-36 SHA-1
  137. * @param LocalRepo $repo
  138. * @param string|bool $timestamp MW_timestamp (optional)
  139. * @return bool|LocalFile
  140. */
  141. static function newFromKey( $sha1, $repo, $timestamp = false ) {
  142. $dbr = $repo->getReplicaDB();
  143. $conds = [ 'img_sha1' => $sha1 ];
  144. if ( $timestamp ) {
  145. $conds['img_timestamp'] = $dbr->timestamp( $timestamp );
  146. }
  147. $fileQuery = self::getQueryInfo();
  148. $row = $dbr->selectRow(
  149. $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
  150. );
  151. if ( $row ) {
  152. return self::newFromRow( $row, $repo );
  153. } else {
  154. return false;
  155. }
  156. }
  157. /**
  158. * Fields in the image table
  159. * @deprecated since 1.31, use self::getQueryInfo() instead.
  160. * @return string[]
  161. */
  162. static function selectFields() {
  163. global $wgActorTableSchemaMigrationStage;
  164. wfDeprecated( __METHOD__, '1.31' );
  165. if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
  166. // If code is using this instead of self::getQueryInfo(), there's a
  167. // decent chance it's going to try to directly access
  168. // $row->img_user or $row->img_user_text and we can't give it
  169. // useful values here once those aren't being written anymore.
  170. throw new BadMethodCallException(
  171. 'Cannot use ' . __METHOD__ . ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
  172. );
  173. }
  174. return [
  175. 'img_name',
  176. 'img_size',
  177. 'img_width',
  178. 'img_height',
  179. 'img_metadata',
  180. 'img_bits',
  181. 'img_media_type',
  182. 'img_major_mime',
  183. 'img_minor_mime',
  184. 'img_user',
  185. 'img_user_text',
  186. 'img_actor' => $wgActorTableSchemaMigrationStage > MIGRATION_OLD ? 'img_actor' : 'NULL',
  187. 'img_timestamp',
  188. 'img_sha1',
  189. ] + MediaWikiServices::getInstance()->getCommentStore()->getFields( 'img_description' );
  190. }
  191. /**
  192. * Return the tables, fields, and join conditions to be selected to create
  193. * a new localfile object.
  194. * @since 1.31
  195. * @param string[] $options
  196. * - omit-lazy: Omit fields that are lazily cached.
  197. * @return array[] With three keys:
  198. * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
  199. * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
  200. * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
  201. */
  202. public static function getQueryInfo( array $options = [] ) {
  203. $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'img_description' );
  204. $actorQuery = ActorMigration::newMigration()->getJoin( 'img_user' );
  205. $ret = [
  206. 'tables' => [ 'image' ] + $commentQuery['tables'] + $actorQuery['tables'],
  207. 'fields' => [
  208. 'img_name',
  209. 'img_size',
  210. 'img_width',
  211. 'img_height',
  212. 'img_metadata',
  213. 'img_bits',
  214. 'img_media_type',
  215. 'img_major_mime',
  216. 'img_minor_mime',
  217. 'img_timestamp',
  218. 'img_sha1',
  219. ] + $commentQuery['fields'] + $actorQuery['fields'],
  220. 'joins' => $commentQuery['joins'] + $actorQuery['joins'],
  221. ];
  222. if ( in_array( 'omit-nonlazy', $options, true ) ) {
  223. // Internal use only for getting only the lazy fields
  224. $ret['fields'] = [];
  225. }
  226. if ( !in_array( 'omit-lazy', $options, true ) ) {
  227. // Note: Keep this in sync with self::getLazyCacheFields()
  228. $ret['fields'][] = 'img_metadata';
  229. }
  230. return $ret;
  231. }
  232. /**
  233. * Do not call this except from inside a repo class.
  234. * @param Title $title
  235. * @param FileRepo $repo
  236. */
  237. function __construct( $title, $repo ) {
  238. parent::__construct( $title, $repo );
  239. $this->metadata = '';
  240. $this->historyLine = 0;
  241. $this->historyRes = null;
  242. $this->dataLoaded = false;
  243. $this->extraDataLoaded = false;
  244. $this->assertRepoDefined();
  245. $this->assertTitleDefined();
  246. }
  247. /**
  248. * Get the memcached key for the main data for this file, or false if
  249. * there is no access to the shared cache.
  250. * @return string|bool
  251. */
  252. function getCacheKey() {
  253. return $this->repo->getSharedCacheKey( 'file', sha1( $this->getName() ) );
  254. }
  255. /**
  256. * @param WANObjectCache $cache
  257. * @return string[]
  258. * @since 1.28
  259. */
  260. public function getMutableCacheKeys( WANObjectCache $cache ) {
  261. return [ $this->getCacheKey() ];
  262. }
  263. /**
  264. * Try to load file metadata from memcached, falling back to the database
  265. */
  266. private function loadFromCache() {
  267. $this->dataLoaded = false;
  268. $this->extraDataLoaded = false;
  269. $key = $this->getCacheKey();
  270. if ( !$key ) {
  271. $this->loadFromDB( self::READ_NORMAL );
  272. return;
  273. }
  274. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  275. $cachedValues = $cache->getWithSetCallback(
  276. $key,
  277. $cache::TTL_WEEK,
  278. function ( $oldValue, &$ttl, array &$setOpts ) use ( $cache ) {
  279. $setOpts += Database::getCacheSetOptions( $this->repo->getReplicaDB() );
  280. $this->loadFromDB( self::READ_NORMAL );
  281. $fields = $this->getCacheFields( '' );
  282. $cacheVal['fileExists'] = $this->fileExists;
  283. if ( $this->fileExists ) {
  284. foreach ( $fields as $field ) {
  285. $cacheVal[$field] = $this->$field;
  286. }
  287. }
  288. $cacheVal['user'] = $this->user ? $this->user->getId() : 0;
  289. $cacheVal['user_text'] = $this->user ? $this->user->getName() : '';
  290. $cacheVal['actor'] = $this->user ? $this->user->getActorId() : null;
  291. // Strip off excessive entries from the subset of fields that can become large.
  292. // If the cache value gets to large it will not fit in memcached and nothing will
  293. // get cached at all, causing master queries for any file access.
  294. foreach ( $this->getLazyCacheFields( '' ) as $field ) {
  295. if ( isset( $cacheVal[$field] )
  296. && strlen( $cacheVal[$field] ) > 100 * 1024
  297. ) {
  298. unset( $cacheVal[$field] ); // don't let the value get too big
  299. }
  300. }
  301. if ( $this->fileExists ) {
  302. $ttl = $cache->adaptiveTTL( wfTimestamp( TS_UNIX, $this->timestamp ), $ttl );
  303. } else {
  304. $ttl = $cache::TTL_DAY;
  305. }
  306. return $cacheVal;
  307. },
  308. [ 'version' => self::VERSION ]
  309. );
  310. $this->fileExists = $cachedValues['fileExists'];
  311. if ( $this->fileExists ) {
  312. $this->setProps( $cachedValues );
  313. }
  314. $this->dataLoaded = true;
  315. $this->extraDataLoaded = true;
  316. foreach ( $this->getLazyCacheFields( '' ) as $field ) {
  317. $this->extraDataLoaded = $this->extraDataLoaded && isset( $cachedValues[$field] );
  318. }
  319. }
  320. /**
  321. * Purge the file object/metadata cache
  322. */
  323. public function invalidateCache() {
  324. $key = $this->getCacheKey();
  325. if ( !$key ) {
  326. return;
  327. }
  328. $this->repo->getMasterDB()->onTransactionPreCommitOrIdle(
  329. function () use ( $key ) {
  330. MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
  331. },
  332. __METHOD__
  333. );
  334. }
  335. /**
  336. * Load metadata from the file itself
  337. */
  338. function loadFromFile() {
  339. $props = $this->repo->getFileProps( $this->getVirtualUrl() );
  340. $this->setProps( $props );
  341. }
  342. /**
  343. * Returns the list of object properties that are included as-is in the cache.
  344. * @param string $prefix Must be the empty string
  345. * @return string[]
  346. * @since 1.31 No longer accepts a non-empty $prefix
  347. */
  348. protected function getCacheFields( $prefix = 'img_' ) {
  349. if ( $prefix !== '' ) {
  350. throw new InvalidArgumentException(
  351. __METHOD__ . ' with a non-empty prefix is no longer supported.'
  352. );
  353. }
  354. // See self::getQueryInfo() for the fetching of the data from the DB,
  355. // self::loadFromRow() for the loading of the object from the DB row,
  356. // and self::loadFromCache() for the caching, and self::setProps() for
  357. // populating the object from an array of data.
  358. return [ 'size', 'width', 'height', 'bits', 'media_type',
  359. 'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'description' ];
  360. }
  361. /**
  362. * Returns the list of object properties that are included as-is in the
  363. * cache, only when they're not too big, and are lazily loaded by self::loadExtraFromDB().
  364. * @param string $prefix Must be the empty string
  365. * @return string[]
  366. * @since 1.31 No longer accepts a non-empty $prefix
  367. */
  368. protected function getLazyCacheFields( $prefix = 'img_' ) {
  369. if ( $prefix !== '' ) {
  370. throw new InvalidArgumentException(
  371. __METHOD__ . ' with a non-empty prefix is no longer supported.'
  372. );
  373. }
  374. // Keep this in sync with the omit-lazy option in self::getQueryInfo().
  375. return [ 'metadata' ];
  376. }
  377. /**
  378. * Load file metadata from the DB
  379. * @param int $flags
  380. */
  381. function loadFromDB( $flags = 0 ) {
  382. $fname = static::class . '::' . __FUNCTION__;
  383. # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
  384. $this->dataLoaded = true;
  385. $this->extraDataLoaded = true;
  386. $dbr = ( $flags & self::READ_LATEST )
  387. ? $this->repo->getMasterDB()
  388. : $this->repo->getReplicaDB();
  389. $fileQuery = static::getQueryInfo();
  390. $row = $dbr->selectRow(
  391. $fileQuery['tables'],
  392. $fileQuery['fields'],
  393. [ 'img_name' => $this->getName() ],
  394. $fname,
  395. [],
  396. $fileQuery['joins']
  397. );
  398. if ( $row ) {
  399. $this->loadFromRow( $row );
  400. } else {
  401. $this->fileExists = false;
  402. }
  403. }
  404. /**
  405. * Load lazy file metadata from the DB.
  406. * This covers fields that are sometimes not cached.
  407. */
  408. protected function loadExtraFromDB() {
  409. $fname = static::class . '::' . __FUNCTION__;
  410. # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
  411. $this->extraDataLoaded = true;
  412. $fieldMap = $this->loadExtraFieldsWithTimestamp( $this->repo->getReplicaDB(), $fname );
  413. if ( !$fieldMap ) {
  414. $fieldMap = $this->loadExtraFieldsWithTimestamp( $this->repo->getMasterDB(), $fname );
  415. }
  416. if ( $fieldMap ) {
  417. foreach ( $fieldMap as $name => $value ) {
  418. $this->$name = $value;
  419. }
  420. } else {
  421. throw new MWException( "Could not find data for image '{$this->getName()}'." );
  422. }
  423. }
  424. /**
  425. * @param IDatabase $dbr
  426. * @param string $fname
  427. * @return string[]|bool
  428. */
  429. private function loadExtraFieldsWithTimestamp( $dbr, $fname ) {
  430. $fieldMap = false;
  431. $fileQuery = self::getQueryInfo( [ 'omit-nonlazy' ] );
  432. $row = $dbr->selectRow(
  433. $fileQuery['tables'],
  434. $fileQuery['fields'],
  435. [
  436. 'img_name' => $this->getName(),
  437. 'img_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
  438. ],
  439. $fname,
  440. [],
  441. $fileQuery['joins']
  442. );
  443. if ( $row ) {
  444. $fieldMap = $this->unprefixRow( $row, 'img_' );
  445. } else {
  446. # File may have been uploaded over in the meantime; check the old versions
  447. $fileQuery = OldLocalFile::getQueryInfo( [ 'omit-nonlazy' ] );
  448. $row = $dbr->selectRow(
  449. $fileQuery['tables'],
  450. $fileQuery['fields'],
  451. [
  452. 'oi_name' => $this->getName(),
  453. 'oi_timestamp' => $dbr->timestamp( $this->getTimestamp() ),
  454. ],
  455. $fname,
  456. [],
  457. $fileQuery['joins']
  458. );
  459. if ( $row ) {
  460. $fieldMap = $this->unprefixRow( $row, 'oi_' );
  461. }
  462. }
  463. if ( isset( $fieldMap['metadata'] ) ) {
  464. $fieldMap['metadata'] = $this->repo->getReplicaDB()->decodeBlob( $fieldMap['metadata'] );
  465. }
  466. return $fieldMap;
  467. }
  468. /**
  469. * @param array|object $row
  470. * @param string $prefix
  471. * @throws MWException
  472. * @return array
  473. */
  474. protected function unprefixRow( $row, $prefix = 'img_' ) {
  475. $array = (array)$row;
  476. $prefixLength = strlen( $prefix );
  477. // Sanity check prefix once
  478. if ( substr( key( $array ), 0, $prefixLength ) !== $prefix ) {
  479. throw new MWException( __METHOD__ . ': incorrect $prefix parameter' );
  480. }
  481. $decoded = [];
  482. foreach ( $array as $name => $value ) {
  483. $decoded[substr( $name, $prefixLength )] = $value;
  484. }
  485. return $decoded;
  486. }
  487. /**
  488. * Decode a row from the database (either object or array) to an array
  489. * with timestamps and MIME types decoded, and the field prefix removed.
  490. * @param object $row
  491. * @param string $prefix
  492. * @throws MWException
  493. * @return array
  494. */
  495. function decodeRow( $row, $prefix = 'img_' ) {
  496. $decoded = $this->unprefixRow( $row, $prefix );
  497. $decoded['description'] = MediaWikiServices::getInstance()->getCommentStore()
  498. ->getComment( 'description', (object)$decoded )->text;
  499. $decoded['user'] = User::newFromAnyId(
  500. $decoded['user'] ?? null,
  501. $decoded['user_text'] ?? null,
  502. $decoded['actor'] ?? null
  503. );
  504. unset( $decoded['user_text'], $decoded['actor'] );
  505. $decoded['timestamp'] = wfTimestamp( TS_MW, $decoded['timestamp'] );
  506. $decoded['metadata'] = $this->repo->getReplicaDB()->decodeBlob( $decoded['metadata'] );
  507. if ( empty( $decoded['major_mime'] ) ) {
  508. $decoded['mime'] = 'unknown/unknown';
  509. } else {
  510. if ( !$decoded['minor_mime'] ) {
  511. $decoded['minor_mime'] = 'unknown';
  512. }
  513. $decoded['mime'] = $decoded['major_mime'] . '/' . $decoded['minor_mime'];
  514. }
  515. // Trim zero padding from char/binary field
  516. $decoded['sha1'] = rtrim( $decoded['sha1'], "\0" );
  517. // Normalize some fields to integer type, per their database definition.
  518. // Use unary + so that overflows will be upgraded to double instead of
  519. // being trucated as with intval(). This is important to allow >2GB
  520. // files on 32-bit systems.
  521. foreach ( [ 'size', 'width', 'height', 'bits' ] as $field ) {
  522. $decoded[$field] = +$decoded[$field];
  523. }
  524. return $decoded;
  525. }
  526. /**
  527. * Load file metadata from a DB result row
  528. *
  529. * @param object $row
  530. * @param string $prefix
  531. */
  532. function loadFromRow( $row, $prefix = 'img_' ) {
  533. $this->dataLoaded = true;
  534. $this->extraDataLoaded = true;
  535. $array = $this->decodeRow( $row, $prefix );
  536. foreach ( $array as $name => $value ) {
  537. $this->$name = $value;
  538. }
  539. $this->fileExists = true;
  540. $this->maybeUpgradeRow();
  541. }
  542. /**
  543. * Load file metadata from cache or DB, unless already loaded
  544. * @param int $flags
  545. */
  546. function load( $flags = 0 ) {
  547. if ( !$this->dataLoaded ) {
  548. if ( $flags & self::READ_LATEST ) {
  549. $this->loadFromDB( $flags );
  550. } else {
  551. $this->loadFromCache();
  552. }
  553. }
  554. if ( ( $flags & self::LOAD_ALL ) && !$this->extraDataLoaded ) {
  555. // @note: loads on name/timestamp to reduce race condition problems
  556. $this->loadExtraFromDB();
  557. }
  558. }
  559. /**
  560. * Upgrade a row if it needs it
  561. */
  562. function maybeUpgradeRow() {
  563. global $wgUpdateCompatibleMetadata;
  564. if ( wfReadOnly() || $this->upgrading ) {
  565. return;
  566. }
  567. $upgrade = false;
  568. if ( is_null( $this->media_type ) || $this->mime == 'image/svg' ) {
  569. $upgrade = true;
  570. } else {
  571. $handler = $this->getHandler();
  572. if ( $handler ) {
  573. $validity = $handler->isMetadataValid( $this, $this->getMetadata() );
  574. if ( $validity === MediaHandler::METADATA_BAD ) {
  575. $upgrade = true;
  576. } elseif ( $validity === MediaHandler::METADATA_COMPATIBLE ) {
  577. $upgrade = $wgUpdateCompatibleMetadata;
  578. }
  579. }
  580. }
  581. if ( $upgrade ) {
  582. $this->upgrading = true;
  583. // Defer updates unless in auto-commit CLI mode
  584. DeferredUpdates::addCallableUpdate( function () {
  585. $this->upgrading = false; // avoid duplicate updates
  586. try {
  587. $this->upgradeRow();
  588. } catch ( LocalFileLockError $e ) {
  589. // let the other process handle it (or do it next time)
  590. }
  591. } );
  592. }
  593. }
  594. /**
  595. * @return bool Whether upgradeRow() ran for this object
  596. */
  597. function getUpgraded() {
  598. return $this->upgraded;
  599. }
  600. /**
  601. * Fix assorted version-related problems with the image row by reloading it from the file
  602. */
  603. function upgradeRow() {
  604. $this->lock(); // begin
  605. $this->loadFromFile();
  606. # Don't destroy file info of missing files
  607. if ( !$this->fileExists ) {
  608. $this->unlock();
  609. wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
  610. return;
  611. }
  612. $dbw = $this->repo->getMasterDB();
  613. list( $major, $minor ) = self::splitMime( $this->mime );
  614. if ( wfReadOnly() ) {
  615. $this->unlock();
  616. return;
  617. }
  618. wfDebug( __METHOD__ . ': upgrading ' . $this->getName() . " to the current schema\n" );
  619. $dbw->update( 'image',
  620. [
  621. 'img_size' => $this->size, // sanity
  622. 'img_width' => $this->width,
  623. 'img_height' => $this->height,
  624. 'img_bits' => $this->bits,
  625. 'img_media_type' => $this->media_type,
  626. 'img_major_mime' => $major,
  627. 'img_minor_mime' => $minor,
  628. 'img_metadata' => $dbw->encodeBlob( $this->metadata ),
  629. 'img_sha1' => $this->sha1,
  630. ],
  631. [ 'img_name' => $this->getName() ],
  632. __METHOD__
  633. );
  634. $this->invalidateCache();
  635. $this->unlock(); // done
  636. $this->upgraded = true; // avoid rework/retries
  637. }
  638. /**
  639. * Set properties in this object to be equal to those given in the
  640. * associative array $info. Only cacheable fields can be set.
  641. * All fields *must* be set in $info except for getLazyCacheFields().
  642. *
  643. * If 'mime' is given, it will be split into major_mime/minor_mime.
  644. * If major_mime/minor_mime are given, $this->mime will also be set.
  645. *
  646. * @param array $info
  647. */
  648. function setProps( $info ) {
  649. $this->dataLoaded = true;
  650. $fields = $this->getCacheFields( '' );
  651. $fields[] = 'fileExists';
  652. foreach ( $fields as $field ) {
  653. if ( isset( $info[$field] ) ) {
  654. $this->$field = $info[$field];
  655. }
  656. }
  657. if ( isset( $info['user'] ) || isset( $info['user_text'] ) || isset( $info['actor'] ) ) {
  658. $this->user = User::newFromAnyId(
  659. $info['user'] ?? null,
  660. $info['user_text'] ?? null,
  661. $info['actor'] ?? null
  662. );
  663. }
  664. // Fix up mime fields
  665. if ( isset( $info['major_mime'] ) ) {
  666. $this->mime = "{$info['major_mime']}/{$info['minor_mime']}";
  667. } elseif ( isset( $info['mime'] ) ) {
  668. $this->mime = $info['mime'];
  669. list( $this->major_mime, $this->minor_mime ) = self::splitMime( $this->mime );
  670. }
  671. }
  672. /** splitMime inherited */
  673. /** getName inherited */
  674. /** getTitle inherited */
  675. /** getURL inherited */
  676. /** getViewURL inherited */
  677. /** getPath inherited */
  678. /** isVisible inherited */
  679. /**
  680. * @return bool
  681. */
  682. function isMissing() {
  683. if ( $this->missing === null ) {
  684. list( $fileExists ) = $this->repo->fileExists( $this->getVirtualUrl() );
  685. $this->missing = !$fileExists;
  686. }
  687. return $this->missing;
  688. }
  689. /**
  690. * Return the width of the image
  691. *
  692. * @param int $page
  693. * @return int
  694. */
  695. public function getWidth( $page = 1 ) {
  696. $page = (int)$page;
  697. if ( $page < 1 ) {
  698. $page = 1;
  699. }
  700. $this->load();
  701. if ( $this->isMultipage() ) {
  702. $handler = $this->getHandler();
  703. if ( !$handler ) {
  704. return 0;
  705. }
  706. $dim = $handler->getPageDimensions( $this, $page );
  707. if ( $dim ) {
  708. return $dim['width'];
  709. } else {
  710. // For non-paged media, the false goes through an
  711. // intval, turning failure into 0, so do same here.
  712. return 0;
  713. }
  714. } else {
  715. return $this->width;
  716. }
  717. }
  718. /**
  719. * Return the height of the image
  720. *
  721. * @param int $page
  722. * @return int
  723. */
  724. public function getHeight( $page = 1 ) {
  725. $page = (int)$page;
  726. if ( $page < 1 ) {
  727. $page = 1;
  728. }
  729. $this->load();
  730. if ( $this->isMultipage() ) {
  731. $handler = $this->getHandler();
  732. if ( !$handler ) {
  733. return 0;
  734. }
  735. $dim = $handler->getPageDimensions( $this, $page );
  736. if ( $dim ) {
  737. return $dim['height'];
  738. } else {
  739. // For non-paged media, the false goes through an
  740. // intval, turning failure into 0, so do same here.
  741. return 0;
  742. }
  743. } else {
  744. return $this->height;
  745. }
  746. }
  747. /**
  748. * Returns user who uploaded the file
  749. *
  750. * @param string $type 'text', 'id', or 'object'
  751. * @return int|string|User
  752. * @since 1.31 Added 'object'
  753. */
  754. function getUser( $type = 'text' ) {
  755. $this->load();
  756. if ( $type === 'object' ) {
  757. return $this->user;
  758. } elseif ( $type === 'text' ) {
  759. return $this->user->getName();
  760. } elseif ( $type === 'id' ) {
  761. return $this->user->getId();
  762. }
  763. throw new MWException( "Unknown type '$type'." );
  764. }
  765. /**
  766. * Get short description URL for a file based on the page ID.
  767. *
  768. * @return string|null
  769. * @throws MWException
  770. * @since 1.27
  771. */
  772. public function getDescriptionShortUrl() {
  773. $pageId = $this->title->getArticleID();
  774. if ( $pageId !== null ) {
  775. $url = $this->repo->makeUrl( [ 'curid' => $pageId ] );
  776. if ( $url !== false ) {
  777. return $url;
  778. }
  779. }
  780. return null;
  781. }
  782. /**
  783. * Get handler-specific metadata
  784. * @return string
  785. */
  786. function getMetadata() {
  787. $this->load( self::LOAD_ALL ); // large metadata is loaded in another step
  788. return $this->metadata;
  789. }
  790. /**
  791. * @return int
  792. */
  793. function getBitDepth() {
  794. $this->load();
  795. return (int)$this->bits;
  796. }
  797. /**
  798. * Returns the size of the image file, in bytes
  799. * @return int
  800. */
  801. public function getSize() {
  802. $this->load();
  803. return $this->size;
  804. }
  805. /**
  806. * Returns the MIME type of the file.
  807. * @return string
  808. */
  809. function getMimeType() {
  810. $this->load();
  811. return $this->mime;
  812. }
  813. /**
  814. * Returns the type of the media in the file.
  815. * Use the value returned by this function with the MEDIATYPE_xxx constants.
  816. * @return string
  817. */
  818. function getMediaType() {
  819. $this->load();
  820. return $this->media_type;
  821. }
  822. /** canRender inherited */
  823. /** mustRender inherited */
  824. /** allowInlineDisplay inherited */
  825. /** isSafeFile inherited */
  826. /** isTrustedFile inherited */
  827. /**
  828. * Returns true if the file exists on disk.
  829. * @return bool Whether file exist on disk.
  830. */
  831. public function exists() {
  832. $this->load();
  833. return $this->fileExists;
  834. }
  835. /** getTransformScript inherited */
  836. /** getUnscaledThumb inherited */
  837. /** thumbName inherited */
  838. /** createThumb inherited */
  839. /** transform inherited */
  840. /** getHandler inherited */
  841. /** iconThumb inherited */
  842. /** getLastError inherited */
  843. /**
  844. * Get all thumbnail names previously generated for this file
  845. * @param string|bool $archiveName Name of an archive file, default false
  846. * @return array First element is the base dir, then files in that base dir.
  847. */
  848. function getThumbnails( $archiveName = false ) {
  849. if ( $archiveName ) {
  850. $dir = $this->getArchiveThumbPath( $archiveName );
  851. } else {
  852. $dir = $this->getThumbPath();
  853. }
  854. $backend = $this->repo->getBackend();
  855. $files = [ $dir ];
  856. try {
  857. $iterator = $backend->getFileList( [ 'dir' => $dir ] );
  858. foreach ( $iterator as $file ) {
  859. $files[] = $file;
  860. }
  861. } catch ( FileBackendError $e ) {
  862. } // suppress (T56674)
  863. return $files;
  864. }
  865. /**
  866. * Refresh metadata in memcached, but don't touch thumbnails or CDN
  867. */
  868. function purgeMetadataCache() {
  869. $this->invalidateCache();
  870. }
  871. /**
  872. * Delete all previously generated thumbnails, refresh metadata in memcached and purge the CDN.
  873. *
  874. * @param array $options An array potentially with the key forThumbRefresh.
  875. *
  876. * @note This used to purge old thumbnails by default as well, but doesn't anymore.
  877. */
  878. function purgeCache( $options = [] ) {
  879. // Refresh metadata cache
  880. $this->purgeMetadataCache();
  881. // Delete thumbnails
  882. $this->purgeThumbnails( $options );
  883. // Purge CDN cache for this file
  884. DeferredUpdates::addUpdate(
  885. new CdnCacheUpdate( [ $this->getUrl() ] ),
  886. DeferredUpdates::PRESEND
  887. );
  888. }
  889. /**
  890. * Delete cached transformed files for an archived version only.
  891. * @param string $archiveName Name of the archived file
  892. */
  893. function purgeOldThumbnails( $archiveName ) {
  894. // Get a list of old thumbnails and URLs
  895. $files = $this->getThumbnails( $archiveName );
  896. // Purge any custom thumbnail caches
  897. Hooks::run( 'LocalFilePurgeThumbnails', [ $this, $archiveName ] );
  898. // Delete thumbnails
  899. $dir = array_shift( $files );
  900. $this->purgeThumbList( $dir, $files );
  901. // Purge the CDN
  902. $urls = [];
  903. foreach ( $files as $file ) {
  904. $urls[] = $this->getArchiveThumbUrl( $archiveName, $file );
  905. }
  906. DeferredUpdates::addUpdate( new CdnCacheUpdate( $urls ), DeferredUpdates::PRESEND );
  907. }
  908. /**
  909. * Delete cached transformed files for the current version only.
  910. * @param array $options
  911. */
  912. public function purgeThumbnails( $options = [] ) {
  913. $files = $this->getThumbnails();
  914. // Always purge all files from CDN regardless of handler filters
  915. $urls = [];
  916. foreach ( $files as $file ) {
  917. $urls[] = $this->getThumbUrl( $file );
  918. }
  919. array_shift( $urls ); // don't purge directory
  920. // Give media handler a chance to filter the file purge list
  921. if ( !empty( $options['forThumbRefresh'] ) ) {
  922. $handler = $this->getHandler();
  923. if ( $handler ) {
  924. $handler->filterThumbnailPurgeList( $files, $options );
  925. }
  926. }
  927. // Purge any custom thumbnail caches
  928. Hooks::run( 'LocalFilePurgeThumbnails', [ $this, false ] );
  929. // Delete thumbnails
  930. $dir = array_shift( $files );
  931. $this->purgeThumbList( $dir, $files );
  932. // Purge the CDN
  933. DeferredUpdates::addUpdate( new CdnCacheUpdate( $urls ), DeferredUpdates::PRESEND );
  934. }
  935. /**
  936. * Prerenders a configurable set of thumbnails
  937. *
  938. * @since 1.28
  939. */
  940. public function prerenderThumbnails() {
  941. global $wgUploadThumbnailRenderMap;
  942. $jobs = [];
  943. $sizes = $wgUploadThumbnailRenderMap;
  944. rsort( $sizes );
  945. foreach ( $sizes as $size ) {
  946. if ( $this->isVectorized() || $this->getWidth() > $size ) {
  947. $jobs[] = new ThumbnailRenderJob(
  948. $this->getTitle(),
  949. [ 'transformParams' => [ 'width' => $size ] ]
  950. );
  951. }
  952. }
  953. if ( $jobs ) {
  954. JobQueueGroup::singleton()->lazyPush( $jobs );
  955. }
  956. }
  957. /**
  958. * Delete a list of thumbnails visible at urls
  959. * @param string $dir Base dir of the files.
  960. * @param array $files Array of strings: relative filenames (to $dir)
  961. */
  962. protected function purgeThumbList( $dir, $files ) {
  963. $fileListDebug = strtr(
  964. var_export( $files, true ),
  965. [ "\n" => '' ]
  966. );
  967. wfDebug( __METHOD__ . ": $fileListDebug\n" );
  968. $purgeList = [];
  969. foreach ( $files as $file ) {
  970. if ( $this->repo->supportsSha1URLs() ) {
  971. $reference = $this->getSha1();
  972. } else {
  973. $reference = $this->getName();
  974. }
  975. # Check that the reference (filename or sha1) is part of the thumb name
  976. # This is a basic sanity check to avoid erasing unrelated directories
  977. if ( strpos( $file, $reference ) !== false
  978. || strpos( $file, "-thumbnail" ) !== false // "short" thumb name
  979. ) {
  980. $purgeList[] = "{$dir}/{$file}";
  981. }
  982. }
  983. # Delete the thumbnails
  984. $this->repo->quickPurgeBatch( $purgeList );
  985. # Clear out the thumbnail directory if empty
  986. $this->repo->quickCleanDir( $dir );
  987. }
  988. /** purgeDescription inherited */
  989. /** purgeEverything inherited */
  990. /**
  991. * @param int|null $limit Optional: Limit to number of results
  992. * @param string|int|null $start Optional: Timestamp, start from
  993. * @param string|int|null $end Optional: Timestamp, end at
  994. * @param bool $inc
  995. * @return OldLocalFile[]
  996. */
  997. function getHistory( $limit = null, $start = null, $end = null, $inc = true ) {
  998. $dbr = $this->repo->getReplicaDB();
  999. $oldFileQuery = OldLocalFile::getQueryInfo();
  1000. $tables = $oldFileQuery['tables'];
  1001. $fields = $oldFileQuery['fields'];
  1002. $join_conds = $oldFileQuery['joins'];
  1003. $conds = $opts = [];
  1004. $eq = $inc ? '=' : '';
  1005. $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBkey() );
  1006. if ( $start ) {
  1007. $conds[] = "oi_timestamp <$eq " . $dbr->addQuotes( $dbr->timestamp( $start ) );
  1008. }
  1009. if ( $end ) {
  1010. $conds[] = "oi_timestamp >$eq " . $dbr->addQuotes( $dbr->timestamp( $end ) );
  1011. }
  1012. if ( $limit ) {
  1013. $opts['LIMIT'] = $limit;
  1014. }
  1015. // Search backwards for time > x queries
  1016. $order = ( !$start && $end !== null ) ? 'ASC' : 'DESC';
  1017. $opts['ORDER BY'] = "oi_timestamp $order";
  1018. $opts['USE INDEX'] = [ 'oldimage' => 'oi_name_timestamp' ];
  1019. // Avoid PHP 7.1 warning from passing $this by reference
  1020. $localFile = $this;
  1021. Hooks::run( 'LocalFile::getHistory', [ &$localFile, &$tables, &$fields,
  1022. &$conds, &$opts, &$join_conds ] );
  1023. $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $opts, $join_conds );
  1024. $r = [];
  1025. foreach ( $res as $row ) {
  1026. $r[] = $this->repo->newFileFromRow( $row );
  1027. }
  1028. if ( $order == 'ASC' ) {
  1029. $r = array_reverse( $r ); // make sure it ends up descending
  1030. }
  1031. return $r;
  1032. }
  1033. /**
  1034. * Returns the history of this file, line by line.
  1035. * starts with current version, then old versions.
  1036. * uses $this->historyLine to check which line to return:
  1037. * 0 return line for current version
  1038. * 1 query for old versions, return first one
  1039. * 2, ... return next old version from above query
  1040. * @return bool
  1041. */
  1042. public function nextHistoryLine() {
  1043. # Polymorphic function name to distinguish foreign and local fetches
  1044. $fname = static::class . '::' . __FUNCTION__;
  1045. $dbr = $this->repo->getReplicaDB();
  1046. if ( $this->historyLine == 0 ) { // called for the first time, return line from cur
  1047. $fileQuery = self::getQueryInfo();
  1048. $this->historyRes = $dbr->select( $fileQuery['tables'],
  1049. $fileQuery['fields'] + [
  1050. 'oi_archive_name' => $dbr->addQuotes( '' ),
  1051. 'oi_deleted' => 0,
  1052. ],
  1053. [ 'img_name' => $this->title->getDBkey() ],
  1054. $fname,
  1055. [],
  1056. $fileQuery['joins']
  1057. );
  1058. if ( 0 == $dbr->numRows( $this->historyRes ) ) {
  1059. $this->historyRes = null;
  1060. return false;
  1061. }
  1062. } elseif ( $this->historyLine == 1 ) {
  1063. $fileQuery = OldLocalFile::getQueryInfo();
  1064. $this->historyRes = $dbr->select(
  1065. $fileQuery['tables'],
  1066. $fileQuery['fields'],
  1067. [ 'oi_name' => $this->title->getDBkey() ],
  1068. $fname,
  1069. [ 'ORDER BY' => 'oi_timestamp DESC' ],
  1070. $fileQuery['joins']
  1071. );
  1072. }
  1073. $this->historyLine++;
  1074. return $dbr->fetchObject( $this->historyRes );
  1075. }
  1076. /**
  1077. * Reset the history pointer to the first element of the history
  1078. */
  1079. public function resetHistory() {
  1080. $this->historyLine = 0;
  1081. if ( !is_null( $this->historyRes ) ) {
  1082. $this->historyRes = null;
  1083. }
  1084. }
  1085. /** getHashPath inherited */
  1086. /** getRel inherited */
  1087. /** getUrlRel inherited */
  1088. /** getArchiveRel inherited */
  1089. /** getArchivePath inherited */
  1090. /** getThumbPath inherited */
  1091. /** getArchiveUrl inherited */
  1092. /** getThumbUrl inherited */
  1093. /** getArchiveVirtualUrl inherited */
  1094. /** getThumbVirtualUrl inherited */
  1095. /** isHashed inherited */
  1096. /**
  1097. * Upload a file and record it in the DB
  1098. * @param string|FSFile $src Source storage path, virtual URL, or filesystem path
  1099. * @param string $comment Upload description
  1100. * @param string $pageText Text to use for the new description page,
  1101. * if a new description page is created
  1102. * @param int|bool $flags Flags for publish()
  1103. * @param array|bool $props File properties, if known. This can be used to
  1104. * reduce the upload time when uploading virtual URLs for which the file
  1105. * info is already known
  1106. * @param string|bool $timestamp Timestamp for img_timestamp, or false to use the
  1107. * current time
  1108. * @param User|null $user User object or null to use $wgUser
  1109. * @param string[] $tags Change tags to add to the log entry and page revision.
  1110. * (This doesn't check $user's permissions.)
  1111. * @param bool $createNullRevision Set to false to avoid creation of a null revision on file
  1112. * upload, see T193621
  1113. * @return Status On success, the value member contains the
  1114. * archive name, or an empty string if it was a new file.
  1115. */
  1116. function upload( $src, $comment, $pageText, $flags = 0, $props = false,
  1117. $timestamp = false, $user = null, $tags = [],
  1118. $createNullRevision = true
  1119. ) {
  1120. if ( $this->getRepo()->getReadOnlyReason() !== false ) {
  1121. return $this->readOnlyFatalStatus();
  1122. } elseif ( MediaWikiServices::getInstance()->getRevisionStore()->isReadOnly() ) {
  1123. // Check this in advance to avoid writing to FileBackend and the file tables,
  1124. // only to fail on insert the revision due to the text store being unavailable.
  1125. return $this->readOnlyFatalStatus();
  1126. }
  1127. $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
  1128. if ( !$props ) {
  1129. if ( $this->repo->isVirtualUrl( $srcPath )
  1130. || FileBackend::isStoragePath( $srcPath )
  1131. ) {
  1132. $props = $this->repo->getFileProps( $srcPath );
  1133. } else {
  1134. $mwProps = new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() );
  1135. $props = $mwProps->getPropsFromPath( $srcPath, true );
  1136. }
  1137. }
  1138. $options = [];
  1139. $handler = MediaHandler::getHandler( $props['mime'] );
  1140. if ( $handler ) {
  1141. $metadata = Wikimedia\quietCall( 'unserialize', $props['metadata'] );
  1142. if ( !is_array( $metadata ) ) {
  1143. $metadata = [];
  1144. }
  1145. $options['headers'] = $handler->getContentHeaders( $metadata );
  1146. } else {
  1147. $options['headers'] = [];
  1148. }
  1149. // Trim spaces on user supplied text
  1150. $comment = trim( $comment );
  1151. $this->lock(); // begin
  1152. $status = $this->publish( $src, $flags, $options );
  1153. if ( $status->successCount >= 2 ) {
  1154. // There will be a copy+(one of move,copy,store).
  1155. // The first succeeding does not commit us to updating the DB
  1156. // since it simply copied the current version to a timestamped file name.
  1157. // It is only *preferable* to avoid leaving such files orphaned.
  1158. // Once the second operation goes through, then the current version was
  1159. // updated and we must therefore update the DB too.
  1160. $oldver = $status->value;
  1161. $uploadStatus = $this->recordUpload2(
  1162. $oldver,
  1163. $comment,
  1164. $pageText,
  1165. $props,
  1166. $timestamp,
  1167. $user,
  1168. $tags,
  1169. $createNullRevision
  1170. );
  1171. if ( !$uploadStatus->isOK() ) {
  1172. if ( $uploadStatus->hasMessage( 'filenotfound' ) ) {
  1173. // update filenotfound error with more specific path
  1174. $status->fatal( 'filenotfound', $srcPath );
  1175. } else {
  1176. $status->merge( $uploadStatus );
  1177. }
  1178. }
  1179. }
  1180. $this->unlock(); // done
  1181. return $status;
  1182. }
  1183. /**
  1184. * Record a file upload in the upload log and the image table
  1185. * @param string $oldver
  1186. * @param string $desc
  1187. * @param string $license
  1188. * @param string $copyStatus
  1189. * @param string $source
  1190. * @param bool $watch
  1191. * @param string|bool $timestamp
  1192. * @param User|null $user User object or null to use $wgUser
  1193. * @return bool
  1194. */
  1195. function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
  1196. $watch = false, $timestamp = false, User $user = null ) {
  1197. if ( !$user ) {
  1198. global $wgUser;
  1199. $user = $wgUser;
  1200. }
  1201. $pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source );
  1202. if ( !$this->recordUpload2( $oldver, $desc, $pageText, false, $timestamp, $user )->isOK() ) {
  1203. return false;
  1204. }
  1205. if ( $watch ) {
  1206. $user->addWatch( $this->getTitle() );
  1207. }
  1208. return true;
  1209. }
  1210. /**
  1211. * Record a file upload in the upload log and the image table
  1212. * @param string $oldver
  1213. * @param string $comment
  1214. * @param string $pageText
  1215. * @param bool|array $props
  1216. * @param string|bool $timestamp
  1217. * @param null|User $user
  1218. * @param string[] $tags
  1219. * @param bool $createNullRevision Set to false to avoid creation of a null revision on file
  1220. * upload, see T193621
  1221. * @return Status
  1222. */
  1223. function recordUpload2(
  1224. $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null, $tags = [],
  1225. $createNullRevision = true
  1226. ) {
  1227. global $wgCommentTableSchemaMigrationStage, $wgActorTableSchemaMigrationStage;
  1228. if ( is_null( $user ) ) {
  1229. global $wgUser;
  1230. $user = $wgUser;
  1231. }
  1232. $dbw = $this->repo->getMasterDB();
  1233. # Imports or such might force a certain timestamp; otherwise we generate
  1234. # it and can fudge it slightly to keep (name,timestamp) unique on re-upload.
  1235. if ( $timestamp === false ) {
  1236. $timestamp = $dbw->timestamp();
  1237. $allowTimeKludge = true;
  1238. } else {
  1239. $allowTimeKludge = false;
  1240. }
  1241. $props = $props ?: $this->repo->getFileProps( $this->getVirtualUrl() );
  1242. $props['description'] = $comment;
  1243. $props['user'] = $user->getId();
  1244. $props['user_text'] = $user->getName();
  1245. $props['actor'] = $user->getActorId( $dbw );
  1246. $props['timestamp'] = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
  1247. $this->setProps( $props );
  1248. # Fail now if the file isn't there
  1249. if ( !$this->fileExists ) {
  1250. wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" );
  1251. return Status::newFatal( 'filenotfound', $this->getRel() );
  1252. }
  1253. $dbw->startAtomic( __METHOD__ );
  1254. # Test to see if the row exists using INSERT IGNORE
  1255. # This avoids race conditions by locking the row until the commit, and also
  1256. # doesn't deadlock. SELECT FOR UPDATE causes a deadlock for every race condition.
  1257. $commentStore = MediaWikiServices::getInstance()->getCommentStore();
  1258. list( $commentFields, $commentCallback ) =
  1259. $commentStore->insertWithTempTable( $dbw, 'img_description', $comment );
  1260. $actorMigration = ActorMigration::newMigration();
  1261. $actorFields = $actorMigration->getInsertValues( $dbw, 'img_user', $user );
  1262. $dbw->insert( 'image',
  1263. [
  1264. 'img_name' => $this->getName(),
  1265. 'img_size' => $this->size,
  1266. 'img_width' => intval( $this->width ),
  1267. 'img_height' => intval( $this->height ),
  1268. 'img_bits' => $this->bits,
  1269. 'img_media_type' => $this->media_type,
  1270. 'img_major_mime' => $this->major_mime,
  1271. 'img_minor_mime' => $this->minor_mime,
  1272. 'img_timestamp' => $timestamp,
  1273. 'img_metadata' => $dbw->encodeBlob( $this->metadata ),
  1274. 'img_sha1' => $this->sha1
  1275. ] + $commentFields + $actorFields,
  1276. __METHOD__,
  1277. 'IGNORE'
  1278. );
  1279. $reupload = ( $dbw->affectedRows() == 0 );
  1280. if ( $reupload ) {
  1281. $row = $dbw->selectRow(
  1282. 'image',
  1283. [ 'img_timestamp', 'img_sha1' ],
  1284. [ 'img_name' => $this->getName() ],
  1285. __METHOD__,
  1286. [ 'LOCK IN SHARE MODE' ]
  1287. );
  1288. if ( $row && $row->img_sha1 === $this->sha1 ) {
  1289. $dbw->endAtomic( __METHOD__ );
  1290. wfDebug( __METHOD__ . ": File " . $this->getRel() . " already exists!\n" );
  1291. $title = Title::newFromText( $this->getName(), NS_FILE );
  1292. return Status::newFatal( 'fileexists-no-change', $title->getPrefixedText() );
  1293. }
  1294. if ( $allowTimeKludge ) {
  1295. # Use LOCK IN SHARE MODE to ignore any transaction snapshotting
  1296. $lUnixtime = $row ? wfTimestamp( TS_UNIX, $row->img_timestamp ) : false;
  1297. # Avoid a timestamp that is not newer than the last version
  1298. # TODO: the image/oldimage tables should be like page/revision with an ID field
  1299. if ( $lUnixtime && wfTimestamp( TS_UNIX, $timestamp ) <= $lUnixtime ) {
  1300. sleep( 1 ); // fast enough re-uploads would go far in the future otherwise
  1301. $timestamp = $dbw->timestamp( $lUnixtime + 1 );
  1302. $this->timestamp = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
  1303. }
  1304. }
  1305. $tables = [ 'image' ];
  1306. $fields = [
  1307. 'oi_name' => 'img_name',
  1308. 'oi_archive_name' => $dbw->addQuotes( $oldver ),
  1309. 'oi_size' => 'img_size',
  1310. 'oi_width' => 'img_width',
  1311. 'oi_height' => 'img_height',
  1312. 'oi_bits' => 'img_bits',
  1313. 'oi_timestamp' => 'img_timestamp',
  1314. 'oi_metadata' => 'img_metadata',
  1315. 'oi_media_type' => 'img_media_type',
  1316. 'oi_major_mime' => 'img_major_mime',
  1317. 'oi_minor_mime' => 'img_minor_mime',
  1318. 'oi_sha1' => 'img_sha1',
  1319. ];
  1320. $joins = [];
  1321. if ( $wgCommentTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) {
  1322. $fields['oi_description'] = 'img_description';
  1323. }
  1324. if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) {
  1325. $tables[] = 'image_comment_temp';
  1326. $fields['oi_description_id'] = 'imgcomment_description_id';
  1327. $joins['image_comment_temp'] = [
  1328. $wgCommentTableSchemaMigrationStage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN',
  1329. [ 'imgcomment_name = img_name' ]
  1330. ];
  1331. }
  1332. if ( $wgCommentTableSchemaMigrationStage !== MIGRATION_OLD &&
  1333. $wgCommentTableSchemaMigrationStage !== MIGRATION_NEW
  1334. ) {
  1335. // Upgrade any rows that are still old-style. Otherwise an upgrade
  1336. // might be missed if a deletion happens while the migration script
  1337. // is running.
  1338. $res = $dbw->select(
  1339. [ 'image', 'image_comment_temp' ],
  1340. [ 'img_name', 'img_description' ],
  1341. [ 'img_name' => $this->getName(), 'imgcomment_name' => null ],
  1342. __METHOD__,
  1343. [],
  1344. [ 'image_comment_temp' => [ 'LEFT JOIN', [ 'imgcomment_name = img_name' ] ] ]
  1345. );
  1346. foreach ( $res as $row ) {
  1347. list( , $callback ) = $commentStore->insertWithTempTable(
  1348. $dbw, 'img_description', $row->img_description
  1349. );
  1350. $callback( $row->img_name );
  1351. }
  1352. }
  1353. if ( $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) {
  1354. $fields['oi_user'] = 'img_user';
  1355. $fields['oi_user_text'] = 'img_user_text';
  1356. }
  1357. if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) {
  1358. $fields['oi_actor'] = 'img_actor';
  1359. }
  1360. if ( $wgActorTableSchemaMigrationStage !== MIGRATION_OLD &&
  1361. $wgActorTableSchemaMigrationStage !== MIGRATION_NEW
  1362. ) {
  1363. // Upgrade any rows that are still old-style. Otherwise an upgrade
  1364. // might be missed if a deletion happens while the migration script
  1365. // is running.
  1366. $res = $dbw->select(
  1367. [ 'image' ],
  1368. [ 'img_name', 'img_user', 'img_user_text' ],
  1369. [ 'img_name' => $this->getName(), 'img_actor' => 0 ],
  1370. __METHOD__
  1371. );
  1372. foreach ( $res as $row ) {
  1373. $actorId = User::newFromAnyId( $row->img_user, $row->img_user_text, null )->getActorId( $dbw );
  1374. $dbw->update(
  1375. 'image',
  1376. [ 'img_actor' => $actorId ],
  1377. [ 'img_name' => $row->img_name, 'img_actor' => 0 ],
  1378. __METHOD__
  1379. );
  1380. }
  1381. }
  1382. # (T36993) Note: $oldver can be empty here, if the previous
  1383. # version of the file was broken. Allow registration of the new
  1384. # version to continue anyway, because that's better than having
  1385. # an image that's not fixable by user operations.
  1386. # Collision, this is an update of a file
  1387. # Insert previous contents into oldimage
  1388. $dbw->insertSelect( 'oldimage', $tables, $fields,
  1389. [ 'img_name' => $this->getName() ], __METHOD__, [], [], $joins );
  1390. # Update the current image row
  1391. $dbw->update( 'image',
  1392. [
  1393. 'img_size' => $this->size,
  1394. 'img_width' => intval( $this->width ),
  1395. 'img_height' => intval( $this->height ),
  1396. 'img_bits' => $this->bits,
  1397. 'img_media_type' => $this->media_type,
  1398. 'img_major_mime' => $this->major_mime,
  1399. 'img_minor_mime' => $this->minor_mime,
  1400. 'img_timestamp' => $timestamp,
  1401. 'img_metadata' => $dbw->encodeBlob( $this->metadata ),
  1402. 'img_sha1' => $this->sha1
  1403. ] + $commentFields + $actorFields,
  1404. [ 'img_name' => $this->getName() ],
  1405. __METHOD__
  1406. );
  1407. if ( $wgCommentTableSchemaMigrationStage > MIGRATION_OLD ) {
  1408. // So $commentCallback can insert the new row
  1409. $dbw->delete( 'image_comment_temp', [ 'imgcomment_name' => $this->getName() ], __METHOD__ );
  1410. }
  1411. }
  1412. $commentCallback( $this->getName() );
  1413. $descTitle = $this->getTitle();
  1414. $descId = $descTitle->getArticleID();
  1415. $wikiPage = new WikiFilePage( $descTitle );
  1416. $wikiPage->setFile( $this );
  1417. // Add the log entry...
  1418. $logEntry = new ManualLogEntry( 'upload', $reupload ? 'overwrite' : 'upload' );
  1419. $logEntry->setTimestamp( $this->timestamp );
  1420. $logEntry->setPerformer( $user );
  1421. $logEntry->setComment( $comment );
  1422. $logEntry->setTarget( $descTitle );
  1423. // Allow people using the api to associate log entries with the upload.
  1424. // Log has a timestamp, but sometimes different from upload timestamp.
  1425. $logEntry->setParameters(
  1426. [
  1427. 'img_sha1' => $this->sha1,
  1428. 'img_timestamp' => $timestamp,
  1429. ]
  1430. );
  1431. // Note we keep $logId around since during new image
  1432. // creation, page doesn't exist yet, so log_page = 0
  1433. // but we want it to point to the page we're making,
  1434. // so we later modify the log entry.
  1435. // For a similar reason, we avoid making an RC entry
  1436. // now and wait until the page exists.
  1437. $logId = $logEntry->insert();
  1438. if ( $descTitle->exists() ) {
  1439. // Use own context to get the action text in content language
  1440. $formatter = LogFormatter::newFromEntry( $logEntry );
  1441. $formatter->setContext( RequestContext::newExtraneousContext( $descTitle ) );
  1442. $editSummary = $formatter->getPlainActionText();
  1443. $nullRevision = $createNullRevision === false ? null : Revision::newNullRevision(
  1444. $dbw,
  1445. $descId,
  1446. $editSummary,
  1447. false,
  1448. $user
  1449. );
  1450. if ( $nullRevision ) {
  1451. $nullRevision->insertOn( $dbw );
  1452. Hooks::run(
  1453. 'NewRevisionFromEditComplete',
  1454. [ $wikiPage, $nullRevision, $nullRevision->getParentId(), $user ]
  1455. );
  1456. $wikiPage->updateRevisionOn( $dbw, $nullRevision );
  1457. // Associate null revision id
  1458. $logEntry->setAssociatedRevId( $nullRevision->getId() );
  1459. }
  1460. $newPageContent = null;
  1461. } else {
  1462. // Make the description page and RC log entry post-commit
  1463. $newPageContent = ContentHandler::makeContent( $pageText, $descTitle );
  1464. }
  1465. # Defer purges, page creation, and link updates in case they error out.
  1466. # The most important thing is that files and the DB registry stay synced.
  1467. $dbw->endAtomic( __METHOD__ );
  1468. # Do some cache purges after final commit so that:
  1469. # a) Changes are more likely to be seen post-purge
  1470. # b) They won't cause rollback of the log publish/update above
  1471. DeferredUpdates::addUpdate(
  1472. new AutoCommitUpdate(
  1473. $dbw,
  1474. __METHOD__,
  1475. function () use (
  1476. $reupload, $wikiPage, $newPageContent, $comment, $user,
  1477. $logEntry, $logId, $descId, $tags
  1478. ) {
  1479. # Update memcache after the commit
  1480. $this->invalidateCache();
  1481. $updateLogPage = false;
  1482. if ( $newPageContent ) {
  1483. # New file page; create the description page.
  1484. # There's already a log entry, so don't make a second RC entry
  1485. # CDN and file cache for the description page are purged by doEditContent.
  1486. $status = $wikiPage->doEditContent(
  1487. $newPageContent,
  1488. $comment,
  1489. EDIT_NEW | EDIT_SUPPRESS_RC,
  1490. false,
  1491. $user
  1492. );
  1493. if ( isset( $status->value['revision'] ) ) {
  1494. /** @var Revision $rev */
  1495. $rev = $status->value['revision'];
  1496. // Associate new page revision id
  1497. $logEntry->setAssociatedRevId( $rev->getId() );
  1498. }
  1499. // This relies on the resetArticleID() call in WikiPage::insertOn(),
  1500. // which is triggered on $descTitle by doEditContent() above.
  1501. if ( isset( $status->value['revision'] ) ) {
  1502. /** @var Revision $rev */
  1503. $rev = $status->value['revision'];
  1504. $updateLogPage = $rev->getPage();
  1505. }
  1506. } else {
  1507. # Existing file page: invalidate description page cache
  1508. $wikiPage->getTitle()->invalidateCache();
  1509. $wikiPage->getTitle()->purgeSquid();
  1510. # Allow the new file version to be patrolled from the page footer
  1511. Article::purgePatrolFooterCache( $descId );
  1512. }
  1513. # Update associated rev id. This should be done by $logEntry->insert() earlier,
  1514. # but setAssociatedRevId() wasn't called at that point yet...
  1515. $logParams = $logEntry->getParameters();
  1516. $logParams['associated_rev_id'] = $logEntry->getAssociatedRevId();
  1517. $update = [ 'log_params' => LogEntryBase::makeParamBlob( $logParams ) ];
  1518. if ( $updateLogPage ) {
  1519. # Also log page, in case where we just created it above
  1520. $update['log_page'] = $updateLogPage;
  1521. }
  1522. $this->getRepo()->getMasterDB()->update(
  1523. 'logging',
  1524. $update,
  1525. [ 'log_id' => $logId ],
  1526. __METHOD__
  1527. );
  1528. $this->getRepo()->getMasterDB()->insert(
  1529. 'log_search',
  1530. [
  1531. 'ls_field' => 'associated_rev_id',
  1532. 'ls_value' => $logEntry->getAssociatedRevId(),
  1533. 'ls_log_id' => $logId,
  1534. ],
  1535. __METHOD__
  1536. );
  1537. # Add change tags, if any
  1538. if ( $tags ) {
  1539. $logEntry->setTags( $tags );
  1540. }
  1541. # Uploads can be patrolled
  1542. $logEntry->setIsPatrollable( true );
  1543. # Now that the log entry is up-to-date, make an RC entry.
  1544. $logEntry->publish( $logId );
  1545. # Run hook for other updates (typically more cache purging)
  1546. Hooks::run( 'FileUpload', [ $this, $reupload, !$newPageContent ] );
  1547. if ( $reupload ) {
  1548. # Delete old thumbnails
  1549. $this->purgeThumbnails();
  1550. # Remove the old file from the CDN cache
  1551. DeferredUpdates::addUpdate(
  1552. new CdnCacheUpdate( [ $this->getUrl() ] ),
  1553. DeferredUpdates::PRESEND
  1554. );
  1555. } else {
  1556. # Update backlink pages pointing to this title if created
  1557. LinksUpdate::queueRecursiveJobsForTable(
  1558. $this->getTitle(),
  1559. 'imagelinks',
  1560. 'upload-image',
  1561. $user->getName()
  1562. );
  1563. }
  1564. $this->prerenderThumbnails();
  1565. }
  1566. ),
  1567. DeferredUpdates::PRESEND
  1568. );
  1569. if ( !$reupload ) {
  1570. # This is a new file, so update the image count
  1571. DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) );
  1572. }
  1573. # Invalidate cache for all pages using this file
  1574. DeferredUpdates::addUpdate(
  1575. new HTMLCacheUpdate( $this->getTitle(), 'imagelinks', 'file-upload' )
  1576. );
  1577. return Status::newGood();
  1578. }
  1579. /**
  1580. * Move or copy a file to its public location. If a file exists at the
  1581. * destination, move it to an archive. Returns a Status object with
  1582. * the archive name in the "value" member on success.
  1583. *
  1584. * The archive name should be passed through to recordUpload for database
  1585. * registration.
  1586. *
  1587. * @param string|FSFile $src Local filesystem path or virtual URL to the source image
  1588. * @param int $flags A bitwise combination of:
  1589. * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy
  1590. * @param array $options Optional additional parameters
  1591. * @return Status On success, the value member contains the
  1592. * archive name, or an empty string if it was a new file.
  1593. */
  1594. function publish( $src, $flags = 0, array $options = [] ) {
  1595. return $this->publishTo( $src, $this->getRel(), $flags, $options );
  1596. }
  1597. /**
  1598. * Move or copy a file to a specified location. Returns a Status
  1599. * object with the archive name in the "value" member on success.
  1600. *
  1601. * The archive name should be passed through to recordUpload for database
  1602. * registration.
  1603. *
  1604. * @param string|FSFile $src Local filesystem path or virtual URL to the source image
  1605. * @param string $dstRel Target relative path
  1606. * @param int $flags A bitwise combination of:
  1607. * File::DELETE_SOURCE Delete the source file, i.e. move rather than copy
  1608. * @param array $options Optional additional parameters
  1609. * @return Status On success, the value member contains the
  1610. * archive name, or an empty string if it was a new file.
  1611. */
  1612. function publishTo( $src, $dstRel, $flags = 0, array $options = [] ) {
  1613. $srcPath = ( $src instanceof FSFile ) ? $src->getPath() : $src;
  1614. $repo = $this->getRepo();
  1615. if ( $repo->getReadOnlyReason() !== false ) {
  1616. return $this->readOnlyFatalStatus();
  1617. }
  1618. $this->lock(); // begin
  1619. $archiveName = wfTimestamp( TS_MW ) . '!' . $this->getName();
  1620. $archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
  1621. if ( $repo->hasSha1Storage() ) {
  1622. $sha1 = $repo->isVirtualUrl( $srcPath )
  1623. ? $repo->getFileSha1( $srcPath )
  1624. : FSFile::getSha1Base36FromPath( $srcPath );
  1625. /** @var FileBackendDBRepoWrapper $wrapperBackend */
  1626. $wrapperBackend = $repo->getBackend();
  1627. $dst = $wrapperBackend->getPathForSHA1( $sha1 );
  1628. $status = $repo->quickImport( $src, $dst );
  1629. if ( $flags & File::DELETE_SOURCE ) {
  1630. unlink( $srcPath );
  1631. }
  1632. if ( $this->exists() ) {
  1633. $status->value = $archiveName;
  1634. }
  1635. } else {
  1636. $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0;
  1637. $status = $repo->publish( $srcPath, $dstRel, $archiveRel, $flags, $options );
  1638. if ( $status->value == 'new' ) {
  1639. $status->value = '';
  1640. } else {
  1641. $status->value = $archiveName;
  1642. }
  1643. }
  1644. $this->unlock(); // done
  1645. return $status;
  1646. }
  1647. /** getLinksTo inherited */
  1648. /** getExifData inherited */
  1649. /** isLocal inherited */
  1650. /** wasDeleted inherited */
  1651. /**
  1652. * Move file to the new title
  1653. *
  1654. * Move current, old version and all thumbnails
  1655. * to the new filename. Old file is deleted.
  1656. *
  1657. * Cache purging is done; checks for validity
  1658. * and logging are caller's responsibility
  1659. *
  1660. * @param Title $target New file name
  1661. * @return Status
  1662. */
  1663. function move( $target ) {
  1664. if ( $this->getRepo()->getReadOnlyReason() !== false ) {
  1665. return $this->readOnlyFatalStatus();
  1666. }
  1667. wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() );
  1668. $batch = new LocalFileMoveBatch( $this, $target );
  1669. $this->lock(); // begin
  1670. $batch->addCurrent();
  1671. $archiveNames = $batch->addOlds();
  1672. $status = $batch->execute();
  1673. $this->unlock(); // done
  1674. wfDebugLog( 'imagemove', "Finished moving {$this->name}" );
  1675. // Purge the source and target files...
  1676. $oldTitleFile = wfLocalFile( $this->title );
  1677. $newTitleFile = wfLocalFile( $target );
  1678. // To avoid slow purges in the transaction, move them outside...
  1679. DeferredUpdates::addUpdate(
  1680. new AutoCommitUpdate(
  1681. $this->getRepo()->getMasterDB(),
  1682. __METHOD__,
  1683. function () use ( $oldTitleFile, $newTitleFile, $archiveNames ) {
  1684. $oldTitleFile->purgeEverything();
  1685. foreach ( $archiveNames as $archiveName ) {
  1686. $oldTitleFile->purgeOldThumbnails( $archiveName );
  1687. }
  1688. $newTitleFile->purgeEverything();
  1689. }
  1690. ),
  1691. DeferredUpdates::PRESEND
  1692. );
  1693. if ( $status->isOK() ) {
  1694. // Now switch the object
  1695. $this->title = $target;
  1696. // Force regeneration of the name and hashpath
  1697. unset( $this->name );
  1698. unset( $this->hashPath );
  1699. }
  1700. return $status;
  1701. }
  1702. /**
  1703. * Delete all versions of the file.
  1704. *
  1705. * Moves the files into an archive directory (or deletes them)
  1706. * and removes the database rows.
  1707. *
  1708. * Cache purging is done; logging is caller's responsibility.
  1709. *
  1710. * @param string $reason
  1711. * @param bool $suppress
  1712. * @param User|null $user
  1713. * @return Status
  1714. */
  1715. function delete( $reason, $suppress = false, $user = null ) {
  1716. if ( $this->getRepo()->getReadOnlyReason() !== false ) {
  1717. return $this->readOnlyFatalStatus();
  1718. }
  1719. $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
  1720. $this->lock(); // begin
  1721. $batch->addCurrent();
  1722. // Get old version relative paths
  1723. $archiveNames = $batch->addOlds();
  1724. $status = $batch->execute();
  1725. $this->unlock(); // done
  1726. if ( $status->isOK() ) {
  1727. DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => -1 ] ) );
  1728. }
  1729. // To avoid slow purges in the transaction, move them outside...
  1730. DeferredUpdates::addUpdate(
  1731. new AutoCommitUpdate(
  1732. $this->getRepo()->getMasterDB(),
  1733. __METHOD__,
  1734. function () use ( $archiveNames ) {
  1735. $this->purgeEverything();
  1736. foreach ( $archiveNames as $archiveName ) {
  1737. $this->purgeOldThumbnails( $archiveName );
  1738. }
  1739. }
  1740. ),
  1741. DeferredUpdates::PRESEND
  1742. );
  1743. // Purge the CDN
  1744. $purgeUrls = [];
  1745. foreach ( $archiveNames as $archiveName ) {
  1746. $purgeUrls[] = $this->getArchiveUrl( $archiveName );
  1747. }
  1748. DeferredUpdates::addUpdate( new CdnCacheUpdate( $purgeUrls ), DeferredUpdates::PRESEND );
  1749. return $status;
  1750. }
  1751. /**
  1752. * Delete an old version of the file.
  1753. *
  1754. * Moves the file into an archive directory (or deletes it)
  1755. * and removes the database row.
  1756. *
  1757. * Cache purging is done; logging is caller's responsibility.
  1758. *
  1759. * @param string $archiveName
  1760. * @param string $reason
  1761. * @param bool $suppress
  1762. * @param User|null $user
  1763. * @throws MWException Exception on database or file store failure
  1764. * @return Status
  1765. */
  1766. function deleteOld( $archiveName, $reason, $suppress = false, $user = null ) {
  1767. if ( $this->getRepo()->getReadOnlyReason() !== false ) {
  1768. return $this->readOnlyFatalStatus();
  1769. }
  1770. $batch = new LocalFileDeleteBatch( $this, $reason, $suppress, $user );
  1771. $this->lock(); // begin
  1772. $batch->addOld( $archiveName );
  1773. $status = $batch->execute();
  1774. $this->unlock(); // done
  1775. $this->purgeOldThumbnails( $archiveName );
  1776. if ( $status->isOK() ) {
  1777. $this->purgeDescription();
  1778. }
  1779. DeferredUpdates::addUpdate(
  1780. new CdnCacheUpdate( [ $this->getArchiveUrl( $archiveName ) ] ),
  1781. DeferredUpdates::PRESEND
  1782. );
  1783. return $status;
  1784. }
  1785. /**
  1786. * Restore all or specified deleted revisions to the given file.
  1787. * Permissions and logging are left to the caller.
  1788. *
  1789. * May throw database exceptions on error.
  1790. *
  1791. * @param array $versions Set of record ids of deleted items to restore,
  1792. * or empty to restore all revisions.
  1793. * @param bool $unsuppress
  1794. * @return Status
  1795. */
  1796. function restore( $versions = [], $unsuppress = false ) {
  1797. if ( $this->getRepo()->getReadOnlyReason() !== false ) {
  1798. return $this->readOnlyFatalStatus();
  1799. }
  1800. $batch = new LocalFileRestoreBatch( $this, $unsuppress );
  1801. $this->lock(); // begin
  1802. if ( !$versions ) {
  1803. $batch->addAll();
  1804. } else {
  1805. $batch->addIds( $versions );
  1806. }
  1807. $status = $batch->execute();
  1808. if ( $status->isGood() ) {
  1809. $cleanupStatus = $batch->cleanup();
  1810. $cleanupStatus->successCount = 0;
  1811. $cleanupStatus->failCount = 0;
  1812. $status->merge( $cleanupStatus );
  1813. }
  1814. $this->unlock(); // done
  1815. return $status;
  1816. }
  1817. /** isMultipage inherited */
  1818. /** pageCount inherited */
  1819. /** scaleHeight inherited */
  1820. /** getImageSize inherited */
  1821. /**
  1822. * Get the URL of the file description page.
  1823. * @return string
  1824. */
  1825. function getDescriptionUrl() {
  1826. return $this->title->getLocalURL();
  1827. }
  1828. /**
  1829. * Get the HTML text of the description page
  1830. * This is not used by ImagePage for local files, since (among other things)
  1831. * it skips the parser cache.
  1832. *
  1833. * @param Language|null $lang What language to get description in (Optional)
  1834. * @return string|false
  1835. */
  1836. function getDescriptionText( Language $lang = null ) {
  1837. $store = MediaWikiServices::getInstance()->getRevisionStore();
  1838. $revision = $store->getRevisionByTitle( $this->title, 0, Revision::READ_NORMAL );
  1839. if ( !$revision ) {
  1840. return false;
  1841. }
  1842. $renderer = MediaWikiServices::getInstance()->getRevisionRenderer();
  1843. $rendered = $renderer->getRenderedRevision( $revision, new ParserOptions( null, $lang ) );
  1844. if ( !$rendered ) {
  1845. // audience check failed
  1846. return false;
  1847. }
  1848. $pout = $rendered->getRevisionParserOutput();
  1849. return $pout->getText();
  1850. }
  1851. /**
  1852. * @param int $audience
  1853. * @param User|null $user
  1854. * @return string
  1855. */
  1856. function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
  1857. $this->load();
  1858. if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) {
  1859. return '';
  1860. } elseif ( $audience == self::FOR_THIS_USER
  1861. && !$this->userCan( self::DELETED_COMMENT, $user )
  1862. ) {
  1863. return '';
  1864. } else {
  1865. return $this->description;
  1866. }
  1867. }
  1868. /**
  1869. * @return bool|string
  1870. */
  1871. function getTimestamp() {
  1872. $this->load();
  1873. return $this->timestamp;
  1874. }
  1875. /**
  1876. * @return bool|string
  1877. */
  1878. public function getDescriptionTouched() {
  1879. // The DB lookup might return false, e.g. if the file was just deleted, or the shared DB repo
  1880. // itself gets it from elsewhere. To avoid repeating the DB lookups in such a case, we
  1881. // need to differentiate between null (uninitialized) and false (failed to load).
  1882. if ( $this->descriptionTouched === null ) {
  1883. $cond = [
  1884. 'page_namespace' => $this->title->getNamespace(),
  1885. 'page_title' => $this->title->getDBkey()
  1886. ];
  1887. $touched = $this->repo->getReplicaDB()->selectField( 'page', 'page_touched', $cond, __METHOD__ );
  1888. $this->descriptionTouched = $touched ? wfTimestamp( TS_MW, $touched ) : false;
  1889. }
  1890. return $this->descriptionTouched;
  1891. }
  1892. /**
  1893. * @return string
  1894. */
  1895. function getSha1() {
  1896. $this->load();
  1897. // Initialise now if necessary
  1898. if ( $this->sha1 == '' && $this->fileExists ) {
  1899. $this->lock(); // begin
  1900. $this->sha1 = $this->repo->getFileSha1( $this->getPath() );
  1901. if ( !wfReadOnly() && strval( $this->sha1 ) != '' ) {
  1902. $dbw = $this->repo->getMasterDB();
  1903. $dbw->update( 'image',
  1904. [ 'img_sha1' => $this->sha1 ],
  1905. [ 'img_name' => $this->getName() ],
  1906. __METHOD__ );
  1907. $this->invalidateCache();
  1908. }
  1909. $this->unlock(); // done
  1910. }
  1911. return $this->sha1;
  1912. }
  1913. /**
  1914. * @return bool Whether to cache in RepoGroup (this avoids OOMs)
  1915. */
  1916. function isCacheable() {
  1917. $this->load();
  1918. // If extra data (metadata) was not loaded then it must have been large
  1919. return $this->extraDataLoaded
  1920. && strlen( serialize( $this->metadata ) ) <= self::CACHE_FIELD_MAX_LEN;
  1921. }
  1922. /**
  1923. * @return Status
  1924. * @since 1.28
  1925. */
  1926. public function acquireFileLock() {
  1927. return Status::wrap( $this->getRepo()->getBackend()->lockFiles(
  1928. [ $this->getPath() ], LockManager::LOCK_EX, 10
  1929. ) );
  1930. }
  1931. /**
  1932. * @return Status
  1933. * @since 1.28
  1934. */
  1935. public function releaseFileLock() {
  1936. return Status::wrap( $this->getRepo()->getBackend()->unlockFiles(
  1937. [ $this->getPath() ], LockManager::LOCK_EX
  1938. ) );
  1939. }
  1940. /**
  1941. * Start an atomic DB section and lock the image for update
  1942. * or increments a reference counter if the lock is already held
  1943. *
  1944. * This method should not be used outside of LocalFile/LocalFile*Batch
  1945. *
  1946. * @throws LocalFileLockError Throws an error if the lock was not acquired
  1947. * @return bool Whether the file lock owns/spawned the DB transaction
  1948. */
  1949. public function lock() {
  1950. if ( !$this->locked ) {
  1951. $logger = LoggerFactory::getInstance( 'LocalFile' );
  1952. $dbw = $this->repo->getMasterDB();
  1953. $makesTransaction = !$dbw->trxLevel();
  1954. $dbw->startAtomic( self::ATOMIC_SECTION_LOCK );
  1955. // T56736: use simple lock to handle when the file does not exist.
  1956. // SELECT FOR UPDATE prevents changes, not other SELECTs with FOR UPDATE.
  1957. // Also, that would cause contention on INSERT of similarly named rows.
  1958. $status = $this->acquireFileLock(); // represents all versions of the file
  1959. if ( !$status->isGood() ) {
  1960. $dbw->endAtomic( self::ATOMIC_SECTION_LOCK );
  1961. $logger->warning( "Failed to lock '{file}'", [ 'file' => $this->name ] );
  1962. throw new LocalFileLockError( $status );
  1963. }
  1964. // Release the lock *after* commit to avoid row-level contention.
  1965. // Make sure it triggers on rollback() as well as commit() (T132921).
  1966. $dbw->onTransactionResolution(
  1967. function () use ( $logger ) {
  1968. $status = $this->releaseFileLock();
  1969. if ( !$status->isGood() ) {
  1970. $logger->error( "Failed to unlock '{file}'", [ 'file' => $this->name ] );
  1971. }
  1972. },
  1973. __METHOD__
  1974. );
  1975. // Callers might care if the SELECT snapshot is safely fresh
  1976. $this->lockedOwnTrx = $makesTransaction;
  1977. }
  1978. $this->locked++;
  1979. return $this->lockedOwnTrx;
  1980. }
  1981. /**
  1982. * Decrement the lock reference count and end the atomic section if it reaches zero
  1983. *
  1984. * This method should not be used outside of LocalFile/LocalFile*Batch
  1985. *
  1986. * The commit and loc release will happen when no atomic sections are active, which
  1987. * may happen immediately or at some point after calling this
  1988. */
  1989. public function unlock() {
  1990. if ( $this->locked ) {
  1991. --$this->locked;
  1992. if ( !$this->locked ) {
  1993. $dbw = $this->repo->getMasterDB();
  1994. $dbw->endAtomic( self::ATOMIC_SECTION_LOCK );
  1995. $this->lockedOwnTrx = false;
  1996. }
  1997. }
  1998. }
  1999. /**
  2000. * @return Status
  2001. */
  2002. protected function readOnlyFatalStatus() {
  2003. return $this->getRepo()->newFatal( 'filereadonlyerror', $this->getName(),
  2004. $this->getRepo()->getName(), $this->getRepo()->getReadOnlyReason() );
  2005. }
  2006. /**
  2007. * Clean up any dangling locks
  2008. */
  2009. function __destruct() {
  2010. $this->unlock();
  2011. }
  2012. } // LocalFile class
  2013. # ------------------------------------------------------------------------------
  2014. /**
  2015. * Helper class for file deletion
  2016. * @ingroup FileAbstraction
  2017. */
  2018. class LocalFileDeleteBatch {
  2019. /** @var LocalFile */
  2020. private $file;
  2021. /** @var string */
  2022. private $reason;
  2023. /** @var array */
  2024. private $srcRels = [];
  2025. /** @var array */
  2026. private $archiveUrls = [];
  2027. /** @var array Items to be processed in the deletion batch */
  2028. private $deletionBatch;
  2029. /** @var bool Whether to suppress all suppressable fields when deleting */
  2030. private $suppress;
  2031. /** @var Status */
  2032. private $status;
  2033. /** @var User */
  2034. private $user;
  2035. /**
  2036. * @param File $file
  2037. * @param string $reason
  2038. * @param bool $suppress
  2039. * @param User|null $user
  2040. */
  2041. function __construct( File $file, $reason = '', $suppress = false, $user = null ) {
  2042. $this->file = $file;
  2043. $this->reason = $reason;
  2044. $this->suppress = $suppress;
  2045. if ( $user ) {
  2046. $this->user = $user;
  2047. } else {
  2048. global $wgUser;
  2049. $this->user = $wgUser;
  2050. }
  2051. $this->status = $file->repo->newGood();
  2052. }
  2053. public function addCurrent() {
  2054. $this->srcRels['.'] = $this->file->getRel();
  2055. }
  2056. /**
  2057. * @param string $oldName
  2058. */
  2059. public function addOld( $oldName ) {
  2060. $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName );
  2061. $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName );
  2062. }
  2063. /**
  2064. * Add the old versions of the image to the batch
  2065. * @return string[] List of archive names from old versions
  2066. */
  2067. public function addOlds() {
  2068. $archiveNames = [];
  2069. $dbw = $this->file->repo->getMasterDB();
  2070. $result = $dbw->select( 'oldimage',
  2071. [ 'oi_archive_name' ],
  2072. [ 'oi_name' => $this->file->getName() ],
  2073. __METHOD__
  2074. );
  2075. foreach ( $result as $row ) {
  2076. $this->addOld( $row->oi_archive_name );
  2077. $archiveNames[] = $row->oi_archive_name;
  2078. }
  2079. return $archiveNames;
  2080. }
  2081. /**
  2082. * @return array
  2083. */
  2084. protected function getOldRels() {
  2085. if ( !isset( $this->srcRels['.'] ) ) {
  2086. $oldRels =& $this->srcRels;
  2087. $deleteCurrent = false;
  2088. } else {
  2089. $oldRels = $this->srcRels;
  2090. unset( $oldRels['.'] );
  2091. $deleteCurrent = true;
  2092. }
  2093. return [ $oldRels, $deleteCurrent ];
  2094. }
  2095. /**
  2096. * @return array
  2097. */
  2098. protected function getHashes() {
  2099. $hashes = [];
  2100. list( $oldRels, $deleteCurrent ) = $this->getOldRels();
  2101. if ( $deleteCurrent ) {
  2102. $hashes['.'] = $this->file->getSha1();
  2103. }
  2104. if ( count( $oldRels ) ) {
  2105. $dbw = $this->file->repo->getMasterDB();
  2106. $res = $dbw->select(
  2107. 'oldimage',
  2108. [ 'oi_archive_name', 'oi_sha1' ],
  2109. [ 'oi_archive_name' => array_keys( $oldRels ),
  2110. 'oi_name' => $this->file->getName() ], // performance
  2111. __METHOD__
  2112. );
  2113. foreach ( $res as $row ) {
  2114. if ( rtrim( $row->oi_sha1, "\0" ) === '' ) {
  2115. // Get the hash from the file
  2116. $oldUrl = $this->file->getArchiveVirtualUrl( $row->oi_archive_name );
  2117. $props = $this->file->repo->getFileProps( $oldUrl );
  2118. if ( $props['fileExists'] ) {
  2119. // Upgrade the oldimage row
  2120. $dbw->update( 'oldimage',
  2121. [ 'oi_sha1' => $props['sha1'] ],
  2122. [ 'oi_name' => $this->file->getName(), 'oi_archive_name' => $row->oi_archive_name ],
  2123. __METHOD__ );
  2124. $hashes[$row->oi_archive_name] = $props['sha1'];
  2125. } else {
  2126. $hashes[$row->oi_archive_name] = false;
  2127. }
  2128. } else {
  2129. $hashes[$row->oi_archive_name] = $row->oi_sha1;
  2130. }
  2131. }
  2132. }
  2133. $missing = array_diff_key( $this->srcRels, $hashes );
  2134. foreach ( $missing as $name => $rel ) {
  2135. $this->status->error( 'filedelete-old-unregistered', $name );
  2136. }
  2137. foreach ( $hashes as $name => $hash ) {
  2138. if ( !$hash ) {
  2139. $this->status->error( 'filedelete-missing', $this->srcRels[$name] );
  2140. unset( $hashes[$name] );
  2141. }
  2142. }
  2143. return $hashes;
  2144. }
  2145. protected function doDBInserts() {
  2146. global $wgCommentTableSchemaMigrationStage, $wgActorTableSchemaMigrationStage;
  2147. $now = time();
  2148. $dbw = $this->file->repo->getMasterDB();
  2149. $commentStore = MediaWikiServices::getInstance()->getCommentStore();
  2150. $actorMigration = ActorMigration::newMigration();
  2151. $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
  2152. $encUserId = $dbw->addQuotes( $this->user->getId() );
  2153. $encGroup = $dbw->addQuotes( 'deleted' );
  2154. $ext = $this->file->getExtension();
  2155. $dotExt = $ext === '' ? '' : ".$ext";
  2156. $encExt = $dbw->addQuotes( $dotExt );
  2157. list( $oldRels, $deleteCurrent ) = $this->getOldRels();
  2158. // Bitfields to further suppress the content
  2159. if ( $this->suppress ) {
  2160. $bitfield = Revision::SUPPRESSED_ALL;
  2161. } else {
  2162. $bitfield = 'oi_deleted';
  2163. }
  2164. if ( $deleteCurrent ) {
  2165. $tables = [ 'image' ];
  2166. $fields = [
  2167. 'fa_storage_group' => $encGroup,
  2168. 'fa_storage_key' => $dbw->conditional(
  2169. [ 'img_sha1' => '' ],
  2170. $dbw->addQuotes( '' ),
  2171. $dbw->buildConcat( [ "img_sha1", $encExt ] )
  2172. ),
  2173. 'fa_deleted_user' => $encUserId,
  2174. 'fa_deleted_timestamp' => $encTimestamp,
  2175. 'fa_deleted' => $this->suppress ? $bitfield : 0,
  2176. 'fa_name' => 'img_name',
  2177. 'fa_archive_name' => 'NULL',
  2178. 'fa_size' => 'img_size',
  2179. 'fa_width' => 'img_width',
  2180. 'fa_height' => 'img_height',
  2181. 'fa_metadata' => 'img_metadata',
  2182. 'fa_bits' => 'img_bits',
  2183. 'fa_media_type' => 'img_media_type',
  2184. 'fa_major_mime' => 'img_major_mime',
  2185. 'fa_minor_mime' => 'img_minor_mime',
  2186. 'fa_timestamp' => 'img_timestamp',
  2187. 'fa_sha1' => 'img_sha1'
  2188. ];
  2189. $joins = [];
  2190. $fields += array_map(
  2191. [ $dbw, 'addQuotes' ],
  2192. $commentStore->insert( $dbw, 'fa_deleted_reason', $this->reason )
  2193. );
  2194. if ( $wgCommentTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) {
  2195. $fields['fa_description'] = 'img_description';
  2196. }
  2197. if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) {
  2198. $tables[] = 'image_comment_temp';
  2199. $fields['fa_description_id'] = 'imgcomment_description_id';
  2200. $joins['image_comment_temp'] = [
  2201. $wgCommentTableSchemaMigrationStage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN',
  2202. [ 'imgcomment_name = img_name' ]
  2203. ];
  2204. }
  2205. if ( $wgCommentTableSchemaMigrationStage !== MIGRATION_OLD &&
  2206. $wgCommentTableSchemaMigrationStage !== MIGRATION_NEW
  2207. ) {
  2208. // Upgrade any rows that are still old-style. Otherwise an upgrade
  2209. // might be missed if a deletion happens while the migration script
  2210. // is running.
  2211. $res = $dbw->select(
  2212. [ 'image', 'image_comment_temp' ],
  2213. [ 'img_name', 'img_description' ],
  2214. [ 'img_name' => $this->file->getName(), 'imgcomment_name' => null ],
  2215. __METHOD__,
  2216. [],
  2217. [ 'image_comment_temp' => [ 'LEFT JOIN', [ 'imgcomment_name = img_name' ] ] ]
  2218. );
  2219. foreach ( $res as $row ) {
  2220. list( , $callback ) = $commentStore->insertWithTempTable(
  2221. $dbw, 'img_description', $row->img_description
  2222. );
  2223. $callback( $row->img_name );
  2224. }
  2225. }
  2226. if ( $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) {
  2227. $fields['fa_user'] = 'img_user';
  2228. $fields['fa_user_text'] = 'img_user_text';
  2229. }
  2230. if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) {
  2231. $fields['fa_actor'] = 'img_actor';
  2232. }
  2233. if ( $wgActorTableSchemaMigrationStage !== MIGRATION_OLD &&
  2234. $wgActorTableSchemaMigrationStage !== MIGRATION_NEW
  2235. ) {
  2236. // Upgrade any rows that are still old-style. Otherwise an upgrade
  2237. // might be missed if a deletion happens while the migration script
  2238. // is running.
  2239. $res = $dbw->select(
  2240. [ 'image' ],
  2241. [ 'img_name', 'img_user', 'img_user_text' ],
  2242. [ 'img_name' => $this->file->getName(), 'img_actor' => 0 ],
  2243. __METHOD__
  2244. );
  2245. foreach ( $res as $row ) {
  2246. $actorId = User::newFromAnyId( $row->img_user, $row->img_user_text, null )->getActorId( $dbw );
  2247. $dbw->update(
  2248. 'image',
  2249. [ 'img_actor' => $actorId ],
  2250. [ 'img_name' => $row->img_name, 'img_actor' => 0 ],
  2251. __METHOD__
  2252. );
  2253. }
  2254. }
  2255. $dbw->insertSelect( 'filearchive', $tables, $fields,
  2256. [ 'img_name' => $this->file->getName() ], __METHOD__, [], [], $joins );
  2257. }
  2258. if ( count( $oldRels ) ) {
  2259. $fileQuery = OldLocalFile::getQueryInfo();
  2260. $res = $dbw->select(
  2261. $fileQuery['tables'],
  2262. $fileQuery['fields'],
  2263. [
  2264. 'oi_name' => $this->file->getName(),
  2265. 'oi_archive_name' => array_keys( $oldRels )
  2266. ],
  2267. __METHOD__,
  2268. [ 'FOR UPDATE' ],
  2269. $fileQuery['joins']
  2270. );
  2271. $rowsInsert = [];
  2272. if ( $res->numRows() ) {
  2273. $reason = $commentStore->createComment( $dbw, $this->reason );
  2274. foreach ( $res as $row ) {
  2275. $comment = $commentStore->getComment( 'oi_description', $row );
  2276. $user = User::newFromAnyId( $row->oi_user, $row->oi_user_text, $row->oi_actor );
  2277. $rowsInsert[] = [
  2278. // Deletion-specific fields
  2279. 'fa_storage_group' => 'deleted',
  2280. 'fa_storage_key' => ( $row->oi_sha1 === '' )
  2281. ? ''
  2282. : "{$row->oi_sha1}{$dotExt}",
  2283. 'fa_deleted_user' => $this->user->getId(),
  2284. 'fa_deleted_timestamp' => $dbw->timestamp( $now ),
  2285. // Counterpart fields
  2286. 'fa_deleted' => $this->suppress ? $bitfield : $row->oi_deleted,
  2287. 'fa_name' => $row->oi_name,
  2288. 'fa_archive_name' => $row->oi_archive_name,
  2289. 'fa_size' => $row->oi_size,
  2290. 'fa_width' => $row->oi_width,
  2291. 'fa_height' => $row->oi_height,
  2292. 'fa_metadata' => $row->oi_metadata,
  2293. 'fa_bits' => $row->oi_bits,
  2294. 'fa_media_type' => $row->oi_media_type,
  2295. 'fa_major_mime' => $row->oi_major_mime,
  2296. 'fa_minor_mime' => $row->oi_minor_mime,
  2297. 'fa_timestamp' => $row->oi_timestamp,
  2298. 'fa_sha1' => $row->oi_sha1
  2299. ] + $commentStore->insert( $dbw, 'fa_deleted_reason', $reason )
  2300. + $commentStore->insert( $dbw, 'fa_description', $comment )
  2301. + $actorMigration->getInsertValues( $dbw, 'fa_user', $user );
  2302. }
  2303. }
  2304. $dbw->insert( 'filearchive', $rowsInsert, __METHOD__ );
  2305. }
  2306. }
  2307. function doDBDeletes() {
  2308. global $wgCommentTableSchemaMigrationStage;
  2309. $dbw = $this->file->repo->getMasterDB();
  2310. list( $oldRels, $deleteCurrent ) = $this->getOldRels();
  2311. if ( count( $oldRels ) ) {
  2312. $dbw->delete( 'oldimage',
  2313. [
  2314. 'oi_name' => $this->file->getName(),
  2315. 'oi_archive_name' => array_keys( $oldRels )
  2316. ], __METHOD__ );
  2317. }
  2318. if ( $deleteCurrent ) {
  2319. $dbw->delete( 'image', [ 'img_name' => $this->file->getName() ], __METHOD__ );
  2320. if ( $wgCommentTableSchemaMigrationStage > MIGRATION_OLD ) {
  2321. $dbw->delete(
  2322. 'image_comment_temp', [ 'imgcomment_name' => $this->file->getName() ], __METHOD__
  2323. );
  2324. }
  2325. }
  2326. }
  2327. /**
  2328. * Run the transaction
  2329. * @return Status
  2330. */
  2331. public function execute() {
  2332. $repo = $this->file->getRepo();
  2333. $this->file->lock();
  2334. // Prepare deletion batch
  2335. $hashes = $this->getHashes();
  2336. $this->deletionBatch = [];
  2337. $ext = $this->file->getExtension();
  2338. $dotExt = $ext === '' ? '' : ".$ext";
  2339. foreach ( $this->srcRels as $name => $srcRel ) {
  2340. // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
  2341. if ( isset( $hashes[$name] ) ) {
  2342. $hash = $hashes[$name];
  2343. $key = $hash . $dotExt;
  2344. $dstRel = $repo->getDeletedHashPath( $key ) . $key;
  2345. $this->deletionBatch[$name] = [ $srcRel, $dstRel ];
  2346. }
  2347. }
  2348. if ( !$repo->hasSha1Storage() ) {
  2349. // Removes non-existent file from the batch, so we don't get errors.
  2350. // This also handles files in the 'deleted' zone deleted via revision deletion.
  2351. $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch );
  2352. if ( !$checkStatus->isGood() ) {
  2353. $this->status->merge( $checkStatus );
  2354. return $this->status;
  2355. }
  2356. $this->deletionBatch = $checkStatus->value;
  2357. // Execute the file deletion batch
  2358. $status = $this->file->repo->deleteBatch( $this->deletionBatch );
  2359. if ( !$status->isGood() ) {
  2360. $this->status->merge( $status );
  2361. }
  2362. }
  2363. if ( !$this->status->isOK() ) {
  2364. // Critical file deletion error; abort
  2365. $this->file->unlock();
  2366. return $this->status;
  2367. }
  2368. // Copy the image/oldimage rows to filearchive
  2369. $this->doDBInserts();
  2370. // Delete image/oldimage rows
  2371. $this->doDBDeletes();
  2372. // Commit and return
  2373. $this->file->unlock();
  2374. return $this->status;
  2375. }
  2376. /**
  2377. * Removes non-existent files from a deletion batch.
  2378. * @param array $batch
  2379. * @return Status
  2380. */
  2381. protected function removeNonexistentFiles( $batch ) {
  2382. $files = $newBatch = [];
  2383. foreach ( $batch as $batchItem ) {
  2384. list( $src, ) = $batchItem;
  2385. $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
  2386. }
  2387. $result = $this->file->repo->fileExistsBatch( $files );
  2388. if ( in_array( null, $result, true ) ) {
  2389. return Status::newFatal( 'backend-fail-internal',
  2390. $this->file->repo->getBackend()->getName() );
  2391. }
  2392. foreach ( $batch as $batchItem ) {
  2393. if ( $result[$batchItem[0]] ) {
  2394. $newBatch[] = $batchItem;
  2395. }
  2396. }
  2397. return Status::newGood( $newBatch );
  2398. }
  2399. }
  2400. # ------------------------------------------------------------------------------
  2401. /**
  2402. * Helper class for file undeletion
  2403. * @ingroup FileAbstraction
  2404. */
  2405. class LocalFileRestoreBatch {
  2406. /** @var LocalFile */
  2407. private $file;
  2408. /** @var string[] List of file IDs to restore */
  2409. private $cleanupBatch;
  2410. /** @var string[] List of file IDs to restore */
  2411. private $ids;
  2412. /** @var bool Add all revisions of the file */
  2413. private $all;
  2414. /** @var bool Whether to remove all settings for suppressed fields */
  2415. private $unsuppress = false;
  2416. /**
  2417. * @param File $file
  2418. * @param bool $unsuppress
  2419. */
  2420. function __construct( File $file, $unsuppress = false ) {
  2421. $this->file = $file;
  2422. $this->cleanupBatch = [];
  2423. $this->ids = [];
  2424. $this->unsuppress = $unsuppress;
  2425. }
  2426. /**
  2427. * Add a file by ID
  2428. * @param int $fa_id
  2429. */
  2430. public function addId( $fa_id ) {
  2431. $this->ids[] = $fa_id;
  2432. }
  2433. /**
  2434. * Add a whole lot of files by ID
  2435. * @param int[] $ids
  2436. */
  2437. public function addIds( $ids ) {
  2438. $this->ids = array_merge( $this->ids, $ids );
  2439. }
  2440. /**
  2441. * Add all revisions of the file
  2442. */
  2443. public function addAll() {
  2444. $this->all = true;
  2445. }
  2446. /**
  2447. * Run the transaction, except the cleanup batch.
  2448. * The cleanup batch should be run in a separate transaction, because it locks different
  2449. * rows and there's no need to keep the image row locked while it's acquiring those locks
  2450. * The caller may have its own transaction open.
  2451. * So we save the batch and let the caller call cleanup()
  2452. * @return Status
  2453. */
  2454. public function execute() {
  2455. /** @var Language */
  2456. global $wgLang;
  2457. $repo = $this->file->getRepo();
  2458. if ( !$this->all && !$this->ids ) {
  2459. // Do nothing
  2460. return $repo->newGood();
  2461. }
  2462. $lockOwnsTrx = $this->file->lock();
  2463. $dbw = $this->file->repo->getMasterDB();
  2464. $commentStore = MediaWikiServices::getInstance()->getCommentStore();
  2465. $actorMigration = ActorMigration::newMigration();
  2466. $status = $this->file->repo->newGood();
  2467. $exists = (bool)$dbw->selectField( 'image', '1',
  2468. [ 'img_name' => $this->file->getName() ],
  2469. __METHOD__,
  2470. // The lock() should already prevents changes, but this still may need
  2471. // to bypass any transaction snapshot. However, if lock() started the
  2472. // trx (which it probably did) then snapshot is post-lock and up-to-date.
  2473. $lockOwnsTrx ? [] : [ 'LOCK IN SHARE MODE' ]
  2474. );
  2475. // Fetch all or selected archived revisions for the file,
  2476. // sorted from the most recent to the oldest.
  2477. $conditions = [ 'fa_name' => $this->file->getName() ];
  2478. if ( !$this->all ) {
  2479. $conditions['fa_id'] = $this->ids;
  2480. }
  2481. $arFileQuery = ArchivedFile::getQueryInfo();
  2482. $result = $dbw->select(
  2483. $arFileQuery['tables'],
  2484. $arFileQuery['fields'],
  2485. $conditions,
  2486. __METHOD__,
  2487. [ 'ORDER BY' => 'fa_timestamp DESC' ],
  2488. $arFileQuery['joins']
  2489. );
  2490. $idsPresent = [];
  2491. $storeBatch = [];
  2492. $insertBatch = [];
  2493. $insertCurrent = false;
  2494. $deleteIds = [];
  2495. $first = true;
  2496. $archiveNames = [];
  2497. foreach ( $result as $row ) {
  2498. $idsPresent[] = $row->fa_id;
  2499. if ( $row->fa_name != $this->file->getName() ) {
  2500. $status->error( 'undelete-filename-mismatch', $wgLang->timeanddate( $row->fa_timestamp ) );
  2501. $status->failCount++;
  2502. continue;
  2503. }
  2504. if ( $row->fa_storage_key == '' ) {
  2505. // Revision was missing pre-deletion
  2506. $status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp ) );
  2507. $status->failCount++;
  2508. continue;
  2509. }
  2510. $deletedRel = $repo->getDeletedHashPath( $row->fa_storage_key ) .
  2511. $row->fa_storage_key;
  2512. $deletedUrl = $repo->getVirtualUrl() . '/deleted/' . $deletedRel;
  2513. if ( isset( $row->fa_sha1 ) ) {
  2514. $sha1 = $row->fa_sha1;
  2515. } else {
  2516. // old row, populate from key
  2517. $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
  2518. }
  2519. # Fix leading zero
  2520. if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) {
  2521. $sha1 = substr( $sha1, 1 );
  2522. }
  2523. if ( is_null( $row->fa_major_mime ) || $row->fa_major_mime == 'unknown'
  2524. || is_null( $row->fa_minor_mime ) || $row->fa_minor_mime == 'unknown'
  2525. || is_null( $row->fa_media_type ) || $row->fa_media_type == 'UNKNOWN'
  2526. || is_null( $row->fa_metadata )
  2527. ) {
  2528. // Refresh our metadata
  2529. // Required for a new current revision; nice for older ones too. :)
  2530. $props = RepoGroup::singleton()->getFileProps( $deletedUrl );
  2531. } else {
  2532. $props = [
  2533. 'minor_mime' => $row->fa_minor_mime,
  2534. 'major_mime' => $row->fa_major_mime,
  2535. 'media_type' => $row->fa_media_type,
  2536. 'metadata' => $row->fa_metadata
  2537. ];
  2538. }
  2539. $comment = $commentStore->getComment( 'fa_description', $row );
  2540. $user = User::newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
  2541. if ( $first && !$exists ) {
  2542. // This revision will be published as the new current version
  2543. $destRel = $this->file->getRel();
  2544. list( $commentFields, $commentCallback ) =
  2545. $commentStore->insertWithTempTable( $dbw, 'img_description', $comment );
  2546. $actorFields = $actorMigration->getInsertValues( $dbw, 'img_user', $user );
  2547. $insertCurrent = [
  2548. 'img_name' => $row->fa_name,
  2549. 'img_size' => $row->fa_size,
  2550. 'img_width' => $row->fa_width,
  2551. 'img_height' => $row->fa_height,
  2552. 'img_metadata' => $props['metadata'],
  2553. 'img_bits' => $row->fa_bits,
  2554. 'img_media_type' => $props['media_type'],
  2555. 'img_major_mime' => $props['major_mime'],
  2556. 'img_minor_mime' => $props['minor_mime'],
  2557. 'img_timestamp' => $row->fa_timestamp,
  2558. 'img_sha1' => $sha1
  2559. ] + $commentFields + $actorFields;
  2560. // The live (current) version cannot be hidden!
  2561. if ( !$this->unsuppress && $row->fa_deleted ) {
  2562. $status->fatal( 'undeleterevdel' );
  2563. $this->file->unlock();
  2564. return $status;
  2565. }
  2566. } else {
  2567. $archiveName = $row->fa_archive_name;
  2568. if ( $archiveName == '' ) {
  2569. // This was originally a current version; we
  2570. // have to devise a new archive name for it.
  2571. // Format is <timestamp of archiving>!<name>
  2572. $timestamp = wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
  2573. do {
  2574. $archiveName = wfTimestamp( TS_MW, $timestamp ) . '!' . $row->fa_name;
  2575. $timestamp++;
  2576. } while ( isset( $archiveNames[$archiveName] ) );
  2577. }
  2578. $archiveNames[$archiveName] = true;
  2579. $destRel = $this->file->getArchiveRel( $archiveName );
  2580. $insertBatch[] = [
  2581. 'oi_name' => $row->fa_name,
  2582. 'oi_archive_name' => $archiveName,
  2583. 'oi_size' => $row->fa_size,
  2584. 'oi_width' => $row->fa_width,
  2585. 'oi_height' => $row->fa_height,
  2586. 'oi_bits' => $row->fa_bits,
  2587. 'oi_timestamp' => $row->fa_timestamp,
  2588. 'oi_metadata' => $props['metadata'],
  2589. 'oi_media_type' => $props['media_type'],
  2590. 'oi_major_mime' => $props['major_mime'],
  2591. 'oi_minor_mime' => $props['minor_mime'],
  2592. 'oi_deleted' => $this->unsuppress ? 0 : $row->fa_deleted,
  2593. 'oi_sha1' => $sha1
  2594. ] + $commentStore->insert( $dbw, 'oi_description', $comment )
  2595. + $actorMigration->getInsertValues( $dbw, 'oi_user', $user );
  2596. }
  2597. $deleteIds[] = $row->fa_id;
  2598. if ( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
  2599. // private files can stay where they are
  2600. $status->successCount++;
  2601. } else {
  2602. $storeBatch[] = [ $deletedUrl, 'public', $destRel ];
  2603. $this->cleanupBatch[] = $row->fa_storage_key;
  2604. }
  2605. $first = false;
  2606. }
  2607. unset( $result );
  2608. // Add a warning to the status object for missing IDs
  2609. $missingIds = array_diff( $this->ids, $idsPresent );
  2610. foreach ( $missingIds as $id ) {
  2611. $status->error( 'undelete-missing-filearchive', $id );
  2612. }
  2613. if ( !$repo->hasSha1Storage() ) {
  2614. // Remove missing files from batch, so we don't get errors when undeleting them
  2615. $checkStatus = $this->removeNonexistentFiles( $storeBatch );
  2616. if ( !$checkStatus->isGood() ) {
  2617. $status->merge( $checkStatus );
  2618. return $status;
  2619. }
  2620. $storeBatch = $checkStatus->value;
  2621. // Run the store batch
  2622. // Use the OVERWRITE_SAME flag to smooth over a common error
  2623. $storeStatus = $this->file->repo->storeBatch( $storeBatch, FileRepo::OVERWRITE_SAME );
  2624. $status->merge( $storeStatus );
  2625. if ( !$status->isGood() ) {
  2626. // Even if some files could be copied, fail entirely as that is the
  2627. // easiest thing to do without data loss
  2628. $this->cleanupFailedBatch( $storeStatus, $storeBatch );
  2629. $status->setOK( false );
  2630. $this->file->unlock();
  2631. return $status;
  2632. }
  2633. }
  2634. // Run the DB updates
  2635. // Because we have locked the image row, key conflicts should be rare.
  2636. // If they do occur, we can roll back the transaction at this time with
  2637. // no data loss, but leaving unregistered files scattered throughout the
  2638. // public zone.
  2639. // This is not ideal, which is why it's important to lock the image row.
  2640. if ( $insertCurrent ) {
  2641. $dbw->insert( 'image', $insertCurrent, __METHOD__ );
  2642. $commentCallback( $insertCurrent['img_name'] );
  2643. }
  2644. if ( $insertBatch ) {
  2645. $dbw->insert( 'oldimage', $insertBatch, __METHOD__ );
  2646. }
  2647. if ( $deleteIds ) {
  2648. $dbw->delete( 'filearchive',
  2649. [ 'fa_id' => $deleteIds ],
  2650. __METHOD__ );
  2651. }
  2652. // If store batch is empty (all files are missing), deletion is to be considered successful
  2653. if ( $status->successCount > 0 || !$storeBatch || $repo->hasSha1Storage() ) {
  2654. if ( !$exists ) {
  2655. wfDebug( __METHOD__ . " restored {$status->successCount} items, creating a new current\n" );
  2656. DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'images' => 1 ] ) );
  2657. $this->file->purgeEverything();
  2658. } else {
  2659. wfDebug( __METHOD__ . " restored {$status->successCount} as archived versions\n" );
  2660. $this->file->purgeDescription();
  2661. }
  2662. }
  2663. $this->file->unlock();
  2664. return $status;
  2665. }
  2666. /**
  2667. * Removes non-existent files from a store batch.
  2668. * @param array $triplets
  2669. * @return Status
  2670. */
  2671. protected function removeNonexistentFiles( $triplets ) {
  2672. $files = $filteredTriplets = [];
  2673. foreach ( $triplets as $file ) {
  2674. $files[$file[0]] = $file[0];
  2675. }
  2676. $result = $this->file->repo->fileExistsBatch( $files );
  2677. if ( in_array( null, $result, true ) ) {
  2678. return Status::newFatal( 'backend-fail-internal',
  2679. $this->file->repo->getBackend()->getName() );
  2680. }
  2681. foreach ( $triplets as $file ) {
  2682. if ( $result[$file[0]] ) {
  2683. $filteredTriplets[] = $file;
  2684. }
  2685. }
  2686. return Status::newGood( $filteredTriplets );
  2687. }
  2688. /**
  2689. * Removes non-existent files from a cleanup batch.
  2690. * @param string[] $batch
  2691. * @return string[]
  2692. */
  2693. protected function removeNonexistentFromCleanup( $batch ) {
  2694. $files = $newBatch = [];
  2695. $repo = $this->file->repo;
  2696. foreach ( $batch as $file ) {
  2697. $files[$file] = $repo->getVirtualUrl( 'deleted' ) . '/' .
  2698. rawurlencode( $repo->getDeletedHashPath( $file ) . $file );
  2699. }
  2700. $result = $repo->fileExistsBatch( $files );
  2701. foreach ( $batch as $file ) {
  2702. if ( $result[$file] ) {
  2703. $newBatch[] = $file;
  2704. }
  2705. }
  2706. return $newBatch;
  2707. }
  2708. /**
  2709. * Delete unused files in the deleted zone.
  2710. * This should be called from outside the transaction in which execute() was called.
  2711. * @return Status
  2712. */
  2713. public function cleanup() {
  2714. if ( !$this->cleanupBatch ) {
  2715. return $this->file->repo->newGood();
  2716. }
  2717. $this->cleanupBatch = $this->removeNonexistentFromCleanup( $this->cleanupBatch );
  2718. $status = $this->file->repo->cleanupDeletedBatch( $this->cleanupBatch );
  2719. return $status;
  2720. }
  2721. /**
  2722. * Cleanup a failed batch. The batch was only partially successful, so
  2723. * rollback by removing all items that were successfully copied.
  2724. *
  2725. * @param Status $storeStatus
  2726. * @param array[] $storeBatch
  2727. */
  2728. protected function cleanupFailedBatch( $storeStatus, $storeBatch ) {
  2729. $cleanupBatch = [];
  2730. foreach ( $storeStatus->success as $i => $success ) {
  2731. // Check if this item of the batch was successfully copied
  2732. if ( $success ) {
  2733. // Item was successfully copied and needs to be removed again
  2734. // Extract ($dstZone, $dstRel) from the batch
  2735. $cleanupBatch[] = [ $storeBatch[$i][1], $storeBatch[$i][2] ];
  2736. }
  2737. }
  2738. $this->file->repo->cleanupBatch( $cleanupBatch );
  2739. }
  2740. }
  2741. # ------------------------------------------------------------------------------
  2742. /**
  2743. * Helper class for file movement
  2744. * @ingroup FileAbstraction
  2745. */
  2746. class LocalFileMoveBatch {
  2747. /** @var LocalFile */
  2748. protected $file;
  2749. /** @var Title */
  2750. protected $target;
  2751. protected $cur;
  2752. protected $olds;
  2753. protected $oldCount;
  2754. protected $archive;
  2755. /** @var IDatabase */
  2756. protected $db;
  2757. /**
  2758. * @param File $file
  2759. * @param Title $target
  2760. */
  2761. function __construct( File $file, Title $target ) {
  2762. $this->file = $file;
  2763. $this->target = $target;
  2764. $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
  2765. $this->newHash = $this->file->repo->getHashPath( $this->target->getDBkey() );
  2766. $this->oldName = $this->file->getName();
  2767. $this->newName = $this->file->repo->getNameFromTitle( $this->target );
  2768. $this->oldRel = $this->oldHash . $this->oldName;
  2769. $this->newRel = $this->newHash . $this->newName;
  2770. $this->db = $file->getRepo()->getMasterDB();
  2771. }
  2772. /**
  2773. * Add the current image to the batch
  2774. */
  2775. public function addCurrent() {
  2776. $this->cur = [ $this->oldRel, $this->newRel ];
  2777. }
  2778. /**
  2779. * Add the old versions of the image to the batch
  2780. * @return string[] List of archive names from old versions
  2781. */
  2782. public function addOlds() {
  2783. $archiveBase = 'archive';
  2784. $this->olds = [];
  2785. $this->oldCount = 0;
  2786. $archiveNames = [];
  2787. $result = $this->db->select( 'oldimage',
  2788. [ 'oi_archive_name', 'oi_deleted' ],
  2789. [ 'oi_name' => $this->oldName ],
  2790. __METHOD__,
  2791. [ 'LOCK IN SHARE MODE' ] // ignore snapshot
  2792. );
  2793. foreach ( $result as $row ) {
  2794. $archiveNames[] = $row->oi_archive_name;
  2795. $oldName = $row->oi_archive_name;
  2796. $bits = explode( '!', $oldName, 2 );
  2797. if ( count( $bits ) != 2 ) {
  2798. wfDebug( "Old file name missing !: '$oldName' \n" );
  2799. continue;
  2800. }
  2801. list( $timestamp, $filename ) = $bits;
  2802. if ( $this->oldName != $filename ) {
  2803. wfDebug( "Old file name doesn't match: '$oldName' \n" );
  2804. continue;
  2805. }
  2806. $this->oldCount++;
  2807. // Do we want to add those to oldCount?
  2808. if ( $row->oi_deleted & File::DELETED_FILE ) {
  2809. continue;
  2810. }
  2811. $this->olds[] = [
  2812. "{$archiveBase}/{$this->oldHash}{$oldName}",
  2813. "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
  2814. ];
  2815. }
  2816. return $archiveNames;
  2817. }
  2818. /**
  2819. * Perform the move.
  2820. * @return Status
  2821. */
  2822. public function execute() {
  2823. $repo = $this->file->repo;
  2824. $status = $repo->newGood();
  2825. $destFile = wfLocalFile( $this->target );
  2826. $this->file->lock(); // begin
  2827. $destFile->lock(); // quickly fail if destination is not available
  2828. $triplets = $this->getMoveTriplets();
  2829. $checkStatus = $this->removeNonexistentFiles( $triplets );
  2830. if ( !$checkStatus->isGood() ) {
  2831. $destFile->unlock();
  2832. $this->file->unlock();
  2833. $status->merge( $checkStatus ); // couldn't talk to file backend
  2834. return $status;
  2835. }
  2836. $triplets = $checkStatus->value;
  2837. // Verify the file versions metadata in the DB.
  2838. $statusDb = $this->verifyDBUpdates();
  2839. if ( !$statusDb->isGood() ) {
  2840. $destFile->unlock();
  2841. $this->file->unlock();
  2842. $statusDb->setOK( false );
  2843. return $statusDb;
  2844. }
  2845. if ( !$repo->hasSha1Storage() ) {
  2846. // Copy the files into their new location.
  2847. // If a prior process fataled copying or cleaning up files we tolerate any
  2848. // of the existing files if they are identical to the ones being stored.
  2849. $statusMove = $repo->storeBatch( $triplets, FileRepo::OVERWRITE_SAME );
  2850. wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: " .
  2851. "{$statusMove->successCount} successes, {$statusMove->failCount} failures" );
  2852. if ( !$statusMove->isGood() ) {
  2853. // Delete any files copied over (while the destination is still locked)
  2854. $this->cleanupTarget( $triplets );
  2855. $destFile->unlock();
  2856. $this->file->unlock();
  2857. wfDebugLog( 'imagemove', "Error in moving files: "
  2858. . $statusMove->getWikiText( false, false, 'en' ) );
  2859. $statusMove->setOK( false );
  2860. return $statusMove;
  2861. }
  2862. $status->merge( $statusMove );
  2863. }
  2864. // Rename the file versions metadata in the DB.
  2865. $this->doDBUpdates();
  2866. wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: " .
  2867. "{$statusDb->successCount} successes, {$statusDb->failCount} failures" );
  2868. $destFile->unlock();
  2869. $this->file->unlock(); // done
  2870. // Everything went ok, remove the source files
  2871. $this->cleanupSource( $triplets );
  2872. $status->merge( $statusDb );
  2873. return $status;
  2874. }
  2875. /**
  2876. * Verify the database updates and return a new Status indicating how
  2877. * many rows would be updated.
  2878. *
  2879. * @return Status
  2880. */
  2881. protected function verifyDBUpdates() {
  2882. $repo = $this->file->repo;
  2883. $status = $repo->newGood();
  2884. $dbw = $this->db;
  2885. $hasCurrent = $dbw->lockForUpdate(
  2886. 'image',
  2887. [ 'img_name' => $this->oldName ],
  2888. __METHOD__
  2889. );
  2890. $oldRowCount = $dbw->lockForUpdate(
  2891. 'oldimage',
  2892. [ 'oi_name' => $this->oldName ],
  2893. __METHOD__
  2894. );
  2895. if ( $hasCurrent ) {
  2896. $status->successCount++;
  2897. } else {
  2898. $status->failCount++;
  2899. }
  2900. $status->successCount += $oldRowCount;
  2901. // T36934: oldCount is based on files that actually exist.
  2902. // There may be more DB rows than such files, in which case $affected
  2903. // can be greater than $total. We use max() to avoid negatives here.
  2904. $status->failCount += max( 0, $this->oldCount - $oldRowCount );
  2905. if ( $status->failCount ) {
  2906. $status->error( 'imageinvalidfilename' );
  2907. }
  2908. return $status;
  2909. }
  2910. /**
  2911. * Do the database updates and return a new Status indicating how
  2912. * many rows where updated.
  2913. */
  2914. protected function doDBUpdates() {
  2915. global $wgCommentTableSchemaMigrationStage;
  2916. $dbw = $this->db;
  2917. // Update current image
  2918. $dbw->update(
  2919. 'image',
  2920. [ 'img_name' => $this->newName ],
  2921. [ 'img_name' => $this->oldName ],
  2922. __METHOD__
  2923. );
  2924. if ( $wgCommentTableSchemaMigrationStage > MIGRATION_OLD ) {
  2925. $dbw->update(
  2926. 'image_comment_temp',
  2927. [ 'imgcomment_name' => $this->newName ],
  2928. [ 'imgcomment_name' => $this->oldName ],
  2929. __METHOD__
  2930. );
  2931. }
  2932. // Update old images
  2933. $dbw->update(
  2934. 'oldimage',
  2935. [
  2936. 'oi_name' => $this->newName,
  2937. 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name',
  2938. $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ),
  2939. ],
  2940. [ 'oi_name' => $this->oldName ],
  2941. __METHOD__
  2942. );
  2943. }
  2944. /**
  2945. * Generate triplets for FileRepo::storeBatch().
  2946. * @return array[]
  2947. */
  2948. protected function getMoveTriplets() {
  2949. $moves = array_merge( [ $this->cur ], $this->olds );
  2950. $triplets = []; // The format is: (srcUrl, destZone, destUrl)
  2951. foreach ( $moves as $move ) {
  2952. // $move: (oldRelativePath, newRelativePath)
  2953. $srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
  2954. $triplets[] = [ $srcUrl, 'public', $move[1] ];
  2955. wfDebugLog(
  2956. 'imagemove',
  2957. "Generated move triplet for {$this->file->getName()}: {$srcUrl} :: public :: {$move[1]}"
  2958. );
  2959. }
  2960. return $triplets;
  2961. }
  2962. /**
  2963. * Removes non-existent files from move batch.
  2964. * @param array $triplets
  2965. * @return Status
  2966. */
  2967. protected function removeNonexistentFiles( $triplets ) {
  2968. $files = [];
  2969. foreach ( $triplets as $file ) {
  2970. $files[$file[0]] = $file[0];
  2971. }
  2972. $result = $this->file->repo->fileExistsBatch( $files );
  2973. if ( in_array( null, $result, true ) ) {
  2974. return Status::newFatal( 'backend-fail-internal',
  2975. $this->file->repo->getBackend()->getName() );
  2976. }
  2977. $filteredTriplets = [];
  2978. foreach ( $triplets as $file ) {
  2979. if ( $result[$file[0]] ) {
  2980. $filteredTriplets[] = $file;
  2981. } else {
  2982. wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
  2983. }
  2984. }
  2985. return Status::newGood( $filteredTriplets );
  2986. }
  2987. /**
  2988. * Cleanup a partially moved array of triplets by deleting the target
  2989. * files. Called if something went wrong half way.
  2990. * @param array[] $triplets
  2991. */
  2992. protected function cleanupTarget( $triplets ) {
  2993. // Create dest pairs from the triplets
  2994. $pairs = [];
  2995. foreach ( $triplets as $triplet ) {
  2996. // $triplet: (old source virtual URL, dst zone, dest rel)
  2997. $pairs[] = [ $triplet[1], $triplet[2] ];
  2998. }
  2999. $this->file->repo->cleanupBatch( $pairs );
  3000. }
  3001. /**
  3002. * Cleanup a fully moved array of triplets by deleting the source files.
  3003. * Called at the end of the move process if everything else went ok.
  3004. * @param array[] $triplets
  3005. */
  3006. protected function cleanupSource( $triplets ) {
  3007. // Create source file names from the triplets
  3008. $files = [];
  3009. foreach ( $triplets as $triplet ) {
  3010. $files[] = $triplet[0];
  3011. }
  3012. $this->file->repo->cleanupBatch( $files );
  3013. }
  3014. }
  3015. class LocalFileLockError extends ErrorPageError {
  3016. public function __construct( Status $status ) {
  3017. parent::__construct(
  3018. 'actionfailed',
  3019. $status->getMessage()
  3020. );
  3021. }
  3022. public function report() {
  3023. global $wgOut;
  3024. $wgOut->setStatusCode( 429 );
  3025. parent::report();
  3026. }
  3027. }