@@ -102,393 +102,6 @@ public function microtime_diff(
102102 return floatval ( $ diff_sec ) + $ diff_usec ;
103103 }
104104
105-
106- /**
107- * Get info on plugin status.
108- *
109- * ## OPTIONS
110- *
111- * [--no-next]
112- *
113- * <jobs>
114- *
115- * Get status on jobs
116- *
117- * <crawled-site>
118- *
119- * Get status on the crawled site
120- *
121- * <processed-site>
122- *
123- * Get status on the processed site
124- *
125- * <deployers>
126- *
127- * Get info on deployers
128- *
129- * ## EXAMPLES
130- *
131- * List current plugin status and show next step
132- *
133- * wp wp2static status
134- *
135- * List current plugin status but don't show next step
136- *
137- * wp wp2static status --no-next
138- *
139- * @param string[] $args Arguments after command
140- * @param string[] $assoc_args Parameters after command
141- */
142- public function status (
143- array $ args ,
144- array $ assoc_args
145- ): void {
146- list ($ action ) = $ this ->parseArgs ( $ args );
147- $ this ->assoc_args = $ assoc_args ;
148-
149- if ( $ action === 'jobs ' ) {
150- $ this ->jobStatus ();
151- } elseif ( $ action === 'crawled-site ' ) {
152- $ this ->crawledSiteStatus ();
153- } elseif ( $ action === 'processed-site ' ) {
154- $ this ->processedSiteStatus ();
155- } elseif ( $ action === 'deployers ' ) {
156- $ this ->deployersStatus ();
157- } else {
158- $ this ->defaultStatus ();
159- }
160-
161- // separate output from next command
162- WP_CLI ::line ();
163- }
164-
165- /**
166- * Output default status
167- */
168- protected function defaultStatus (): void {
169- // TODO handle when count doesn't change but different URLs are present
170- // - maybe that's an edge case that it's not worth caring about...
171- $ detected_url_count = URLDetector::countURLs ();
172- // WP_CLI::line(sprintf('%d URLs detected', $detected_url_count));
173-
174- $ crawlable_urls = DetectedFiles::getCrawlablePaths ();
175- if ( count ( $ crawlable_urls ) === 0 ) {
176- WP_CLI ::line ( 'No URLs are queued for crawling. ' );
177- $ this ->hintDetectNext ();
178- return ;
179- } elseif ( $ detected_url_count > count ( $ crawlable_urls ) ) {
180- WP_CLI ::warning (
181- sprintf (
182- 'There are more URLs on the site (%d) than queued for crawling (%d). ' ,
183- $ detected_url_count ,
184- count ( $ crawlable_urls )
185- )
186- );
187- $ this ->hintDetectNext ();
188- return ;
189- } else {
190- WP_CLI ::line (
191- WP_CLI ::colorize (
192- sprintf ( '%%c%d URLs%%n %%mqueued for crawling%%n ' , count ( $ crawlable_urls ) )
193- )
194- );
195- }
196-
197- $ crawled_urls = StaticSite::getPaths ();
198- if ( $ crawled_urls ) {
199- WP_CLI ::line (
200- WP_CLI ::colorize (
201- sprintf ( '%%c%d URLs%%n %%mcrawled%%n ' , count ( $ crawled_urls ) )
202- )
203- );
204- if ( count ( $ crawled_urls ) < count ( $ crawlable_urls ) ) {
205- WP_CLI ::line (
206- WP_CLI ::colorize (
207- sprintf (
208- '%%MThere are more URLs queued for crawling (%d) '
209- . ' than there are urls that have been crawled (%d)%%n ' ,
210- count ( $ crawlable_urls ),
211- count ( $ crawled_urls )
212- )
213- )
214- );
215-
216- if ( $ this ->should_show_next () ) {
217- WP_CLI ::line (
218- WP_CLI ::colorize (
219- "\n\tYou can run `%gwp wp2static status crawled-site%n` "
220- . " to see the list of path differences \n"
221- )
222- );
223- }
224- $ this ->hintCrawlNext ();
225- }
226- } else {
227- WP_CLI ::line ( 'No URLs crawled. ' );
228- $ this ->hintCrawlNext ();
229- }
230-
231- $ processed_site_path = ProcessedSite::getPath ();
232- $ processed_site_urls = [];
233- if ( ! is_dir ( $ processed_site_path ) ) {
234- WP_CLI ::line (
235- WP_CLI ::colorize ( '%rProcessed site does not exist%n ' )
236- );
237- $ this ->hintProcessNext ();
238- } else {
239- $ processed_site_urls = ProcessedSite::getPaths ();
240- WP_CLI ::line (
241- WP_CLI ::colorize (
242- sprintf (
243- '%%c%d URLs%%n in the %%mprocessed site%%n ' ,
244- count ( $ processed_site_urls )
245- )
246- )
247- );
248- if ( count ( $ processed_site_urls ) < count ( $ crawled_urls ) ) {
249- WP_CLI ::line (
250- WP_CLI ::colorize (
251- sprintf (
252- '%%MThere are more URLs crawled (%d) '
253- . ' than there are urls that have been processed (%d)%%n ' ,
254- count ( $ crawled_urls ),
255- count ( $ processed_site_urls )
256- )
257- )
258- );
259-
260- if ( $ this ->should_show_next () ) {
261- WP_CLI ::line (
262- WP_CLI ::colorize (
263- "\n\tYou can run `%gwp wp2static status processed-site%n` "
264- . " to see the list of path differences \n"
265- )
266- );
267- }
268- $ this ->hintProcessNext ();
269- }
270- }
271-
272- if ( count ( $ processed_site_urls ) > 0 ) {
273- $ deployer_enabled = Addons::getDeployer ();
274- if ( $ deployer_enabled ) {
275- $ this ->hintDeployNext ();
276- } else {
277- $ this ->hintEnableDeployer ();
278- }
279- }
280- }
281-
282- private function deployersStatus (): void {
283- $ deployers = Addons::getAll ( 'deploy ' );
284-
285- $ data = array_map (
286- function ( $ _ ) {
287- return [
288- 'name ' => $ _ ->name ,
289- 'slug ' => $ _ ->slug ,
290- 'description ' => $ _ ->description ,
291- 'enabled ' => $ _ ->enabled ? 'Yes ' : 'No ' ,
292- ];
293- },
294- $ deployers
295- );
296-
297- WP_CLI \Utils \format_items (
298- 'table ' ,
299- $ data ,
300- [ 'name ' , 'description ' , 'slug ' , 'enabled ' ]
301- );
302-
303- if ( Addons::getDeployer () ) {
304- $ this ->hintDeployNext ();
305- } else {
306- $ this ->hintEnableDeployer ();
307- }
308- }
309-
310- private function hintEnableDeployer (): void {
311- if ( $ this ->should_show_next () ) {
312- WP_CLI ::line (
313- WP_CLI ::colorize (
314- "\n\tYou can run `%gwp wp2static addons toggle <slug>%n` "
315- . ' to enable a deployer '
316- )
317- );
318- }
319- }
320-
321- private function hintDeployNext (): void {
322- if ( $ this ->should_show_next () ) {
323- WP_CLI ::line (
324- WP_CLI ::colorize (
325- "\n\tYou can run `%gwp wp2static deploy%n` "
326- . ' to deploy your site '
327- )
328- );
329- }
330- }
331-
332- private function crawledSiteStatus (): void {
333- $ crawlable_urls = array_map (
334- '\WP2Static\StaticSite::transformPath ' ,
335- DetectedFiles::getCrawlablePaths (),
336- );
337- $ crawled_urls = StaticSite::getPaths ();
338-
339- $ queued_but_not_crawled_urls = array_diff ( $ crawlable_urls , $ crawled_urls );
340- $ crawled_but_not_queued_urls = array_diff ( $ crawled_urls , $ crawlable_urls );
341-
342- if ( count ( $ queued_but_not_crawled_urls ) > 0 ) {
343- WP_CLI ::line ( 'You have URLs that are queued but not crawled: ' );
344- WP_CLI \Utils \format_items (
345- 'table ' ,
346- $ this ->urlsToTableData ( $ queued_but_not_crawled_urls ),
347- [ 'url ' ]
348- );
349- }
350-
351- if ( count ( $ crawled_but_not_queued_urls ) > 0 ) {
352- WP_CLI ::line ( 'You have URLs that are crawled but not queued: ' );
353- WP_CLI \Utils \format_items (
354- 'table ' ,
355- $ this ->urlsToTableData ( $ crawled_but_not_queued_urls ),
356- [ 'url ' ]
357- );
358- }
359- }
360-
361- private function processedSiteStatus (): void {
362- $ crawled_urls = StaticSite::getPaths ();
363- $ processed_site_urls = ProcessedSite::getPaths ();
364-
365- $ crawled_but_not_processed_urls = array_diff ( $ crawled_urls , $ processed_site_urls );
366- $ processed_but_not_crawled_urls = array_diff ( $ processed_site_urls , $ crawled_urls );
367-
368- if ( count ( $ crawled_but_not_processed_urls ) > 0 ) {
369- WP_CLI ::line ( 'You have URLs that are crawled but not processed: ' );
370- WP_CLI \Utils \format_items (
371- 'table ' ,
372- $ this ->urlsToTableData ( $ crawled_but_not_processed_urls ),
373- [ 'url ' ]
374- );
375- }
376-
377- if ( count ( $ processed_but_not_crawled_urls ) > 0 ) {
378- if ( count ( $ crawled_but_not_processed_urls ) > 0 ) {
379- WP_CLI ::line ();
380- }
381-
382- WP_CLI ::line ( 'You have URLs that are processed but not crawled: ' );
383- WP_CLI \Utils \format_items (
384- 'table ' ,
385- $ this ->urlsToTableData ( $ processed_but_not_crawled_urls ),
386- [ 'url ' ]
387- );
388- }
389- }
390-
391- /**
392- * Convert a list of URLs to a table for CLI output
393- *
394- * @param array<string> $urls List of URLs
395- * @return array<array<string>>
396- */
397- private function urlsToTableData ( array $ urls ): array {
398- return array_map (
399- function ( $ url ) {
400- return [ 'url ' => $ url ]; },
401- $ urls
402- );
403- }
404-
405- private function hintProcessNext (): void {
406- if ( $ this ->should_show_next () ) {
407- WP_CLI ::line (
408- WP_CLI ::colorize ( "\n\tYou should run `%gwp wp2static post_process%n` " )
409- );
410- }
411- }
412-
413- /**
414- * Show next action hint for detect unless suppressed.
415- *
416- * @uses should_show_next() to know if should show next or not
417- */
418- private function hintDetectNext (): void {
419- if ( $ this ->should_show_next () ) {
420- WP_CLI ::line (
421- WP_CLI ::colorize (
422- "\n\tYou should run `%gwp wp2static detect%n`. "
423- )
424- );
425- }
426- }
427-
428- /**
429- * Show next action hint for crawl unless suppressed.
430- *
431- * @uses should_show_next() to know if should show next or not
432- */
433- private function hintCrawlNext (): void {
434- if ( $ this ->should_show_next () ) {
435- WP_CLI ::line (
436- WP_CLI ::colorize ( "\n\tYou should run `%gwp wp2static crawl%n` \n" )
437- );
438- }
439- }
440-
441- /**
442- * Check to see if display of next is enabled
443- */
444- private function should_show_next (): bool {
445- if ( ! isset ( $ this ->assoc_args ['next ' ] ) ) {
446- return true ;
447- }
448-
449- return (bool ) $ this ->assoc_args ['next ' ];
450- }
451-
452- /**
453- * Parse CLI args
454- *
455- * @param string[] $args CLI args
456- *
457- * @return mixed[] string or null: `[ $action, $option_name, $value ]`
458- */
459- private function parseArgs ( $ args ) {
460- $ action = isset ( $ args [0 ] ) ? $ args [0 ] : null ;
461- $ option_name = isset ( $ args [1 ] ) ? $ args [1 ] : null ;
462- $ value = isset ( $ args [2 ] ) ? $ args [2 ] : null ;
463-
464- return [ $ action , $ option_name , $ value ];
465- }
466-
467- /**
468- * Output job status details.
469- */
470- protected function jobStatus (): void {
471- $ job_count = JobQueue::getWaitingJobsCount ();
472- WP_CLI ::line ( sprintf ( 'Waiting job count: %d ' , $ job_count ) );
473- WP_CLI ::line (
474- sprintf (
475- 'Total job count: %d ' ,
476- JobQueue::getTotalJobs ()
477- )
478- );
479-
480- $ jobs_by_type = JobQueue::getJobCountByType ();
481- $ output_data = [];
482- $ output_headers = [ 'type ' , 'count ' ];
483- foreach ( $ jobs_by_type as $ type => $ count ) {
484- $ output_data [] = [
485- 'type ' => $ type ,
486- 'count ' => $ count ,
487- ];
488- }
489- WP_CLI \Utils \format_items ( 'table ' , $ output_data , $ output_headers );
490- }
491-
492105 /**
493106 * Deploy the generated static site.
494107 * ## OPTIONS
0 commit comments