1<?php
2
3/**
4 * Performs an Automatic Link Verification over all the faq records.
5 *
6 * You can set a cron entry:
7 * a. using PHP CLI
8 * b. using a Web Hit to this file
9 *
10 * This Source Code Form is subject to the terms of the Mozilla Public License,
11 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
12 * obtain one at http://mozilla.org/MPL/2.0/.
13 *
14 * @package phpMyFAQ
15 * @author Matteo Scaramuccia <matteo@phpmyfaq.de>
16 * @author Thorsten Rinne <thorsten@phpmyfaq.de>
17 * @copyright 2006-2020 phpMyFAQ Team
18 * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
19 * @link https://www.phpmyfaq.de
20 * @since 2006-09-17
21 */
22
23use phpMyFAQ\Faq;
24use phpMyFAQ\Language;
25use phpMyFAQ\Language\Plurals;
26use phpMyFAQ\LinkVerifier;
27use phpMyFAQ\Strings;
28use phpMyFAQ\Utils;
29
30/**
31 * This is the flag with which you define the language of this cron script.
32 *
33 * @var string en
34 */
35define('LANGCODE', 'en');
36
37// Do not change anything below this line!
38define('PMF_ROOT_DIR', __DIR__);
39
40$output = '';
41$isCronRequest = false;
42$isRequestedByCLI = isset($_SERVER['argv']) && (isset($_SERVER['argv'][0]));
43$isRequestedByWebLocalhost = isset($_SERVER['REMOTE_ADDR']) && ('127.0.0.1' == $_SERVER['REMOTE_ADDR']);
44
45$isCronRequest = $isRequestedByCLI || $isRequestedByWebLocalhost;
46
47if ($isCronRequest && file_exists(PMF_ROOT_DIR . '/config/database.php')) {
48    // Hack: set dummy values for those entries evaluated during a Web request but not during a CLI request
49    if ($isRequestedByCLI) {
50        $_SERVER['HTTP_HOST'] = '';
51        $_SERVER['HTTP_USER_AGENT'] = '';
52    }
53
54    define('IS_VALID_PHPMYFAQ', null);
55
56    require PMF_ROOT_DIR . '/src/Bootstrap.php';
57
58    // Preload English strings
59    require_once PMF_ROOT_DIR . '/lang/language_en.php';
60
61    if ((LANGCODE != 'en') && Language::isASupportedLanguage(LANGCODE)) {
62        // Overwrite English strings with the ones we have in the current language
63        require_once PMF_ROOT_DIR . '/lang/language_' . LANGCODE . '.php';
64    }
65
66    //Load plurals support for selected language
67    $plr = new Plurals($PMF_LANG);
68
69    //
70    // Initalizing static string wrapper
71    //
72    Strings::init(LANGCODE);
73
74    $oLnk = new LinkVerifier($faqConfig);
75    $faq = new Faq($faqConfig);
76    $totStart = microtime(true);
77
78    // Read the data directly from the faqdata table (all faq records in all languages)
79    $start = microtime(true);
80    $output .= ($isRequestedByWebLocalhost ? '' : "\n");
81    $output .= 'Extracting faq records...';
82
83    $faq->getAllRecords();
84    $_records = $faq->faqRecords;
85    $tot = count($_records);
86    $end = microtime(true);
87    $output .= ' #' . $tot . ', done in ' . round($end - $start,
88            4) . ' sec.' . ($isRequestedByWebLocalhost ? '' : "\n");
89    $output .= ($isRequestedByWebLocalhost ? '' : "\n");
90    if ($isRequestedByWebLocalhost) {
91        echo '<pre>';
92    }
93    $output = $output . "\n";
94    echo $output;
95
96    $i = 0;
97    foreach ($_records as $_r) {
98        ++$i;
99        $output = '';
100        $output .= sprintf('%0' . strlen((string)$tot) . 'd',
101                $i) . '/' . $tot . '. Checking ' . $_r['solution_id'] . ' (' . Utils::makeShorterText(strip_tags($_r['title']),
102                8) . '):';
103        $start = microtime(true);
104        if ($oLnk->getEntryState($_r['id'], $_r['lang'], true) === true) {
105            $output .= $oLnk->verifyArticleURL($_r['content'], $_r['id'], $_r['lang'], true);
106        }
107        $end = microtime(true);
108        $output .= ' done in ' . round($end - $start, 4) . ' sec.';
109        $output .= ($isRequestedByWebLocalhost ? '' : "\n");
110        if ($isRequestedByWebLocalhost) {
111            $output = $output . "\n";
112        }
113        echo $output;
114    }
115
116    $output = '';
117    $totEnd = microtime(true);
118    $output .= ($isRequestedByWebLocalhost ? '' : "\n");
119    $output .= 'Done in ' . round($totEnd - $totStart, 4) . ' sec.';
120    $output .= ($isRequestedByWebLocalhost ? '' : "\n");
121    if ($isRequestedByWebLocalhost) {
122        $output = $output . "\n";
123    }
124    echo $output;
125
126    if ($isRequestedByWebLocalhost) {
127        echo '</pre>';
128    }
129}
130
131//
132// Disconnect from database
133//
134$faqConfig->getDb()->close();
135