Wiki.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <?php
  2. /**
  3. * MediaWiki is the to-be base class for this whole project
  4. */
  5. class MediaWiki {
  6. var $GET; /* Stores the $_GET variables at time of creation, can be changed */
  7. var $params = array();
  8. /** Constructor. It just save the $_GET variable */
  9. function __construct() {
  10. $this->GET = $_GET;
  11. }
  12. /**
  13. * Stores key/value pairs to circumvent global variables
  14. * Note that keys are case-insensitive!
  15. *
  16. * @param $key String: key to store
  17. * @param $value Mixed: value to put for the key
  18. */
  19. function setVal( $key, &$value ) {
  20. $key = strtolower( $key );
  21. $this->params[$key] =& $value;
  22. }
  23. /**
  24. * Retrieves key/value pairs to circumvent global variables
  25. * Note that keys are case-insensitive!
  26. *
  27. * @param $key String: key to get
  28. * @param $default Mixed: default value if if the key doesn't exist
  29. */
  30. function getVal( $key, $default = '' ) {
  31. $key = strtolower( $key );
  32. if( isset( $this->params[$key] ) ) {
  33. return $this->params[$key];
  34. }
  35. return $default;
  36. }
  37. /**
  38. * Initialization of ... everything
  39. * Performs the request too
  40. * FIXME: why is this crap called "initialize" when it performs everything?
  41. *
  42. * @param $title Title ($wgTitle)
  43. * @param $article Article
  44. * @param $output OutputPage
  45. * @param $user User
  46. * @param $request WebRequest
  47. */
  48. function initialize( &$title, &$article, &$output, &$user, $request ) {
  49. wfProfileIn( __METHOD__ );
  50. if( !$this->preliminaryChecks( $title, $output, $request ) ) {
  51. wfProfileOut( __METHOD__ );
  52. return;
  53. }
  54. if( !$this->initializeSpecialCases( $title, $output, $request ) ) {
  55. $new_article = $this->initializeArticle( $title, $request );
  56. if( is_object( $new_article ) ) {
  57. $article = $new_article;
  58. $this->performAction( $output, $article, $title, $user, $request );
  59. } elseif( is_string( $new_article ) ) {
  60. $output->redirect( $new_article );
  61. } else {
  62. wfProfileOut( __METHOD__ );
  63. throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
  64. }
  65. }
  66. wfProfileOut( __METHOD__ );
  67. }
  68. /**
  69. * Check if the maximum lag of database slaves is higher that $maxLag, and
  70. * if it's the case, output an error message
  71. *
  72. * @param $maxLag int: maximum lag allowed for the request, as supplied by
  73. * the client
  74. * @return bool true if the request can continue
  75. */
  76. function checkMaxLag( $maxLag ) {
  77. list( $host, $lag ) = wfGetLB()->getMaxLag();
  78. if( $lag > $maxLag ) {
  79. wfMaxlagError( $host, $lag, $maxLag );
  80. return false;
  81. } else {
  82. return true;
  83. }
  84. }
  85. /**
  86. * Checks some initial queries
  87. * Note that $title here is *not* a Title object, but a string!
  88. *
  89. * @param $title String
  90. * @param $action String
  91. * @return Title object to be $wgTitle
  92. */
  93. function checkInitialQueries( $title, $action ) {
  94. global $wgOut, $wgRequest, $wgContLang;
  95. if( $wgRequest->getVal( 'printable' ) === 'yes' ) {
  96. $wgOut->setPrintable();
  97. }
  98. $ret = NULL;
  99. if( $curid = $wgRequest->getInt( 'curid' ) ) {
  100. # URLs like this are generated by RC, because rc_title isn't always accurate
  101. $ret = Title::newFromID( $curid );
  102. } elseif( '' == $title && 'delete' != $action ) {
  103. $ret = Title::newMainPage();
  104. } else {
  105. $ret = Title::newFromURL( $title );
  106. // check variant links so that interwiki links don't have to worry
  107. // about the possible different language variants
  108. if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
  109. $wgContLang->findVariantLink( $title, $ret );
  110. }
  111. # For non-special titles, check for implicit titles
  112. if( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
  113. // We can have urls with just ?diff=,?oldid= or even just ?diff=
  114. $oldid = $wgRequest->getInt( 'oldid' );
  115. $oldid = $oldid ? $oldid : $wgRequest->getInt( 'diff' );
  116. // Allow oldid to override a changed or missing title
  117. if( $oldid ) {
  118. $rev = Revision::newFromId( $oldid );
  119. $ret = $rev ? $rev->getTitle() : $ret;
  120. }
  121. }
  122. return $ret;
  123. }
  124. /**
  125. * Checks for search query and anon-cannot-read case
  126. *
  127. * @param $title Title
  128. * @param $output OutputPage
  129. * @param $request WebRequest
  130. */
  131. function preliminaryChecks( &$title, &$output, $request ) {
  132. if( $request->getCheck( 'search' ) ) {
  133. // Compatibility with old search URLs which didn't use Special:Search
  134. // Just check for presence here, so blank requests still
  135. // show the search page when using ugly URLs (bug 8054).
  136. // Do this above the read whitelist check for security...
  137. $title = SpecialPage::getTitleFor( 'Search' );
  138. }
  139. # If the user is not logged in, the Namespace:title of the article must be in
  140. # the Read array in order for the user to see it. (We have to check here to
  141. # catch special pages etc. We check again in Article::view())
  142. if( !is_null( $title ) && !$title->userCanRead() ) {
  143. $output->loginToUse();
  144. $output->output();
  145. $output->disable();
  146. return false;
  147. }
  148. return true;
  149. }
  150. /**
  151. * Initialize some special cases:
  152. * - bad titles
  153. * - local interwiki redirects
  154. * - redirect loop
  155. * - special pages
  156. *
  157. * FIXME: why is this crap called "initialize" when it performs everything?
  158. *
  159. * @param $title Title
  160. * @param $output OutputPage
  161. * @param $request WebRequest
  162. * @return bool true if the request is already executed
  163. */
  164. function initializeSpecialCases( &$title, &$output, $request ) {
  165. wfProfileIn( __METHOD__ );
  166. $action = $this->getVal( 'Action' );
  167. if( is_null($title) || $title->getDBkey() == '' ) {
  168. $title = SpecialPage::getTitleFor( 'Badtitle' );
  169. # Die now before we mess up $wgArticle and the skin stops working
  170. throw new ErrorPageError( 'badtitle', 'badtitletext' );
  171. } else if( $title->getInterwiki() != '' ) {
  172. if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
  173. $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
  174. } else {
  175. $url = $title->getFullURL();
  176. }
  177. /* Check for a redirect loop */
  178. if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
  179. $output->redirect( $url );
  180. } else {
  181. $title = SpecialPage::getTitleFor( 'Badtitle' );
  182. throw new ErrorPageError( 'badtitle', 'badtitletext' );
  183. }
  184. } else if( $action == 'view' && !$request->wasPosted() &&
  185. ( !isset($this->GET['title']) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
  186. !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
  187. {
  188. $targetUrl = $title->getFullURL();
  189. // Redirect to canonical url, make it a 301 to allow caching
  190. if( $targetUrl == $request->getFullRequestURL() ) {
  191. $message = "Redirect loop detected!\n\n" .
  192. "This means the wiki got confused about what page was " .
  193. "requested; this sometimes happens when moving a wiki " .
  194. "to a new server or changing the server configuration.\n\n";
  195. if( $this->getVal( 'UsePathInfo' ) ) {
  196. $message .= "The wiki is trying to interpret the page " .
  197. "title from the URL path portion (PATH_INFO), which " .
  198. "sometimes fails depending on the web server. Try " .
  199. "setting \"\$wgUsePathInfo = false;\" in your " .
  200. "LocalSettings.php, or check that \$wgArticlePath " .
  201. "is correct.";
  202. } else {
  203. $message .= "Your web server was detected as possibly not " .
  204. "supporting URL path components (PATH_INFO) correctly; " .
  205. "check your LocalSettings.php for a customized " .
  206. "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
  207. "to true.";
  208. }
  209. wfHttpError( 500, "Internal error", $message );
  210. return false;
  211. } else {
  212. $output->setSquidMaxage( 1200 );
  213. $output->redirect( $targetUrl, '301' );
  214. }
  215. } else if( NS_SPECIAL == $title->getNamespace() ) {
  216. /* actions that need to be made when we have a special pages */
  217. SpecialPage::executePath( $title );
  218. } else {
  219. /* No match to special cases */
  220. wfProfileOut( __METHOD__ );
  221. return false;
  222. }
  223. /* Did match a special case */
  224. wfProfileOut( __METHOD__ );
  225. return true;
  226. }
  227. /**
  228. * Create an Article object of the appropriate class for the given page.
  229. *
  230. * @param $title Title
  231. * @return Article object
  232. */
  233. static function articleFromTitle( &$title ) {
  234. if( NS_MEDIA == $title->getNamespace() ) {
  235. // FIXME: where should this go?
  236. $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
  237. }
  238. $article = null;
  239. wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
  240. if( $article ) {
  241. return $article;
  242. }
  243. switch( $title->getNamespace() ) {
  244. case NS_FILE:
  245. return new ImagePage( $title );
  246. case NS_CATEGORY:
  247. return new CategoryPage( $title );
  248. default:
  249. return new Article( $title );
  250. }
  251. }
  252. /**
  253. * Initialize the object to be known as $wgArticle for "standard" actions
  254. * Create an Article object for the page, following redirects if needed.
  255. *
  256. * @param $title Title ($wgTitle)
  257. * @param $request WebRequest
  258. * @return mixed an Article, or a string to redirect to another URL
  259. */
  260. function initializeArticle( &$title, $request ) {
  261. wfProfileIn( __METHOD__ );
  262. $action = $this->getVal( 'action', 'view' );
  263. $article = self::articleFromTitle( $title );
  264. # NS_MEDIAWIKI has no redirects.
  265. # It is also used for CSS/JS, so performance matters here...
  266. if( $title->getNamespace() == NS_MEDIAWIKI ) {
  267. wfProfileOut( __METHOD__ );
  268. return $article;
  269. }
  270. // Namespace might change when using redirects
  271. // Check for redirects ...
  272. $file = ($title->getNamespace() == NS_FILE) ? $article->getFile() : null;
  273. if( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
  274. && !$request->getVal( 'oldid' ) && // ... and are not old revisions
  275. $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
  276. // ... and the article is not a non-redirect image page with associated file
  277. !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
  278. {
  279. # Give extensions a change to ignore/handle redirects as needed
  280. $ignoreRedirect = $target = false;
  281. $dbr = wfGetDB( DB_SLAVE );
  282. $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
  283. wfRunHooks( 'InitializeArticleMaybeRedirect',
  284. array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
  285. // Follow redirects only for... redirects
  286. if( !$ignoreRedirect && $article->isRedirect() ) {
  287. # Is the target already set by an extension?
  288. $target = $target ? $target : $article->followRedirect();
  289. if( is_string( $target ) ) {
  290. if( !$this->getVal( 'DisableHardRedirects' ) ) {
  291. // we'll need to redirect
  292. return $target;
  293. }
  294. }
  295. if( is_object($target) ) {
  296. // Rewrite environment to redirected article
  297. $rarticle = self::articleFromTitle( $target );
  298. $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
  299. if( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
  300. $rarticle->setRedirectedFrom( $title );
  301. $article = $rarticle;
  302. $title = $target;
  303. }
  304. }
  305. } else {
  306. $title = $article->getTitle();
  307. }
  308. }
  309. wfProfileOut( __METHOD__ );
  310. return $article;
  311. }
  312. /**
  313. * Cleaning up by doing deferred updates, calling LBFactory and doing the output
  314. *
  315. * @param $deferredUpdates array of updates to do
  316. * @param $output OutputPage
  317. */
  318. function finalCleanup( &$deferredUpdates, &$output ) {
  319. wfProfileIn( __METHOD__ );
  320. # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
  321. $factory = wfGetLBFactory();
  322. $factory->commitMasterChanges();
  323. # Output everything!
  324. $output->output();
  325. # Do any deferred jobs
  326. $this->doUpdates( $deferredUpdates );
  327. $this->doJobs();
  328. # Commit and close up!
  329. $factory->shutdown();
  330. wfProfileOut( __METHOD__ );
  331. }
  332. /**
  333. * Deferred updates aren't really deferred anymore. It's important to report
  334. * errors to the user, and that means doing this before OutputPage::output().
  335. * Note that for page saves, the client will wait until the script exits
  336. * anyway before following the redirect.
  337. *
  338. * @param $updates array of objects that hold an update to do
  339. */
  340. function doUpdates( &$updates ) {
  341. wfProfileIn( __METHOD__ );
  342. /* No need to get master connections in case of empty updates array */
  343. if (!$updates) {
  344. wfProfileOut( __METHOD__ );
  345. return;
  346. }
  347. $dbw = wfGetDB( DB_MASTER );
  348. foreach( $updates as $up ) {
  349. $up->doUpdate();
  350. # Commit after every update to prevent lock contention
  351. if( $dbw->trxLevel() ) {
  352. $dbw->commit();
  353. }
  354. }
  355. wfProfileOut( __METHOD__ );
  356. }
  357. /**
  358. * Do a job from the job queue
  359. */
  360. function doJobs() {
  361. $jobRunRate = $this->getVal( 'JobRunRate' );
  362. if( $jobRunRate <= 0 || wfReadOnly() ) {
  363. return;
  364. }
  365. if( $jobRunRate < 1 ) {
  366. $max = mt_getrandmax();
  367. if( mt_rand( 0, $max ) > $max * $jobRunRate ) {
  368. return;
  369. }
  370. $n = 1;
  371. } else {
  372. $n = intval( $jobRunRate );
  373. }
  374. while ( $n-- && false != ( $job = Job::pop() ) ) {
  375. $output = $job->toString() . "\n";
  376. $t = -wfTime();
  377. $success = $job->run();
  378. $t += wfTime();
  379. $t = round( $t*1000 );
  380. if( !$success ) {
  381. $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
  382. } else {
  383. $output .= "Success, Time: $t ms\n";
  384. }
  385. wfDebugLog( 'jobqueue', $output );
  386. }
  387. }
  388. /**
  389. * Ends this task peacefully
  390. */
  391. function restInPeace() {
  392. wfLogProfilingData();
  393. wfDebug( "Request ended normally\n" );
  394. }
  395. /**
  396. * Perform one of the "standard" actions
  397. *
  398. * @param $output OutputPage
  399. * @param $article Article
  400. * @param $title Title
  401. * @param $user User
  402. * @param $request WebRequest
  403. */
  404. function performAction( &$output, &$article, &$title, &$user, &$request ) {
  405. wfProfileIn( __METHOD__ );
  406. if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
  407. wfProfileOut( __METHOD__ );
  408. return;
  409. }
  410. $action = $this->getVal( 'Action' );
  411. if( in_array( $action, $this->getVal( 'DisabledActions', array() ) ) ) {
  412. /* No such action; this will switch to the default case */
  413. $action = 'nosuchaction';
  414. }
  415. switch( $action ) {
  416. case 'view':
  417. $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
  418. $article->view();
  419. break;
  420. case 'raw': // includes JS/CSS
  421. wfProfileIn( __METHOD__.'-raw' );
  422. $raw = new RawPage( $article );
  423. $raw->view();
  424. wfProfileOut( __METHOD__.'-raw' );
  425. break;
  426. case 'watch':
  427. case 'unwatch':
  428. case 'delete':
  429. case 'revert':
  430. case 'rollback':
  431. case 'protect':
  432. case 'unprotect':
  433. case 'info':
  434. case 'markpatrolled':
  435. case 'render':
  436. case 'deletetrackback':
  437. case 'purge':
  438. $article->$action();
  439. break;
  440. case 'print':
  441. $article->view();
  442. break;
  443. case 'dublincore':
  444. if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
  445. wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
  446. } else {
  447. $rdf = new DublinCoreRdf( $article );
  448. $rdf->show();
  449. }
  450. break;
  451. case 'creativecommons':
  452. if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
  453. wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
  454. } else {
  455. $rdf = new CreativeCommonsRdf( $article );
  456. $rdf->show();
  457. }
  458. break;
  459. case 'credits':
  460. Credits::showPage( $article );
  461. break;
  462. case 'submit':
  463. if( session_id() == '' ) {
  464. /* Send a cookie so anons get talk message notifications */
  465. wfSetupSession();
  466. }
  467. /* Continue... */
  468. case 'edit':
  469. case 'editredlink':
  470. if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
  471. $internal = $request->getVal( 'internaledit' );
  472. $external = $request->getVal( 'externaledit' );
  473. $section = $request->getVal( 'section' );
  474. $oldid = $request->getVal( 'oldid' );
  475. if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
  476. $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
  477. $editor = new EditPage( $article );
  478. $editor->submit();
  479. } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
  480. $mode = $request->getVal( 'mode' );
  481. $extedit = new ExternalEdit( $article, $mode );
  482. $extedit->edit();
  483. }
  484. }
  485. break;
  486. case 'history':
  487. if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
  488. $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
  489. }
  490. $history = new PageHistory( $article );
  491. $history->history();
  492. break;
  493. default:
  494. if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
  495. $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
  496. }
  497. }
  498. wfProfileOut( __METHOD__ );
  499. }
  500. }; /* End of class MediaWiki */