1007225e5Sgerardnico<?php 2007225e5Sgerardnico/** 3007225e5Sgerardnico * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved. 4007225e5Sgerardnico * 5007225e5Sgerardnico * This source code is licensed under the GPL license found in the 6007225e5Sgerardnico * COPYING file in the root directory of this source tree. 7007225e5Sgerardnico * 8007225e5Sgerardnico * @license GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html) 9007225e5Sgerardnico * @author ComboStrap <support@combostrap.com> 10007225e5Sgerardnico * 11007225e5Sgerardnico */ 12007225e5Sgerardnico 13*04fd306cSNickeauuse ComboStrap\Meta\Field\BacklinkCount; 14c3437056SNickeauuse ComboStrap\Event; 15*04fd306cSNickeauuse ComboStrap\ExceptionBadSyntax; 16*04fd306cSNickeauuse ComboStrap\ExceptionCompile; 17*04fd306cSNickeauuse ComboStrap\ExceptionNotFound; 18*04fd306cSNickeauuse ComboStrap\ExceptionRuntime; 1937748cd8SNickeauuse ComboStrap\FsWikiUtility; 20c3437056SNickeauuse ComboStrap\LogUtility; 21*04fd306cSNickeauuse ComboStrap\MarkupPath; 22*04fd306cSNickeauuse ComboStrap\Meta\Field\PageH1; 23c3437056SNickeauuse ComboStrap\MetadataFrontmatterStore; 2471f916b9Sgerardnicouse ComboStrap\Sqlite; 25007225e5Sgerardnicouse splitbrain\phpcli\Options; 26007225e5Sgerardnico 27e8b2ff59SNickeau/** 28*04fd306cSNickeau * All dependency are loaded 29e8b2ff59SNickeau */ 30*04fd306cSNickeaurequire_once(__DIR__ . '/vendor/autoload.php'); 31007225e5Sgerardnico 32007225e5Sgerardnico/** 33007225e5Sgerardnico * The memory of the server 128 is not enough 34007225e5Sgerardnico */ 35007225e5Sgerardnicoini_set('memory_limit', '256M'); 36007225e5Sgerardnico 37c3437056SNickeau 38007225e5Sgerardnico/** 39007225e5Sgerardnico * Class cli_plugin_combo 40007225e5Sgerardnico * 41007225e5Sgerardnico * This is a cli: 42007225e5Sgerardnico * https://www.dokuwiki.org/devel:cli_plugins#example 43007225e5Sgerardnico * 44007225e5Sgerardnico * Usage: 45007225e5Sgerardnico * 46007225e5Sgerardnico * ``` 47007225e5Sgerardnico * docker exec -ti $(CONTAINER) /bin/bash 48c3437056SNickeau * ``` 49c3437056SNickeau * ``` 50c3437056SNickeau * set animal=animal-directory-name 51c3437056SNickeau * php ./bin/plugin.php combo --help 52007225e5Sgerardnico * ``` 53007225e5Sgerardnico * or via the IDE 54007225e5Sgerardnico * 55007225e5Sgerardnico * 56007225e5Sgerardnico * Example: 57007225e5Sgerardnico * https://www.dokuwiki.org/tips:grapher 58007225e5Sgerardnico * 59007225e5Sgerardnico */ 60007225e5Sgerardnicoclass cli_plugin_combo extends DokuWiki_CLI_Plugin 61007225e5Sgerardnico{ 62c3437056SNickeau 63c3437056SNickeau const METADATA_TO_DATABASE = "metadata-to-database"; 6471f916b9Sgerardnico const ANALYTICS = "analytics"; 65c3437056SNickeau const METADATA_TO_FRONTMATTER = "metadata-to-frontmatter"; 6671f916b9Sgerardnico const SYNC = "sync"; 67c3437056SNickeau const PLUGINS_TO_UPDATE = "plugins-to-update"; 68c3437056SNickeau const FORCE_OPTION = 'force'; 69c3437056SNickeau const PORT_OPTION = 'port'; 70c3437056SNickeau const HOST_OPTION = 'host'; 71c3437056SNickeau 72007225e5Sgerardnico 73007225e5Sgerardnico /** 74007225e5Sgerardnico * register options and arguments 75007225e5Sgerardnico * @param Options $options 7682a60d03SNickeau * 7782a60d03SNickeau * Note the animal is set in {@link DokuWikiFarmCore::detectAnimal()} 7882a60d03SNickeau * via the environment variable `animal` that is passed in the $_SERVER variable 79007225e5Sgerardnico */ 80007225e5Sgerardnico protected function setup(Options $options) 81007225e5Sgerardnico { 82c3437056SNickeau $help = <<<EOF 83c3437056SNickeauComboStrap Administrative Commands 84c3437056SNickeau 85c3437056SNickeau 86c3437056SNickeauExample: 87c3437056SNickeau * Replicate all pages into the database 88c3437056SNickeau```bash 89*04fd306cSNickeauphp ./bin/plugin.php combo metadata-to-database --host serverHostName --port 80 : 90c3437056SNickeau# or 91*04fd306cSNickeauphp ./bin/plugin.php combo metadata-to-database --host serverHostName --port 80 / 92c3437056SNickeau``` 93c3437056SNickeau * Replicate only the page `:namespace:my-page` 94c3437056SNickeau```bash 95*04fd306cSNickeauphp ./bin/plugin.php combo metadata-to-database --host serverHostName --port 80 :namespace:my-page 96c3437056SNickeau# or 97*04fd306cSNickeauphp ./bin/plugin.php combo metadata-to-database --host serverHostName --port 80 /namespace/my-page 98c3437056SNickeau``` 99c3437056SNickeau 100c3437056SNickeauAnimal: If you want to use it for an animal farm, you need to set first the animal directory name in a environment variable 101c3437056SNickeau```bash 1024cadd4f8SNickeauanimal=animal-directory-name php ./bin/plugin.php combo 103c3437056SNickeau``` 104c3437056SNickeau 105c3437056SNickeauEOF; 106c3437056SNickeau 107c3437056SNickeau $options->setHelp($help); 108007225e5Sgerardnico $options->registerOption('version', 'print version', 'v'); 109c3437056SNickeau $options->registerCommand(self::METADATA_TO_DATABASE, "Replicate the file system metadata into the database"); 110c3437056SNickeau $options->registerCommand(self::ANALYTICS, "Start the analytics and export optionally the data"); 111c3437056SNickeau $options->registerCommand(self::PLUGINS_TO_UPDATE, "List the plugins to update"); 112c3437056SNickeau $options->registerCommand(self::METADATA_TO_FRONTMATTER, "Replicate the file system metadata into the page frontmatter"); 113c3437056SNickeau $options->registerCommand(self::SYNC, "Delete the non-existing pages in the database"); 114c3437056SNickeau $options->registerArgument( 115c3437056SNickeau 'path', 116c3437056SNickeau "The start path (a page or a directory). For all pages, type the root directory '/'", 117c3437056SNickeau false 11871f916b9Sgerardnico ); 119007225e5Sgerardnico $options->registerOption( 120007225e5Sgerardnico 'output', 121007225e5Sgerardnico "Optional, where to store the analytical data as csv eg. a filename.", 122c3437056SNickeau 'o', 123c3437056SNickeau true 124c3437056SNickeau ); 125007225e5Sgerardnico $options->registerOption( 126c3437056SNickeau self::HOST_OPTION, 127c3437056SNickeau "The http host name of your server. This value is used by dokuwiki in the rendering cache key", 128c3437056SNickeau null, 129c3437056SNickeau true, 130c3437056SNickeau self::METADATA_TO_DATABASE 131c3437056SNickeau ); 132c3437056SNickeau $options->registerOption( 133c3437056SNickeau self::PORT_OPTION, 134c3437056SNickeau "The http host port of your server. This value is used by dokuwiki in the rendering cache key", 135c3437056SNickeau null, 136c3437056SNickeau true, 137c3437056SNickeau self::METADATA_TO_DATABASE 138c3437056SNickeau ); 139c3437056SNickeau $options->registerOption( 140c3437056SNickeau self::FORCE_OPTION, 141c3437056SNickeau "Replicate with force", 142c3437056SNickeau 'f', 143c3437056SNickeau false, 144c3437056SNickeau self::METADATA_TO_DATABASE 145c3437056SNickeau ); 14671f916b9Sgerardnico $options->registerOption( 14771f916b9Sgerardnico 'dry', 14871f916b9Sgerardnico "Optional, dry-run", 14971f916b9Sgerardnico 'd', false); 150c3437056SNickeau 151007225e5Sgerardnico 152007225e5Sgerardnico } 153007225e5Sgerardnico 154007225e5Sgerardnico /** 155007225e5Sgerardnico * The main entry 156007225e5Sgerardnico * @param Options $options 157*04fd306cSNickeau * @throws ExceptionCompile 158007225e5Sgerardnico */ 159007225e5Sgerardnico protected function main(Options $options) 160007225e5Sgerardnico { 161007225e5Sgerardnico 162007225e5Sgerardnico 16382a60d03SNickeau if(isset($_REQUEST['animal'])){ 1644cadd4f8SNickeau // on linux 16582a60d03SNickeau echo "Animal detected: ".$_REQUEST['animal']."\n"; 16682a60d03SNickeau } else { 1674cadd4f8SNickeau // on windows 16882a60d03SNickeau echo "No Animal detected\n"; 16982a60d03SNickeau echo "Conf: ".DOKU_CONF."\n"; 17082a60d03SNickeau } 17182a60d03SNickeau 172c3437056SNickeau $args = $options->getArgs(); 173c3437056SNickeau 174c3437056SNickeau 17571f916b9Sgerardnico $depth = $options->getOpt('depth', 0); 17621913ab3SNickeau $cmd = $options->getCmd(); 17721913ab3SNickeau switch ($cmd) { 178c3437056SNickeau case self::METADATA_TO_DATABASE: 179c3437056SNickeau $startPath = $this->getStartPath($args); 180c3437056SNickeau $force = $options->getOpt(self::FORCE_OPTION, false); 181c3437056SNickeau $hostOptionValue = $options->getOpt(self::HOST_OPTION, null); 182c3437056SNickeau if ($hostOptionValue === null) { 183c3437056SNickeau fwrite(STDERR, "The host name is mandatory"); 184c3437056SNickeau return; 185c3437056SNickeau } 186c3437056SNickeau $_SERVER['HTTP_HOST'] = $hostOptionValue; 187c3437056SNickeau $portOptionName = $options->getOpt(self::PORT_OPTION, null); 188c3437056SNickeau if ($portOptionName === null) { 189c3437056SNickeau fwrite(STDERR, "The host port is mandatory"); 190c3437056SNickeau return; 191c3437056SNickeau } 192c3437056SNickeau $_SERVER['SERVER_PORT'] = $portOptionName; 193c3437056SNickeau $this->index($startPath, $force, $depth); 194c3437056SNickeau break; 195c3437056SNickeau case self::METADATA_TO_FRONTMATTER: 196c3437056SNickeau $startPath = $this->getStartPath($args); 197c3437056SNickeau $this->frontmatter($startPath, $depth); 198c3437056SNickeau break; 19971f916b9Sgerardnico case self::ANALYTICS: 200c3437056SNickeau $startPath = $this->getStartPath($args); 201007225e5Sgerardnico $output = $options->getOpt('output', ''); 202007225e5Sgerardnico //if ($output == '-') $output = 'php://stdout'; 203c3437056SNickeau $this->analytics($startPath, $output, $depth); 20471f916b9Sgerardnico break; 20571f916b9Sgerardnico case self::SYNC: 206c3437056SNickeau $this->deleteNonExistingPageFromDatabase(); 207c3437056SNickeau break; 208c3437056SNickeau case self::PLUGINS_TO_UPDATE: 209c3437056SNickeau /** 210c3437056SNickeau * Endpoint: 211c3437056SNickeau * self::EXTENSION_REPOSITORY_API.'?fmt=php&ext[]='.urlencode($name) 212c3437056SNickeau * `http://www.dokuwiki.org/lib/plugins/pluginrepo/api.php?fmt=php&ext[]=`.urlencode($name) 213c3437056SNickeau */ 214c3437056SNickeau $pluginList = plugin_list('', true); 215c3437056SNickeau /* @var helper_plugin_extension_extension $extension */ 216c3437056SNickeau $extension = $this->loadHelper('extension_extension'); 217c3437056SNickeau foreach ($pluginList as $name) { 218c3437056SNickeau $extension->setExtension($name); 219c3437056SNickeau if ($extension->updateAvailable()) { 220c3437056SNickeau echo "The extension $name should be updated"; 221c3437056SNickeau } 222c3437056SNickeau } 22371f916b9Sgerardnico break; 22471f916b9Sgerardnico default: 225c3437056SNickeau if ($cmd !== "") { 226c3437056SNickeau fwrite(STDERR, "Combo: Command unknown (" . $cmd . ")"); 227c3437056SNickeau } else { 228c3437056SNickeau echo $options->help(); 229c3437056SNickeau } 230c3437056SNickeau exit(1); 23171f916b9Sgerardnico } 232007225e5Sgerardnico 233007225e5Sgerardnico 234007225e5Sgerardnico } 235007225e5Sgerardnico 236007225e5Sgerardnico /** 23771f916b9Sgerardnico * @param array $namespaces 238c3437056SNickeau * @param bool $rebuild 239007225e5Sgerardnico * @param int $depth recursion depth. 0 for unlimited 240*04fd306cSNickeau * @throws ExceptionCompile 241007225e5Sgerardnico */ 242c3437056SNickeau private function index($namespaces = array(), $rebuild = false, $depth = 0) 243c3437056SNickeau { 244c3437056SNickeau 245c3437056SNickeau /** 246c3437056SNickeau * Run as admin to overcome the fact that 247c3437056SNickeau * anonymous user cannot see all links and backlinks 248c3437056SNickeau */ 249c3437056SNickeau global $USERINFO; 250c3437056SNickeau $USERINFO['grps'] = array('admin'); 251c3437056SNickeau global $INPUT; 252c3437056SNickeau $INPUT->server->set('REMOTE_USER', "cli"); 253c3437056SNickeau 254c3437056SNickeau $pages = FsWikiUtility::getPages($namespaces, $depth); 255c3437056SNickeau 256c3437056SNickeau $pageCounter = 0; 257c3437056SNickeau $totalNumberOfPages = sizeof($pages); 258c3437056SNickeau while ($pageArray = array_shift($pages)) { 259c3437056SNickeau $id = $pageArray['id']; 2604cadd4f8SNickeau global $ID; 2614cadd4f8SNickeau $ID = $id; 262c3437056SNickeau /** 263c3437056SNickeau * Indexing the page start the database replication 264*04fd306cSNickeau * See {@link action_plugin_combo_indexer} 265c3437056SNickeau */ 266c3437056SNickeau $pageCounter++; 267c3437056SNickeau try { 268c3437056SNickeau /** 269c3437056SNickeau * If the page does not need to be indexed, there is no run 270c3437056SNickeau * and false is returned 271c3437056SNickeau */ 272c3437056SNickeau $indexedOrNot = idx_addPage($id, true, true); 273c3437056SNickeau if ($indexedOrNot) { 274c3437056SNickeau LogUtility::msg("The page {$id} ($pageCounter / $totalNumberOfPages) was indexed and replicated", LogUtility::LVL_MSG_INFO); 275c3437056SNickeau } else { 276c3437056SNickeau LogUtility::msg("The page {$id} ($pageCounter / $totalNumberOfPages) has an error", LogUtility::LVL_MSG_ERROR); 277c3437056SNickeau } 278*04fd306cSNickeau } catch (ExceptionRuntime $e) { 279c3437056SNickeau LogUtility::msg("The page {$id} ($pageCounter / $totalNumberOfPages) has an error: " . $e->getMessage(), LogUtility::LVL_MSG_ERROR); 280c3437056SNickeau } 281c3437056SNickeau } 282c3437056SNickeau /** 283c3437056SNickeau * Process all backlinks 284c3437056SNickeau */ 285c3437056SNickeau echo "Processing Replication Request\n"; 286c3437056SNickeau Event::dispatchEvent(PHP_INT_MAX); 287c3437056SNickeau 288c3437056SNickeau } 289c3437056SNickeau 290c3437056SNickeau private function analytics($namespaces = array(), $output = null, $depth = 0) 291007225e5Sgerardnico { 292007225e5Sgerardnico 293007225e5Sgerardnico $fileHandle = null; 294007225e5Sgerardnico if (!empty($output)) { 295007225e5Sgerardnico $fileHandle = @fopen($output, 'w'); 296007225e5Sgerardnico if (!$fileHandle) $this->fatal("Failed to open $output"); 297007225e5Sgerardnico } 298007225e5Sgerardnico 29937748cd8SNickeau /** 30037748cd8SNickeau * Run as admin to overcome the fact that 30137748cd8SNickeau * anonymous user cannot see all links and backlinks 30237748cd8SNickeau */ 30337748cd8SNickeau global $USERINFO; 30437748cd8SNickeau $USERINFO['grps'] = array('admin'); 30537748cd8SNickeau global $INPUT; 30637748cd8SNickeau $INPUT->server->set('REMOTE_USER', "cli"); 30737748cd8SNickeau 30837748cd8SNickeau $pages = FsWikiUtility::getPages($namespaces, $depth); 309007225e5Sgerardnico 310007225e5Sgerardnico 311007225e5Sgerardnico if (!empty($fileHandle)) { 312007225e5Sgerardnico $header = array( 313007225e5Sgerardnico 'id', 314007225e5Sgerardnico 'backlinks', 315007225e5Sgerardnico 'broken_links', 316007225e5Sgerardnico 'changes', 317007225e5Sgerardnico 'chars', 318007225e5Sgerardnico 'external_links', 319007225e5Sgerardnico 'external_medias', 320007225e5Sgerardnico 'h1', 321007225e5Sgerardnico 'h2', 322007225e5Sgerardnico 'h3', 323007225e5Sgerardnico 'h4', 324007225e5Sgerardnico 'h5', 325007225e5Sgerardnico 'internal_links', 326007225e5Sgerardnico 'internal_medias', 327007225e5Sgerardnico 'words', 328007225e5Sgerardnico 'score' 329007225e5Sgerardnico ); 330007225e5Sgerardnico fwrite($fileHandle, implode(",", $header) . PHP_EOL); 331007225e5Sgerardnico } 3329da76789Sgerardnico $pageCounter = 0; 333e8b2ff59SNickeau $totalNumberOfPages = sizeof($pages); 334c3437056SNickeau while ($pageArray = array_shift($pages)) { 335c3437056SNickeau $id = $pageArray['id']; 336*04fd306cSNickeau $page = MarkupPath::createMarkupFromId($id); 337c3437056SNickeau 338007225e5Sgerardnico 3399da76789Sgerardnico $pageCounter++; 340c3437056SNickeau echo "Analytics Processing for the page {$id} ($pageCounter / $totalNumberOfPages)\n"; 341007225e5Sgerardnico 342c3437056SNickeau /** 343c3437056SNickeau * Analytics 344c3437056SNickeau */ 345*04fd306cSNickeau $analyticsPath = $page->fetchAnalyticsPath(); 346*04fd306cSNickeau try { 347*04fd306cSNickeau $data = \ComboStrap\Json::createFromPath($analyticsPath)->toArray(); 348*04fd306cSNickeau } catch (ExceptionBadSyntax $e) { 349*04fd306cSNickeau LogUtility::error("The analytics json of the page ($page) is not conform"); 350*04fd306cSNickeau continue; 351*04fd306cSNickeau } catch (ExceptionNotFound $e) { 352*04fd306cSNickeau LogUtility::error("The analytics document ({$analyticsPath}) for the page ($page) was not found"); 353*04fd306cSNickeau continue; 354*04fd306cSNickeau } 355c3437056SNickeau 356007225e5Sgerardnico if (!empty($fileHandle)) { 357*04fd306cSNickeau $statistics = $data[renderer_plugin_combo_analytics::STATISTICS]; 358007225e5Sgerardnico $row = array( 359007225e5Sgerardnico 'id' => $id, 360c3437056SNickeau 'backlinks' => $statistics[BacklinkCount::getPersistentName()], 361*04fd306cSNickeau 'broken_links' => $statistics[renderer_plugin_combo_analytics::INTERNAL_LINK_BROKEN_COUNT], 362*04fd306cSNickeau 'changes' => $statistics[renderer_plugin_combo_analytics::EDITS_COUNT], 363*04fd306cSNickeau 'chars' => $statistics[renderer_plugin_combo_analytics::CHAR_COUNT], 364*04fd306cSNickeau 'external_links' => $statistics[renderer_plugin_combo_analytics::EXTERNAL_LINK_COUNT], 365*04fd306cSNickeau 'external_medias' => $statistics[renderer_plugin_combo_analytics::EXTERNAL_MEDIA_COUNT], 366*04fd306cSNickeau PageH1::PROPERTY_NAME => $statistics[renderer_plugin_combo_analytics::HEADING_COUNT][PageH1::PROPERTY_NAME], 367*04fd306cSNickeau 'h2' => $statistics[renderer_plugin_combo_analytics::HEADING_COUNT]['h2'], 368*04fd306cSNickeau 'h3' => $statistics[renderer_plugin_combo_analytics::HEADING_COUNT]['h3'], 369*04fd306cSNickeau 'h4' => $statistics[renderer_plugin_combo_analytics::HEADING_COUNT]['h4'], 370*04fd306cSNickeau 'h5' => $statistics[renderer_plugin_combo_analytics::HEADING_COUNT]['h5'], 371*04fd306cSNickeau 'internal_links' => $statistics[renderer_plugin_combo_analytics::INTERNAL_LINK_COUNT], 372*04fd306cSNickeau 'internal_medias' => $statistics[renderer_plugin_combo_analytics::INTERNAL_MEDIA_COUNT], 373*04fd306cSNickeau 'words' => $statistics[renderer_plugin_combo_analytics::WORD_COUNT], 374*04fd306cSNickeau 'low' => $data[renderer_plugin_combo_analytics::QUALITY]['low'] 375007225e5Sgerardnico ); 376007225e5Sgerardnico fwrite($fileHandle, implode(",", $row) . PHP_EOL); 377007225e5Sgerardnico } 378c3437056SNickeau 379007225e5Sgerardnico } 380007225e5Sgerardnico if (!empty($fileHandle)) { 381007225e5Sgerardnico fclose($fileHandle); 382007225e5Sgerardnico } 383007225e5Sgerardnico 384007225e5Sgerardnico } 38571f916b9Sgerardnico 386325fe0c5Sgerardnico 387c3437056SNickeau private function deleteNonExistingPageFromDatabase() 38871f916b9Sgerardnico { 389c3437056SNickeau LogUtility::msg("Starting: Deleting non-existing page from database"); 390c3437056SNickeau $sqlite = Sqlite::createOrGetSqlite(); 391c3437056SNickeau $request = $sqlite 392c3437056SNickeau ->createRequest() 393c3437056SNickeau ->setQuery("select id as \"id\" from pages"); 394c3437056SNickeau $rows = []; 395c3437056SNickeau try { 396c3437056SNickeau $rows = $request 397c3437056SNickeau ->execute() 398c3437056SNickeau ->getRows(); 399*04fd306cSNickeau } catch (ExceptionCompile $e) { 400c3437056SNickeau LogUtility::msg("Error while getting the id pages. {$e->getMessage()}"); 401c3437056SNickeau return; 402c3437056SNickeau } finally { 403c3437056SNickeau $request->close(); 40471f916b9Sgerardnico } 405c3437056SNickeau $counter = 0; 406c3437056SNickeau foreach ($rows as $row) { 407c3437056SNickeau $counter++; 408c3437056SNickeau $id = $row['id']; 40971f916b9Sgerardnico if (!page_exists($id)) { 41071f916b9Sgerardnico echo 'Page does not exist on the file system. Deleted from the database (' . $id . ")\n"; 411*04fd306cSNickeau MarkupPath::createMarkupFromId($id)->getDatabasePage()->delete(); 412c3437056SNickeau } 413c3437056SNickeau } 414c3437056SNickeau LogUtility::msg("Sync finished ($counter pages checked)"); 415c3437056SNickeau 416c3437056SNickeau 417c3437056SNickeau } 418c3437056SNickeau 419c3437056SNickeau private function frontmatter($namespaces, $depth) 420c3437056SNickeau { 421c3437056SNickeau $pages = FsWikiUtility::getPages($namespaces, $depth); 422c3437056SNickeau $pageCounter = 0; 423c3437056SNickeau $totalNumberOfPages = sizeof($pages); 424c3437056SNickeau $pagesWithChanges = []; 425c3437056SNickeau $pagesWithError = []; 426c3437056SNickeau $pagesWithOthers = []; 427c3437056SNickeau $notChangedCounter = 0; 428c3437056SNickeau while ($pageArray = array_shift($pages)) { 429c3437056SNickeau $id = $pageArray['id']; 4304cadd4f8SNickeau global $ID; 4314cadd4f8SNickeau $ID = $id; 432*04fd306cSNickeau $page = MarkupPath::createMarkupFromId($id); 433c3437056SNickeau $pageCounter++; 434c3437056SNickeau LogUtility::msg("Processing page {$id} ($pageCounter / $totalNumberOfPages) ", LogUtility::LVL_MSG_INFO); 435c3437056SNickeau try { 436c3437056SNickeau $message = MetadataFrontmatterStore::createFromPage($page) 437c3437056SNickeau ->sync(); 438c3437056SNickeau switch ($message->getStatus()) { 439c3437056SNickeau case syntax_plugin_combo_frontmatter::UPDATE_EXIT_CODE_NOT_CHANGED: 440c3437056SNickeau $notChangedCounter++; 441c3437056SNickeau break; 442c3437056SNickeau case syntax_plugin_combo_frontmatter::UPDATE_EXIT_CODE_DONE: 443c3437056SNickeau $pagesWithChanges[] = $id; 444c3437056SNickeau break; 445c3437056SNickeau case syntax_plugin_combo_frontmatter::UPDATE_EXIT_CODE_ERROR: 446c3437056SNickeau $pagesWithError[$id] = $message->getPlainTextContent(); 447c3437056SNickeau break; 448c3437056SNickeau default: 449c3437056SNickeau $pagesWithOthers[$id] = $message->getPlainTextContent(); 450c3437056SNickeau break; 451c3437056SNickeau 452c3437056SNickeau } 453*04fd306cSNickeau } catch (ExceptionCompile $e) { 454c3437056SNickeau $pagesWithError[$id] = $e->getMessage(); 455c3437056SNickeau } 456c3437056SNickeau 457c3437056SNickeau } 458c3437056SNickeau 459c3437056SNickeau echo "\n"; 460c3437056SNickeau echo "Result:\n"; 461c3437056SNickeau echo "$notChangedCounter pages without any frontmatter modifications\n"; 462c3437056SNickeau 463c3437056SNickeau if (sizeof($pagesWithError) > 0) { 464c3437056SNickeau echo "\n"; 465c3437056SNickeau echo "The following pages had errors\n"; 466c3437056SNickeau $pageCounter = 0; 467c3437056SNickeau $totalNumberOfPages = sizeof($pagesWithError); 468c3437056SNickeau foreach ($pagesWithError as $id => $message) { 469c3437056SNickeau $pageCounter++; 470c3437056SNickeau LogUtility::msg("Page {$id} ($pageCounter / $totalNumberOfPages): " . $message, LogUtility::LVL_MSG_ERROR); 471c3437056SNickeau } 472c3437056SNickeau } else { 473c3437056SNickeau echo "No error\n"; 474c3437056SNickeau } 475c3437056SNickeau 476c3437056SNickeau if (sizeof($pagesWithChanges) > 0) { 477c3437056SNickeau echo "\n"; 478c3437056SNickeau echo "The following pages had changed:\n"; 479c3437056SNickeau $pageCounter = 0; 480c3437056SNickeau $totalNumberOfPages = sizeof($pagesWithChanges); 481c3437056SNickeau foreach ($pagesWithChanges as $id) { 482c3437056SNickeau $pageCounter++; 483c3437056SNickeau LogUtility::msg("Page {$id} ($pageCounter / $totalNumberOfPages) ", LogUtility::LVL_MSG_ERROR); 484c3437056SNickeau } 485c3437056SNickeau } else { 486c3437056SNickeau echo "No changes\n"; 487c3437056SNickeau } 488c3437056SNickeau 489c3437056SNickeau if (sizeof($pagesWithOthers) > 0) { 490c3437056SNickeau echo "\n"; 491c3437056SNickeau echo "The following pages had an other status"; 492c3437056SNickeau $pageCounter = 0; 493c3437056SNickeau $totalNumberOfPages = sizeof($pagesWithOthers); 494c3437056SNickeau foreach ($pagesWithOthers as $id => $message) { 495c3437056SNickeau $pageCounter++; 496c3437056SNickeau LogUtility::msg("Page {$id} ($pageCounter / $totalNumberOfPages) " . $message, LogUtility::LVL_MSG_ERROR); 497c3437056SNickeau } 49871f916b9Sgerardnico } 49971f916b9Sgerardnico } 50071f916b9Sgerardnico 501c3437056SNickeau private function getStartPath($args) 502c3437056SNickeau { 503c3437056SNickeau $sizeof = sizeof($args); 504c3437056SNickeau switch ($sizeof) { 505c3437056SNickeau case 0: 506c3437056SNickeau fwrite(STDERR, "The start path is mandatory and was not given"); 507c3437056SNickeau exit(1); 508c3437056SNickeau case 1: 509c3437056SNickeau $startPath = $args[0]; 510c3437056SNickeau if (!in_array($startPath, [":", "/"])) { 511c3437056SNickeau // cleanId would return blank for a root 512c3437056SNickeau $startPath = cleanID($startPath); 513c3437056SNickeau } 514c3437056SNickeau break; 515c3437056SNickeau default: 516c3437056SNickeau fwrite(STDERR, "Too much arguments given $sizeof"); 517c3437056SNickeau exit(1); 518c3437056SNickeau } 519c3437056SNickeau return $startPath; 52071f916b9Sgerardnico } 521007225e5Sgerardnico} 522