summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/WfextStyle/maintenance/generateThumbsImages.php
blob: 62260bbff4b7cb27594b90ab17ffa7e9d4e3307b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
 * Run pending jobs.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

require_once __DIR__ . '/../../../maintenance/Maintenance.php';

/**
 * Maintenance script that runs pending jobs.
 *
 * @ingroup Maintenance
 */
class GenerateThumbsImages extends Maintenance {

	protected $sizes = [];

	public function __construct() {
		parent::__construct();
		$this->addDescription( 'Run pending jobs' );
		$this->addOption( 'reallyRun', 'if not set, this will only show what it would have done', false, false );
		$this->addOption( 'maxThumbs', 'Maximum number of thumbs to generat', false, true );
		$this->addOption( 'maxTime', 'Maximum amount of wall-clock time', false, true );
		$this->addOption( 'startName', 'imageNameToStart', false, true );
		$this->addOption( 'lastMin', 'number of min for select recents pictures only ', false, true );
	}

	public function execute() {
		global $wgCommandLineMode, $wgThumbLimits;

		$this->sizes = $wgThumbLimits;


		$reallyRun = $this->hasOption( 'reallyRun' );

		$maxThumbs = $this->getOption( 'maxThumbs', false );
		$maxTime = $this->getOption( 'maxTime', false );

		$this->output("Really run : $reallyRun \n");
		$this->output("Really maxThumbs : $maxThumbs \n");


		// look for all files :
		$titles =[];

		$this->mBatchSize = 20;
		$lastName = $this->getOption( 'startName', '' );
		$lastMin = $this->getOption( 'lastMin', '' );

		$conds = [];

		$count = 0;
		$startTime = wfTimestampNow();

		$repo = RepoGroup::singleton()->getLocalRepo();
		//$repo->enumFiles( ['GenerateThumbsImages', 'generateThumbsImagesCallBack'] ) ;
		$dbw = $repo->getMasterDB ();

		if ($lastMin) {
			$lastMinTimestamp = date("YmdH0000", strtotime("-" . $lastMin . " minutes"));
			$this->output( "timestamp : $lastMinTimestamp \n" );
			$conds[] = 'img_timestamp >= ' .$lastMinTimestamp;
		}

		$totalCount = $dbw->estimateRowCount('image', $vars = '*', $conds);
		$this->output( "Nb image to check : $totalCount \n" );

		do {
			$res = $dbw->select ( 'image',
				array ('img_name', 'img_sha1'),
				array_merge ( array ('img_name > ' . $dbw->addQuotes ( $lastName )), $conds ),
				__METHOD__,
				array (
					'LIMIT' => $this->mBatchSize,
					'ORDER BY' => 'img_name'
				)
			);
			foreach ( $res as $row ) {
				$lastName = $row->img_name;
				$sha1 = $row->img_sha1;
				if (! strlen ( $sha1 )) {
					$this->error ( "Image SHA-1 not set for {$row->img_name}." );
				} else {
					$file = $repo->newFile ( $row->img_name );
					$this->generateAllThumbs($file, $reallyRun);

				}
			}
			$count += $res->numRows ();
			if ($maxThumbs && $count >= $maxThumbs ) {
				break;
			}
			$this->output( "processing images $count/$totalCount \n" );
		} while ( $res->numRows () );
	}

	public function generateAllThumbs(File $file, $really) {

		foreach ($this->sizes as $size) {


			$thumbName = $file->thumbName( ['width' => $size] );
			$thumbPath = $file->getThumbPath( $thumbName );
			//echo '<br/>';
			$exist = $file->getRepo()->fileExists( $thumbPath );
			if ( !$exist ) {
				$transformParams = ["width" => $size ];
				$thumb = $file->transform( $transformParams,File::RENDER_NOW);

				if ( $thumb && !$thumb->isError()) {
					$result = True;
				} else {
					$result = 'fail';
				}
				$this->output("generate Thumb size ". $size  ." file :" . $file->getName() . " result : $result\n");
			} else {
				$this->output("existing Thumb size ". $size  ." file :" . $file->getName() . " \n");
			}
		}
	}

	protected function getRepo() {
		return RepoGroup::singleton()->getLocalRepo();
	}

	/**
	 * @param string $s
	 */
	public function debugInternal( $s ) {
		$this->output( $s );
	}
}

$maintClass = "GenerateThumbsImages";
require_once RUN_MAINTENANCE_IF_MAIN;