LinkHolderArray.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. class LinkHolderArray {
  3. var $internals = array(), $interwikis = array();
  4. var $size = 0;
  5. var $parent;
  6. function __construct( $parent ) {
  7. $this->parent = $parent;
  8. }
  9. /**
  10. * Reduce memory usage to reduce the impact of circular references
  11. */
  12. function __destruct() {
  13. foreach ( $this as $name => $value ) {
  14. unset( $this->$name );
  15. }
  16. }
  17. /**
  18. * Merge another LinkHolderArray into this one
  19. */
  20. function merge( $other ) {
  21. foreach ( $other->internals as $ns => $entries ) {
  22. $this->size += count( $entries );
  23. if ( !isset( $this->internals[$ns] ) ) {
  24. $this->internals[$ns] = $entries;
  25. } else {
  26. $this->internals[$ns] += $entries;
  27. }
  28. }
  29. $this->interwikis += $other->interwikis;
  30. }
  31. /**
  32. * Returns true if the memory requirements of this object are getting large
  33. */
  34. function isBig() {
  35. global $wgLinkHolderBatchSize;
  36. return $this->size > $wgLinkHolderBatchSize;
  37. }
  38. /**
  39. * Clear all stored link holders.
  40. * Make sure you don't have any text left using these link holders, before you call this
  41. */
  42. function clear() {
  43. $this->internals = array();
  44. $this->interwikis = array();
  45. $this->size = 0;
  46. }
  47. /**
  48. * Make a link placeholder. The text returned can be later resolved to a real link with
  49. * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
  50. * parsing of interwiki links, and secondly to allow all existence checks and
  51. * article length checks (for stub links) to be bundled into a single query.
  52. *
  53. */
  54. function makeHolder( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
  55. wfProfileIn( __METHOD__ );
  56. if ( ! is_object($nt) ) {
  57. # Fail gracefully
  58. $retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
  59. } else {
  60. # Separate the link trail from the rest of the link
  61. list( $inside, $trail ) = Linker::splitTrail( $trail );
  62. $entry = array(
  63. 'title' => $nt,
  64. 'text' => $prefix.$text.$inside,
  65. 'pdbk' => $nt->getPrefixedDBkey(),
  66. );
  67. if ( $query !== '' ) {
  68. $entry['query'] = $query;
  69. }
  70. if ( $nt->isExternal() ) {
  71. // Use a globally unique ID to keep the objects mergable
  72. $key = $this->parent->nextLinkID();
  73. $this->interwikis[$key] = $entry;
  74. $retVal = "<!--IWLINK $key-->{$trail}";
  75. } else {
  76. $key = $this->parent->nextLinkID();
  77. $ns = $nt->getNamespace();
  78. $this->internals[$ns][$key] = $entry;
  79. $retVal = "<!--LINK $ns:$key-->{$trail}";
  80. }
  81. $this->size++;
  82. }
  83. wfProfileOut( __METHOD__ );
  84. return $retVal;
  85. }
  86. /**
  87. * Get the stub threshold
  88. */
  89. function getStubThreshold() {
  90. global $wgUser;
  91. if ( !isset( $this->stubThreshold ) ) {
  92. $this->stubThreshold = $wgUser->getOption('stubthreshold');
  93. }
  94. return $this->stubThreshold;
  95. }
  96. /**
  97. * Replace <!--LINK--> link placeholders with actual links, in the buffer
  98. * Placeholders created in Skin::makeLinkObj()
  99. * Returns an array of link CSS classes, indexed by PDBK.
  100. */
  101. function replace( &$text ) {
  102. wfProfileIn( __METHOD__ );
  103. $colours = $this->replaceInternal( $text );
  104. $this->replaceInterwiki( $text );
  105. wfProfileOut( __METHOD__ );
  106. return $colours;
  107. }
  108. /**
  109. * Replace internal links
  110. */
  111. protected function replaceInternal( &$text ) {
  112. if ( !$this->internals ) {
  113. return;
  114. }
  115. wfProfileIn( __METHOD__ );
  116. global $wgContLang;
  117. $colours = array();
  118. $sk = $this->parent->getOptions()->getSkin();
  119. $linkCache = LinkCache::singleton();
  120. $output = $this->parent->getOutput();
  121. wfProfileIn( __METHOD__.'-check' );
  122. $dbr = wfGetDB( DB_SLAVE );
  123. $page = $dbr->tableName( 'page' );
  124. $threshold = $this->getStubThreshold();
  125. # Sort by namespace
  126. ksort( $this->internals );
  127. # Generate query
  128. $query = false;
  129. $current = null;
  130. foreach ( $this->internals as $ns => $entries ) {
  131. foreach ( $entries as $index => $entry ) {
  132. $key = "$ns:$index";
  133. $title = $entry['title'];
  134. $pdbk = $entry['pdbk'];
  135. # Skip invalid entries.
  136. # Result will be ugly, but prevents crash.
  137. if ( is_null( $title ) ) {
  138. continue;
  139. }
  140. # Check if it's a static known link, e.g. interwiki
  141. if ( $title->isAlwaysKnown() ) {
  142. $colours[$pdbk] = '';
  143. } elseif ( ( $id = $linkCache->getGoodLinkID( $pdbk ) ) != 0 ) {
  144. $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
  145. $output->addLink( $title, $id );
  146. } elseif ( $linkCache->isBadLink( $pdbk ) ) {
  147. $colours[$pdbk] = 'new';
  148. } else {
  149. # Not in the link cache, add it to the query
  150. if ( !isset( $current ) ) {
  151. $current = $ns;
  152. $query = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
  153. $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
  154. } elseif ( $current != $ns ) {
  155. $current = $ns;
  156. $query .= ")) OR (page_namespace=$ns AND page_title IN(";
  157. } else {
  158. $query .= ', ';
  159. }
  160. $query .= $dbr->addQuotes( $title->getDBkey() );
  161. }
  162. }
  163. }
  164. if ( $query ) {
  165. $query .= '))';
  166. $res = $dbr->query( $query, __METHOD__ );
  167. # Fetch data and form into an associative array
  168. # non-existent = broken
  169. $linkcolour_ids = array();
  170. while ( $s = $dbr->fetchObject($res) ) {
  171. $title = Title::makeTitle( $s->page_namespace, $s->page_title );
  172. $pdbk = $title->getPrefixedDBkey();
  173. $linkCache->addGoodLinkObj( $s->page_id, $title, $s->page_len, $s->page_is_redirect );
  174. $output->addLink( $title, $s->page_id );
  175. # FIXME: convoluted data flow
  176. # The redirect status and length is passed to getLinkColour via the LinkCache
  177. # Use formal parameters instead
  178. $colours[$pdbk] = $sk->getLinkColour( $title, $threshold );
  179. //add id to the extension todolist
  180. $linkcolour_ids[$s->page_id] = $pdbk;
  181. }
  182. unset( $res );
  183. //pass an array of page_ids to an extension
  184. wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
  185. }
  186. wfProfileOut( __METHOD__.'-check' );
  187. # Do a second query for different language variants of links and categories
  188. if($wgContLang->hasVariants()) {
  189. $this->doVariants( $colours );
  190. }
  191. # Construct search and replace arrays
  192. wfProfileIn( __METHOD__.'-construct' );
  193. $replacePairs = array();
  194. foreach ( $this->internals as $ns => $entries ) {
  195. foreach ( $entries as $index => $entry ) {
  196. $pdbk = $entry['pdbk'];
  197. $title = $entry['title'];
  198. $query = isset( $entry['query'] ) ? $entry['query'] : '';
  199. $key = "$ns:$index";
  200. $searchkey = "<!--LINK $key-->";
  201. if ( !isset( $colours[$pdbk] ) || $colours[$pdbk] == 'new' ) {
  202. $linkCache->addBadLinkObj( $title );
  203. $colours[$pdbk] = 'new';
  204. $output->addLink( $title, 0 );
  205. $replacePairs[$searchkey] = $sk->makeBrokenLinkObj( $title,
  206. $entry['text'],
  207. $query );
  208. } else {
  209. $replacePairs[$searchkey] = $sk->makeColouredLinkObj( $title, $colours[$pdbk],
  210. $entry['text'],
  211. $query );
  212. }
  213. }
  214. }
  215. $replacer = new HashtableReplacer( $replacePairs, 1 );
  216. wfProfileOut( __METHOD__.'-construct' );
  217. # Do the thing
  218. wfProfileIn( __METHOD__.'-replace' );
  219. $text = preg_replace_callback(
  220. '/(<!--LINK .*?-->)/',
  221. $replacer->cb(),
  222. $text);
  223. wfProfileOut( __METHOD__.'-replace' );
  224. wfProfileOut( __METHOD__ );
  225. }
  226. /**
  227. * Replace interwiki links
  228. */
  229. protected function replaceInterwiki( &$text ) {
  230. if ( empty( $this->interwikis ) ) {
  231. return;
  232. }
  233. wfProfileIn( __METHOD__ );
  234. # Make interwiki link HTML
  235. $sk = $this->parent->getOptions()->getSkin();
  236. $replacePairs = array();
  237. foreach( $this->interwikis as $key => $link ) {
  238. $replacePairs[$key] = $sk->link( $link['title'], $link['text'] );
  239. }
  240. $replacer = new HashtableReplacer( $replacePairs, 1 );
  241. $text = preg_replace_callback(
  242. '/<!--IWLINK (.*?)-->/',
  243. $replacer->cb(),
  244. $text );
  245. wfProfileOut( __METHOD__ );
  246. }
  247. /**
  248. * Modify $this->internals and $colours according to language variant linking rules
  249. */
  250. protected function doVariants( &$colours ) {
  251. global $wgContLang;
  252. $linkBatch = new LinkBatch();
  253. $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
  254. $output = $this->parent->getOutput();
  255. $linkCache = LinkCache::singleton();
  256. $sk = $this->parent->getOptions()->getSkin();
  257. $threshold = $this->getStubThreshold();
  258. // Add variants of links to link batch
  259. foreach ( $this->internals as $ns => $entries ) {
  260. foreach ( $entries as $index => $entry ) {
  261. $key = "$ns:$index";
  262. $pdbk = $entry['pdbk'];
  263. $title = $entry['title'];
  264. $titleText = $title->getText();
  265. // generate all variants of the link title text
  266. $allTextVariants = $wgContLang->convertLinkToAllVariants($titleText);
  267. // if link was not found (in first query), add all variants to query
  268. if ( !isset($colours[$pdbk]) ){
  269. foreach($allTextVariants as $textVariant){
  270. if($textVariant != $titleText){
  271. $variantTitle = Title::makeTitle( $ns, $textVariant );
  272. if(is_null($variantTitle)) continue;
  273. $linkBatch->addObj( $variantTitle );
  274. $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. // process categories, check if a category exists in some variant
  281. $categoryMap = array(); // maps $category_variant => $category (dbkeys)
  282. $varCategories = array(); // category replacements oldDBkey => newDBkey
  283. foreach( $output->getCategoryLinks() as $category ){
  284. $variants = $wgContLang->convertLinkToAllVariants($category);
  285. foreach($variants as $variant){
  286. if($variant != $category){
  287. $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
  288. if(is_null($variantTitle)) continue;
  289. $linkBatch->addObj( $variantTitle );
  290. $categoryMap[$variant] = $category;
  291. }
  292. }
  293. }
  294. if(!$linkBatch->isEmpty()){
  295. // construct query
  296. $dbr = wfGetDB( DB_SLAVE );
  297. $page = $dbr->tableName( 'page' );
  298. $titleClause = $linkBatch->constructSet('page', $dbr);
  299. $variantQuery = "SELECT page_id, page_namespace, page_title, page_is_redirect, page_len";
  300. $variantQuery .= " FROM $page WHERE $titleClause";
  301. $varRes = $dbr->query( $variantQuery, __METHOD__ );
  302. $linkcolour_ids = array();
  303. // for each found variants, figure out link holders and replace
  304. while ( $s = $dbr->fetchObject($varRes) ) {
  305. $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
  306. $varPdbk = $variantTitle->getPrefixedDBkey();
  307. $vardbk = $variantTitle->getDBkey();
  308. $holderKeys = array();
  309. if(isset($variantMap[$varPdbk])){
  310. $holderKeys = $variantMap[$varPdbk];
  311. $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
  312. $output->addLink( $variantTitle, $s->page_id );
  313. }
  314. // loop over link holders
  315. foreach($holderKeys as $key){
  316. list( $ns, $index ) = explode( ':', $key, 2 );
  317. $entry =& $this->internals[$ns][$index];
  318. $pdbk = $entry['pdbk'];
  319. if(!isset($colours[$pdbk])){
  320. // found link in some of the variants, replace the link holder data
  321. $entry['title'] = $variantTitle;
  322. $entry['pdbk'] = $varPdbk;
  323. // set pdbk and colour
  324. # FIXME: convoluted data flow
  325. # The redirect status and length is passed to getLinkColour via the LinkCache
  326. # Use formal parameters instead
  327. $colours[$varPdbk] = $sk->getLinkColour( $variantTitle, $threshold );
  328. $linkcolour_ids[$s->page_id] = $pdbk;
  329. }
  330. }
  331. // check if the object is a variant of a category
  332. if(isset($categoryMap[$vardbk])){
  333. $oldkey = $categoryMap[$vardbk];
  334. if($oldkey != $vardbk)
  335. $varCategories[$oldkey]=$vardbk;
  336. }
  337. }
  338. wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
  339. // rebuild the categories in original order (if there are replacements)
  340. if(count($varCategories)>0){
  341. $newCats = array();
  342. $originalCats = $output->getCategories();
  343. foreach($originalCats as $cat => $sortkey){
  344. // make the replacement
  345. if( array_key_exists($cat,$varCategories) )
  346. $newCats[$varCategories[$cat]] = $sortkey;
  347. else $newCats[$cat] = $sortkey;
  348. }
  349. $output->setCategoryLinks($newCats);
  350. }
  351. }
  352. }
  353. /**
  354. * Replace <!--LINK--> link placeholders with plain text of links
  355. * (not HTML-formatted).
  356. * @param string $text
  357. * @return string
  358. */
  359. function replaceText( $text ) {
  360. wfProfileIn( __METHOD__ );
  361. $text = preg_replace_callback(
  362. '/<!--(LINK|IWLINK) (.*?)-->/',
  363. array( &$this, 'replaceTextCallback' ),
  364. $text );
  365. wfProfileOut( __METHOD__ );
  366. return $text;
  367. }
  368. /**
  369. * @param array $matches
  370. * @return string
  371. * @private
  372. */
  373. function replaceTextCallback( $matches ) {
  374. $type = $matches[1];
  375. $key = $matches[2];
  376. if( $type == 'LINK' ) {
  377. list( $ns, $index ) = explode( ':', $key, 2 );
  378. if( isset( $this->internals[$ns][$index]['text'] ) ) {
  379. return $this->internals[$ns][$index]['text'];
  380. }
  381. } elseif( $type == 'IWLINK' ) {
  382. if( isset( $this->interwikis[$key]['text'] ) ) {
  383. return $this->interwikis[$key]['text'];
  384. }
  385. }
  386. return $matches[0];
  387. }
  388. }