This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | /** |
||
3 | * Build file cache for content pages |
||
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 Maintenance |
||
22 | */ |
||
23 | |||
24 | require_once __DIR__ . '/Maintenance.php'; |
||
25 | |||
26 | /** |
||
27 | * Maintenance script that builds file cache for content pages. |
||
28 | * |
||
29 | * @ingroup Maintenance |
||
30 | */ |
||
31 | class RebuildFileCache extends Maintenance { |
||
32 | private $enabled = true; |
||
33 | |||
34 | View Code Duplication | public function __construct() { |
|
35 | parent::__construct(); |
||
36 | $this->addDescription( 'Build file cache for content pages' ); |
||
37 | $this->addOption( 'start', 'Page_id to start from', false, true ); |
||
38 | $this->addOption( 'end', 'Page_id to end on', false, true ); |
||
39 | $this->addOption( 'overwrite', 'Refresh page cache' ); |
||
40 | $this->setBatchSize( 100 ); |
||
41 | } |
||
42 | |||
43 | public function finalSetup() { |
||
44 | global $wgDebugToolbar, $wgUseFileCache, $wgReadOnly; |
||
45 | |||
46 | $this->enabled = $wgUseFileCache; |
||
47 | // Script will handle capturing output and saving it itself |
||
48 | $wgUseFileCache = false; |
||
49 | // Debug toolbar makes content uncacheable so we disable it. |
||
50 | // Has to be done before Setup.php initialize MWDebug |
||
51 | $wgDebugToolbar = false; |
||
52 | // Avoid DB writes (like enotif/counters) |
||
53 | $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters) |
||
54 | |||
55 | parent::finalSetup(); |
||
56 | } |
||
57 | |||
58 | public function execute() { |
||
0 ignored issues
–
show
execute uses the super-global variable $_SERVER which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
59 | global $wgRequestTime; |
||
60 | |||
61 | if ( !$this->enabled ) { |
||
62 | $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true ); |
||
63 | } |
||
64 | |||
65 | $start = $this->getOption( 'start', "0" ); |
||
66 | if ( !ctype_digit( $start ) ) { |
||
67 | $this->error( "Invalid value for start parameter.", true ); |
||
68 | } |
||
69 | $start = intval( $start ); |
||
70 | |||
71 | $end = $this->getOption( 'end', "0" ); |
||
72 | if ( !ctype_digit( $end ) ) { |
||
73 | $this->error( "Invalid value for end parameter.", true ); |
||
74 | } |
||
75 | $end = intval( $end ); |
||
76 | |||
77 | $this->output( "Building content page file cache from page {$start}!\n" ); |
||
78 | |||
79 | $dbr = $this->getDB( DB_REPLICA ); |
||
80 | $overwrite = $this->getOption( 'overwrite', false ); |
||
81 | $start = ( $start > 0 ) |
||
82 | ? $start |
||
83 | : $dbr->selectField( 'page', 'MIN(page_id)', false, __METHOD__ ); |
||
84 | $end = ( $end > 0 ) |
||
85 | ? $end |
||
86 | : $dbr->selectField( 'page', 'MAX(page_id)', false, __METHOD__ ); |
||
87 | if ( !$start ) { |
||
88 | $this->error( "Nothing to do.", true ); |
||
89 | } |
||
90 | |||
91 | $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client |
||
92 | |||
93 | # Do remaining chunk |
||
94 | $end += $this->mBatchSize - 1; |
||
95 | $blockStart = $start; |
||
96 | $blockEnd = $start + $this->mBatchSize - 1; |
||
97 | |||
98 | $dbw = $this->getDB( DB_MASTER ); |
||
99 | // Go through each page and save the output |
||
100 | while ( $blockEnd <= $end ) { |
||
101 | // Get the pages |
||
102 | $res = $dbr->select( 'page', |
||
103 | [ 'page_namespace', 'page_title', 'page_id' ], |
||
104 | [ 'page_namespace' => MWNamespace::getContentNamespaces(), |
||
105 | "page_id BETWEEN $blockStart AND $blockEnd" ], |
||
106 | __METHOD__, |
||
107 | [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ] |
||
108 | ); |
||
109 | |||
110 | $this->beginTransaction( $dbw, __METHOD__ ); // for any changes |
||
0 ignored issues
–
show
It seems like
$dbw defined by $this->getDB(DB_MASTER) on line 98 can be null ; however, Maintenance::beginTransaction() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
111 | foreach ( $res as $row ) { |
||
112 | $rebuilt = false; |
||
113 | |||
114 | $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); |
||
115 | if ( null == $title ) { |
||
116 | $this->output( "Page {$row->page_id} has bad title\n" ); |
||
117 | continue; // broken title? |
||
118 | } |
||
119 | |||
120 | $context = new RequestContext(); |
||
121 | $context->setTitle( $title ); |
||
122 | $article = Article::newFromTitle( $title, $context ); |
||
123 | $context->setWikiPage( $article->getPage() ); |
||
124 | |||
125 | // If the article is cacheable, then load it |
||
126 | if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) { |
||
127 | $viewCache = new HTMLFileCache( $title, 'view' ); |
||
128 | $historyCache = new HTMLFileCache( $title, 'history' ); |
||
129 | if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) { |
||
130 | if ( $overwrite ) { |
||
131 | $rebuilt = true; |
||
132 | } else { |
||
133 | $this->output( "Page '$title' (id {$row->page_id}) already cached\n" ); |
||
134 | continue; // done already! |
||
135 | } |
||
136 | } |
||
137 | |||
138 | MediaWiki\suppressWarnings(); // header notices |
||
139 | // Cache ?action=view |
||
140 | $wgRequestTime = microtime( true ); # bug 22852 |
||
141 | ob_start(); |
||
142 | $article->view(); |
||
143 | $context->getOutput()->output(); |
||
144 | $context->getOutput()->clearHTML(); |
||
145 | $viewHtml = ob_get_clean(); |
||
146 | $viewCache->saveToFileCache( $viewHtml ); |
||
147 | // Cache ?action=history |
||
148 | $wgRequestTime = microtime( true ); # bug 22852 |
||
149 | ob_start(); |
||
150 | Action::factory( 'history', $article, $context )->show(); |
||
151 | $context->getOutput()->output(); |
||
152 | $context->getOutput()->clearHTML(); |
||
153 | $historyHtml = ob_get_clean(); |
||
154 | $historyCache->saveToFileCache( $historyHtml ); |
||
155 | MediaWiki\restoreWarnings(); |
||
156 | |||
157 | if ( $rebuilt ) { |
||
158 | $this->output( "Re-cached page '$title' (id {$row->page_id})..." ); |
||
159 | } else { |
||
160 | $this->output( "Cached page '$title' (id {$row->page_id})..." ); |
||
161 | } |
||
162 | $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " . |
||
163 | "history: " . strlen( $historyHtml ) . " bytes]\n" ); |
||
164 | } else { |
||
165 | $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" ); |
||
166 | } |
||
167 | } |
||
168 | $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity) |
||
0 ignored issues
–
show
It seems like
$dbw defined by $this->getDB(DB_MASTER) on line 98 can be null ; however, Maintenance::commitTransaction() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
169 | |||
170 | $blockStart += $this->mBatchSize; |
||
171 | $blockEnd += $this->mBatchSize; |
||
172 | } |
||
173 | $this->output( "Done!\n" ); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | $maintClass = "RebuildFileCache"; |
||
178 | require_once RUN_MAINTENANCE_IF_MAIN; |
||
179 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.